5 Replies - 1953 Views - Last Post: 14 October 2010 - 11:03 AM Rate Topic: -----

#1 DirtyMartyr  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 12
  • Joined: 30-September 10

How to use several methods with arguments/condition statements

Posted 11 October 2010 - 07:51 PM

I'm working on my latest assignment, & I'm having a lot of trouble arranging my script in the right format. The original assigment is as follows:

Write a program that asks the user to enter a distance in meters. The program will then present the following menu of selections:

1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program

The program will convert the distance to kilometers, inches, or feet, depending on the user’s selection. Here are the specific requirements:

• Write a method named calcKilometers, which accepts the number of meters as an argument and returns the number of kilometers using the following formula: kilometers = meters * .001
• Write a method named calcInches, which accepts the number of meters as an argument and returns the number of inches using the following formula: inches = meters * 39.37
• Write a method named calcFeet, which accepts the number of meters as an argument and returns the number of feet using the following formula: feet = meters * 3.281
• Write a void method named menu that displays the menu of selections. This method should not accept any arguments.
• The program should continue to display the menu until the user enters 4 to quit the program.
• The program should not accept negative numbers for the distance in meters.
• If the user selects an invalid choice from the menu, the program should display an error message.
• Here is an example session with the program, using console input. The users input is shown in bold:

Enter a distance in meters: 500 [Enter]
1. Convert to kilometers.
2. Convert to inches.
3. Convert to feet.
4. Quit the program.

Enter your choice: 1 [Enter]
500 Meters is 0.5 kilometers.

1. Convert to kilometers.
2. Convert to inches.
3. Convert to feet.
4. Quit the program.

Enter your choice: 4 [Enter]
Bye!

The purpose of this assignment is to get familiar with writing and calling methods. I have most of my script already written out (except I need to add error statements when user enters negative number, or invalid menu choice), but I'm having a lot of trouble organizing & correctly laying out my methods/variables/arguments/conditions. I'm getting about 20+ errors, & I know this is due to incorrect format. Otherwise, I'm usually pretty decent at identifying & correcting errors. I was wondering if someone could please offer some advice, an example script, or any other tidbits to get me going on the right track?

Here is my script thus far:

/***********************************************************************
@Title:	BSarahConversion.java
@Purpose:	To get familiar with writing and calling methods
@Author:    (BSarah)
@Date:   	(October 09, 2010)
@Version:	1.0
************************************************************************/
import java.util.Scanner;
public class BSarahConversion
{
public static void main(String[] args)
{

	int quit = 4;

	int usersChoice;

	Scanner sc = new Scanner(System.in);
	System.out.print("Please enter distance in meters: ");
	inputMeters = sc.nextDouble();
	menu();
	System.out.println("Please make a selection between 1-4: ");
	usersChoice = sc.nextInt();

do
menu();
while (usersChoice = sc.nextInt());
	if (usersChoice==1)
	{
		calcKilometers();
		}
	if (usersChoice==2)
	{
		calcInches();
		}
	if (usersChoice==3)
	{
		calcFeet();
		}
	while (usersChoice==quit);
	{
		System.out.println("Good bye!");
		}

public static void menu()
{
	System.out.println("1. Convert to kilometers");
	System.out.println("2. Convert to inches");
        System.out.println("3. Convert to feet");
        System.out.println("4. Quit the program");

}

public static calcKilometers()
{
	double convertedInput = (inputMeters * .001);
	System.out.println(convertedInput);

	}

public static void calcInches()
{
	double convertedInput = (inputMeters * 39.37);
	System.out.println(convertedInput);

	}

public static void calcFeet()
{
	double convertedInput = (inputMeters * 3.281);
	System.out.println(convertedInput);

}
}
}



**I also suspect my loop is wrong. Especially since I only need to loop the menu & user inputMeters. I think once I place my condition statments in the methods as arugments, then that will correct most of that problem. Still not sure how to do this correctly tho.

