# Variables

• (2 Pages)
• 1
• 2

## 24 Replies - 1264 Views - Last Post: 01 March 2010 - 04:49 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=158389&amp;s=991db10fe61af2ba0997110e987318b0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: -4
• 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

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 =>

Thanks,
PsychoCoder

Is This A Good Question/Topic? 0

## Replies To: Variables

### #2 pbl

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

Reputation: 8369
• Posts: 31,956
• Joined: 06-March 08

## Re: Variables

Posted 25 February 2010 - 08:05 PM

After 7 postrs you should know the rules:

You dewclare a variable

double Payment

but when you use it to call it payment

Java is case sensitive

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

## Re: Variables

Posted 26 February 2010 - 01:19 PM

pbl, on 25 February 2010 - 07:05 PM, said:

After 7 postrs you should know the rules:

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.

Reputation: -4
• 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

### #5 NoobKnight

Reputation: 49
• 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;

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

## Re: Variables

Posted 26 February 2010 - 09:40 PM

NoobKnight, 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.

### #7 pbl

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

Reputation: 8369
• Posts: 31,956
• Joined: 06-March 08

## Re: Variables

Posted 26 February 2010 - 09:46 PM

NoobKnight, 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;

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

## Re: Variables

Posted 26 February 2010 - 10:03 PM

pbl, on 26 February 2010 - 08:46 PM, said:

NoobKnight, 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

### #9 erik.price

• D.I.C Lover

Reputation: 486
• 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.

### #10 pbl

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

Reputation: 8369
• Posts: 31,956
• Joined: 06-March 08

## Re: Variables

Posted 26 February 2010 - 10:10 PM

LadiPrather, 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 ?

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

## Re: Variables

Posted 27 February 2010 - 12:58 PM

pbl, on 26 February 2010 - 09:10 PM, said:

LadiPrather, 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.

### #12 japanir

• jaVanir

Reputation: 1013
• 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

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

## Re: Variables

Posted 28 February 2010 - 07:56 PM

japanir, 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.

### #14 erik.price

• D.I.C Lover

Reputation: 486
• 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.

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

## Re: Variables

Posted 01 March 2010 - 01:05 PM

erik.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.