Write a Java program to continue creating your own userdefined
methods and introduce some do while loops. Write two valuereturning methods called farToCel() and celToFar(). These two methods will convert temperatures from Fahrenheit to Celsius and Celsius to Fahrenheit respectively. They will each take a single int parameter and return the converted value as an int.
Write an additional value returning method called displayMenu() to display a three
item menu and read input from the user which is their selection and return that as a
char. Consider using the Character class method toUpperCase() to narrow the
number of choices from six {F, f, C, c, Q, q} to three {F, C, Q}.
Note that displayMenu() has no parameters. The displayMenu() method should
only return one of three possible values. The choices are 'F' for Fahrenheit to Celsius
conversion, 'C' for Celsius to Fahrenheit conversion and 'Q' to quit.
Start by calling displayMenu() within a dowhile loop in the main() method
which will capture the return value of displayMenu() into a char variable. This value
will be used to determine what type of conversion is to be done and the loop will termine
when the user enters 'Q'.This would be a good opportunity for a dowhile
loop in the displayMenu()method as the user continues to make a bad selection. We would stay in the displayMenu() method until the user chooses a valid selection. By doing so, this method can never return bad selections to the main() method.
Once you have mastered your displayMenu() method, you can proceed to the other
two. Also, note that displayMenu() is not responsible for reading the temperatures;
the main() method will do this based on the choice made by the user.
The calculation for Fahrenheit to Celsius is:
( f - 32 ) * 5 / 9
The calculation for celsius to fahrenheit is:
c * 9 / 5 + 32

Just started and wanted to get this started for the class before I left class :) Below is just the start of my program not completed yet but always open to input from others to tell me how bad it looks and what I should change to make it look better.

For instance the printf lines will be changed later to println with /t to make the spaces.

Keep your head up dunkin donuts girl :)_________________________________________________________________________________________

import java.util.*;
public class p7
{

public static void main(String[] args)
 {
 displayMenu();
// farToCel();
// celToFar();

 }

	public static void displayMenu()
 	{
 	 char letter;
 	 int x, y;
	 String input;


	 Scanner kb = new Scanner(System.in);

	 System.out.println("Please select one of the following: \n");
	 System.out.printf("        F - to convert Fahrnheit to Celsius %n");
	 System.out.printf("        C - to conv2ert Celsius to Fahrnheit %n");
	 System.out.printf("%n        Q - to Quit. %n");
	 System.out.printf("%nChoice:");
	 input = kb.nextLine();
	 letter = input.charAt(0);



 	/*while(input.toUpperCase("F, C, Q"))
		{
		 System.out.println("The temperature " + input + "is" + (farToCel) + "Celsius.");
		 input = kb.nextLine();
		 letter = input.charAt(0);

		 System.out.println("The temperature " + input + "is" + (celToFar) + "Fahrenheit.");
		}
	 }

	 public static void farToCel()
	 	{
	 	 System.out.print("Please enter the Fahrenheit temperature:");
	 	 x = kb.nextInt();
	 	}

     public static void celToFar()
	 	{
	 	 System.out.println("Please enter the Celsius temperature:");
	 	 y = kb.nextInt();
		}
*/
 }
}

Recommended Answers

All 12 Replies

1. First, display menu should return a char so it should be

public static char displayMenu()

.
2. Then you need to enclose it in a dowhile loop so that if user does not enter (FC) then it will not terminate. Do while structure is

do {
// your input method here
}
while (letter.toUpperCase() != 'C' || letter.toUpperCase() != 'F')

Restarted too many problems with preious code and am now getting stuck trying to link everything together. How would I go about linking what they entered to the next user defined method? Below I have F, C, and Q = to what they represent but how do I link it to what the user entered. And is System.Exit(0) the appropriate way to end program in DOS? I know it works for JPaneOption but not sure about DOS.

import java.util.*;

public class p7
{
 static Scanner kb = new Scanner(System.in);

 public static void main(String[] args)

