Variables

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 962 Views - Last Post: 01 March 2010 - 04:49 PM Rate Topic: -----

#1 LadiPrather  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 23
  • Joined: 12-February 10

Variables

Posted 25 February 2010 - 08:01 PM

I cannot fix what is wrong it ask for payment variable and it is there. What have I done wrong thats the only problem I see, help please.

/**
 *Week 4 Assignment- Diane Beck
 * Write the program in Java (without graphical user interface) and have it 
 * calculate the payments amount for 3 mortgage loans: -7 years at 5.35%, -15 years at 5.5%,
 *-30 years at 5.75%. Use and array for the differents loans. Display the mortgage payment
 *amount for each loan.
 * 
 * 
 * Student: Diane Beck
 */
import java.util.Scanner;
import java.lang.Math;
import java.text.DecimalFormat;
import java.io.IOException;//imports code for handling of Enter key input
import java.io.BufferedReader;
import java.io.InputStreamReader;

class MortgageLoans {

    public static void main (String[] args)   //start main() function
    {
            //declare variables array
        double mortgageCalculator1[] = new double[6];
        double MortgageCalculator2[] = new double[6];
        double MortgageCalculator3[] = new double[6];
        double Payment;
        //construct the array
        //mortgageCalculator1 = new double[6];
        //mortgageCalculator2 = new double[6];
        //mortgageCalculator3 = new double[6];
        
        
        double loan = 200000;
        double term1 = 360;//*360 month for 30 year mortgage             
        double interestRate1 = 0.0575;//*5.75% 5.75/100interest rate 30 year mortgage
        double monthlyRate1 = (interestRate1/12);
        double term2 = 84;//*7 year mortgage loan
        double interestRate2 = 0.0535;//*5.35 5.35/100interest rate 7 year mortgage
        double monthlyRate2 = (interestRate2/12);//*rate calculation on 7 year mortgage loan
        double term3 = 180;// 15 year mortgage
        double interestRate3 = 0.0550;//*5.5 5.5/100 interest rate on 15 year mortgage
        double monthlyRate3 = (interestRate3/12);//*monthlyRate for 15 year mortgage

        //Discount factr calculator for the three loans
        double discountFactor1 = (Math.pow((1 + monthlyRate1),term1)
                -1/(monthlyRate1 * Math.pow((1 + monthlyRate1),term1)));
        
        double discountFactor2 = (Math.pow((1 + monthlyRate2),term2)
                -1/(monthlyRate2 * Math.pow((1 + monthlyRate2),term2)));
        
        double discountFactor3 = (Math.pow((1 + monthlyRate3),term3)
                -1/(monthlyRate3 * Math.pow((1 + monthlyRate3),term3)));
        
        double payment1 = loan/discountFactor1;//*Rate Calculation for 30 year mortgage
        double payment2 = loan/discountFactor2;//*Rate Calculation for 7 year mortgage
        double payment3 = loan/discountFactor3;//*Rate Calculation for 15 year mortgage
        
        //loop while not done
        while(loan>0)
        {
          
        java.text.DecimalFormat dfm = new java.text.DecimalFormat(",###.00");
        System.out.println("Your monthly payment is $" + dfm.format(payment) + "cents");
        //loop while done
        if(loan>0)
        {
         payment1 = loan/discountFactor1;//*Rate Calculation
         payment2 = loan/discountFactor2;//*Rate Calculation
         payment3 = loan/discountFactor3;//*Rate Calculation
        }
        else
        payment1 = payment/discountFactor1;//Rate Calculation
        payment2 = payment/discountFactor2;//Rate Calculation
        payment3 = payment/discountFactor3;//Rate Calculation
        }
        //Output

    }

}



Where did I go wrong.

Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: Variables

#2 pbl  Icon User is offline

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

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

Re: Variables

Posted 25 February 2010 - 08:05 PM

After 7 postrs you should know the rules:
:code:

You dewclare a variable

double Payment

but when you use it to call it payment

Java is case sensitive
Was This Post Helpful? 1
  • +
  • -

#3 LadiPrather  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 23
  • Joined: 12-February 10

Re: Variables

Posted 26 February 2010 - 01:19 PM

View Postpbl, on 25 February 2010 - 07:05 PM, said:

After 7 postrs you should know the rules:
:code:

You dewclare a variable

double Payment

but when you use it to call it payment

Java is case sensitive


Thank You, this is my first java class of two, and I am still trying to understand all of this.

Thanks I will try it.
Was This Post Helpful? 0
  • +
  • -

#4 LadiPrather  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 23
  • Joined: 12-February 10

Re: Variables

Posted 26 February 2010 - 01:33 PM

That helped, now System.out.println("Your monthly payment is $" + dfm.format(Payment) + "cents"); state payment may not have been initialized. Thats the payment in ().

This post has been edited by LadiPrather: 26 February 2010 - 01:35 PM

Was This Post Helpful? 0
  • +
  • -

#5 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Variables

Posted 26 February 2010 - 01:49 PM

When you declare your double Payment;

change it to

double Payment = 0.0;
Was This Post Helpful? 1
  • +
  • -

#6 LadiPrather  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 23
  • Joined: 12-February 10

Re: Variables

