CS 116 Spring 2020 Lab #2: Due: Wednesday, February 5th, 5:00 PM Points: 20

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Andrew Cordell

Elizabeth Aquino

CS 116 Spring 2020 Lab #2


Due: Wednesday, February 5th, 5:00 PM
Points: 20

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.

Problem 1 [12 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.

Java InvestCalcApp class


import java.util.*;
import java.text.*;

public class InvestCalcApp {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double interest, principal;//vars for the interest rate and initial investment
final NumberFormat DOLLAR_FORMAT =
NumberFormat.getCurrencyInstance();

//instantiate a default object of the InvestCalc class


InvestCalc value1 = new InvestCalc();
System.out.println("Default InvestCalc Object");
System.out.println(value1.toString()+ "\n");

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();

//change object and output


value1.setRate(interest);
value1.setPrincipal(principal);
System.out.println("Updated InvestCalc Object");
System.out.println(value1.toString()+ "\n");

//test the futureValue and displayTable methods


// UNCOMMENT THE NEXT TWO STATEMENTS WHEN YOU ARE READY TO TEST
System.out.println("Value after 1 year " +
DOLLAR_FORMAT.format(value1.futureValue(1)) + "\n");
value1.displayTable();

//query for another interest and principal


System.out.print("Enter another interest rate in decimal format: ");
interest = input.nextDouble();
System.out.print("Enter another initial investment value: ");
principal = input.nextDouble();

//instantiate an object of the InvestCalc class


InvestCalc value2 = new InvestCalc(interest, principal);
System.out.println("Non-Default InvestCalc Object");
System.out.println(value2.toString()+ "\n");

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.

Here is some sample output using InvestCalcApp.java.

4
Sample InvestCalcApp.java output:

Default InvestCalc Object


Principal: $0.00 Interest rate: 0.00%

Enter an interest rate in decimal format: .05


Enter the initial investment value: 10000
Updated InvestCalc Object
Principal: $10,000.00 Interest rate: 5.00%
Value after 1 year $10,500.00

YEAR INTEREST RATE PRINCIPAL FUTURE VALUE


5 5.00% $10,000.00 $12,762.82
10 5.00% $10,000.00 $16,288.95
20 5.00% $10,000.00 $26,532.98

Enter another interest rate in decimal format: .06


Enter another initial investment value: 8000
Non-Default InvestCalc Object
Principal: $8,000.00 Interest rate: 6.00%

YEAR INTEREST RATE PRINCIPAL FUTURE VALUE


5 6.00% $8,000.00 $10,705.80
10 6.00% $8,000.00 $14,326.78
20 6.00% $8,000.00 $25,657.08

Press any key to continue . . .

Problem 2 [8 points] Write and test user-defined class:

5
Create a Vehicle class with the following:

 Instance variables (all integers is fine) for the Vehicle's


 unique, system generated ID
 time the Vehicle arrives to the toll booth line
 time the Vehicle gets to the toll booth (the front of the line)
 time the Vehicle leaves the toll booth
 A static instance variable to keep track of how many Vehicle objects have been
instantiated. Use this for the unique system generated ID for each Vehicle.
 Non-Default Constructor - generates the unique ID, sets line arrival time via
method parameter, assume default values for the other instance variables at
instantiation time.
 Accessor Methods - for each instance variable
 Mutator (set*) Methods - for each non-system generated instance variable.
Check for valid values (>0) and logical relationships between "line arrival
time"<="toll booth arrival time" and "toll booth arrival time"<"toll booth leave
time". If logical relationships do not exist, set the instance variable to zero.
 toString Method

Now, create a VehicleClient driver program to do test your Vehicle class:

 Write pseudocode for solving the above problem.

Pseudocode:
public class Vehicle {
//counter for how many new class instances there are, used to create ID
static int count = 0; {
count += 1; }

private int ID;


private int timeInLine;
private int timeAtBooth;
private int timeLeftBooth;

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

Vehicle v2 = new Vehicle(13);


Standard test 2

7
//invalid inputs for timeLeftBooth and timeAtBooth, will display 0
Vehicle v3 = new Vehicle(16);
Time left booth<timeAtBooth

//default inputs for timeLeftBooth and timeAtBooth, will display 0


Vehicle v4 = new Vehicle(18);
}
}

 Step through your pseudocode to be sure that it works.


 Complete the test plan table below.

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.

You might also like