Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

0. Introduction. This laboratory assignment is intended as a short introduction to Java for students who are already familiar with C or C++. It asks

0. Introduction.

This laboratory assignment is intended as a short introduction to Java for students who are already familiar with C or C++. It asks you to rewrite the Python class Zillion from Lab 2 in Java. Like the Python class, the Java class Zillion implements a decimal counter. However, since you will be using a Java array instead of a Python list, your counter will have a fixed maximum number of digits.

1. Example.

All the code in this example goes inside the main method of your driver class. It first creates an instance of Zillion in the variable counter, like this.

Zillion counter = new Zillion(12);

The instance of Zillion can hold up to twelve decimal digits. Its initial value is 000000000000. Note that twelve digits can represent a much larger number than will fit in a 32-bit Java int, which is only 2 147 483 647, or around two billion. The main method then adds 1 to the counter by calling the method increment, like this.

counter.increment();

Now the counter contains 000000000001. The main method now prints the counter like this. Java automatically calls counters toString method when you try to print it.

System.out.println(counter);

You should see 000000000001 printed. If you call increment again, and then print the counter, then you should see 000000000002. If you call increment and print the counter one more time, then you should see000000000003, etc.

2. Theory.

Your class Zillion must represent the digits of a number using a private array of integers (ints). Each digit d in the array must be an integer 0 d 9. For example, suppose that your array is called digits, has a length of 3, and represents the number 57. Then digits[0] is 0, digits[1] is 5, and digits[2] is 7. The method increment must work like this. Starting at the right end of the array, and moving toward the left end, it must change 9s into 0s, until it finds a digit that is not 9, or until there are no more digits left to be visited. If it stops because it has visited a digit that is not 9, then it must add 1 to that digit. If it stops because there are no more digits left, then the counter has overflowed and it must not do anything else. For example, if your counter has three digits, and represents the integer 999, then incrementing it must produce 000. It must not indicate an error in any way.

3. Implementation.

The class Zillion must have all the following methods. To simplify grading, your methods must use the same names as these. However, they need not use the same parameter names.

public Zillion(int size)

Constructor. Create a private array of size integers (ints). You may assume that size is greater than or equal to 0. You need not initialize the array, because Java initializes all the elements to 0 for you.

public void increment()

Increment the counter, using the algorithm described in part 2. One way to do that is by using a while loop; there may be other ways. If the counter contains all 9s, then you must not generate an illegal array index.

public String toString()

Convert the digits in the counter to a String, and return that String. One way to do that is by using a for loop that concatenates all the digits of the array together using the operator +. Note that Java automatically converts an integer to a string when you concatenate it to a string.

4. Tests.

The file Driver.java contains Java source code for a driver class, unimaginatively called Driver. This class contains a main method, which in turn contains code that tests your class Zillion. Note that the class Zillion must not contain the main method! To grade your work, the TAs will run the main method. If a test succeeds, then you will receive all the points for that test. If a test fails, then you will receive no points for that test. Starting with this lab assignment, the TAs may also perform secret tests that you have not been told about. They will use the same secret tests for all students. If a secret test fails, then you will lose points for it. You will be told what the secret tests were, and how many points they were worth, after this assignment has been graded.

4. Deliverables.

You must turn in Java source code for your class Zillion. If your lab is on Monday, February 12, then your work must be submitted by 11:55 PM on Monday, February 19. If your lab is on Tuesday, February 13, then your work must be submitted by 11:55 PM on Tuesday, February 20. If your lab is on Wednesday, February 14, then your work must be submitted by 11:55 PM on Wednesday, February 21. To avoid late penalties, do not confuse these dates!

// DRIVER. The MAIN method has tests for your class ZILLION. Each test has a // comment that shows what the test should print if your code is correct. It // also has the number of points you will receive if the test is successful. // These tests are worth a total of 25 points. class Driver { public static void main(String[] args) { Zillion z = new Zillion(2); System.out.println(z); // 00 2 points z.increment(); System.out.println(z); // 01 2 points z.increment(); System.out.println(z); // 02 2 points z.increment(); z.increment(); z.increment(); z.increment(); z.increment(); z.increment(); z.increment(); z.increment(); System.out.println(z); // 10 2 points z.increment(); System.out.println(z); // 11 2 points z = new Zillion(4); System.out.println(z); // 0000 2 points for (int j = 1; j <= 999; j += 1) { z.increment(); } System.out.println(z); // 0999 2 points z.increment(); System.out.println(z); // 1000 2 points for (int j = 1; j <= 999; j += 1) { z.increment(); } System.out.println(z); // 1999 2 points z.increment(); System.out.println(z); // 2000 2 points for (int j = 1; j <= 7999; j += 1) { z.increment(); } System.out.println(z); // 9999 2 points z.increment(); System.out.println(z); // 0000 2 points z.increment(); System.out.println(z); // 0001 1 point } } 

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

Database And Expert Systems Applications Dexa 2022 Workshops 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 In Computer And Information Science 33

Authors: Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil ,Bernhard Moser ,Alfred Taudes ,Atif Mashkoor ,Johannes Sametinger ,Jorge Martinez-Gil ,Florian Sobieczky ,Lukas Fischer ,Rudolf Ramler ,Maqbool Khan ,Gerald Czech

1st Edition

3031143426, 978-3031143427

More Books

Students also viewed these Databases questions