4 Replies - 1503 Views - Last Post: 29 October 2011 - 05:52 PM Rate Topic: -----

#1 missy24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 08-December 09

Having a difficult time understanding arraylist

Posted 29 October 2011 - 04:33 PM

I am having issues with this assignment. My book offers few examples!
I am not sure about #3.
2. Declare a variable at the beginning of the main method for an array list that stores strings.
3. after codes that calculate, format, and display the result for each calculation, add a code that formats a string with the results of the calculation then stores the string to an array list.


Did I even complete #2 correctly. Could someone provide an example so I can get a better understanding?

it is suppose to print in a row. I think I need to use a rectangular array too.
Here is my code
import java.text.*;
import java.util.ArrayList;
public class FutureValueApp
{
    public static void main(String[] args)
    {

        ArrayList<String> code =  new ArrayList<String>();//I ADDED THIS

        // display a welcome message
        System.out.println("Welcome to the Future Value Calculator");
        System.out.println();

        // perform 1 or more calculations
        Scanner sc = new Scanner(System.in);
        String choice = "y";
        while (choice.equalsIgnoreCase("y"))
        {

            // get the input from the user
            System.out.println("DATA ENTRY");
            double monthlyInvestment = getDoubleWithinRange(sc,
                "Enter monthly investment: ", 0, 1000);
            double interestRate = getDoubleWithinRange(sc,
                "Enter yearly interest rate: ", 0, 30);
            int years = getIntWithinRange(sc,
                "Enter number of years: ", 0, 100);

            // calculate the future value
            double monthlyInterestRate = interestRate/12/100;
            int months = years * 12;
            double futureValue = calculateFutureValue(
                monthlyInvestment, monthlyInterestRate, months);

            // get the currency and percent formatters
            NumberFormat currency = NumberFormat.getCurrencyInstance();
            NumberFormat percent = NumberFormat.getPercentInstance();
            percent.setMinimumFractionDigits(1);

            // format the result as a single string
            String results =
                  "Monthly investment:\t"
                      + currency.format(monthlyInvestment) + "\n"
                + "Yearly interest rate:\t"
                      + percent.format(interestRate/100) + "\n"
                + "Number of years:\t"
                      +  years + "\n"
                + "Future value:\t\t"
                      + currency.format(futureValue) + "\n";

		  

            // print the results  //I ADDED THIS
            System.out.println();
            System.out.println("FORMATTED RESULTS");
           System.out.println("Inv/Mo.\tRate\t\t\tYears\t\t\tFuture\tValue");
           System.out.println("------.\t----\t\t\t-----\t\t\t------\t-----");
           System.out.println(code);



          



            // see if the user wants to continue
            System.out.print("Continue? (y/n): ");
            choice = sc.next();
            System.out.println();
        }
    }

    public static double getDouble(Scanner sc, String prompt)
    {
        boolean isValid = false;
        double d = 0;
        while (isValid == false)
        {
            System.out.print(prompt);
            if (sc.hasNextDouble())
            {
                d = sc.nextDouble();
                isValid = true;
            }
            else
            {
                System.out.println("Error! Invalid decimal value. Try again.");
            }
            sc.nextLine();  // discard any other data entered on the line
        }
        return d;
    }

    public static double getDoubleWithinRange(Scanner sc, String prompt,
    double min, double max)
    {
        double d = 0;
        boolean isValid = false;
        while (isValid == false)
        {
            d = getDouble(sc, prompt);
            if (d <= min)
                System.out.println(
                    "Error! Number must be greater than " + min + ".");
            else if (d >= max)
                System.out.println(
                    "Error! Number must be less than " + max + ".");
            else
                isValid = true;
        }
        return d;
    }

    public static int getInt(Scanner sc, String prompt)
    {
        boolean isValidInt = false;
        int i = 0;
        while (isValidInt == false)
        {
            System.out.print(prompt);
            if (sc.hasNextInt())
            {
                i = sc.nextInt();
                isValidInt = true;
            }
            else
            {
                System.out.println("Error! Invalid integer value. Try again.");
            }
            sc.nextLine();  // discard any other data entered on the line
        }
        return i;
    }