 {

  char letter, C, F, Q;
  int cel,fah;

  displayMenu();

	}


public static char displayMenu()
	{

	 do
		{

		 System.out.println("Please select one of the following: \n");
		 System.out.println("\tF - to convert Fahrnheit to Celsius ");
		 System.out.println("\tC - to convert Celsius to Fahrnheit\n");
		 System.out.println("\tQ - to Quit.");
		 System.out.printf("%nChoice:");
		 letter = kb.charAt(0);

		// F = farToCel();
		// C = celToFar();
		// Q = System.exit(0);
		}
	 while(letter.toUpperCase() != 'C' || letter.toUpperCase() != 'F' || letter.toUpperCase() != 'Q');

//public static int farToCel()

	//( f - 32 ) * 5 / 9

//public static int celToFar()

	//c * 9 / 5 + 32

 }
}

WEEE!!! Program goes boom... umm... yep... I don't got it... any help would be great at this time.

import java.util.*;

public class p7
{
 static Scanner kb = new Scanner(System.in);

 public static void main(String[] args)

 {

  char letter, C, F, Q;
  int cel,fah, fahrenheit, celsius;

  displayMenu();

  if(letter == 'C')
     {
      celToFar();
	 }
  else if (letter == 'F')
	 {
	 farToCel();
	 }
  else (letter == 'Q');
	 {
	  System.Exit(0);
     }
 }


public static char displayMenu()
	 {
	  do
		{
 		 char letter, C, F, Q;
 		 letter = kb.next().charAt(0);
		 System.out.println("Please select one of the following: \n");
		 System.out.println("\tF - to convert Fahrnheit to Celsius ");
		 System.out.println("\tC - to convert Celsius to Fahrnheit\n");
		 System.out.println("\tQ - to Quit.");
		 System.out.printf("%nChoice:");
		}

	while(letter.toUpperCase() == 'C' || letter.toUpperCase() == 'F' || letter.toUpperCase() == 'Q');
	 }

public static int celToFar()
	{
	 do
	 {
	 System.out.println("Please enter the Celsius temperature:");
	 cel = kb.nextInt();
	 celsius = (cel) * 9 / 5 + 32;
	 System.out.println("The temperature " + cel + " Celsius is " + celsius + "Fahrenheit.");
	 }
	while(letter.toUpperCase() != 'F');
	}

public static int farToCel()
	{
	 do
 	 {
	 System.out.println("Please enter the Fahrenheit temperature:");
	 fah = kb.nextInt();
	 fahrenheit = ((fah) - 32) * 5 / 9;
	 System.out.println("The temperature " + fah + " Fahrenheit is " + fahrenheit + " Celsius.");
 	 }
	while(letter.toUpperCase() != 'C');
	}
}

1. You need to do the display menu while letters are NOT C, F, Q.
condition should be

while(letter.toUpperCase() != 'C' || letter.toUpperCase() != 'F' || letter.toUpperCase() != 'Q');

instead of

while(letter.toUpperCase() == 'C' || letter.toUpperCase() == 'F' || letter.toUpperCase() == 'Q');

notice the != and the ==

2. you dont need to do the do-while loop in celToFar and farToCel.

Try revising your code and run it. And please be specific with your problem. Is it doing an infinite loop?? Or program gives you wrong valuers. Like that..

Haven't had much time to work on this but here is what i have so far. I am still having trouble with the looping other than that the program works fine. Any help with the looping part would be great. Thanks for everyones help.

import java.util.*;

public class p7
{
 static Scanner kb = new Scanner(System.in);

 public static void main(String[] args)

