CS 207 Intellj Programming 2

CS 207 – Homework 1
This homework is due on Monday February 6th at 11:59p.
To submit, follow the syllabus guidelines.
PART 1 (40pts)
In this question, you are given a completed superclass – Shape (do not make any changes to
this class!). This class represents generic shapes, and contains subclasses Circle and
Rectangle. You will implement these two subclasses (Circle and Rectangle), so that they
override the definitions in the Shape superclass. You will then test using the provided test code.
Implement the Circle and Rectangle classes:
Implement the Circle and the Rectangle classes in the folder P1 using the instructions below.
You are NOT allowed to create any other classes, additional instance variables, or make any
changes to classes other than Circle and Rectangle. You will lose points if you do.
For the Circle class, implement the following:
A private double instance variable m_radius.
A constructor that takes two parameters (in the order of color, radius). It should use the
parameter for radius to set the m_radius instance variable and then explicitly call the
parent class constructor using the super keyword and pass in the color parameter.
A method that overrides the calcArea method of the superclass and calculates the area
of the circle. The area of a circle can be calculated as 3.14 × 𝑟^2 (where 𝑟 is the radius).
A method that overrides the calcPerimeter method of the superclass and calculates
the perimeter of the circle. The perimeter of a circle can be calculated as 2 × 3.14 × 𝑟
(where r is the radius).
For the Rectangle class, create the following:
Two private double instance variables m_width and m_length.
A constructor that takes three parameters (in the order of color, width, length). It should
use the parameters for width and length to set the instance variables and then explicitly
call the parent class constructor using the super keyword and pass in the color
parameter.
A method that overrides the calcArea method of the superclass and calculates the area
of the rectangle. The area of a rectangle can be calculated as width × height.
A method that overrides the calcPerimeter method of the superclass and calculates
the perimeter of the rectangle. The perimeter of a rectangle can be calculated as 2 ×
(width × height).
Test your code:
You are provided with a test driver implemented in “TestShapes.java” (do not make any changes
to this file!) so there is no need to write your own testing code.
Once you have completed the above classes, you can run the test. You should create a plain text
file named “output-P1.txt”, copy and paste the output (if your code crashes or does not compile,
copy and paste the error messages) to this file and save it.
Grading Rubric:
Code does not compile: -10
Code compiles but crashes when executed: -5
Changes were made to things other than the Circle and the Rectangle classes: -5
Has output file: 5
Circle class has correct instance variables: 2
Circle’s constructor was correctly implemented: 5
Circle’s calcArea method was correctly implemented: 5
Circle’s calcPerimeter method was correctly implemented: 5
Rectangle class has correct instance variables: 3
Rectangle’s constructor was correctly implemented: 5
Rectangle’s calcArea method was correctly implemented: 5
Rectangle’s calcPerimeter method was correctly implemented: 5
Sample Output:
Test 1: Create and add Shape(“Red”) ==> [Passed]
Test 2: Create and add Circle(“White”, 5.5) ==> [Passed] …
Test 10: TestOutput for object Rectangle ==> [Passed]
Expected:
Color: Green
Area: 4.0
Perimeter: 10.0
Yours:
Color: Green
Area: 4.0
Perimeter: 10.0
Total test cases: 10
Correct: 10
Wrong: 0
PART 2 (30pts)
In this question, you are given a Time class and an incomplete EnhancedTime class. The
EnhancedTime class inherits from the Time class with one more method – addSeconds(int
numSeconds). You will implement the addSeconds method in the EnhancedTime class. You will
then test using the provided test code.
Implement the EnhancedTime class:
Implement the addSeconds method in the EnhancedTime class in the P2 folder using the
instructions below. You are NOT allowed to create any other methods, instance variables, or
make any changes to methods other than addSeconds or files other than “EnhancedTime.java”.
You will lose points if you do.
The addSeconds method adds a certain number of seconds to the time represented by the
current Time object. The changes should be left on the current object.
First, create an object of the EnhancedTime class in the test driver with an initial time and then
call the addSeconds method on this object. For example, say you create a new object using the
EnhancedTime class, named currentTime, and this new object contains the time 11:59:59 AM.
You would then call the addSeconds(1) method on the currentTime object, making the time
be 12:00:00 PM because 1 second has been added to the original time. If the current time is
08:17:36 PM and 18,000 seconds (5 hours) are added, the new time would be 01:17:36 AM.
Note that adding a large number of seconds can move the time across days, and you need to
update AM and PM for the time properly.
Do NOT use any off-the-shelf implementations (e.g. Java’s built-in time library and its
methods) for the above method. You are required to do it from scratch using basic date
structures!
Test your code:
You are provided with a test driver implemented by “TestTimes.java” (Do not make any changes
to this file!) so there is no need to write your own. You are also given a data file “TestTimes.dat”
that contains 13 test dates. This is a binary file and you will not be able to view its content
using a text editor. We have not covered how to read and write this type of file, so do not worry
if you have no idea how it works. We will introduce the topic later this semester.
Depending on your programming environment, the data file might need to be placed in different
folders so that your test driver can read it. For IntelliJ, you should place it in your project folder in
which you see directories like out and src, etc.
Once you have completed implementing the methods, you can run the test. You should create a
plain text file named “output-P2.txt”, copy and paste the output (if your code crashes or does
not compile, copy and paste the error messages) to this file and save it.
Grading Rubric:
Code does not compile: -10
Code compiles but crashes when executed: -5
Changes were made to things other than the addSeconds method: -5
Off-the-shelf implementation was used in the implementation: -26
Times were hard-coded into any of the methods: -30
Has output file: 4
Code passes 13 test cases: 26 (each test case worth 2 point)
Sample Output:
The current time is 11:59:59 AM and 60 seconds are added.
The correct new time is 12:00:59 PM and the one calculated by your program is
12:00:59 PM.
Correct!
The current time is 06:12:27 AM and 60 seconds are added.
The correct new time is 06:13:27 AM and the one calculated by your program is
06:13:27 AM.
Correct!

The current time is 02:18:27 PM and 123456 seconds are added.
The correct new time is 12:36:03 AM and the one calculated by your program is
12:36:03 AM.
Correct!
The current time is 04:38:32 AM and 4568915 seconds are added. The correct
new time is 01:47:07 AM and the one calculated by your program is 01:47:07
AM.
Correct!
Total test cases: 13
Correct: 13
Wrong: 0

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper
Still stressed from student homework?
Get quality assistance from academic writers!

Order your essay today and save 25% with the discount code LAVENDER