CS 116 Spring 2020 Lab #2: Due: Wednesday, February 5th, 5:00 PM Points: 20
CS 116 Spring 2020 Lab #2: Due: Wednesday, February 5th, 5:00 PM Points: 20
CS 116 Spring 2020 Lab #2: Due: Wednesday, February 5th, 5:00 PM Points: 20
Elizabeth Aquino
Instructions:
1. Use this document template to report your answers and create separate java files
for your classes. Enter all lab partner names at the top of first page.
2. You don’t need to finish your lab work during the corresponding lab session.
3. ZIP your Java files and lab report into a single file. Name the file as follows:
LastName_FirstName_CS116_Lab2_Report.zip
4. Submit the final document to Blackboard Assignments section before the due
date. No late submissions will be accepted.
5. ALL lab partners need to submit a report, even if it is the same document.
Objectives:
1. (12 points) Write and test a user-defined class.
2. (8 points) Write and test user-defined class.
A few students got together to form an investment group. They wanted to pool their
savings and decide which investments would maximize their return. Each student
was to find an investment, determine the interest (compounded annually, constant
interest rate) then calculate the value (future value) of the initial investment over
time, specifically for 5, 10, and 20 years. Each student was to report about the
investment to the group; the group could then decide which investment(s) were
most promising. You are one of the investors and decided that you would like to
1
write a program that would calculate the future value of the investment given the
interest and the principal.
The formula for calculating the future value knowing interest and principal is:
FV = P(1 + r)n
Where:
r: interest rate compounded annually (in the format .065 for 6.5%),
P: principal
n: number of years of investment
FV: future value after n years.
2
//query for interest and principal
System.out.print("Enter an interest rate in decimal format: ");
interest = input.nextDouble();
System.out.print("Enter the initial investment value: ");
principal = input.nextDouble();
3
value2.displayTable();
}
}
Since the formula is standard and the group will be using this calculation repeatedly,
you determine that instead of just solving the problem with just a single procedure,
you decide to write an InvestCalc class. Create an InvestCalc class with the
following (see InvestCalcApp.java code above for naming of methods):
Declaration of instance variables for the interest rate and the principal, both real
numbers,
Constant class variables for short term (5 years), middle term (10 years) and long
term (20 years) years (see displayTable() method),
Constant class variables to format all dollars and percents (##0.00%) (see sample
output),
Constructors - default (zero for the interest rate and the principal) & non-default
Accessors - returns values of the instance variables,
Mutators - assigns new values to the instance variables,
a "public String toString()" method that displays the values of the
instance variables,
a "public double futureValue(int year)" method that uses the
above formula to compute the future value,
a "public void displayTable()" method that calculates and displays a
table of the future values for 5, 10, and 20 years for the object (see sample
output, Hint: you can use a "\t" as a tab to create columns.)
To get started, create (new java file - copy/paste the code from the box above) the
application (client) class InvestCalcApp.java. Then create a new java class file,
InvestCalc.java, in the same directory. Then one at a time from the above list,
add items to your class. Please test what you have completed up to the
"toString" method first, then complete the last two methods.
4
Sample InvestCalcApp.java output:
5
Create a Vehicle class with the following:
Pseudocode:
public class Vehicle {
//counter for how many new class instances there are, used to create ID
static int count = 0; {
count += 1; }
6
//non-default constructor
//generates ID,sets timeInLine, everything else is default
public Vehicle (int t){
ID=count;
timeInLine = t;
}
//accessor methods
getID
getTimeInLine
getTimeAtBooth
getTimeLeftBooth
getCount
//mutatorMethods
setTimeInLine
setTimeAtBooth
setTimeLeftBooth
toString
}
class VehicleClient {
Main Method
Vehicle v1 = new Vehicle(12);
Standard test
7
//invalid inputs for timeLeftBooth and timeAtBooth, will display 0
Vehicle v3 = new Vehicle(16);
Time left booth<timeAtBooth
Test plan
Test case Sample data Expected result Verified?
V1 Line: 12 Line: 12 Yes
leftBooth: 14 leftBooth: 14
atBooth: 13 atBooth: 13
V2 Line: 13 Line: 13 Yes
leftBooth: 15 leftBooth: 15
atBooth: 14 atBooth: 14
V3 Line: 16 Line: 16 Yes
leftBooth: 15 leftBooth: 0
atBooth: 14 atBooth: 0
V4 Line: 12 Line: 12 Yes
leftBooth: default leftBooth: 0
atBooth: default atBooth: 0
8
Implement your pseudocode in java. Be sure your program is appropriately
documented. Accept user input from the keyboard.
Compile and run your program to see if it runs (no run-time errors).
Test your program with the test plan below. If you discover mistakes in your
program, correct them and execute the test plan again.