    public static int getIntWithinRange(Scanner sc, String prompt,
    int min, int max)
    {
        int i = 0;
        boolean isValid = false;
        while (isValid == false)
        {
            i = getInt(sc, prompt);
            if (i <= min)
                System.out.println(
                    "Error! Number must be greater than " + min + ".");
            else if (i >= max)
                System.out.println(
                    "Error! Number must be less than " + max + ".");
            else
                isValid = true;
        }
        return i;
    }

    public static double calculateFutureValue(double monthlyInvestment,
    double monthlyInterestRate, int months)
    {
        double futureValue = 0;
        for (int i = 1; i <= months; i++)
        {
            futureValue =
                (futureValue + monthlyInvestment) *
                (1 + monthlyInterestRate);
        }
        return futureValue;
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Having a difficult time understanding arraylist

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Having a difficult time understanding arraylist

Posted 29 October 2011 - 04:45 PM

Not really sure of your assignment neither, not really clear, but I think your teacher rather wants something like that

import java.text.*;
import java.util.ArrayList;
public class FutureValueApp
{
    public static void main(String[] args)
    {

        ArrayList<String> code =  new ArrayList<String>();//I ADDED THIS

        // display a welcome message
        System.out.println("Welcome to the Future Value Calculator");
        System.out.println();

        // perform 1 or more calculations
        Scanner sc = new Scanner(System.in);
        String choice = "y";
        while (choice.equalsIgnoreCase("y"))
        {
           .....

 
            // format the result as a single string
            String results =
                  "Monthly investment:\t"
                      + currency.format(monthlyInvestment) + "\n"
                + "Yearly interest rate:\t"
                      + percent.format(interestRate/100) + "\n"
                + "Number of years:\t"
                      +  years + "\n"
                + "Future value:\t\t"
                      + currency.format(futureValue) + "\n";
            // save the String in the ArrayList
            code.add(results);
            // see if the user wants to continue
            System.out.print("Continue? (y/n): ");
            choice = sc.next();
            System.out.println();
        }

        // now that we are all done		  

            // print the results  //I ADDED THIS
            System.out.println();
            System.out.println("FORMATTED RESULTS");
           System.out.println("Inv/Mo.\tRate\t\t\tYears\t\t\tFuture\tValue");
           System.out.println("------.\t----\t\t\t-----\t\t\t------\t-----");
           // prints the Strings stored in the ArrayList
           for(int i = 0; i < code.size(); ++i)
              System.out.println(code.get(i));

    }


Was This Post Helpful? 0
  • +
  • -

#3 missy24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 08-December 09

Re: Having a difficult time understanding arraylist

Posted 29 October 2011 - 05:04 PM

what is stored in the array list right now? I am trying to understand. Appreciate your help

I feel like I have not stored anything in the list. I have always had issues with arrays.
Was This Post Helpful? 0
  • +
  • -

#4 missy24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 08-December 09

Re: Having a difficult time understanding arraylist

Posted 29 October 2011 - 05:15 PM

This is part 4. Maybe it will help.


#4 I have add code to dispal the elements in the array list at the console when the user indicates that program should end. Then test the program by making at least 3 future value calcuations

This is part 4. Maybe it will help.


#4 I have add code to dispal the elements in the array list at the console when the user indicates that program should end. Then test the program by making at least 3 future value calcuations
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Having a difficult time understanding arraylist

Posted 29 October 2011 - 05:52 PM

View Postmissy24, on 29 October 2011 - 08:04 PM, said:

what is stored in the array list right now? I am trying to understand. Appreciate your help

I feel like I have not stored anything in the list. I have always had issues with arrays.

            // format the result as a single string
            String results =                          // <---- that String will be saved in the AL
                  "Monthly investment:\t"
                      + currency.format(monthlyInvestment) + "\n"
                + "Yearly interest rate:\t"
                      + percent.format(interestRate/100) + "\n"
                + "Number of years:\t"
                      +  years + "\n"
                + "Future value:\t\t"
                      + currency.format(futureValue) + "\n";
            // save the String in the ArrayList
            code.add(results);



Was This Post Helpful? 1
  • +
  • -

Page 1 of 1