### Double - COSC 1010

```Casting, Wrapper Classes,
Static Methods,
JOptionPane Class
Casting
• Casting is used to convert one data type
to another.
• Implicit casting
– mixed arithmetic expressions
– assignments
byte -> short -> int -> long -> float -> double
• explicit casting
– use the cast operator
(type)expression
How to code an explicit cast
(type) operand
Example
double average = 93.25;
int gradeInCourse = (int) average;
//gradeInCourse is 93
public class Casting
{
public static void main(String[] args)
{
float num1 = 3;
double num2 = 2;
int num3 = 0;
num3 = (int)(num1/num2);
System.out.println(num3);
}
}
The syntax for declaring a main method
public static void main(String[] args)
{
statements
}
The main method of an InvoiceApp class
public class InvoiceApp
{
public static void main(String[] args)
{
System.out.println("Invoice app");
}
}
Type-wrapper Classes
• All the primitive data types have corresponding
classes that provide some general methods that
are useful when dealing with data of the specified
type.
– Byte, Double, Float, Integer, Long
• The classes are known as wrapper classes since
they “wrap” the primitive data type in a class.
• Look up the specific classes for their methods:
– http://docs.oracle.com/javase/7/docs/api/
Integer Class
• The Integer class wraps a value of the primitive
type int in an object.
• There are two constructors for the Integer class:
– Integer(int value)
• Constructs a newly allocated Integer object that represents
the specified int value
– Integer(String s)
• Constructs a newly allocated Integer object that represents
the int value indicated by the String parameter.
• The class provides several methods for
converting an int to a String and a String to an
int.
Double Class
• The Double class wraps a value of the primitive
type double in an object.
• There are two constructors for the Double class:
– Double(double value)
• Constructs a newly allocated Double object that represents
the primitive double argument.
– Double(String s)
• Constructs a newly allocated Double object that represents
the floating-point value of type double represented by the
string.
• In addition, this class provides several methods
for converting a double to a String and a String
to a double.
Static Methods
• Both the Integer class and the Double class
have static methods.
• Unlike a regular method which is called from
an object, a static method is called from a
class.
• Example:
int quantity = Integer.parseInt(quantityString);
The syntax for using a static method of a class
class.methodName(arguments)
Two static methods of the Integer class
Method
parseInt(String)
toString(int)
Description
Attempts to convert the String object that’s
supplied as an argument to an int type.
Converts the int value that’s supplied as an
argument to a String object and returns that
String object.
Two static methods of the Double class
Method
parseDouble(String)
toString(double)
Description
Attempts to convert the String object
that’s supplied as an argument to a double
type.
Converts the double value that’s supplied
as an argument to a String object and
returns that String object
How to convert a String object to a primitive type
For an int
String quantityString = “25”;
int quantity = Integer.parseInt(quantityString);
For a double
String priceString = “9.95”;
double price = Double.parseDouble(priceString);
How to convert a primitive type to a String object
For an int
int counter = 15;
String counterString = Integer.toString(counter);
For a double
double price = 37.95;
String priceString = Double.toString(price);
import javax.swing.JOptionPane;
public class Addition
{
public static void main(String[] args)
{
String firstNumber;
String secondNumber;
int number1;
int number2;
int sum;
firstNumber = JOptionPane.showInputDialog(null, "Enter first integer",
"Input", JOptionPane.QUESTION_MESSAGE);
secondNumber = JOptionPane.showInputDialog(null, "Enter second integer",
"Input", JOptionPane.QUESTION_MESSAGE);
number1 = Integer.parseInt(firstNumber);
number2 = Integer.parseInt(secondNumber);
sum = number1 + number2;
JOptionPane.showMessageDialog(null, "The sum is " + sum, "Results",
JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}
The Java Swing Package
• Java provides two different technologies for
building a graphical user interface
– Abstract Window Toolkit (AWT)
– Swing
• The Swing classes are stored in the javax.swing
package
• Use the JOptionPane class of the javax.swing
package to display
– dialog boxes
– message boxes
• To use the JOptionPane class
– import javax.swing.JOptionPane;
showInputDialog Method
• Use the static method showInputDialog of the
JOptionPane class to get input from a user.
• Example:
String firstNumber;
firstNumber = JOptionPane.showInputDialog(null, "Enter first integer",
"Input", JOptionPane.QUESTION_MESSAGE);
showMessageDialog Method
• Use the static method showMessageDialog of the
JOptionPane class to display text to a user.
• Example:
sum = 6;
JOptionPane.showMessageDialog(null, "The sum is " + sum, "Message",
JOptionPane.PLAIN_MESSAGE);
System.exit(0);
Main loop structure
boolean finished = false;
while(!finished)
{
do something
if(exit condition)
{
finished = true;
}
else
{
do something more
}
}
```