casting, wrapper classes, static methods, joptionpane class

17
Casting, Wrapper Classes, Static Methods, JOptionPane Class

Upload: amice-patterson

Post on 19-Jan-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Casting, Wrapper Classes, Static Methods, JOptionPane Class

Casting, Wrapper Classes, Static Methods,

JOptionPane Class

Page 2: 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

Page 3: Casting, Wrapper Classes, Static Methods, JOptionPane Class

How to code an explicit cast (type) operand

Example double average = 93.25; int gradeInCourse = (int) average; //gradeInCourse is 93

Page 4: Casting, Wrapper Classes, Static Methods, JOptionPane Class

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); }}

Page 5: Casting, Wrapper Classes, Static Methods, JOptionPane Class

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"); } }

Page 6: Casting, Wrapper Classes, Static Methods, JOptionPane Class

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/

Page 7: Casting, Wrapper Classes, Static Methods, JOptionPane Class

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.

Page 8: Casting, Wrapper Classes, Static Methods, JOptionPane Class

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.

Page 9: Casting, Wrapper Classes, Static Methods, JOptionPane Class

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);

Page 10: Casting, Wrapper Classes, Static Methods, JOptionPane Class

The syntax for using a static method of a class class.methodName(arguments)

Two static methods of the Integer class Method Description parseInt(String) Attempts to convert the String object that’s

supplied as an argument to an int type. toString(int) Converts the int value that’s supplied as an

argument to a String object and returns that String object.

Page 11: Casting, Wrapper Classes, Static Methods, JOptionPane Class

Two static methods of the Double class Method Description parseDouble(String) Attempts to convert the String object

that’s supplied as an argument to a double type.

toString(double) Converts the double value that’s supplied as an argument to a String object and returns that String object

Page 12: Casting, Wrapper Classes, Static Methods, JOptionPane Class

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);

Page 13: Casting, Wrapper Classes, Static Methods, JOptionPane Class

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); }}

Page 14: Casting, Wrapper Classes, Static Methods, JOptionPane Class

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;

Page 15: Casting, Wrapper Classes, Static Methods, JOptionPane Class

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);

Page 16: Casting, Wrapper Classes, Static Methods, JOptionPane Class

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);

Page 17: Casting, Wrapper Classes, Static Methods, JOptionPane Class

Main loop structureboolean finished = false;

while(!finished) {

do something

if(exit condition) { finished = true; } else { do something more }}