Posted 26 February 2010 - 09:40 PM

View PostNoobKnight, on 26 February 2010 - 12:49 PM, said:

When you declare your double Payment;

change it to

double Payment = 0.0;


That fixed that problem, thanks now it running, but it is saying your monthly payment is $.00 cents, thats not what it is suppost to do. It is not giving me the payment for each loan.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

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

Re: Variables

Posted 26 February 2010 - 09:46 PM

View PostNoobKnight, on 26 February 2010 - 02:49 PM, said:

When you declare your double Payment;

change it to

double Payment = 0.0;

Not a good idea
You should rather consider why the compiler sends you a warning saying that the Payment vatiable was not initialized.
The compiler tells you "you may have a logic problem here because, in some cases, Payment may not have a value assigned to it"
That is very helpful and should help you diagnose a logic problem. Do not use the brute force of initializing it to 0.0. At least if you have a reason for it to be 0.0 do it in your code
if(condition for payment init)
..Payment = some calculations
else
..Payment = 0.0;
Was This Post Helpful? 0
  • +
  • -

#8 LadiPrather  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 23
  • Joined: 12-February 10

Re: Variables

Posted 26 February 2010 - 10:03 PM

View Postpbl, on 26 February 2010 - 08:46 PM, said:

View PostNoobKnight, on 26 February 2010 - 02:49 PM, said:

When you declare your double Payment;

change it to

double Payment = 0.0;

Not a good idea
You should rather consider why the compiler sends you a warning saying that the Payment vatiable was not initialized.
The compiler tells you "you may have a logic problem here because, in some cases, Payment may not have a value assigned to it"
That is very helpful and should help you diagnose a logic problem. Do not use the brute force of initializing it to 0.0. At least if you have a reason for it to be 0.0 do it in your code
if(condition for payment init)
..Payment = some calculations
else
..Payment = 0.0;



I am sorry Sir I am not sure of what you mean
Was This Post Helpful? 0
  • +
  • -

#9 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Variables

Posted 26 February 2010 - 10:06 PM

pbl was telling NoobKnight that simply setting Payment to 0 might not be the best answer, because that's just working around a logical error, and might hide the problem, but won't solve it.
Was This Post Helpful? 1
  • +
  • -

#10 pbl  Icon User is offline

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

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

Re: Variables

Posted 26 February 2010 - 10:10 PM

View PostLadiPrather, on 26 February 2010 - 11:03 PM, said:

I am sorry Sir I am not sure of what you mean

The fact that the compiler tells you "variable might not be initialized" is a good indication that you might have a flaw in your design.

If the compile tells you "variable might not be initialized" it means that your code might follow a path where you do not put any value in that variable so why print it ?
Was This Post Helpful? 0
  • +
  • -

#11 LadiPrather  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 23
  • Joined: 12-February 10

Re: Variables

Posted 27 February 2010 - 12:58 PM

View Postpbl, on 26 February 2010 - 09:10 PM, said:

View PostLadiPrather, on 26 February 2010 - 11:03 PM, said:

I am sorry Sir I am not sure of what you mean

The fact that the compiler tells you "variable might not be initialized" is a good indication that you might have a flaw in your design.

If the compile tells you "variable might not be initialized" it means that your code might follow a path where you do not put any value in that variable so why print it ?




It is suppose to print out display the mortgage payment for each loan.
Was This Post Helpful? 0
  • +
  • -

#12 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Variables

Posted 27 February 2010 - 01:26 PM

take a good look at your program.
you declare Payment as:
double Payment;

but you don't assign value to it later in your code.
so when you do all the calculations using Payment, you use it without assigning any value in it.
if you initiate it to 0.0, the compiler will refer to it as 0 for the rest of the code, and in all calculations using Payment you will actually use 0.
first, think what is the role of this Payment variable in your program, and assign it the appropriate value.
if you follow this suggestion, your printing Statements where the Payment variable is involved, will print the desired results :)
Was This Post Helpful? 1
  • +
  • -

#13 LadiPrather  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 23
  • Joined: 12-February 10

Re: Variables

Posted 28 February 2010 - 07:56 PM

View Postjapanir, on 27 February 2010 - 12:26 PM, said:

take a good look at your program.
you declare Payment as:
double Payment;

but you don't assign value to it later in your code.
so when you do all the calculations using Payment, you use it without assigning any value in it.
if you initiate it to 0.0, the compiler will refer to it as 0 for the rest of the code, and in all calculations using Payment you will actually use 0.
first, think what is the role of this Payment variable in your program, and assign it the appropriate value.
if you follow this suggestion, your printing Statements where the Payment variable is involved, will print the desired results :)


So you are saying instead of using double payment just use payment. I will try that.
Was This Post Helpful? 0
  • +
  • -

#14 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Variables

Posted 28 February 2010 - 08:01 PM

He suggested that you actually give Payment a value in your code.
Was This Post Helpful? 0
  • +
  • -

#15 LadiPrather  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 23
  • Joined: 12-February 10

Re: Variables

Posted 01 March 2010 - 01:05 PM

View Posterik.price, on 28 February 2010 - 07:01 PM, said:

He suggested that you actually give Payment a value in your code.


How do I do that.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2