Need help with my first program
Author |
Message |
supahsain08
|
Posted: Wed Feb 13, 2008 8:04 pm Post subject: Need help with my first program |
|
|
Use code (or syntax) tags! - wtd
Hello, I started java couple of weeks ago, and i know almost nothing about it
My teacher gave us our first java assignment couple of weeks go
We are suppose to make a program which calculate and print out the interest for each month for one year (12 months), then add the interest to the principal to get a new total and print out the new total principle.
My proggy
Java: | class work
{
public static void main (String[] args )
{
double interestrate, principal, interest, time, total;
interestrate = 0. 12;
principal = 100. 0;
time = 0. 083;
interest = interestrate * principal * time ;
total = interestrate * principal * time + principal;
int n;
for (n = 1; n <= 12; n = n + 1)
{
System. out. println("The interest for month " + n + " is "+ interest );
}
}
} |
the problem is i don't know how to put interest and principle in a for loop too
any hints will be appreciated |
|
|
|
|
|
Sponsor Sponsor
|
|
|
HeavenAgain
|
Posted: Wed Feb 13, 2008 8:37 pm Post subject: RE:Need help with my first program |
|
|
Quote: put interest and principle in a for loop just do what you just said, but i dont think "principal" needs to be added in, only the interest and total.........
Ps. your total should only be your interest (for every month) + the initial amount deposit , unless im wrong... |
|
|
|
|
|
supahsain08
|
Posted: Thu Feb 14, 2008 3:52 pm Post subject: Re: Need help with my first program |
|
|
That's my Assignment
This following requirements must be met by the program you write:
Your program should do the following:
* create some variables named interestrate, principal, interest, time, and total (all numbers should be doubles).
* Set the values of the variables interestrate, and principal, to some appropriate numbers (interestrate should be between 0.0 and 1.0)
* Use a for loop to calculate and print out the interest for each month for one year (12 months), then add the interest to the principal to get a new total and print out the new total principle.
Here's my code
code: | import java.text.*;
public class asn1
{
public static void main(String[] args)
{
double interestrate, principal, interest, time, total;
int month;
String pattern1 = "#.##";
String pattern2 = "###.##";
DecimalFormat x = new DecimalFormat(pattern1);
DecimalFormat y = new DecimalFormat(pattern2);
interestrate = 0.12;
principal = 100;
time = 0.0833;
interest = interestrate * principal * time ;
System.out.println ("Initial Principle = " + principal);
System.out.println ("Interest Rate = " + interestrate);
for (month = 1; month <= 12; month = month + 1)
{
interest = interest + 0.01;
principal = principal + interest;
System.out.println ("The interest for month " + month + " is " + x.format(interest) + " and the total is " + y.format(principal));
}
}
} |
This is what i don't understand, how am i suppose to calculate the interest each month, i know how to calculate for first month
I = 0.083 * 100 * .12 = 0.996
how do you find the interest for second and third month, what's the formula [/syntax] |
|
|
|
|
|
HeavenAgain
|
Posted: Thu Feb 14, 2008 5:34 pm Post subject: RE:Need help with my first program |
|
|
what's the formula?
your teacher just gaved it to you. "interest = principal x rate x time"
in your case, "interest = interestrate * principal * time"
but in your for-loop, you have interest = interest + 0.01; , now what you did is instead of using the equation you have, you just add 0.01 to interest everytime (which is wrong), and after, you have principal = principal + interest; which just says, ok principal just add the interest, which is correct, IF you did your interest correct.
im not sure if you learned a runtime table or whatever it is called, where you follow your code line by line, and put the values in a table, it helps alot when you just begin to learn about loops, do that, and you'll see where you are wrong. (learn to read your own code, and follow it) |
|
|
|
|
|
supahsain08
|
Posted: Thu Feb 14, 2008 6:08 pm Post subject: Re: Need help with my first program |
|
|
K did some more modifications
code: | import java.text.*;
public class asn1
{
public static void main(String[] args)
{
// Declaring Double variables for calculations
double interestrate, principle, interest, time, total;
// Declaring int variable for months calculation
int month;
// Creating a pattern for outputing desired amount of decimals
String pattern1 = "#.00";
String pattern2 = "###.00";
DecimalFormat x = new DecimalFormat(pattern1);
DecimalFormat y = new DecimalFormat(pattern2);
// Initializing values for the variables
interestrate = 0.120;
principle = 100.0;
time = 0.0833;
interest = interestrate * principle * time ;
System.out.println ("Initial Principle = " + principle);
System.out.println ("Interest Rate = " + interestrate);
// Skipping two lines
System.out.println("\n");
System.out.println("\n");
// Using a for loop for months
for (month = 1; month <= 12; month++)
{
// Using for loop to calculate principle
principle = principle + interest;
// Outputing all the Calculations
System.out.println ("The interest for month " + month + " is $" +x.format(interest) + " and the new total is $" + y.format(principle));
// Using for loop to calculate interest
interest = interestrate * principle * time;
}
}
} |
Output
any mistake? |
|
|
|
|
|
HeavenAgain
|
Posted: Thu Feb 14, 2008 6:47 pm Post subject: RE:Need help with my first program |
|
|
if i were to be picky about it, your output did not exactly match your teacher's output, the Initial Principle = 100.00 you are missing another 0 on the end, so format that properly, and also code: | // Skipping two lines
System.out.println("\n");
System.out.println("\n"); | what this does, is not just skip "two" lines, it skiped 4 lines, you have a system.out.println() which already prints 1 line, and in it you are printing another line with "\n".... |
|
|
|
|
|
syntax_error
|
Posted: Thu Feb 14, 2008 6:49 pm Post subject: RE:Need help with my first program |
|
|
instead of asking us; do the math on paper first and think; you will answer your own questions that way; be self aware a bit more.
On a side note your class names should have the first letter in CAPS |
|
|
|
|
|
supahsain08
|
Posted: Thu Feb 14, 2008 7:01 pm Post subject: RE:Need help with my first program |
|
|
Thanks HeavenAgain
@ syntax_error: I don't have the first letter in caps because my teacher said file name should be "asn1.java" |
|
|
|
|
|
Sponsor Sponsor
|
|
|
syntax_error
|
Posted: Thu Feb 14, 2008 7:06 pm Post subject: RE:Need help with my first program |
|
|
oo well good coding conventions state: have the first letter of your class in CAPS; jsut keep in as reference for work maybe not for your teacher? |
|
|
|
|
|
|
|