Download presentation
Presentation is loading. Please wait.
Published byAmice Patterson Modified over 9 years ago
1
Casting, Wrapper Classes, Static Methods, JOptionPane Class
2
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
4
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); }
6
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/
7
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.
8
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.
9
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);
13
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); }
14
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;
15
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);
16
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);
17
Main loop structure boolean finished = false; while(!finished) { do something if(exit condition) { finished = true; } else { do something more }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.