-
April 23rd, 2007, 12:42 PM
#1
Help with Java assignment
Programming Assignment #8
(ArrayLists)
I. The Problem
A banking institution offers certificates of deposit (CD's) with a variety of interest rates, and maturities of 1, 3, 5, and 10 years. Interest is always compounded, daily, monthly, or quarterly.
Write a Java program that will compute the accumulated value (principal plus interest earned), at yearly intervals, of any number of such CD's. Assume that a year always consists of exactly 365 days.
Output will be a series of 10 annual reports:
1. Each report must be clearly labeled and will include one line for each active CD. For each active CD, all the data will appear, along with the interest earned for the year, total interest earned to date, and the accumulated value.
2. Each report will also print the total interest earned by all active CDs for the year, total interest earned by all active CDs to date, and the total accumulated value of all active CDs.
Once a CD reaches maturity, it will stop earning interest and will not appear in any future reports. E.g., a CD with a maturity of 5 years will appear in the reports for years one through five, but not thereafter.
II. The CD Class
Begin by creating a class to model a CD. Each CD object "knows" its own principal, interest rate, maturity, and compounding mode (private instance variables) and can display this data and compute its accumulated value (methods).
III. The CDList Class
Now create a class to maintain a list of CDs. You will need methods to add a CD to the list, and to print an annual report.
IV. The Driver (i.e., "Test") Class
Your test class or "driver" class will read data for any number of CDs from a file that I will supply. Each line of the file will contain the data – principal, interest rate (as an annual per cent), maturity, and compounding mode – for one CD.
For line read, a CD object will be created and added to the list. Then, the method that prints the report will be called once for each of the 10 years.
V. Additional Specifications
1. Your CDList class must use an ArrayList as the principal (no pun intended) data structure.
2. Your test class is to read the data file one time only. No credit will be given for programs that read the data file more than once.
3. Make sure your program is well documented and adheres to the style conventions discussed in class.
VI. Due date: The last day of class.
VII. Formula
The accumulated value (i.e., principal plus interest), A, is given by the formula:
nt
A = p ( 1 + r/n )
where
p = principal
n = number of times compounded per year
r = annual interest rate, expressed as a decimal
t = elapsed time in years
-
April 23rd, 2007, 12:45 PM
#2
Re: Help with Java assignment
This is what i got for my Cd class so far . Am i headed in the right direction?
Code:
class Cd
{
// instance variable
private int principal ;
private double intRate ;
private int maturity ;
private double compVal ;
private int years ;
public Cd()
{
this.principal = principal ;
this.intRate = intRate ;
this.maturity = maturity ;
this.compVal = compVal ;
this.years = years ;
}
public void accumVal(accumBal)
{
accumBal = principal* Math.pow((1 + intRate/compVal),compVal*years) ;
}
}
Last edited by Strobe; April 23rd, 2007 at 12:51 PM.
-
April 23rd, 2007, 01:53 PM
#3
Re: Help with Java assignment
This is what i got for my Cd class so far . Am i headed in the right direction?
Well I suppose the answer has to be yes in that you have a class with some attributes and a method that calculates the accumulated value. Unfortunately it doesn't compile (and your calculation method should be returning the new value). I suppose if you were running a 1500m race one could say that you have stepped over the start line, so you are on your way but you have a long way to go yet.
Before you write any more code make sure you have completed your design. Then if you get stuck post your design and code and ask a specific question and we'll help.
-
April 23rd, 2007, 05:00 PM
#4
Re: Help with Java assignment
It's the bagel-man!
Agrees w/ keang. Think through your program logic on paper, lots and lots of paper, and THEN code it.
-
April 23rd, 2007, 09:30 PM
#5
Re: Help with Java assignment
-
April 25th, 2007, 11:16 AM
#6
Re: Help with Java assignment
I'm having a problems figuring out what to do next this is what the assignment calls for:
Output will be a series of 10 annual reports:
1. Each report must be clearly labeled and will include one line for each active CD. For each active CD, all the data will appear, along with the interest earned for the year, total interest earned to date, and the accumulated value.
2. Each report will also print the total interest earned by all active CDs for the year, total interest earned by all active CDs to date, and the total accumulated value of all active CDs.
Once a CD reaches maturity, it will stop earning interest and will not appear in any future reports. E.g., a CD with a maturity of 5 years will appear in the reports for years one through five, but not thereafter.
II. The CD Class
Begin by creating a class to model a CD. Each CD object "knows" its own principal, interest rate, maturity, and compounding mode (private instance variables) and can display this data and compute its accumulated value (methods).
III. The CDList Class
Now create a class to maintain a list of CDs. You will need methods to add a CD to the list, and to print an annual report.
IV. The Driver (i.e., "Test") Class
Your test class or "driver" class will read data for any number of CDs from a file that I will supply. Each line of the file will contain the data – principal, interest rate (as an annual per cent), maturity, and compounding mode – for one CD.
For line read, a CD object will be created and added to the list. Then, the method that prints the report will be called once for each of the 10 years.
This is what i have:
Code:
import java.util.* ; // for ArrayList and Scanner
import java.io.* ; // for File and FileNotFoundException
class Cd
{
// instance variable
private int principal ;
private double intRate ;
private int maturity ;
private double compVal ;
private int years ;
public Cd()
{
this.principal = principal ;
this.intRate = intRate ;
this.maturity = maturity ;
this.compVal = compVal ;
this.years = years ;
}
public double accumVal()
{
double accumBal = principal* Math.pow((1 + intRate/compVal),compVal*years) ;
return accumBal ;
}
}
// end of cd class
class CdList
{
// instance variable
private ArrayList<String>list ;
// creates an empty list
public CdList()
{
list = new ArrayList<String>() ;
}
/**
* Appends a name to the end of the list.
* @param name the name to be appended
*/
public void append(String name)
{
list.add(name) ; // calling add method of ArrayList class
}
/**
* Converts the list into a multi-line String.
* @return a String containing all the names on the list, one per line.
*/
public String toString()
{
String out = "" ;
for(int i = 0 ; i < list.size() ; i++)
{
out = out + list.get(i) + "" ;
}
return out ;
}
}
How would i go about printing out the annual report i'm really lost he the professor wants it so that it prints out from a data file he provides . I'll really appreciate any input.
Last edited by Strobe; April 25th, 2007 at 11:23 AM.
-
April 25th, 2007, 11:27 AM
#7
Re: Help with Java assignment
do you need a constructor for the CD class that has parameters so that you can somehow update your CD class instance variables?
-
April 25th, 2007, 12:12 PM
#8
Re: Help with Java assignment
only thing he told us about the cd class was:
II. The CD Class
Begin by creating a class to model a CD. Each CD object "knows" its own principal, interest rate, maturity, and compounding mode (private instance variables) and can display this data and compute its accumulated value (methods).
Then the test class will read data for any number of CDs from a file that he will supply. Each line of the file will contain the data – principal, interest rate (as an annual per cent), maturity, and compounding mode – for one CD.For line read, a CD object will be created and added to the list. Then, the method that prints the report will be called once for each of the 10 years.
I got so much repect for you programmers cause this class is making me rethink my computer engineering major. I need to really understand what i'm doing on this assignment because i'm sure it'll play a role in the final i got tomorrow.
Last edited by Strobe; April 25th, 2007 at 12:15 PM.
-
April 25th, 2007, 12:38 PM
#9
Re: Help with Java assignment
petes1234's post wasn't so much a question as a big hint. Admittedly it wasn't in flashing neon letters but a fairly big hint nonetheless. Look at your constructor and ask yourself what it is doing. In fact better still, tell us what it is doing and then we can correct you if you've got it wrong.
BTW your CdList class is maintaining a list of Strings and not CD's as the assignment requires.
-
April 25th, 2007, 12:58 PM
#10
Re: Help with Java assignment
ok so in the constructor i would do this?
Code:
public Cd(principal,intRate,maturity,compVal,years)
{
this.principal = principal ;
this.intRate = intRate ;
this.maturity = maturity ;
this.compVal = compVal ;
this.years = years ;
}
and the arraylist should be this:
Code:
class CdList
{
// instance variable
private ArrayList<double>list ;
// creates an empty list
public CdList()
{
list = new ArrayList<double>() ;
}
-
April 25th, 2007, 01:15 PM
#11
Re: Help with Java assignment
ok so in the constructor i would do this?
Does this compile?
-
April 25th, 2007, 01:20 PM
#12
Re: Help with Java assignment
i fixed the cd constructor by placing the identifier before each paramater. But it is giving me an error that says unexpected type double found on the arraylist part. What am i doing wrong?
-
April 25th, 2007, 01:35 PM
#13
Re: Help with Java assignment
i fixed the cd constructor by placing the identifier before each paramater
Now you can, no doubt, see why it's not a good idea to use the same name for a parameter as a declared attribute. If you'd originally used different names you would have picked up this problem when you first compiled the code.
But it is giving me an error that says unexpected type double found on the arraylist part. What am i doing wrong?
I've no idea unless you post your latest code and the exact error message
-
April 25th, 2007, 01:37 PM
#14
Re: Help with Java assignment
I figured out why it wasn't compiling this is what i did:
Code:
import java.util.* ; // for ArrayList and Scanner
import java.io.* ; // for File and FileNotFoundException
class Cd
{
// instance variable
private double principal ;
private double intRate ;
private int maturity ;
private double compVal ;
private int years ;
public Cd(double principal,double intRate,int maturity,double compVal,int years)
{
this.principal = principal ;
this.intRate = intRate ;
this.maturity = maturity ;
this.compVal = compVal ;
this.years = years ;
}
public double accumVal()
{
double accumBal = principal* Math.pow((1 + intRate/compVal),compVal*years) ;
return accumBal ;
}
}
// end of cd class
class CdList
{
// instance variable
private ArrayList<Double>list ;
// creates an empty list
public CdList()
{
list = new ArrayList<Double>() ;
}
// add cd to the list
public void addTo(double cd)
{
list.add(cd) ;
}
}
Now can you give me a hint as to what i need to do so that it prints out an annual report one line per cd
Last edited by Strobe; April 25th, 2007 at 01:47 PM.
-
April 25th, 2007, 01:56 PM
#15
Re: Help with Java assignment
Before you move ahead. The requirement that you posted was:
III. The CDList Class
Now create a class to maintain a list of CDs. You will need methods to add a CD to the list, and to print an annual report.
What you have now is a List of Double references, not a List of CD references (a Double is not a CD). Is this what you want?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|