Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Help using parameters

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help using parameters

    Okay, so I have 2 classes (Investment and Project4)
    This program is designed to display 5 menu options.

    The first menu option asks the user for a starting balance and an interest rate
    The second menu option asks the user for the number of years they want to invest and the number of times they want the value compounded each year.
    * Here is where my problem comes in*
    The third menu is supposed to display these 4 variables(principal, interestRate, numberOfYears, numberOfTimes)

    My investment class handles all the calculations for which the algorithm is
    principal * Math.pow((1 + interestRate / numberOfTimes), (numberOfYears * numberOfTimes));

    Here is the code from my Project4 class which has my main in it. I am on the method named menu3() and I don't know how to incorporate the above values into this method mainly because it has to go through the Investment class to calculate the futureValue and then go through this method that is not the main method.

    /**
    * Class Name : Project4<br>
    * Class Purpose: Display Menu options for a user to select and display corresponding data in the chosen menu. Continues until the user ends program
    import java.util.Scanner;
    import java.text.DecimalFormat;
     
    public class Project4
     
    	private static Scanner keyboard = new Scanner(System.in);
     
    	public static void main(String[] args)
    	{
    		Project4 app = new Project4();
     
    		//Display all Menu options
    		app.displayMenus();
    		//Asks user to pick a menu option
    		System.out.println("Please choose a menu option:");
    		int option = keyboard.nextInt();
    		while(option >=1 || option <= 5)
    		{
    			if(option == 1)
    			{
    				app.menu1();
    				app.displayMenus();
    				System.out.println("Please choose a menu option:");
    				option = keyboard.nextInt();
    			}
     
    			if(option == 2)
    			{
    				app.menu2();
    				app.displayMenus();
    				System.out.println("Please choose a menu option:");
    				option = keyboard.nextInt();
    			}
    			else if(option == 3)
    			{
    				app.menu3();
    				System.out.println("Please choose a menu option:");
    				option = keyboard.nextInt();
    			}
    			else if(option == 4)
    			{
    				System.out.println("Under construction");
    				app.displayMenus();
    				System.out.println("Please choose a menu option:");
    				option = keyboard.nextInt();
    			}
     
    			else if(option == 5)
    			{
    				System.out.println("Are you sure you want to exit? (y/n)");
    				String answer = keyboard.nextLine();
     
    				if(answer.equalsIgnoreCase("y"))
    				{
    					System.out.println("End of program");
    					break;
    				}
    				else if(answer.equalsIgnoreCase("n"))
    				{
    					app.displayMenus();
    					System.out.println("Please choose a menu option:");
    					option = keyboard.nextInt();
    				}
    			}
     
    		}
     
    	}
     
    	public void displayMenus()
    	{
    		System.out.println("1 - Enter starting balance and interest rate." +
    		" \n2 - Enter the number of years and the number of times." +
    		" \n3 - Display the balance schedule." +
    		" \n4 - Display the bar graph." +
    		" \n5 - Exit.");
    	}
     
    	public void menu1()
    	{
    		System.out.println("What is the starting balance (value from 1 to 10,000).");
    		double principal = keyboard.nextDouble();
     
    		while(principal < 1 || principal > 10000)
    		{
    			System.out.println("Please enter a value between 1 and 10,000");
    			principal = keyboard.nextDouble();
    		}
     
    		System.out.println("What is the interest rate (value from 0 to 1)");
    		double interestRate = keyboard.nextDouble();	
     
    		while(interestRate < 0 || interestRate > 1)
    		{
    			System.out.println("Please enter a value between 0 and 1");
    			interestRate = keyboard.nextDouble();
    		}
    	}
     
     
    	public void menu2()
    	{
    		System.out.println("How many years do you plan to invest? (1-20)");
    		int numberOfYears = keyboard.nextInt();
     
    		while(numberOfYears < 1 || numberOfYears > 20)
    		{
    			System.out.println("Please enter a number between 1 and 20.");
    			numberOfYears = keyboard.nextInt();
    		}
     
    		System.out.println("How many times per year? (1-12)");
    		int numberOfTimes = keyboard.nextInt();
     
    		while(numberOfTimes < 1 || numberOfTimes > 12)
    		{
    			System.out.println("Please enter a number between 1 and 12.");
    			numberOfTimes = keyboard.nextInt();
    		}
     
     
    	}
     
     
    	public void menu3(double principal, double interestRate, int numberOfYears, int numberOfTimes)
    	{
    		System.out.println("If you invested " + principal + " in a bank account paying " 
    			+ interestRate + " for " + numberOfYears + "years, \ncompounded " + numberOfTimes
    			+ "times per year, then your balance will be:");
     
    	}
     
    	public void menu4()
    	{
     
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help using parameters

    Please edit your code and add code tags to preserve the formatting.
    See: BB Code List - Java Programming Forums

    I don't know how to incorporate the above values into this method
    Are the variables with the needed values at the class level of scope or are they local to some methods?
    If they were class variables all the methods can see them.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help using parameters

    All the variables have to go through my other class to be calculated into the futureValue variable. Thats what is confusing me. Here is my other class if that helps.
    //This class calculates values for an Investment.
     
    public class Investment
    {
    	private double principal = 0.0;
    	private double interestRate = 0.0;
     
    	//Constructor
    	public Investment(double balance, double rate)
    	{
    		principal = balance;
    		interestRate = rate;
    	}
    	//Setter for principal
    	public void setPrincipal(double p)
    	{
    		principal = p;
    	}
    	//Setter for Interest Rate
    	public void setInterestRate(double i)
    	{
    		interestRate = i;
    	}
     
    	//Getter for principal
    	public double getPrincipal()
    	{
    		return principal;
    	}
     
    	//Getter for Interest Rate
    	public double getInterestRate()
    	{
    		return interestRate;
    	}
     
     
    	//Future Compounded Value
    	public double getFutureValue(int numberOfYears, int numberOfTimes)
    	{
    		return principal * Math.pow((1 + interestRate / numberOfTimes), (numberOfYears * numberOfTimes));
    	}
     
     
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help using parameters

    Where are the variables that hold the values you need? If they are in the Interest class, then you need a reference variable that points to an instance of the Interest class so you can call the Interest class's methods to get those values.
    Interest theInterest = new Interest(...); // some where create an instance and save its address
    ...
    aVar = theInterest.getSomeValue(); // use that variable from above to get to the class's data

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help using parameters

    Do I create my object in the method menu3() or do I have to declare it in the main method?

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help using parameters

    The variables I need are principal, interestRate, numberOfYears, and numberOfTimes, in the main class and they have to go through to Investment class to calculate the value. I want to display them in menu3(). I didn't know if I had to create a setter and getter for numberOfYears as well as numberOftimes in the Investment class or could I just pass them to it when I call the method.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help using parameters

    pass them to it when I call the method.
    If the caller of the method has the values, then passing them as parameters would be the way to do it.

    Do I create my object in the method menu3() or do I have to declare it in the main method?
    You'll probably want to define it as a class variable, so you can use it in different methods. If you define it in a method as a local variable, then no other methods will be able to use it.
    You can define a variable as a class variable and give it a value in any of the methods in the class.
    Last edited by Norm; August 9th, 2011 at 07:30 PM.

  8. The Following User Says Thank You to Norm For This Useful Post:

    white97 (August 9th, 2011)

Similar Threads

  1. Replies: 7
    Last Post: July 21st, 2011, 02:29 PM
  2. Actual and Formal Parameters
    By mikec2500 in forum Object Oriented Programming
    Replies: 4
    Last Post: February 10th, 2011, 08:48 AM
  3. formal and actual parameters
    By gemma in forum Java Theory & Questions
    Replies: 3
    Last Post: December 7th, 2010, 11:14 AM
  4. Actual and formal parameters
    By javanoobie in forum Java Theory & Questions
    Replies: 4
    Last Post: December 5th, 2010, 08:29 AM
  5. New to vectors passing parameters
    By osmaavenro in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 30th, 2010, 04:32 PM