Personally I’m a little bored with the basics; especially since they hardly do anything of use. This tutorial will attempt to show you how to prompt the user for input and then have your program perform a basic operation with that input. Before reading this tutorial, you should have a basic understanding of flow control and operators.
The first thing I am going to do is create a new package called math. By naming conventions, all characters in package names are lowercase. Within the math package I am going to create a new class called CelsiusToKelvin. Again sticking with the naming conventions, the class name starts with an uppercase letter and each subsequent word is capitalized. We are going to create our constructor and our main method which instantiates our class. Note I included comments which are denoted by two forward slashes “//” everything to the right of those is considered a comment and is ignored by the compiler. At this point your code should look something like this, with out the comments:
Code:
package math;//package declaration
public class CelsiusToKelvin {//class header
public CelsiusToKelvin(){ //constructor header
}
public static void main (String[] args){//main
new CelsiusToKelvin();//instantiation
}
}
At this point, it is a good idea to step back and examine the properties in the domain of your application. Our goal is to create a program that prompts the user for input, takes his input stores it as a variable, performs a basic calculation and creates a new converted variable. We can assume we are going to need two variables, a Kelvin value and a Celsius variable. So not let’s declare them:
Code:
package math;//package declaration
public class CelsiusToKelvin {//class header
double celsiusValue = 0.0; //declares celsiusValue as a double variable
double kelvinValue = 0.0; //declares kelvinValue as a double variable
public CelsiusToKelvin(){ //constructor header
}
public static void main (String[] args){//main
new CelsiusToKelvin();//instantiation
}
}
Now we want to “get” data from a user. To do that we take advantage of a class available to us
Code:
String userInput = JOptionPane.showInputDialog("Enter a Celsius Value");
At this point it’s a good idea to create a debug statement to make sure your program is actually receiving input from the user. So lets just print what they enter by adding the System.out.println after the input dialog.
Code:
package math;//package decleration
import javax.swing.JOptionPane;
public class CelsiusToKelvin {//class header
double celsiusValue = 0.0; //declares celsiusValue as a double variable
double kelvinValue = 0.0; //declares kelvinValue as a double variable
public CelsiusToKelvin(){ //constructor header
String userInput = JOptionPane.showInputDialog("Enter a Celsius Value");
System.out.println(userInput);
}
public static void main (String[] args){//main
new CelsiusToKelvin();//instantiation
}
}
However, if you haven’t noticed we have a problem. When the user enters a value, it is returned as a string, and because Java is pickey, you cant perform mathematical operations on strings. So we now need to make use of another class available to us.
We now add this line:
Code:
celsiusValue = Double.parseDouble(userInput);
We are now going to program our equation and we are finished.
Code:
package math;//package decleration
import javax.swing.JOptionPane;
public class CelsiusToKelvin {//class header
double celsiusValue = 0.0; //declares celsiusValue as a double variable
double kelvinValue = 0.0; //declares kelvinValue as a double variable
public CelsiusToKelvin(){ //constructor header
String userInput = JOptionPane.showInputDialog("Enter a Celsius Value");
//System.out.println(userInput);
celsiusValue = Double.parseDouble(userInput);
kelvinValue = celsiusValue - 273.0;
System.out.println(kelvinValue);
}
public static void main (String[] args){//main
new CelsiusToKelvin();//instantiation
}
}