CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 33
  1. #1
    Join Date
    Apr 2007
    Posts
    33

    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

  2. #2
    Join Date
    Apr 2007
    Posts
    33

    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.

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.

  4. #4
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    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.

  5. #5
    Join Date
    Apr 2007
    Posts
    33

    Re: Help with Java assignment

    thanks i'll try that

  6. #6
    Join Date
    Apr 2007
    Posts
    33

    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.

  7. #7
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    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?

  8. #8
    Join Date
    Apr 2007
    Posts
    33

    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.

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.

  10. #10
    Join Date
    Apr 2007
    Posts
    33

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

  11. #11
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Help with Java assignment

    ok so in the constructor i would do this?
    Does this compile?

  12. #12
    Join Date
    Apr 2007
    Posts
    33

    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?

  13. #13
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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

  14. #14
    Join Date
    Apr 2007
    Posts
    33

    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.

  15. #15
    Join Date
    Dec 2005
    Posts
    251

    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?

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured