# Sample Java Code for Rectangle and Canvas

Download Solution: Click to Download Solution
Solution File Name: JavaCodeRectangleCanvas.docx
Unzip Password: prestobear.com

Problem:
Question 1 should be solved in a file called A5Q1.java. Question 2 should be solved in a file called
Rectangle.java and a file Canvas.java. Place all the .java files into a folder called A5_xxxxxx,
where you should replace xxxxxx by your student number. Compress this folder into A5_xxxxxx.zip
file. Submit your assignment, i.e., A5_xxxxxx.zip file, via Blackboard Learn (as instructed in the
first lab.)
The questions that do not compile will be graded with mark 0. For that reason, if you run out of time
and/or one of your answers contains code that does not compile, then comment out that section of the
code.

Question 1: Recursion
Implement a recursive Java method to calculate the sum of all digits of a number. Your method must
have the following header:
public static int digitSum( int n )
Then, implement a recursive Java method to compute the digital root of a number. Your method must
use the digitSum method. Your method must have the following header:
public static int digitalRoot( int n )
The digital root of a number is calculated by taking the sum of all of the digits in a number, and
repeating the process with the resulting sum until only a single digit remains. For example, if you start
with 1969, you must first add 1+9+6+9 to get 25. Since the value 25 has more than a single digit, you
must repeat the operation to obtain 7 as a final answer.

Question 2: Objects
Part (a): In this question, you will create a class Rectangle that will store information about 2D
rectangles that a user is drawing on a computer screen. Place your solution in Rectangle.java file.
See the UML diagram for the class Rectangle for complete information about the class. Your
implementation of Rectangle class in Rectangle.java must match the given UML diagram.
(UML diagrams specify the attributes of a class and their types and the method headers that you must
use in your Java code, including the types of their parameters and the return types. + means public
and – means private. Something underlined should be static.)

Part (b): In this question, you will create a class Canvas that will store a collection of Rectangle
objects, and allow several operations on the collection. See the below UML diagram for the class
Canvas for all the information about the class (and for the return types of the methods). Your
implementation of Canvas class in Canvas.java must match the given UML diagram.

``````
1. A5Q1.java
import java.util.Scanner;

public class A5Q1 {

}
``````
``````
2. A5Q1Test.java
import junit.framework.Assert;
import junit.framework.TestCase;

public class A5Q1Test extends TestCase {

}
``````
``````
3. A5Q2.java
import java.io.* ;
import java.util.Scanner;

class A5Q2 {

}
``````
``````
4. Canvas.java

public class Canvas {

}
``````
``````
5. CanvasTest.java
import junit.framework.Assert;
import junit.framework.TestCase;

public class CanvasTest extends TestCase {

}
``````
``````
6. Rectangle.java

public class Rectangle {

}
``````
``````
7. RectangleTest.java
import junit.framework.Assert;
import junit.framework.TestCase;

public class RectangleTest extends TestCase {

}
``````