 {

  char letter;
  int cel = 0,fah = 0, fahrenheit = 0, celsius = 0;
  letter = displayMenu();
	
	do
	{

	  if(letter == 'C')
     {
      celToFar(cel);
      System.out.println("Please enter the Celsius temperature:");
      cel = kb.nextInt();
      celsius = celToFar(cel);
      System.out.println("The temperature " + cel + " Celsius is " + celsius + "Fahrenheit.");
	 }

	  else if (letter == 'F')
	 {
	  farToCel(fah);
	  System.out.println("Please enter the Fahrenheit temperature:");
	  fah = kb.nextInt();
	  fahrenheit = farToCel(fah);
	  System.out.println("The temperature " + fah + " Fahrenheit is " + fahrenheit + " Celsius.");
	 }

	letter = displayMenu();
	}
	
	while(letter == 'Q');

 }

public static char displayMenu()
	 {
	  char letter;
	  do
		{
 		 System.out.println("Please select one of the following: \n");
		 System.out.println("\tF - to convert Fahrnheit to Celsius ");
		 System.out.println("\tC - to convert Celsius to Fahrnheit\n");
		 System.out.println("\tQ - to Quit.");
		 System.out.printf("%nChoice:");
		 letter = kb.next().charAt(0);
		 letter = Character.toUpperCase(letter);
		}

     while(letter != 'C' && letter != 'F' && letter != 'Q');
	 return letter;
	}

public static int celToFar(int cel)
	 {
      return (cel) * 9 / 5 + 32;
	 }

public static int farToCel(int fah)
	{
	 return ((fah) - 32) * 5 / 9;
 	}
}

LOL nvm problem solved... the while statment in main should be:

while(letter == 'C' || letter == 'F');

The programs runs perfectly now. Not!!! after testing it I reliazed that if "Q" is your first letter choice you are prompted again will solve this problem and repost :)

Because you use 'do-while' in your main() instead of 'while'... ;)

But according to the instructions it has to be a do while loop in the main. That part works correctly. My problem is that if the 'Q' is entered first the loop is still continuing which it shouldn't it should exit upon 'Q'. I don't think the teacher will accept this flaw and I really don't want to risk it.

You need to understand this...

do-while will ALWAYS execute anything inside the loop once before checking the condition. But while will ALWAYS check the condition before executing the content in the loop.

If your teacher said that you MUST apply ONLY the do-while to complete this assignment, ask him/her to show you how to. The do-while you are talking about is the part where you take the input from the user (displayMenu()), not the part after you have the choice from user input to work on conversion (main()).

Entering 'Q' as first option does not end program but other than that all is correct.

import java.util.*;

public class p7
{
 static Scanner kb = new Scanner(System.in);

 public static void main(String[] args)

 {

  char letter;
  int cel = 0,fah = 0, fahrenheit = 0, celsius = 0;

	letter = displayMenu();
	do
	{


	  if(letter == 'C')
     {
      celToFar(cel);
      System.out.println("Please enter the Celsius temperature:");
      cel = kb.nextInt();
      celsius = celToFar(cel);
      System.out.println("The temperature " + cel + " Celsius is " + celsius + " Fahrenheit.");
	 }

	  else if (letter == 'F')
	 {
	  farToCel(fah);
	  System.out.println("Please enter the Fahrenheit temperature:");
	  fah = kb.nextInt();
	  fahrenheit = farToCel(fah);
	  System.out.println("The temperature " + fah + " Fahrenheit is " + fahrenheit + " Celsius.");
	 }
	  letter = displayMenu();
	}
	while(letter != 'Q');

 }

public static char displayMenu()
	 {

	  char letter;

	  do
		{

 		 System.out.println("Please select one of the following: \n");
		 System.out.println("\tF - to convert Fahrnheit to Celsius ");
		 System.out.println("\tC - to convert Celsius to Fahrnheit\n");
		 System.out.println("\tQ - to Quit.");
		 System.out.printf("%nChoice:");
		 letter = kb.next().charAt(0);
		 letter = Character.toUpperCase(letter);
		}

     while(letter != 'C' && letter != 'F' && letter != 'Q');


	return letter;
	}

public static int celToFar(int cel)
	 {
      return (cel) * 9 / 5 + 32;
	 }

public static int farToCel(int fah)
	{
	 return ((fah) - 32) * 5 / 9;
 	}
}

Move Line 14 inside of the do while loop at the top and delete line 36 completely from withing the do while loop

I think your teacher meant a do-while in the main because he/she doest not assume a do-while in your displayMenu() method.

And as Taywin said, a do-while will always execute once.

If your teacher insists a do-while in the main you can.

1. Omit the do-while in your displayMenu() method.
2. Then in the main you can

do { diplayMenu();} while (your != condition heare}

3. Then you need to do a while loop to check the returned letter so if 1st letter entered is 'Q' then the program will exit.

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.