Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a CD Calculator in java. Properly greet the user and explain the purpose

ID: 642291 • Letter: C

Question

Create a CD Calculator in java.

Properly greet the user and explain the purpose of the program

Allow the user to calculate multiple CDs until a sentinel value is entered

There will be no limit on thenumber of months that the user can enter.

The user will be given the following options at the end of each calculation:

Calculate another CD

Output a CD for printing

Quit the program

If the user chooses to output a CD for printing, the following items will be output:

The user's name

The user's Social Security number

The amount invested

The amount the CD is worth at maturity

Terms (legal disclaimer) of the CD (Look up CD terms on theInternet for examples)

The CD does not need to actually be printed out but it needs to be in a logical format.

After the CD is output, the program can quit after the proper closing remarks.

Explanation / Answer

Java Program to create a CD calculator :

import java.util.ArrayList;

public class CD {

// instance vars

private double principal ; // initial value

private double interestRate ; // interest rate applied to the CD

private double maturity ; // how long before the cd reaches maturity

private int compoundRate;

double accumulated; //accumulated value of CD

private String compound ; //how often interest is compounded

private String daily, monthly, quarterly ;

public ArrayList<CD> list ;

double intPerYear = 0 ;

double intToDate = 0 ;

double accumulatedValue = 0 ;

/**

* Creates a CD object.

* @param principal the initial value

* @param interestRate the rate at which interest accumulates

* @param maturity when the cd reaches maturity

* @param compound how often interest is compounded

*/

public CD(int principal, int maturity, double interestRate, String compound)

{
this.principal = principal ;

this.interestRate = interestRate ;

this.maturity = maturity ;

this.compound = compound ;

}

/**

* Returns the principal

* @return the CDs principal

*/

public double getPrincipal()   

{

return principal ;

}

/**
*Returns the CD interest rate.
* @return the CD interest rate

*/

public double getInterestRate()

{

return interestRate ;

}

public double getMaturity()

{

return maturity ;

}

public String getCompound()

{

return compound;

}

public int getCompoundRate()

{

if (compound.equalsIgnoreCase(daily))

{

compoundRate = 365;

}

else if (compound.equalsIgnoreCase(monthly))

{

compoundRate = 12;

}

else if (compound.equalsIgnoreCase(quarterly))

{

compoundRate = 4;

}

return compoundRate;

}

public double computeAndGetIntPerYear()

{

int years = 1;


for(int i = 0; i < 10; i++)

{

CD wallet = list.get(i);

principal = wallet.getPrincipal();

maturity = wallet.getMaturity();

interestRate = wallet.getInterestRate();

int compoundRate = wallet.getCompoundRate();

if (maturity<=years)

{

accumulated = (principal*( 1 + (interestRate/ compoundRate)));
  
double temp = Math.pow(accumulated,(compoundRate*maturity));
  

accumulated = temp + accumulated;

}

years++;
  
}

return accumulated;

}

}

Converts the list to a multi-line string, with each line containing a CD :

import java.util.ArrayList;

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

public class CDList {

//int totRevMiles = 0;

//int totPassMiles = 0;

// instance var

private ArrayList<CD> list ; // a list of AirData objects

String out;

/**

* Creates an empty list

*/

public CDList()

{

list = new ArrayList<CD>() ;

}

public ArrayList getCDList()

{
return list;

}
/**

* Appends an CD object to the list.

* @param current the object to be appended to the list

*/

public void addToList(CD current)

{

list.add(current) ;

}

/**

* Converts the list to a multi-line string, with each line containing a CD

* @return the String containing all the data on the list

*/

public String createReport()

{

for (int i = 0 ; i <= 10 ; i++)

{
CD wallet = list.get(i);

wallet.computeAndGetIntPerYear();

double principal = wallet.getPrincipal();

double maturity = wallet.getMaturity();

double interestRate = wallet.getInterestRate();

int compoundRate = wallet.getCompoundRate();

out = out + Align.right( principal, 12 ) + Align.right( maturity, 16 ) +
Align.right( interestRate, 18 ) + Align.right( compoundRate, 21) + " " ;
}
return out;
}
}

Print :

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

public class Driver {

public static void main(String[] args) throws IOException

{
CDList list = new CDList() ;

// create Scanner object to read each line of file until end of file

Scanner infile = new Scanner ( new File("CD-data.txt") ) ;

System.out.println("Original Data: ") ;

while ( infile.hasNext() ) // while not end of file

{
// read next line

String line = infile.nextLine() ;

// "echo print" data entered

System.out.println(line) ;

// 1. create a Scanner object associated with String "line"

Scanner lineScan = new Scanner(line);   

// 2. extract tokens from current line

int principal = lineScan.nextInt();

int maturity = lineScan.nextInt();

double interestRate = lineScan.nextDouble();

String compound = lineScan.next();

// 3. create AirData object passing the tokens to the constructor

CD wallet = new CD(principal, maturity, interestRate, compound);

// 4. add object to list
  
list.addToList(wallet);

}

System.out.println( list.toString() ) ;

// print the list

list.createReport();

//print report
  
}