Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-19-2005, 12:24 AM
|
#1
|
|
LQ Newbie
Registered: Oct 2005
Location: Stafford, VA
Posts: 6
Rep:
|
java newbie question method headers and if statements
I am currently enrolled in a basic on-line community college course and I desperately need help before pulling out the rest of my hair. I have contacted the instructor and tried the tutors at school to no avail.
I am sure this is probably a very simple fix, but I just cant see it. The exercise involved writing a program using a specific method header and formula for after getting the input from a user of initial investment and annual interest, the program is to display a table showing the year and investment value for years 1 - 30. Also to be displayed at the beginning is the initial investment value, and interest year.
After many unsuccessful attempts, where I would get class or interface expected errors, I was finally able to compile and run. Only, the results is not what I need. Can anyone steer me in the right direction? Many thanks
BTW, I am using JBuilder 2005, 1.4.2
package flanagan_ch4_p43;
/**
* <p>Title: Programming Exercise 4.3</p>
*
* <p>Description: Computing the Future Investment Value</p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: NOVA College, ELI, ITP 120</p>
*
* @author Debora A. Flanagan
* @version 1.0
*/
import javax.swing.JOptionPane;
public class Value {
/**main method*/
public static void main(String[] args) {
//Prompt the user to enter the initial investment amount
String investmentAmountString = JOptionPane.showInputDialog (null, "Enter the Investment Amount,"+
"\nfor example 1500.50", "Programming Exercise 4.3 Input", JOptionPane.QUESTION_MESSAGE);
//Change the string into a double value
double investmentAmount = Double.parseDouble(investmentAmountString);
//Prompt the user to enter the yearly interest rate
String annualInterestRateString = JOptionPane.showInputDialog (null, "Enter the Interest Rate," +
"\nfor example 9.25", "Programming Exercise 4.3 Input", JOptionPane.QUESTION_MESSAGE);
//Change the string into a double value
double annualInterestRate = Double.parseDouble(annualInterestRateString);
//Obtain monthly interest rate
double monthlyInterestRate = annualInterestRate/1200;
//Define number of years to display
int years;
years = 1;
System.out.println ("The amount invested: $" + investmentAmount);
System.out.println("Annual interest rate:" + annualInterestRate + " %");
System.out.println("Year" + "\t" + "Future Value");
double futureInvestmentValue = investmentAmount * Math.pow(1 + monthlyInterestRate, years * 12);
System.out.println(years + "\t" + futureInvestmentValue);
while (years <= 30)
years++;
}
/**Return the table for the investment amount value over the 30 years*/
public static double futureInvestmentValue (double investmentAmount, double monthlyInterestRate,
int years){
double futureInvestmentValue = investmentAmount * Math.pow(1 + monthlyInterestRate, years * 12);
while (years <= 30)
years++;
System.out.println("Year" + "\t" + "Future Value");
System.out.println(years + "\t" + futureInvestmentValue);
return 0.0;
}
}
This is what is displays:
The amount invested: $1000.0
Annual interest rate:5.0 %
Year Future Value
1 1051.161897881733
|
|
|
|
10-19-2005, 08:41 AM
|
#2
|
|
Member
Registered: Aug 2005
Location: Australia
Distribution: Debian Sid
Posts: 60
Rep:
|
I hope this is what you wanted. I can go through each step if you don't understand.
Code:
/**
* <p>Title: Programming Exercise 4.3</p>
*
* <p>Description: Computing the Future Investment Value</p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: NOVA College, ELI, ITP 120</p>
*
* @author Debora A. Flanagan
* @version 1.0
*/
import javax.swing.JOptionPane;
public class Value {
/**main method*/
public static void main(String[] args) {
Value val = new Value();
String investmentAmountString = JOptionPane.showInputDialog (null, "Enter the Investment Amount,"+
"\nfor example 1500.50", "Programming Exercise 4.3 Input", JOptionPane.QUESTION_MESSAGE);
double investmentAmount = Double.parseDouble(investmentAmountString);
String annualInterestRateString = JOptionPane.showInputDialog (null, "Enter the Interest Rate," +
"\nfor example 9.25", "Programming Exercise 4.3 Input", JOptionPane.QUESTION_MESSAGE);
double annualInterestRate = Double.parseDouble(annualInterestRateString);
double monthlyInterestRate = annualInterestRate/1200;
// This gets printed once
System.out.println ("The amount invested: $" + investmentAmount);
System.out.println("Annual interest rate:" + annualInterestRate + " %");
System.out.println("Year" + "\t" + "Future Value");
// Call the futureInvestmentValue method
val.futureInvestmentValue( investmentAmount, monthlyInterestRate, 0 );
}
public void futureInvestmentValue (double investmentAmount, double monthlyInterestRate, int years)
{
double futureInvestmentValue = 0.0;
while (years <= 30){
years++;
futureInvestmentValue = investmentAmount * Math.pow(1 + monthlyInterestRate, years * 12);
System.out.println(years + "\t" + futureInvestmentValue);
}
}
}
Last edited by juvestar15; 10-19-2005 at 08:44 AM.
|
|
|
|
10-19-2005, 06:15 PM
|
#3
|
|
LQ Newbie
Registered: Sep 2005
Distribution: RedHat
Posts: 10
Rep:
|
Here's your problem :
Code:
double futureInvestmentValue = investmentAmount * Math.pow(1 + monthlyInterestRate, years * 12);
while (years <= 30)
years++;
System.out.println("Year" + "\t" + "Future Value");
System.out.println(years + "\t" + futureInvestmentValue);
return 0.0;
}
You are way mixing up your while loop there. What you apparently intended to do
was to calculate and print the value for each year. What you are in fact doing is calculating the value for one year, incrementing the variable years 30 times, printing out the value for that first year, and returning. In short, your brace structure is messed up. Move the calculation inside the while loop and add some nice braces.
|
|
|
|
10-19-2005, 11:15 PM
|
#4
|
|
LQ Newbie
Registered: Oct 2005
Location: Stafford, VA
Posts: 6
Original Poster
Rep:
|
Thank you both for helping me out. I have been going around and around.
Debbie 
|
|
|
|
02-19-2007, 08:48 PM
|
#5
|
|
LQ Newbie
Registered: Feb 2007
Posts: 2
Rep:
|
Similiar question
Hi all,
I have a similar problem I have been working on, and have gotten the results to display in the DOS window, but would like to instead have them display in the JOptionPane. I know you need to use and empty string but I'm not sure how to implement this. I think I could replicate the process if I saw how you would do it in this particular case (using the JOptionPane for the code posted by Juvestar15). Can anyone give me some hints?
Thanks!
Last edited by georacer; 02-19-2007 at 08:52 PM.
|
|
|
|
02-20-2007, 02:04 AM
|
#6
|
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OL
Posts: 9,311
|
Hi georacer. Welcome to LQ.
Your problem is very likely to be unrelated so instead of reviving a 2 years old thread, please start a new one, and post some code showing where your problem is.
|
|
|
|
02-21-2007, 11:18 PM
|
#7
|
|
LQ Newbie
Registered: Feb 2007
Posts: 2
Rep:
|
Hi,
Nevermind, I was able to find the solution. This problem was nearly the same as the one listed, I just needed to use a modified method.
Thanks,
Georacer
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:40 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|