$30
The objectives of this lab are to understand the concept of:
1. Java interfaces
2. Copying object in Java
3. Exception handing in java
The following rules apply to this lab and all other lab assignments in future:
1. Before submitting your lab reports, take a moment to make sure that you are handing in all the material that is required. If you forget to hand something in, that is your fault; you can't use `I forgot' as an excuse to hand in parts of the assignment late.
2. 20% marks will be deducted from the assignments handed in up to 24 hours after each due date. It means if your mark is X out of Y, you will only gain 0.8 times X. There will be no credit for assignments turned in later than 24 hours after the due dates; they will be returned unmarked. Exceptions can be made but the students must inform that Instructor beforehand to accommodate if acceptable.
Page 2 of 11
Exercise 1: Copying Objects in Java (5 Marks)
In this exercise, first you should download the following files from D2L:
Colour4.java, Point4.java, Text4.java, Shape4.java, Circle4.java, Rectangle4.java, Prism4.java, and Geometry4.java.
If you compile and run the program, you will see the program output showing some information about different shape objects. Now, open the file Geometry.java, and uncomment the lines of the code that are confined between commented lines EXERCISE_1_BEGINS and EXERCISE_1_ENDS. In this code segment the program is trying to copy objects r2, c2, and p2, into r1, c1, and p1. However, if you run the program and check the output you will find out that these objects are not copies of each other. In fact, as an example, r1 is just pointing to the same object that r2 is pointing (i.e. by changing r2, the description of r1 is also changing).
What to do: Your task in this exercise is to make any necessary changes to the code (any file as needed) to fix this problem so that objects of shape are properly copied.
What to hand in: In a folder with exercise number as name, include a screenshot of the program output and all source code files (including ones you did not modify)
Exercise 2: Java interface and Exception Handling (10 Marks)
What to Do: Using java files downloaded for lab exercise 1:
• Add two Java interfaces called Resizable, and Accessible to your program, based on the class diagram on the following page.
• Define methods shrink and enlarge in the Resizable interface as follows:
o Method shrink is supposed to reduce the size of measurable elements of any shape, such as width, length, radius, … by a factor of n. For example, if the rectangle’s width is W and its length is L this method should change them to W/n, and L/n. This method is supposed to throw a SizeFactorException if the value of n (i.e. the divisor) is less than LIMIT (e.g. 1.0) which is a variable that will be declared in the Resizeable interface. o Method enlarge is supposed to enlarge the size of the measurable elements of a shape, by the factor of n. For example, the Rectangle’s width W and length L should be changed to W*n, and L*n. This method is also supposed to throw a SizeFactorException, if the value of n (i.e. the multiplier) is less than LIMIT (e.g. 1.0).
• Add a class called SizeFactorException. Objects of this class are supposed to be thrown when the value of the arguments of methods shrink and enlarge are less that LIMIT (declared in the interface Resizeable).
• Uncomment the lines confined between commented lines EXERCISE_2_BEGINS and EXERCISE_2_ENDS, in Geometry4.java to test your program and to show that your code works.
What to hand in: in a folder with exercise number as name, include a screenshot of the program output and all source code files (including ones you did not modify) in the folder.
Exercise 3: Exception Handling (15 Marks)
For this exercise, you need to download the file Sums.java from D2L. This Java program allows the user to compute the sum of integers given as input. The program repeatedly asks the user if they want to calculate the sum; if the answer is yes, then the program takes a sequence of integers terminated by a 0 as input, then prints their sum, and asks the same question again. If the answer is no, then the program terminates.
The program as it is written will not even pass compilation, because of uncaught (checked) exceptions. Please modify the program and add the suitable try-catch statement(s) to cope with the following exceptions:
1. NumberFormatException: an exception of this kind means that the datum returned by in.readLine() does not represent an integer. If this occurs, the user should be asked to reenter the datum and continue.
2. IOException: an exception of this kind means that the readLine() operation could not be completed normally, for instance because of a coding error. Also in this case, the user should be asked to reenter the datum and continue. Example of session:
Do you wish to calculate a sum? (y/n) y
Please input the sequence of integers to sum, terminated by a 0 20 5 a
Invalid number. Please reenter.
2
0
The sum is 27
Do you wish to calculate another sum? (y/n) s
Please input y or n y
Please input the sequence of integers to sum, terminated by a 0 870
30
0
The sum is 900
Do you wish to calculate another sum? (y/n) y
Please input the sequence of integers to sum, terminated by a 0 0
The sum is 0
Do you wish to calculate another sum? (y/n) n
Goodbye
What to hand in: in a folder with exercise number as name, include a screenshot of the program output and all source code files (including ones you did not modify) in the folder.
How to submit: Include all your files for the lab in one folder, zip your folder and upload it in D2L before the deadline.