Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Basic interest program help with Java

Options
  • 26-01-2009 5:38pm
    #1
    Closed Accounts Posts: 4


    hello, i have a very basic assignment that i need help with. Its my first attempt at any program with Java, so it might seem very easy but i'm at a loss right now.
    1. A method that obtains user inputs and displays the results
    2. A method that displays a splash screen describing what the program does
    3. A method that calculates and returns the future value, given three values as arguments by the calling method: principal, interest rate, and years
    Basically its a compound interest program and so far this is what i've got.

    As you can see below i have the three methods, but when i run the program it skips my 'splash' statment to let people know what the program is, and it doenst display the results at the end. i just input the 3 numbers and its done. I've tried to figure this out on my own but im' stuck. Any tips and pointers will be much appreciated, and i know this is probably retardidly easy so please excuse my ignorance on this subject currently.

    import java.util.Scanner;
    
    public class Interest 
    {
    
       public static void SplashScreen(String[] args)
       {
          System.out.printf("This program computes the future value of an investment that pays compound annual interest for a fixed number of years. ");
     
       }
     
       public static void main(String[] args)
       {
       Scanner scan = new Scanner(System.in);
     
          System.out.print("Amount of investment? ");
          int princAmt = scan.nextInt();
     
          System.out.print("Annual Interest Rate? ");
          int rt = scan.nextInt();
     
          System.out.print("Number of years? ");
          int yrs = scan.nextInt();
     
       }
     
       public static double FutureValueCompute(double princAmt, double rt, int yrs, double finalInterest)
       {
         finalInterest = princAmt* Math.pow((1 + (rt/100)),yrs);   
         System.out.printf("Future value will be: " + finalInterest);
         return finalInterest;
     
       }
     
    } // end main()
    


Comments

  • Registered Users Posts: 163 ✭✭stephenlane80


    You dindt call any of the methods that you created, the programs entry point is the main method, if your methods are not called from you main method then they dont get executed, you need to something like this:

    public static void main(String[] args)
    {
    SplashScreen();

    Scanner scan = new Scanner(System.in);

    System.out.print("Amount of investment? ");
    int princAmt = scan.nextInt();

    System.out.print("Annual Interest Rate? ");
    int rt = scan.nextInt();

    System.out.print("Number of years? ");
    int yrs = scan.nextInt();

    FutureValueCompute(princAmt ,rt,yrs);



    }

    your splash screen doesnt use any paramaters so you souldnt define the String[] argument in your prototype like this:


    import java.util.Scanner;

    public class Interest
    {

    public static void SplashScreen()
    {
    System.out.printf("This program computes the future value of an investment that pays compound annual interest for a fixed number of years. ");

    }

    I think you need to go back to the books if you are having trouble with some thing this simple !!


  • Registered Users Posts: 3,945 ✭✭✭Anima


    He said it was his first program.

    The solution is what was said above. You're not calling your methods. Also another thing is your print statements. Remember there are a few types. Print, Printf and println are a few. Stick with printf and println imo. With printf, you'll need to put a '\n' at the end of the statement to make a new line for the next statement. i.e. "System.out.printf("Text\n");". Println will automatically put the next statement on a new line which is grand for what you're doing there.


  • Closed Accounts Posts: 2,349 ✭✭✭nobodythere


    Say we have this function called "multiply", that takes in two integers (int) a and b, and multiplies them.
                 multiply
                 --------
                 |      |
    integer a -->|
                 | a*b  |--> integer out 
    integer b -->|
                 |      |
                 --------
    
    In java this translates like this (don't worry about the "static" keyword for now):
    public int multiply (int a, int b) {
    	System.out.println("Multiplying...");
    	return a*b;
    }
    

    the "public" means it can be used outside the class. The "int" beside "public" is the data type of the output ("out" in the diagram above). "return a*b" means "finish the function and output a*b".

    Get rid of the String[] args in your SplashScreen function. The program always starts in main(). So at the start of main you can put
    SplashScreen();
    

    For our multiply() example you could put:
    int c = multiply(4, 5);
    

    You can imagine the multiply function in the statement above just becoming the output, so that it would be like saying "int c = 20"


    This should give you a clue about your last function


  • Closed Accounts Posts: 4 machan188


    thanks for the help so far guys.
    And ya, first day, of my first programming class ever, before this i had pseudocode so....and the teach said we dont need a book.... /palmface.

    anyhow this is what i got so far. it works, but with a problem, if i try to input a princAmt that has decimals, it errors out. So something like 1000, works. but 1000.83 doesn't.
    import java.util.Scanner;
    public class Interest 
    {
       public static void main(String[] args)
       {
       Scanner scan = new Scanner(System.in);
     
       System.out.println(SplashScreen());
     
          System.out.print("Amount of investment? ");
          double princAmt = scan.nextInt();
     
          System.out.print("Annual Interest Rate? ");
          double rt = scan.nextInt();
     
          System.out.print("Number of years? ");
          int yrs = scan.nextInt();
     
          System.out.println("Future value will be: " + FutureValueCompute(princAmt, rt, yrs));
     
       }//end Main()
     
       public static double FutureValueCompute(double princAmt, double rt, int yrs)
       {
       double finalInterest = 0;
         finalInterest = princAmt* Math.pow((1 + (rt/100)),yrs);
         return finalInterest;
    
       }//end FutureValueCompute()
     
       public static String SplashScreen()
       {
       String title = new String("This program computes the future value of an investment that pays compound annual interest for a fixed number of years.");
         return title;
    
       }//end String SplashScreen()
     
    } //end Interest()
    

    This is the error i get while running.
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at Interest.main(Interest.java:18)


  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    That's because it's expected an integer value (whole number), not a floating point value (decimal). Is there a nextDouble or nextFloat method?


  • Advertisement
  • Registered Users Posts: 2,320 ✭✭✭Q_Ball


    Welcome to the world of exceptions!

    Your problem is that you're currently trying to read an int into a double.

    The Java API is a great source of help. Check the Scanner class, the answer is in there! Or read the above post :D


  • Closed Accounts Posts: 2,349 ✭✭✭nobodythere


    One more thing: Tab in your code properly! Anything inside curly braces should be indented. You might not see the point while you're doing small programs like this, but any programmer would take you out back and shoot you between the eyes :p

    If you're using Eclipse there's a shortcut to auto-indent the code (possibly Alt+Shift+F, can't remember)


Advertisement