Results 1 to 3 of 3
Thread: need help calling methods
- 11-15-2007, 12:01 AM #1
Member
- Join Date
- Nov 2007
- Location
- boston
- Posts
- 10
- Rep Power
- 0
need help calling methods
//this code should calculate future investment but is not
//it should print years from 1 to 30 and future value
// i'm new in java please help me out.thnks adv
import java.util.Scanner;
public class C5E7{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
//double k = futureInvestementValue( a, b, c, d);
System.out.println(" enter amount invest:");
double investmentAmount = input.nextDouble();
System.out.println(" enter monthly Rate");
double rate = input.nextDouble();
System.out.println("THE AMOUNT INVESTED IS:" + investmentAmount);
System.out.println("THE ANNUAL INTEREST RATE IS:" + rate);
for( int i = 1; i <= 30; ++years)
System.out.println(years + "\t\t" + futureInvestmentValue);
System.out.println("YEARS\t\tFUTURE VALUE");
{
public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate int years)
{
// double monthlyInterestRate;
result = 0;
// monthlyInterestRate = rate /12;
futureInvestmentValue = investmentAmount * (1+ monthlyInterest)numberOfYears * 12;
years = years + 1;
double result = futureInvestmentValue;
return result;
}
- 11-15-2007, 12:49 AM #2
One easy to spot problem is with your parameters -//this code should calculate future investment but is not
//it should print years from 1 to 30 and future value
// i'm new in java please help me out.thnks adv
import java.util.Scanner;
public class C5E7{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
//double k = futureInvestementValue( a, b, c, d);
System.out.println(" enter amount invest:");
double investmentAmount = input.nextDouble();
System.out.println(" enter monthly Rate");
double rate = input.nextDouble();
System.out.println("THE AMOUNT INVESTED IS:" + investmentAmount);
System.out.println("THE ANNUAL INTEREST RATE IS:" + rate);
for( int i = 1; i <= 30; ++years)
System.out.println(years + "\t\t" + futureInvestmentValue);
System.out.println("YEARS\t\tFUTURE VALUE");
{
public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate int years)
{
// double monthlyInterestRate;
result = 0;
// monthlyInterestRate = rate /12;
futureInvestmentValue = investmentAmount * (1+ monthlyInterest)numberOfYears * 12;
years = years + 1;
double result = futureInvestmentValue;
return result;
}
change the parameters inside the parintheses,public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate int years)
{
// double monthlyInterestRate;
result = 0;
// monthlyInterestRate = rate /12;
futureInvestmentValue = investmentAmount * (1+ monthlyInterest)numberOfYears * 12;
years = years + 1;
double result = futureInvestmentValue;
return result;
}
you forgot the comma. There must be a comma between each parameter, next error, :public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years)
get rid of those // comment tags, i got no clue why they're there, and your method has 3 parameters while you declare 4 arguments. I dont see where the variables a b c and d came from or why its called k. if this was supposed to be a comment, where did you try calling the method then? and theres another error, you tried acessing local variables from your method in main. that will not work, they are only accessible from their method. instead return the value of the variable you want to access in your method. to get it use syntax in main://double k = futureInvestementValue( a, b, c, d);
yourVariable = futureInvestmentValue(argument 1, argument 2, argument 3);
(write code with that syntax in main, then you can access the value it returns). right now, with your code as it is you can only acess the methods return value, result in any other method besides futureInvestmentValue
you used a static method. a better approach is try using a non-static first, which is after all the point of OOP. to call non statics use syntax:
yourClassName yourObjectName = new yourClassName(); /* use arguments inside parentheses if you have parameters in your constuctor. (if you have one. if you dont, you should still declare an empty constructor.*/
variableType yourVariableName = yourObjectName.futureInvestmentValue(arguments);
you can then use it in your program.
example:
public class accessDemo {
public static void main(String[] args) {
acessDemo acdemo = new acessDemo();
double acessmethodreturn = acDemo.futureInvestmentValue(arguments);
// then you can use it in your program.
acDemo can be named anything you want. if your constructor has parameters write the arguments in new accessDemo(write the arguments here);
write whatever the arguments need to be for calling the method using the newly created object. also the name of the variable and type that gets the calls the method can be whatever is required/you wantLast edited by JT4NK3D; 11-15-2007 at 12:51 AM.
- 11-15-2007, 09:53 AM #3
Member
- Join Date
- Nov 2007
- Location
- boston
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
method calling?
By frejon26 in forum New To JavaReplies: 4Last Post: 01-25-2008, 03:38 AM -
Constructor calling
By ravian in forum New To JavaReplies: 2Last Post: 12-22-2007, 06:53 PM -
Calling Java methods form Python
By mew in forum Advanced JavaReplies: 1Last Post: 12-21-2007, 02:30 PM -
Calling Methods
By bluegreen7hi in forum New To JavaReplies: 3Last Post: 12-17-2007, 06:22 AM -
Problems with readLine() and calling methods
By peachyco in forum New To JavaReplies: 2Last Post: 11-24-2007, 07:44 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks