Results 1 to 6 of 6
  1. #1
    Creature is offline Member
    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Default Need guidance, first Java assignment.

    Recently took up a class on Java and haven't done programming in a couple of year and i'm in a bit of trouble.

    My task:

    I am to write a Java Application that will display a table of the payoff for a car after each payment.
    I will ask the user to enter the monthly payment, the annual interest rate and the # months to completely pay off the car
    I was given a formula:

    Java Code:
    Balance(PaymentNumber)= Payment * (1 - 1 + MonthlyInterestRate) ^ (PaymentNumber - TotalPayments) / MonthlyInterestRate
    at the end the program should look like:

    after payment number payoff
    1 $10000
    2 $9800

    and so on...



    what I have so far...


    Java Code:
    import java.util.Scanner; //Program uses class Scanner
    
    public class assignment
    {
    	//main method begins
    	public static void main(String [] args)
    	{
    		//Create Scanner to obtain input from command line
    		Scanner input = new Scanner (System.in);
    		
    		double monthlyPayment;
    		double annualRate;
    		double totalPayments;
    		double paymentNumber;
    		double monthlyInterestRate = annualRate/12; //Calculates Monthly Interest by deviding annualRate by 12 months
    		double Balance = payment * (1- (1 + monthlyInterestRate) Math.pow(paymentNumber - totalPayments)) / monthlyInterestpayment;
    		
    		
    		
    		System.out.print("Enter Monthly Payment Amount: "); //user input
    		monthlyPayment = input.nextDouble();
    		
    		System.out.print("Enter Annual Interest Rate: "); //user input
    		annualRate = input.nextDouble();
    		
    		System.out.print("Enter Number of Payments: "); //user input
    		totalPayments = input.nextDouble();
    		
    		
    		
    		
    
    	}
    	
    }

    annnnndd frome there i am stuck... its spitting out errors. Help please!

  2. #2
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    107
    Rep Power
    0

    Default Re: Need guidance, first Java assignment.

    Here are some hints:

    1. Create your scanner object first, just like you already have done.
    2. Pay careful attention the the best type of variable to use. Mostly double is good for this exercise, but some things will have to be declared as ints.
    3. Instead of declararing all of your variables first, declare them as they are needed. Such as:

    double monthlyPayment = input.nextDouble();

    This way, you can more easily tell that variables used are declared and that the variables that are declared are getting a value assigned to them.

    4. Before beginning the calculations, ask the user to enter the input, and read the input into variables.
    5. Make sure that the variables are spelled the same everywhere that they are used, such as "payment" and "montlyPayment".
    6. For the big, long expression that calculates the payment, you may need to split it up, using temporary variables to make it easier to understand and / or use parentheses more (when in doubt, throw in parentheses.)

    If you need more help, you can contact me, free of charge:

    -- Kaydell
    Java Coaching: Home

  3. #3
    Creature is offline Member
    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Default Re: Need guidance, first Java assignment.

    Thanks for the help. Ive cleaned up the code a bit:

    Java Code:
    import java.util.Scanner; //Program uses class Scanner
    
    public class homework
    {
    	//main method begins
    	public static void main(String [] args)
    	{
    		//Create Scanner to obtain input from command line
    		Scanner input = new Scanner (System.in);
    	
    		System.out.print("Enter Monthly Payment Amount: "); //user input
    		double monthlyPayment = input.nextDouble();
    		
    		System.out.print("Enter Annual Interest Rate: "); //user input
    		double annualRate = input.nextDouble();
    		
    		System.out.print("Enter Number of Payments: "); //user input
    		int totalPayments = input.nextInt();
    		
    		int paymentNumber = 1;
    		int remainingMonths = (paymentNumber - totalPayments);
    		double monthlyInterest = (annualRate/12);
    		double Balance = (monthlyPayment * ( 1 - (1 + monthlyInterest) * Math.pow (remainingMonths)) / monthlyInterest);
    		
    		System.out.println(Balance);
    		
    
    	}
    	
    }
    Now it keeps spitting out the following code:

    Java Code:
    javac homework.java
    Process started >>>
    homework.java:24: error: method pow in class Math cannot be applied to given types;
    		double Balance = (monthlyPayment * ( 1.0 - (1.0 + monthlyInterest) * Math.pow (remainingMonths)) / monthlyInterest);
    		                                                                         ^
      required: double,double
      found: int
      reason: actual and formal argument lists differ in length
    1 error
    <<< Process finished. (Exit code 1)
    I don't understand why the formula is not compiling??????
    Last edited by Creature; 10-01-2013 at 07:01 AM.

  4. #4
    Creature is offline Member
    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Default Re: Need guidance, first Java assignment.

    Heres and update... I figured out the last errors I was having as math.pow needs two expression "Math.pow(x,y)"

    Java Code:
    import java.util.Scanner; //Program uses class Scanner
    
    public class homework
    {
    	//main method begins
    	public static void main(String [] args)
    	{
    		//Create Scanner to obtain input from command line
    		Scanner input = new Scanner (System.in);
    	
    		System.out.print("Enter Monthly Payment Amount: "); //user input
    		double monthlyPayment = input.nextDouble();
    		
    		System.out.print("Enter Annual Interest Rate: "); //user input
    		double annualRate = input.nextDouble();
    		
    		System.out.print("Enter Number of Payments: "); //user input
    		int totalPayments = input.nextInt();
    		
    		System.out.printf("%s25%s\n", "After payment number",  "Payoff");
    		
    		for (int paymentNumber = 1; paymentNumber <= totalPayments; paymentNumber++)
    		{
    			int remainingMonths = (paymentNumber - totalPayments);
    			double monthlyInterest = (annualRate/12);
    			double balance = (monthlyPayment * ( 1 - (1 + monthlyInterest) * Math.pow (remainingMonths, paymentNumber)) / monthlyInterest);
    			double finalBalance = (balance * paymentNumber);
    		
    			System.out.printf("%4d%,20.2f\n", paymentNumber, finalBalance);
    		}
    
    	}
    	
    }
    Result:

    Java Code:
    Process started >>>
    Enter Monthly Payment Amount: 165.25
    Enter Annual Interest Rate: 9
    Enter Number of Payments: 36
    After payment number25Payoff
       1           13,715.75
       2         -891,028.00
       3       41,570,785.75
       4   -1,617,252,836.00
       5   55,194,618,467.08
       6-1,686,541,498,678.00
       746,558,853,649,225.75
       8-1,165,393,230,865,352.00
       926,462,729,672,278,120.00
      10-544,316,792,990,140,100.00
      1110,112,325,350,443,524,000.00
      12-168,979,647,586,633,780,000.00
      132,526,524,267,236,704,000,000.00
      14-33,586,428,204,175,612,000,000.00
      15394,002,460,104,146,800,000,000.00
      16-4,043,134,293,333,333,500,000,000.00
      1735,923,479,153,874,560,000,000,000.00
      18-273,083,745,246,595,640,000,000,000.00
      191,751,464,586,532,392,600,000,000,000.00
      20-9,322,832,945,594,815,000,000,000,000.00
      2140,388,152,586,456,185,000,000,000,000.00
      22-139,109,797,748,436,840,000,000,000,000.00
      23370,291,030,666,736,400,000,000,000,000.00
      24-735,663,824,020,178,900,000,000,000,000.00
      251,044,420,508,334,541,700,000,000,000,000.00
      26-1,002,516,666,666,666,600,000,000,000,000.00
      27605,382,374,504,399,300,000,000,000,000.00
      28-208,831,457,981,323,840,000,000,000,000.00
      2936,004,717,836,023,334,000,000,000,000.00
      30-2,557,272,566,369,583,000,000,000,000.00
      3155,660,881,722,966,834,000,000,000.00
      32-227,608,226,210,810,900,000,000.00
      3370,734,876,413,994,140,000.00
      34-225,225,221,683,218.00
      35           21,207.08
      36            7,932.00
    Obviously somethings is awfully wrong! arrrggghh when entering those inputs the balance after the first payment should be $5070.31

    EDIT: Not trying to be impatient but i'm in serious need of help as this is due in less than 12 hours.
    Last edited by Creature; 10-01-2013 at 08:41 AM.

  5. #5
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    107
    Rep Power
    0

    Default Re: Need guidance, first Java assignment.

    The first thing that I see is that I believe that the interest rate is wrong.

    Percent, literally means "per 100". An annual rate of 12, divided by 12 months is 1 which is 100% per month. This is a common error when programming is not dividing by 100 in the right place and not multiplying by 100 in the right place to convert from and to percentages.

    I'll get back to you with more. Or, you can email me.

    -- Kaydell
    kaydell@yahoo@.com

  6. #6
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    107
    Rep Power
    0

    Default Re: Need guidance, first Java assignment.

    One thing to always keep in mind when you are programming is what is given and what is to be done or calculated.

    Because you have provided your code, I can get an idea of what the specification is:

    So, we know the following:

    * monthlyPaymentAmount
    * annualInterestRate
    * numberOfPayments


    After payment number 25 Payoff

    * So the program is supposed to count the payment number which seems OK.
    * And it is supposed to print the payoff on each line?

    So, wouldn't you have to know the initial balance? (Can you provide your assignment?)

    If I were you, I would also add calls to print out the values to echo back to the user what they entered to make sure that the value inside of the computer is right, and print out all of the values as they are changed in the loop.

    Such as the following line of code:

    System.out.println("balance: " + balance);

    I mean for you to print out everything after it is calculated.

    -- Kaydell
    kaydell@yahoo.com

Similar Threads

  1. Replies: 3
    Last Post: 12-12-2012, 08:48 PM
  2. Looking for guidance
    By Syphax in forum New To Java
    Replies: 3
    Last Post: 05-30-2012, 08:09 AM
  3. Replies: 2
    Last Post: 01-19-2012, 07:43 PM
  4. Guidance
    By meenakshipuni in forum New To Java
    Replies: 3
    Last Post: 10-30-2011, 12:51 PM
  5. Java assignment, need guidance ...
    By trejorchest in forum New To Java
    Replies: 5
    Last Post: 04-17-2011, 05:44 PM

Posting Permissions

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