Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this lab you will practice recursion on NaturalNumbers by implementing the decrement and printWithCommas static methods. import components.naturalnumber.NaturalNumber; import components.naturalnumber.NaturalNumber2; import components.simplereader.SimpleReader; import components.simplereader.SimpleReader1L;

In this lab you will practice recursion on NaturalNumbers by implementing the decrement and printWithCommas static methods.

import components.naturalnumber.NaturalNumber; import components.naturalnumber.NaturalNumber2; import components.simplereader.SimpleReader; import components.simplereader.SimpleReader1L; import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; /** * Program with implementation of some {@code NaturalNumber} secondary * operations implemented as static methods: increment, decrement, and * printWithCommas, toStringWithCommas. * * @author Put your name here * */ public final class NaturalNumberStaticOps { /** * The base used in representing {@code NaturalNumber}. */ private static final int RADIX = 10; /** * Constant needed to print/convert {@code NaturalNumber} with commas. */ private static final int ONE_THOUSAND = 1000; /** * Private constructor so this utility class cannot be instantiated. */ private NaturalNumberStaticOps() { } /** * Get command from user. * * @param in * the input stream * @param out * the output stream * @return the command entered by the user * @updates in.content * @updates out.content * @requires in.is_open and out.is_open * @ensures 
 * [displays a menu of available commands, prompts the user to * enter a command, inputs and returns the command] * 
*/ private static String getCommand(SimpleReader in, SimpleWriter out) { out.println(); out.println("Command: i [increment]"); out.println(" d [decrement]"); out.println(" p [printWithCommas]"); out.println(" s [toStringWithCommas]"); out.print(" q [quit]: "); return in.nextLine(); } /** * Increments the given {@code NaturalNumber}. * * @param n * the number to increment * @updates n * @ensures n = #n + 1 */ private static void increment(NaturalNumber n) { assert n != null : "Violation of: n is not null"; int digit = n.divideBy10(); digit = digit + 1; if (digit == RADIX) { digit = 0; increment(n); } n.multiplyBy10(digit); } /** * Decrements the given {@code NaturalNumber}. * * @param n * the number to decrement * @updates n * @requires n > 0 * @ensures n = #n - 1 */ private static void decrement(NaturalNumber n) { assert n != null : "Violation of: n is not null"; assert !n.isZero() : "Violation of: n > 0"; // TODO - fill in body } /** * Outputs the given {@code NaturalNumber} with commas to the given output * stream. * * @param n * the number to output with commas * @param out * the output stream * @updates out.content * @requires out.is_open * @ensures out.content = #out.content * [display of n with commas] */ private static void printWithCommas(NaturalNumber n, SimpleWriter out) { assert n != null : "Violation of: n1 is not null"; assert out != null : "Violation of: out is not null"; assert out.isOpen() : "Violation of: out.is_open"; // TODO - fill in body } /** * Converts the given {@code NaturalNumber} to a {@code String} with commas. * * @param n * the number to convert * @return the {@code String} with commas * @ensures toStringWithCommas = [String representation of n with commas] */ private static String toStringWithCommas(NaturalNumber n) { assert n != null : "Violation of: n is not null"; // TODO - fill in body // This line added just to make the program compilable. return ""; } /** * Main method. * * @param args * the command line arguments */ public static void main(String[] args) { SimpleReader in = new SimpleReader1L(); SimpleWriter out = new SimpleWriter1L(); String command = getCommand(in, out); while (!command.equals("q")) { out.println(); if (command.equals("i")) { out.print("Enter a natural number: "); NaturalNumber n = new NaturalNumber2(in.nextLine()); out.println("Before increment: n = " + n); increment(n); out.println("After increment: n = " + n); } else if (command.equals("d")) { out.print("Enter a natural number: "); NaturalNumber n = new NaturalNumber2(in.nextLine()); out.println("Before decrement: n = " + n); decrement(n); out.println("After decrement: n = " + n); } else if (command.equals("p")) { out.print("Enter a natural number: "); NaturalNumber n = new NaturalNumber2(in.nextLine()); out.println("Before printWithCommas: n = " + n); out.print("Number with commas: "); printWithCommas(n, out); out.println(); out.println("After printWithCommas: n = " + n); } else if (command.equals("s")) { out.print("Enter a natural number: "); NaturalNumber n = new NaturalNumber2(in.nextLine()); out.println("Before toStringWithCommas: n = " + n); out.println("Number with commas: " + toStringWithCommas(n)); out.println("After toStringWithCommas: n = " + n); } else { out.println(command); } command = getCommand(in, out); } in.close(); out.close(); } }

Complete the body of the decrement static method using only the NaturalNumberKernel methods (multiplyBy10, divideBy10, and isZero). The code for increment is provided as an example of a recursive method on NaturalNumber.

Run the program and test your implementation of decrement.

Complete the body of the printWithCommas static method. This method outputs the given NaturalNumber to an output stream, inserting a comma before every group of three digits (from right to left). So the number 12345678 would be printed as 12,345,678. For this method you can use any of the NaturalNumber methods. Note that the contract for this method states (implicitly) that the value of the given NaturalNumber must be restored so that the outgoing value of n is the same as the incoming value.

Run the program and test your implementation of printWithCommas.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 11 The Database For Big Data And Analytics

Authors: Cristian Molaro, Surekha Parekh, Terry Purcell, Julian Stuhler

1st Edition

1583473858, 978-1583473856

More Books

Students also viewed these Databases questions

Question

Be familiar with the five basic ways to manage demand.

Answered: 1 week ago