Chapter 02 - Java Basics Part 2
Chapter 02 - Java Basics Part 2
1
Outline
Java packages
Output and Input data from Java
console
Input and output data using Dialog
boxes
Type casting
Data types
2
Java packages
A package in Java is used to group related classes.
Think of it as a folder in a file directory.
We use packages to avoid name conflicts, and to write a better
maintainable code.
Packages are divided into two categories:
Built-in Packages (packages from the Java API)
User-defined Packages (create your own packages)
Two groups of Built-in packages in Java API
Group of all packages known as Java class library or Java
applications programming interface (Java API)
Core packages
Begin with java
Included with Java 2 Software Development Kit
Extension packages
Begin with javax
3
New Java packages
Java packages
import declarations
Used by compiler to identify and locate classes used in Java
programs
For example
import javax.swing.JOptionPane;
Tells compiler to load class JOptionPane from javax.swing
package
JOptionPane is in the javax.swing package
Package has classes for using Graphical User Interfaces (GUIs)
package java.lang
Is a built-in package
No import declaration needed
java.lang automatically imported in every Java program
For example No import statement is
required for System.exit( 0 );because 4
Input data from Java Console
Scanner Class
Scanner is a class in java.util package used for
obtaining the input of the primitive types like int,
double, etc. and strings. It is the easiest way to read
input in a Java program from console
To create an object of Scanner class, we usually pass
the predefined object System.in, which represents the
standard input stream. We may pass an object of class
File if we want to read input from a file.
To read numerical values of a certain data type XYZ,
the function to use is nextXYZ(). For example, to read a
value of type short, we can use nextShort()
To read strings, we use nextLine(). 5
Input data from Java console
To read a single character, we use next().charAt(0). next()
function returns the next token/word in the input as a string
and charAt(0) function returns the first character in that string.
6
Scanner Class in Java
8
Input data from Java Console
9
Input & output using Dialog boxes
JOptionPane Class allows us to use dialog boxex
Dialog Box – is a small graphical window that displays a message to
the user or request input. We can quickly display dialog boxes with
JOptionPane class.
Ty p e s of DIALOG BOXES
Message Dialog – a dialog box that displays a message; an OK
button is also displayed.
Input Dialog – dialog box tat prompts the user for input & provides
text field where input is typed; an OK button and a CANCEL
button are also displayed.
Beginning Statement in your code when using JoptionPane:
import javax. swing.JOptionPane;
Purpose: this statement tells the compiler where to find the JOptionPane
class, and make it available to your program.
Switng Package has classes for using Graphical User Interfaces (GUIs)
At the end use System.exit( 0 ); // terminate
application with window 10
Input & output using Dialog boxes
Message Dialog Boxes
showMessageDialog method- is used to display a message dialog
box.
Call method showMessageDialog of class
JOptionPane
Requires four arguments
First argument: null
Second: string to display
Third: string in title bar (optional by default Message)
Fourth: type of message dialog with icon (optional)
JOptionPane.showMessageDialog(null,"The sum is
"+sum, "Results",OptionPane.PLAIN_MESSAGE);
12
Input & output using Dialog boxes
Input Dialog Box
InputDialog method – to display an input dialog in
JOptionPane class.
Statement to call the method:
String name;
name = JOptionPane.showInputDialog(“Enter
first number”);
13
Input & output using Dialog boxes
Example program: Write a Java program to find area of rectangle
using input output dialog box.
import javax.swing.JOptionPane; // Needed for Dialog Box
public class RectangleTest
{
public static void main(String[] args)
{
String input; // To hold String input. output
int length; // To hold length.
int width; // To hold width.
int area; // To hold area.
// Prompt user to input length.
input = JOptionPane.showInputDialog("Enter Length");
// Convert the String input to an int.
length = Integer.parseInt(input); //Prompt user to input width.
input = JOptionPane.showInputDialog("Enter Width");
// Convert the String input to an int.
width = Integer.parseInt(input);
// Calculate area of rectangle.
area = length * width;
// Display area of rectangle.
14
JOptionPane.showMessageDialog(null, "Area of rectangle is " + area);
Input & output using Dialog boxes
E x a m p l e p ro g r a m : A d d i t i o n o f t w o n u m b e r s
// Importing JOptionPane package
import javax.swing.JOptionPane;
// Beginning of class Addition
public class Addition {
// Beginning of method main
public static void main(String[] args) {
// Declare variables
String firstNumber; // First string entered by user
String secondNumber; // Second string entered by user
int number1; // First number
int number2; // Second number
int sum; // Sum of number1 and number2
// Read in first number from user as a String
firstNumber = JOptionPane.showInputDialog("Enter first integer:");
// Read in second number from user as a String
secondNumber = JOptionPane.showInputDialog("Enter second integer:");
// Convert numbers from type String to type int
number1 = Integer.parseInt(firstNumber);
number2 = Integer.parseInt(secondNumber);
// Add numbers
sum = number1 + number2;
// Display result
JOptionPane.showMessageDialog(null,"The sum is " + sum + ".","Results", JOptionPane.PLAIN_MESSAGE);
// Terminating Java program with window
System.exit(0);
} // End of method main
15
} // End of class Addition
Exercise programs
Write a program to calculate and display area and
circumference of circle. Have the user input radius
of a circle and display area and circumference of a
circle. Use dialog boxes for input and output.
Write a program to calculate and display area and
circumference of circle. Have the user input radius
of a circle and display area and circumference of a
circle. Use System.in & System.out for input and
output on MS Dos prompt.
16
Data types
Data types specify the different sizes and values that can be stored in the
variable.
There are two types of data types in Java:
Primitive data types: The primitive data types include boolean, char, byte,
short, int, long, float and double. (8 data types)
Non-primitive data types: The non-primitive data types include Classes,
Interfaces, and Arrays.
Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits