LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 10-19-2005, 12:24 AM   #1
dflan98783
LQ Newbie
 
Registered: Oct 2005
Location: Stafford, VA
Posts: 6

Rep: Reputation: 0
Unhappy 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
 
Old 10-19-2005, 08:41 AM   #2
juvestar15
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Debian Sid
Posts: 60

Rep: Reputation: 15
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.
 
Old 10-19-2005, 06:15 PM   #3
King of Men
LQ Newbie
 
Registered: Sep 2005
Distribution: RedHat
Posts: 10

Rep: Reputation: 0
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.
 
Old 10-19-2005, 11:15 PM   #4
dflan98783
LQ Newbie
 
Registered: Oct 2005
Location: Stafford, VA
Posts: 6

Original Poster
Rep: Reputation: 0
Thank you both for helping me out. I have been going around and around.

Debbie
 
Old 02-19-2007, 08:48 PM   #5
georacer
LQ Newbie
 
Registered: Feb 2007
Posts: 2

Rep: Reputation: 0
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.
 
Old 02-20-2007, 02:04 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 02-21-2007, 11:18 PM   #7
georacer
LQ Newbie
 
Registered: Feb 2007
Posts: 2

Rep: Reputation: 0
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie question on java coolguy_iiit Programming 6 05-12-2005 12:47 PM
java code executing SQL statements problem randomx Programming 1 11-09-2004 10:59 AM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 02:41 AM
Java Newbie Question patpawlowski Programming 6 02-27-2004 04:25 PM
Newbie troubles with Bash if/then statements jimieee Programming 4 12-04-2003 06:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration