Hi all. How would I modify my current program to make it perform the conversion with the main method calling other methods instead of performing the conversion in the main method itself? I have attached a .pdf file that has a list of all the methods that must be used with this. Any help is greatly appreciated! Here is my current program:

import java.util.Scanner;

public class Javaid1492HW1 {
	public static void main (String [] args){
		Scanner keyboard = new Scanner(System.in);
		System.out.println();
		System.out.println();
		System.out.println("This is Mohammad's program:");
		int value;
        value = 1;
        while(value != 0)
		// Display menu
		{
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println("  Welcome to the Distance Conversion System");
		System.out.println();
		System.out.println("                  Main menu                 ");
		System.out.println("	     ____________________                ");
		System.out.println();
		System.out.println("	1- Convert from Feet to Inch.        ");
		System.out.println("	2- Convert from Inch to Feet.        ");
		System.out.println("	3- Convert from Yard to Feet.        ");
		System.out.println("	4- Convert from Feet to Yard.        ");
		System.out.println("	5- Convert from Yard to Mile.        ");
		System.out.println("	6- Convert from Mile to Yard.        ");
		System.out.println("	0- Exit                              ");


		System.out.println();
		System.out.print("Enter your choice: ");
			value = keyboard.nextInt();



		switch (value){
		case 1:
	System.out.print("Enter a distance in feet to be converted to inches: ");
				value = keyboard.nextInt();				System.out.println(value + " feet to inches: " + value * 12);
			break;
		case 2:
	System.out.print("Enter a distance in inches to be converted to feet: ");
				value = keyboard.nextInt();
	System.out.println(value + " inches to feet: " + (double) value/12.0);
			break;
		case 3:
	System.out.print("Enter a distance in yards to be converted to feet: ");
				value = keyboard.nextInt();
	System.out.println(value + " yards to feet: " + value * 3);
			break;
		case 4:
	System.out.print("Enter a distance in feet to be converted to yards: ");
				value = keyboard.nextInt();
	System.out.println(value + " feet to yards: " + (double) value/3.0);
			break;
		case 5:
	System.out.print("Enter a distance in yards to be converted to miles: ");
				value = keyboard.nextInt();
	System.out.println(value + " yards to miles: " + (double) value/1760.0);
			break;
		case 6:
	System.out.print("Enter a distance in miles to be converted to yards: ");
				value = keyboard.nextInt();
	System.out.println(value + " miles to yards: " + value * 1760);
			break;
		case 0:
			break;

		default:
	System.out.println("Invalid selection, please try again..!");


		}
		}

	}

}

Recommended Answers

All 3 Replies

I've not looked through the PDF but under each case X: you simply replace the code in there by a method call i.e

...
case 1:
feetToInch();
break;
...

and then you declare your feetToInch() method (note that it has to be static in your case)

private/protected/public static void feetToInch(){
...
}

thank you, can anyone please explain in more detail using the rules outlined in the attachment?

this is what I have so far but I don't know where to go from here...

import java.util.Scanner;

public class mo1 {
	public static void main (String [] args){
	Scanner keyboard = new Scanner(System.in);

		System.out.print(main);


switch (value){

case 1:
feet2Inch();
break;

case 2:
inch2Feet();
break;

case 3:
Yard2Feet();
break;

case 4:
feet2Yards();
break;

case 5:
yard2Mile();
break;

case 6:
mile2Yard();
break;

case 0:
break;

default:
System.out.println("Invalid selection, please try again..!");

}

public static void main()
		{

			System.out.println("This is Mohammad's program:");
		int value;
        value = 1;
        while(value != 0)
		// Display menu
		{
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println("  Welcome to the Distance Conversion System");
		System.out.println();
		System.out.println("                  Main menu                 ");
		System.out.println("	     ____________________                ");
		System.out.println();
		System.out.println("	1- Convert from Feet to Inch.        ");
		System.out.println("	2- Convert from Inch to Feet.        ");
		System.out.println("	3- Convert from Yard to Feet.        ");
		System.out.println("	4- Convert from Feet to Yard.        ");
		System.out.println("	5- Convert from Yard to Mile.        ");
		System.out.println("	6- Convert from Mile to Yard.        ");
		System.out.println("	0- Exit                              ");


		System.out.println();
		System.out.print("Enter your choice: ");
			value = keyboard.nextInt();

		}

		public static double feet2Inch(double num1)
{
	double inches;
	inches = (double) num1*12.0;
	return inches;
}

       public static double inch2Feet (double num1)
       {
       	double feet;
       	feet = (double) num1/12.0;
       	return feet;
       }

       public static double Yard2Feet (double num1)
       {
       	double feet;
       	feet = (double) num1*3.0;
       	return feet;
       }

       	public static double feet2Yards (double num1)
       	{
       		double yards;
       		yards = num1/3.0;
       		return yards;
       	}

       	public static double yard2Mile (double num1)
       	{
       		double miles;
       		miles = num1/1760.0;
       		return miles;
       	}

       	public static double mile2Yard (double num1)
       	{
       		double yards;
       		yards = num1*1760.0
       		return yards;
        	}
		}
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.