Skip to main content

Need help creating a basic Temp Converter (Basic)

1 reply [Last post]
lederp
Offline
Joined: 2011-08-08
Points: 0

I am attempting to create a Temperature Converter using a GUI. It will incorporate two classes, one for the GUI and one for the CLI. I am unsure of how to implement them together and I'm also having some trouble with a few things;

First thing is this code I have created, could this be used with the GUI? Or am I going down the wrong road completely? Advice is appreciated;

Quote:
import java.util.Scanner;

public class tempConverter
{
private double celsius;
private double fahrenheit;

public tempConverter()
{
celsius = 0.0;
fahrenheit = 0.0;
}

public double getCelsius()
{
return celsius;
}

public double getFahrenheit()
{
return fahrenheit;
}

public void setCelsius(double temperature)
{
celsius = temperature;
}

public void setFahrenheit(double temperature)
{
fahrenheit = temperature;
}

public double toCelsius(double fahrenheit)
{
return (fahrenheit - 32.0) * (5.0 / 9.0);
}

public double toFahrenheit(double celsius)
{
return (celsius * (9.0 / 5.0)) + 32.0;
}

public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int menuSelection = 0;
double temperature = 0.0;

System.out.printf("Temperature Converter\n");
System.out.printf("\n1. Convert from Celsius to Fahrenheit.");
System.out.printf("2. Convert from Fahrenheit to Celsius.\n");

System.out.printf("\nEnter a selection (1-2): ");
menuSelection = keyboard.nextInt();

System.out.printf("\nEnter a temperature to convert from: ");
temperature = keyboard.nextDouble();

switch(menuSelection)
{
case 1:
// case 1 -> converting from Celsius to Fahrenheit.
//Set Celsius to temperature.
//Set Fahrenheit using mutator method (setFahrenheit(temperature)).
break;
case 2:
// case 2 -> converting from Fahrenheit to Celsius.
//Set Fahrenheit to temperature.
//Set Celsius using mutator method (setCelsius(temperature)).
break;
default:
System.out.println("Invalid selection.");
break;
}

//Print Celsius/Fahrenheit conversion temps.
}
}

The next part is within' the GUI I have created a Frame using JFrame, I have also created a menuBar and within' this menuBar there is three tabs. I am stuck with the 'Help' tab, I've created a drop down called 'Instructions', what I'm looking to do is for a popup window to open when 'Instructions' is clicked displaying some basic instructions on how to use the program.

The final part I am stuck with is creating buttons using a boxLayout, I have tried many guides and I can't seem to get it working, I would like a row of numbers starting from 0-9 that will allow me to click one of these buttons and input a number into a label (Creating the label has also become a problem).

If anyone can help me with these problems or guide me in the right direction I would be so greatful.

Thanks.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
CodeCook
Offline
Joined: 2011-08-24
Points: 0

package ab.video.client;

import java.util.Scanner;

public class tempConverter { /* Big Problem: this should start with a Capital Letter: TempConverter */

/* Question: a "converter" receives input, and spits-out output. Do you really need to maintain (persistant) state variables? What is a tempConverter *responsible* for? */
private double celsius;
private double fahrenheit;

public tempConverter() {
celsius = 0.0;
fahrenheit = 0.0;
}

public double getCelsius() {
return celsius;
}

public double getFahrenheit() {
return fahrenheit;
}

public void setCelsius(double temperature) {
celsius = temperature;
}

public void setFahrenheit(double temperature) {
fahrenheit = temperature;
}

public double toCelsius(double fahrenheit) {
return (fahrenheit - 32.0) * (5.0 / 9.0);
}

public double toFahrenheit(double celsius) {
return (celsius * (9.0 / 5.0)) + 32.0;
}

public static void main(String[] args) {

/* You were missing this line */
tempConverter myTempConverterObject = new tempConverter(); /* You got confused here: a class is a blueprint of a thing. An object is a thing. You need to create a converter *object* (in other words, instantiate your class) */

Scanner keyboard = new Scanner(System.in);
int menuSelection = 0;
double temperatureRecievedFromUser = 0.0; /* in a temperature converter, everything is a "temperature". Be explicit; use good names. */
double convertedTemperature = 0.0;

System.out.printf("Temperature Converter\n");
System.out.printf("1. Convert from Celsius to Fahrenheit.\n");
System.out.printf("2. Convert from Fahrenheit to Celsius.\n");

System.out.printf("Enter a selection (1-2):\n ");
menuSelection = keyboard.nextInt();

System.out.printf("Enter a temperature to convert from: ");
temperatureRecievedFromUser = keyboard.nextDouble();

switch (menuSelection) {
case 1:
// case 1 -> converting from Celsius to Fahrenheit.
//Set Celsius to temperature.
myTempConverterObject.setCelsius(temperatureRecievedFromUser);
//Set Fahrenheit using mutator method (setFahrenheit(temperature)).
convertedTemperature = myTempConverterObject.toFahrenheit( temperatureRecievedFromUser );
System.out.println(convertedTemperature);
break;
case 2:
// case 2 -> converting from Fahrenheit to Celsius.
//Set Fahrenheit to temperature.
myTempConverterObject.setFahrenheit(temperatureRecievedFromUser);
//Set Celsius using mutator method (setCelsius(temperature)).
convertedTemperature = myTempConverterObject.toCelsius( myTempConverterObject.getFahrenheit() );
System.out.println(convertedTemperature);
break;
default:
System.out.println("Invalid selection.");
break;
}

    //Print Celsius/Fahrenheit conversion temps.
    //You could print here but if the user selected an invalid menu option invalid, you'll be printing zero!<br />    //And, the user might not read the message &quot;Invalid selection.&quot; that was printed above<br />    // and believe (wrongly) that 0.0 is a valid conversion.<br />    // Persistent data does allow you to do this...<br />    System.out.println( myTempConverterObject.getCelsius() + &quot; celcius = &quot; + myTempConverterObject.getFahrenheit() + &quot; fahrenheit.&quot;);<br />  }<br />  /* I'm not giving you a clean solution. This code needs more work to clean it up.<br />   * but I hope it helps you on the right path. Keep learning!<br />   */<br />}<br />