This post has been edited by DirtyMartyr: 11 October 2010 - 08:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to use several methods with arguments/condition statements

#2 SegFaulty  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 31
  • Joined: 11-October 10

Re: How to use several methods with arguments/condition statements

Posted 11 October 2010 - 10:10 PM

First you don't want to put other methods inside the main method; they go outside it. Here's how I would have set up the program:

import java.util.Scanner;

public class test{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		System.out.print("Please enter distance in meters: ");
		double inputMeters = sc.nextDouble();
		
		while(true){  //This loop will run until we issue the break command
			menu();
			System.out.print("Please make a selection 1-4: ");
			int usersChoice = sc.nextInt();
			if(usersChoice==1)  //No need for brackets if the there's only one action following the if statement
				calcKilometers(inputMeters);
			if(usersChoice==2)
				calcInches(inputMeters);
			if(usersChoice==3)
				calcFeet(inputMeters);
			if(usersChoice==4)
				break; //here we break the loop
		}
		   
	}
	
	public static void menu(){
		System.out.println("1. Convert to kilometers");
		System.out.println("2. Convert to inches");
		System.out.println("3. Convert to feet");
		System.out.println("4. Quit the program");
	}
	
	public static void calcKilometers(double inputMeters){
		double convertedInput = (inputMeters * .001);
		System.out.println(convertedInput);
	}

	public static void calcInches(double inputMeters){
		double convertedInput = (inputMeters * 39.37);
		System.out.println(convertedInput);
	}
	
	public static void calcFeet(double inputMeters){
		double convertedInput = (inputMeters * 3.281);
		System.out.println(convertedInput);
	}
}



Hope this helps!
Was This Post Helpful? 1
  • +
  • -

#3 DirtyMartyr  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 12
  • Joined: 30-September 10

Re: How to use several methods with arguments/condition statements

Posted 12 October 2010 - 10:08 AM

View PostSegFaulty, on 11 October 2010 - 10:10 PM, said:

First you don't want to put other methods inside the main method; they go outside it. Here's how I would have set up the program:

import java.util.Scanner;

public class test{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		System.out.print("Please enter distance in meters: ");
		double inputMeters = sc.nextDouble();
		
		while(true){  //This loop will run until we issue the break command
			menu();
			System.out.print("Please make a selection 1-4: ");
			int usersChoice = sc.nextInt();
			if(usersChoice==1)  //No need for brackets if the there's only one action following the if statement
				calcKilometers(inputMeters);
			if(usersChoice==2)
				calcInches(inputMeters);
			if(usersChoice==3)
				calcFeet(inputMeters);
			if(usersChoice==4)
				break; //here we break the loop
		}
		   
	}
	
	public static void menu(){
		System.out.println("1. Convert to kilometers");
		System.out.println("2. Convert to inches");
		System.out.println("3. Convert to feet");
		System.out.println("4. Quit the program");
	}
	
	public static void calcKilometers(double inputMeters){
		double convertedInput = (inputMeters * .001);
		System.out.println(convertedInput);
	}

	public static void calcInches(double inputMeters){
		double convertedInput = (inputMeters * 39.37);
		System.out.println(convertedInput);
	}
	
	public static void calcFeet(double inputMeters){
		double convertedInput = (inputMeters * 3.281);
		System.out.println(convertedInput);
	}
}



Hope this helps!



Thanks for working out the kinks!! I'm reading thru your comments, & comparing my original script now to study what I did wrong. I've been staring at my book for days, but the examples in the book seem to show different ways to structure methods depending on what the script is supposed to do (pass an argument, layer, etc.) & I got really confused. Originally, I declared an extra variable in each method. I see now how & where they are supposed to used. Also, I knew my loop was wrong, but now I understand how to use true/false conditions accompanied by the nifty old break statement-- so thanks for that especially! I wasnt sure I used proper indentation either, so I'm glad to see a more correct, cleaner example. Again, many thanks!!!

This post has been edited by DirtyMartyr: 12 October 2010 - 10:11 AM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9025
  • View blog
  • Posts: 33,465
  • Joined: 27-December 08

Re: How to use several methods with arguments/condition statements

Posted 12 October 2010 - 10:18 AM

Better practice would be to have the methods return values rather than printing the values. This makes the methods more modular and reusable, as the internal program can utilize the methods to obtain the results rather than just outputting them to the console, where the program can't further use the data.

public static double milesToFeet(double miles){
   return miles*5860;
}


Was This Post Helpful? 1
  • +
  • -

#5 DirtyMartyr  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 12
  • Joined: 30-September 10

Re: How to use several methods with arguments/condition statements

Posted 12 October 2010 - 10:47 AM

View Postmacosxnerd101, on 12 October 2010 - 10:18 AM, said:

Better practice would be to have the methods return values rather than printing the values. This makes the methods more modular and reusable, as the internal program can utilize the methods to obtain the results rather than just outputting them to the console, where the program can't further use the data.

public static double milesToFeet(double miles){
   return miles*5860;
}



Technically, the assignment asks me to return the values. So, your right, I really need to incorporate this into my script, and correctly-- thanks! :)
Was This Post Helpful? 0
  • +
  • -

#6 DirtyMartyr  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 12
  • Joined: 30-September 10

Re: How to use several methods with arguments/condition statements

Posted 14 October 2010 - 11:03 AM

import java.util.Scanner;

public class BSarahConversion{
	public static void main(String[] args){
		
		Scanner sc = new Scanner(System.in);
		System.out.print("Please enter distance in meters: ");
		double inputMeters = sc.nextDouble();

		while(inputMeters<0){
			System.out.println("Error: Cannot accept negative value.");
			System.out.print("\nPlease enter distance in meters: ");
			inputMeters = sc.nextDouble();
			if(inputMeters>0)
			continue;}

	        menu();
	        int usersChoice = sc.nextInt();

			if(usersChoice<1 || usersChoice>4)

		do{
			System.out.println("Error: invalid menu choice.");
			menu();
			usersChoice = sc.nextInt();

				}while(usersChoice<1 || usersChoice>4);

				if(usersChoice==1)  
					calcKilometers(inputMeters);
				if(usersChoice==2)
					calcInches(inputMeters);
				if(usersChoice==3)
					calcFeet(inputMeters);
				if(usersChoice==4)
					System.out.println("Good bye!");
					System.exit(4);

do{
menu();
usersChoice = sc.nextInt();
}while(usersChoice!=4);

}

	public static void menu(){
		System.out.println("\n1. Convert to kilometers");
		System.out.println("2. Convert to inches");
		System.out.println("3. Convert to feet");
		System.out.println("4. Quit the program");
		System.out.print("Please make a selection 1-4: ");
	}

	public static void calcKilometers(double inputMeters){
		double convertedInput = (inputMeters * .001);
		System.out.println(inputMeters + " meters is " + convertedInput + " kilometers.");
	}

	public static void calcInches(double inputMeters){
		double convertedInput = (inputMeters * 39.37);
		System.out.println(inputMeters + " meters is " + convertedInput + " inches.");
	}

	public static void calcFeet(double inputMeters){
		double convertedInput = (inputMeters * 3.281);
		System.out.println(inputMeters + " meters is " + convertedInput + " feet.");
	}
	}



I'm still having trouble with my script. After the user selects a menu choice, & after the results are outputted, I need to keep looping back to the menu so that the user can pick another menu selection. I dont need to prompt the user to continue, I just need to automatically loop until the user selects menu option 4, which is to exit. Would it be easier to contain the users menu choice in the menu method? Since I'm supposed to write a menu method that doesnt accept any arguments, I figure that option is out?? My code runs correctly thus far, but my third loop doesnt run as it should. Any help would be much appreciated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1