Results 1 to 3 of 3
  1. #1
    lowpro is offline Member
    Join Date
    Nov 2007
    Location
    boston
    Posts
    10
    Rep Power
    0

    Angry 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;

    }

  2. #2
    JT4NK3D's Avatar
    JT4NK3D is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Default

    //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;

    }
    One easy to spot problem is with your parameters -

    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)
    you forgot the comma. There must be a comma between each parameter, next error, :
    //double k = futureInvestementValue( a, b, c, d);
    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:

    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 want
    Last edited by JT4NK3D; 11-15-2007 at 01:51 AM.

  3. #3
    lowpro is offline Member
    Join Date
    Nov 2007
    Location
    boston
    Posts
    10
    Rep Power
    0

    Default

    JT4NK3D thks very much.u really walk me thru. i couldn't understand how it shoulb be done until now. thnks again

Similar Threads

  1. method calling?
    By frejon26 in forum New To Java
    Replies: 4
    Last Post: 01-25-2008, 04:38 AM
  2. Constructor calling
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-22-2007, 07:53 PM
  3. Calling Java methods form Python
    By mew in forum Advanced Java
    Replies: 1
    Last Post: 12-21-2007, 03:30 PM
  4. Calling Methods
    By bluegreen7hi in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 07:22 AM
  5. Problems with readLine() and calling methods
    By peachyco in forum New To Java
    Replies: 2
    Last Post: 11-24-2007, 08:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •