Answer to Question #48932 in Java | JSP | JSF for felly

Question #48932
Write a menu driven program using SWITCHES that provides 3 options.
• The first option allows the user enter a temperature in Celsius and displays the corresponding Fahrenheit (F) temperature.
• The second option allows the user enter a temperature in Fahrenheit and displays the corresponding Celsius (C) temperature.
• The third option allows the user to quit. (Hint: F=(9C/5)+ 32)
1
Expert's answer
2014-11-19T01:18:41-0500
jtf = new JTextField("Enter the temperature", 13);//create the textfield to enter //temperature

jp.add(jtf);

jlTemperature = new JLabel(" ",Label.LEFT);//create the label to display the //textfield

jp.add(jlTemperature);

jf.add(jp);

jf.pack(); //setthe form of the JFrame

jf.setVisible(true);// make visible JFrame

}

public void actionPerformed(ActionEvent ae ){

Stringchoice = ae.getActionCommand();

switch(choice){

case "Convertto Fahrenheit":

try{//checkthe entered text for wrong format(e.g.: “abc” or “$, %”)

StringstringTextField = jtf.getText();//get the entered text

Doubled = Double.parseDouble(stringTextField);// transfer to //number format for calculation

d= 9 * d / 5 + 32;// calculation

stringTextField= String.valueOf(format.format("%.0f", d));//correctingthe result

jlTemperature.setText(stringTextField);

}catch(NumberFormatException e){

jtf.setText("Enterthe temperature");//if the entered text has //wrong format

jlTemperature.setText("");

}

break;

case "Convertto Celsius":

StringstringTextField = jtf.getText();

try{

Doubled = Double.parseDouble(stringTextField);

d= 5 * (d - 32) / 9;

stringTextField= String.valueOf(format.format("%.0f", d));

jlTemperature.setText(stringTextField);

}catch(NumberFormatException e){

jtf.setText("Enterthe temperature");

jlTemperature.setText("");

}

break;

case "Exit":

System.exit(0);//if you select item “Exit”
}

}//method
}//class


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS