Professional Documents
Culture Documents
1
Lecture-1/Programming in Java Third Class
Basic Syntax:
About Java programs, it is very important to keep in mind the following points.
Case Sensitivity: Java is case sensitive, which means identifier Hello and hello would have different
meaning in Java.
2
Lecture-1/Programming in Java Third Class
Class Names: For all class names the first letter should be in Upper Case.
If several words are used to form a name of the class, each inner word's first letter should be in Upper
Case.
Example
class MyFirstJavaClass
Method Names: All method names should start with a Lower Case letter.If several words are used to
form the name of the method, then each inner word's first letter should be in Upper Case.
Example
public void myMethodName()
Program File Name - Name of the program file should exactly match the class name.
Java Identifiers:
All Java components require names. Names used for classes, variables and methods are called identifiers.
In Java, there are several points to remember about identifiers. They are as follows:
All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).
After the first character, identifiers can have any combination of characters.
A key word cannot be used as an identifier.
Most importantly, identifiers are case sensitive.
Examples of legal identifiers: age, $salary, _value, __1_value
Examples of illegal identifiers: 123abc, -salary
Java Modifiers:
Like other languages, it is possible to modify classes, methods, etc., by using modifiers. There are two
categories of modifiers:
Access Modifiers: default, public , protected, private
Non-access Modifiers: final, abstract, strictfp
Java Keywords:
The following list shows the reserved words in Java. These reserved words may not be used as constant or
variable or any other identifier names.
abstract assert boolean break
byte case catch char
class const continue default
do double else enum
3
Lecture-1/Programming in Java Third Class
Comments in Java
Java supports single-line and multi-line comments very similar to c and c++. All characters available inside
any comment are ignored by Java compiler.
Classes in Java:
A class is a blue print from which individual objects are created.
A sample of a class is given below:
void barking(){
}
void hungry(){
}
void sleeping(){
}
}
5
Lecture-1/Programming in Java Third Class
Instance variables: Instance variables are variables within a class but outside any method. These
variables are initialized when the class is instantiated. Instance variables can be accessed from inside
any method, constructor or blocks of that particular class.
6
Lecture-1/Programming in Java Third Class
Class variables: Class variables are variables declared with in a class, outside any method, with the
static keyword.
Constructors:
Every class has a constructor. If we do not explicitly write a constructor for a class the Java compiler builds
a default constructor for that class.
Each time a new object is created, at least one constructor will be invoked. The main rule of constructors is
that they should have the same name as the class. A class can have more than one constructor.
Example of a constructor is given below:
Creating an Object:
As mentioned previously, a class provides the blueprints for objects. So basically an object is created from a
class. In Java, the new key word is used to create new objects.
There are three steps when creating an object from a class:
Declaration: A variable declaration with a variable name with an object type.
7
Lecture-1/Programming in Java Third Class
8
Lecture-1/Programming in Java Third Class
Example:
This example explains how to access instance variables and methods of a class:
1 + ( Addition )
Adds values on either side of the operator
Example: A + B will give 30
10
Lecture-1/Programming in Java Third Class
2 - ( Subtraction )
Subtracts right hand operand from left hand operand
Example: A - B will give -10
3 * ( Multiplication )
Multiplies values on either side of the operator
Example: A * B will give 200
4 / (Division)
Divides left hand operand by right hand operand
Example: B / A will give 2
5 % (Modulus)
Divides left hand operand by right hand operand and returns remainder
Example: B % A will give 0
6 ++ (Increment)
Increases the value of operand by 1
Example: B++ gives 21
7 -- ( Decrement )
Decreases the value of operand by 1
Example: B-- gives 19
1 == (equal to)
Checks if the values of two operands are equal or not, if yes then condition becomes true.
Example: (A == B) is not true.
11
Lecture-1/Programming in Java Third Class
Checks if the value of left operand is greater than the value of right operand, if yes then
condition becomes true.
Example: (A > B) is not true.
Assume if a = 60; and b = 13; now in binary format they will be as follows:
a = 0011 1100
b = 0000 1101
2 | (bitwise or)
Binary OR Operator copies a bit if it exists in either operand.
12
Lecture-1/Programming in Java Third Class
3 ^ (bitwise XOR)
Binary XOR Operator copies the bit if it is set in one operand but not both.
Example: (A ^ B) will give 49 which is 0011 0001
4 ~ (bitwise compliment)
Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.
Example: (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed
binary number.
Operator Description
13
Lecture-1/Programming in Java Third Class
2 || (logical or)
Called Logical OR Operator. If any of the two operands are non-zero, then the condition
becomes true.
Example (A || B) is true.
3 ! (logical not)
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition
is true then Logical NOT operator will make false.
Example !(A && B) is true.
1 =
Simple assignment operator, Assigns values from right side operands to left side operand.
Example: C = A + B will assign value of A + B into C
2 +=
Add AND assignment operator, It adds right operand to the left operand and assign the result
to left operand.
Example: C += A is equivalent to C = C + A
3 -=
Subtract AND assignment operator, It subtracts right operand from the left operand and assign
the result to left operand.
Example:C -= A is equivalent to C = C - A
4 *=
Multiply AND assignment operator, It multiplies right operand with the left operand and assign
the result to left operand.
Example: C *= A is equivalent to C = C * A
5 /=
14
Lecture-1/Programming in Java Third Class
Divide AND assignment operator, It divides left operand with the right operand and assign the
result to left operand
ExampleC /= A is equivalent to C = C / A
6 %=
Modulus AND assignment operator, It takes modulus using two operands and assign the result
to left operand.
Example: C %= A is equivalent to C = C % A
7 <<=
Left shift AND assignment operator.
ExampleC <<= 2 is same as C = C << 2
8 >>=
Right shift AND assignment operator
Example C >>= 2 is same as C = C >> 2
9 &=
Bitwise AND assignment operator.
Example: C &= 2 is same as C = C & 2
10 ^=
bitwise exclusive OR and assignment operator.
Example: C ^= 2 is same as C = C ^ 2
11 |=
bitwise inclusive OR and assignment operator.
Example: C |= 2 is same as C = C | 2
Miscellaneous Operators
There are few other operators supported by Java Language.
Conditional Operator ( ? : )
Conditional operator is also known as the ternary operator. This operator consists of three operands and is
used to evaluate Boolean expressions. The goal of the operator is to decide which value should be assigned
to the variable. The operator is written as:
instance of Operator:
This operator is used only for object reference variables. The operator checks whether the object is of a
particular type (class type or interface type). instanceof operator is written as:
If the object referred by the variable on the left side of the operator passes the IS-A check for the
class/interface type on the right side, then the result will be true. Following is the example:
This operator will still return true if the object being compared is the assignment compatible with the type on
the right.
16
Lecture-1/Programming in Java Third Class
class Vehicle {}
public class Car extends Vehicle {
public static void main(String args[]){
Vehicle a = new Car();
boolean result = a instanceof Car;
System.out.println( result );
}
}
Creating Strings:
18
Lecture-1/Programming in Java Third Class
Java - Methods
public class ExampleMinNumber{
public static void main(String[] args) {
int a = 11;
int b = 6;
int c = minFunction(a, b);
System.out.println("Minimum Value = " + c);
}
19
Lecture-2/ Programming Java Third Class
static variable
Example 1
class Student {
int rollno;
String name;
static String college = "ITS";
void display() {
System.out.println(rollno + " " + name + " " + college);
}
1
Lecture-2/ Programming Java Third Class
Example 2
class Counter {
static int count = 0;
Counter() {
count++;
System.out.println(count);
}
}
}
Flow control
There are three types of loop statements: iteration statements (for, while, and
do-while) create loops
selection statements (switch and all the if statements) tell the program under
what circumstances the program will use statements
jump statements (break, continue, and return) shift control out to another part
of the program.
Loops
The while loop
while ( <boolean condition statement> ) {
<code to execute as long as that condition is true>
}
Example:
public class TestWhile {
public static void main(String[] arg) {
int x = 0; // Initiates x at 0.
while (x < 10) { // Boolean condition statement.
System.out.println("Looping");
x++; // Increments x for the next iteration.
}
}
}
2
Lecture-2/ Programming Java Third Class
3
Lecture-2/ Programming Java Third Class
The continue statement: The continue statement is used to skip the rest of the
loop and resume execution at the next loop iteration.
Example:
public class TestContinue {
public static void main(String[] arg) {
for (int x = 0; x < 10; x++) {
if (x == 5)
continue;//go back to beginning of loop with x=6
System.out.println(x + " Looping");
}
Syntax Example
for ( type item : anArray ) { int[] numbers = {8, 2, 6, 4, 3};
body ; int sum = 0;
} // for each int number in int[] numbers
// type must be the same as the for (int number : numbers) {
// anArray's type
sum += number;
}
System.out.println("The sum is " + sum);
Conditional statements
Conditional statements are used to provide your code with decision making capabilities.
There are two conditional structures in Java: the if-else statement, and the switch
statement.
The if-else statement
The syntax of an if-else statement is as follows:
if (<condition1>) {
... //code block 1
}
else if (<condition2>) {
... //code block 2
}
else {
... //code block 3
}
4
Lecture-2/ Programming Java Third Class
Example
public class TestIf {
public static void main(String[] arg) {
int x = 5, y = 6;
if (x % 2 == 0)
System.out.println("x is even");
else
System.out.println("x is odd");
if (x == y)
System.out.println("x equals y");
else if (x < y)
System.out.println("x is less than y");
else
System.out.println("x is greater than y");
}
The switch statement
switch (<expression>){
case <value1>: <codeBlock1>;
break;
case <value2>: <codeBlock2>;
break;
default : <codeBlock3>;
}
Example:
public class TestSwitch {
public static void main(String[] arg) {
char c='k';
switch (c) {
case '1':
case '3':
case '5':
case '7':
case '9':
System.out.println("c is an odd number");
break;
case '0':
case '2':
case '4':
case '6':
case '8':
System.out.println("c is an even number");
break;
case ' ':
System.out.println("c is a space");
5
Lecture-2/ Programming Java Third Class
break;
default:
System.out.println("c is not a number or a
space");
}
}
}
Terminating Program
System.exit(int exitCode): You could invoke the method System.exit(int
exitCode) to terminate the program and return the control to the Java runtime. By
convention, return code of zero indicates normal termination; while a non-
zero exitCode indicates abnormal termination. For example,
String
A String is a sequence of characters.
1 + 2 → 3
1.2 + 2.2 → 3.4
If both operands are Strings, '+' concatenates the two Strings and returns the
concatenated String. E.g.,
6
Lecture-2/ Programming Java Third Class
If one of the operand is a String and the other is numeric, the numeric operand will
be converted to String and the two Strings concatenated, e.g.,
"The number is " + 5 → "The number is " + "5" → "The number is 5"
(suppose average=5.5)
"The average is " + average + "!" → "The average is 5.5!"
String Operations
String/Primitive Conversion
"String" to "int/byte/short/long":
You could use the JDK built-in methods Integer.parseInt(anIntStr) to convert
a String containing a valid integer literal (e.g., "1234") into an int (e.g., 1234). For
example,
7
Lecture-2/ Programming Java Third Class
8
Lecture-2/ Programming Java Third Class
String.valueOf(aPrimitve)
Integer.toString(anInt),
Double.toString(aDouble),
Character.toString(aChar),
Boolean.toString(aBoolean), etc.
For example,
// char to String
char c = 'a';
String s5 = c; // Compilation Error: incompatible types
String s6 = c + ""; // Convert the char to String
// boolean to String
boolean done = false;
String s7 = done + ""; // boolean false -> "false"
String s8 = Boolean.toString(done);
String s9 = String.valueOf(done);
9
Lecture-2/ Programming Java Third Class
Example 1:
import java.util.Scanner;
class ScannerTest {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
10
Lecture-2/ Programming Java Third Class
Example 2:
import java.util.Scanner;
Method Returns
boolean hasNextLine() Returns true if the scanner has another line in its
input; falseotherwise.
hasNext() Returns true if this scanner has another token in
its input.
boolean hasNextInt() Returns true if the next token in the scanner can
be interpreted as an int value.
boolean hasNextFloat() Returns true if the next toke in the scanner can be
interpreted as a float value.
Homework: Write a Java program to separate a string into separate "tokens". These
tokens can be words, numbers, commands, or whatever.
11
Lecture-3/ Programming in Java Third Class
1
Lecture-3/ Programming in Java Third Class
The this keyword can be used to refer current class instance variable.
If there is ambiguity between the instance variable and parameter, this keyword resolves the problem
of ambiguity.
Example:
public class Student {
int id;
String name;
Student(int id, String name) {
id = id;
name = name;
}
void display() {
System.out.println(id + " " + name);
}
public static void main(String args[]) {
Student s1 = new Student(111, "Karan");
Student s2 = new Student(321, "Aryan");
s1.display();
s2.display();
}
}
2
Lecture-3/ Programming in Java Third Class
Output: ?
Solution of the above problem by this keyword
//example of this keyword
public class Student {
int id;
String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
void display() {
System.out.println(id + " " + name);
}
public static void main(String args[]) {
Student s1 = new Student(111, "Karan");
Student s2 = new Student(222, "Aryan");
s1.display();
s2.display();
}
}
Output: ?
3
Lecture-3/ Programming in Java Third Class
}
}
}
The this keyword can be used to invoke current class method (implicitly).
You may invoke the method of the current class by using the this keyword. If you don't use the this
keyword, compiler automatically adds this keyword while invoking the method.
public class Student {
void show() {
System.out.println("You got A+");
}
void marks() {
this.show(); // no need to use this here because compiler does it.
}
void display() {
show(); // compiler act marks() as this.marks()
}
public static void main(String args[]) {
Student s = new Student();
s.display();
}
}
The this keyword can also be passed as an argument in the method. It is mainly used in the event
handling
class Student {
String name;
int age;
5
Lecture-3/ Programming in Java Third Class
b.display();
}
public static void main(String args[]) {
A a = new A();
}
}
Output:?
Example of this keyword that you return as a statement from the method
public class ReturnThis {
ReturnThis getReturnThis() {
return this;
}
void msg() {
System.out.println("Hello java");
}
}
public class TestThis {
public static void main(String args[]) {
ReturnThis a = new ReturnThis();
a.getReturnThis().msg();
}
}
Output:?
Layouts: They define how UI elements should be organized on the screen and provide a final
look and feel to the GUI
BorderLayout: The borderlayout arranges the components to fit in the five regions:
east, west, north, south and center.
JFrame Constructor
JFrame(): Constructs a new frame that is initially invisible.
JFrame(String title)
JFrame(GraphicsConfiguration gc): Creates a Frame in the specified GraphicsConfiguration of a
screen device and a blank title.
Example
import javax.swing.*;
public class SwingJFrameDemo {
}
}
Or
frame.setLayout(new FlowLayout());
frame.getContentPane().setLayout(layout);
JButton class:
The JButton class is used to create a button that have platform-independent implementation with the
following constructor methods:
9
Lecture-3/ Programming in Java Third Class
Example
import javax.swing.JButton;
import javax.swing.JFrame;
// Creating Button
JButton but = new JButton("Click Me..");
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
10
Lecture-3/ Programming in Java Third Class
frame.getContentPane().add(Component);
A JButton generally represents a button that, when clicked by the user, carries out a particular action.
A JButton has a so-called ActionListener attached to it, which in effect defines the task to be
performed when the button is clicked.
Example
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
12
Lecture-3/ Programming in Java Third Class
Example
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
BorderLayoutTest() {
JFrame frame = new JFrame("BorderLayout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton jb1 = new JButton("one");
JButton jb2 = new JButton("two");
JButton jb3 = new JButton("three");
JButton jb4 = new JButton("four");
JButton jb5 = new JButton("five");
frame.add(BorderLayout.NORTH,jb1);
frame.add(BorderLayout. WEST,jb2);
frame.add(BorderLayout. CENTER,jb3);
frame.add(BorderLayout. EAST,jb4);
frame.add(BorderLayout.SOUTH,jb5);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
BorderLayoutTest bt = new BorderLayoutTest();
}
}
JLabel Class
The class JLabel can display either text, an image, or both.
JLabel Constructor
JLabel(): Creates a JLabel instance with no image and with an empty string for the title.
JLabel(String text):Creates a JLabel instance with the specified text.
JLabel(Icon image):Creates a JLabel instance with the specified image
Homework
Write a java program to design the following figure. Each time the button is clicked, the label is
updated to show the total number of clicks.
13
Lecture-3/ Programming in Java Third Class
14
Lecture-4/ Programming in Java Third Class
JPanel Class
The JPanel is a virtual container that can be used to group certain components to create a user
interface for Java Desktop Application.
Creating a JPanel:
JPanel panel = new JPanel();
An example of how to set the layout manager when creating the panel.
JPanel panel = new JPanel(new BorderLayout());
Or Setting layout manager
Like other containers, a panel uses a layout manager to position and size its components. By
default, a panel's layout manager is an instance of FlowLayout, which places the panel's
contents in a row. You can easily make a panel use any other layout manager by invoking
the setLayout method or by specifying a layout manager when creating the panel.
panel.setLayout(new BorderLayout());
Adding Components
panel.add(aComponent);
panel.add(anotherComponent);
When the layout manager is BorderLayout, you need to provide an argument specifying the
added component's position within the panel. For example:
panel.add(aComponent, BorderLayout.CENTER);
panel.add(aComponent);
Example
1
Lecture-4/ Programming in Java Third Class
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
JTextField Class
Creating a JTextField object
When creating a text field component, it’s common to specify some initial text and/or a number
of columns from which the field’s width is calculated.
Create a text field with some initial text:
JTextField textField = new JTextField("This is a text");
Create a text field with a specified number of columns:
JTextField textField = new JTextField(20);
Create a text field with both initial text and number of columns:
JTextField textField = new JTextField("This is a text", 20);
Create a default and empty text field then set the text and the number of columns later:
JTextField textField = new JTextField();
textField.setText("This is a text");
textField.setColumns(20);
2
Lecture-4/ Programming in Java Third Class
3
Lecture-4/ Programming in Java Third Class
textField.setForeground(Color.BLUE);
textField.setBackground(Color.YELLOW);
Example:
import javax.swing.JFrame; // A frame (main window)
import javax.swing.JPanel; // A container for other GUI components
import javax.swing.border.EtchedBorder; // A simple border
import java.awt.FlowLayout; // The simplest layout manager
import javax.swing.JButton; // A button
import javax.swing.JTextField; // A text field
frame.setVisible(true);
}
}
4
Lecture-4/ Programming in Java Third Class
Output:
JPasswordField Class
JPasswordField is a subclass of the JTextField so it acts like an ordinary text field but it hides
the actual characters typed by showing a series of echo characters such as asterisks (*) or a dots,
for security purpose.
Creating new JPasswordField object
When creating a new JPasswordField object, we can specify an initial text and the field’s width
in number of columns, using one of the following constructors:
JPasswordField(int columns)
JPasswordField(String text)
JPasswordField(String text, int columns)
Note that the parameter columns specifies minimum width of the field in a number of columns,
not in number of pixels nor characters. Here are some examples:
JPasswordField passwordField = new JPasswordField(20);
JPasswordField passwordField = new JPasswordField("secret");
JPasswordField passwordField = new JPasswordField("secret", 20);
We can also use an empty constructor to create new object and then set the columns and initial
password later, for example:
5
Lecture-4/ Programming in Java Third Class
To retrieve password typed into the field, use the getPassword() method:
char[] password = passwordField.getPassword();
JOptionPane Class
JOptionPane Facilitates data entry and data output. class JOptionPane contains methods that
display a dialog box.
String showInputDialog
prompt for input
return a string
void showMessageDialog
display a message
wait for an acknowledgement
int showConfirmDialog
ask a yes/no question
6
Lecture-4/ Programming in Java Third Class
To show different messages with different icons, you'll need 4 arguments instead of 2.
JOptionPane.showMessageDialog (null, "Messagehere", "Titlehere", JOptionPane.PLAIN_MESSA
GE);
There are different kinds of icons for a dialog box, just replace the last argument:
JOptionPane.PLAIN_MESSAGE // this is a plain message
JOptionPane.INFORMATION_MESSAGE // this is a info message
JOptionPane.ERROR_MESSAGE // this is a error message
JOptionPane.WARNING_MESSAGE // this is a warning message
you can also use JOptionPane for dialog boxes for input, and assign them to variables:
name= JOptionPane.showInputDialog ( "put your message here" );
Example:
import javax.swing.JOptionPane;
public class JOptionPane Example{
public static void main(String[] args) {
String breadth = JOptionPane.showInputDialog("Rectangle Breadth");
String height = JOptionPane.showInputDialog("Rectangle Height");
int area = Integer.parseInt(breadth) * Integer.parseInt(height);
JOptionPane.showMessageDialog(null, "Area=" + area, "",
JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
}
7
Lecture-4/ Programming in Java Third Class
Homework
Write a java program to display two password fields and verify if the passwords are matched
and correct. As shown in the figure below
On clicking the OK button, if the two passwords are not matched, the following message
appears:
If the two passwords are matched, but it is not correct, this message appears:
and if the both passwords are matched and correct, this message appears:
8
Lecture-5/ Programming in Java Third Class
JTextArea
The JTextArea class provides a component that displays multiple lines of text and optionally
allows the user to edit the text.
Constructor: Creates a text area. When present, the String argument contains the initial
text. The int arguments specify the desired width in columns and height in rows,
respectively.
JTextArea()
JTextArea(String)
JTextArea(String, int, int)
JTextArea(int, int)
Sets or indicates whether the user can edit the text in the text area.
void setEditable(boolean)
boolean isEditable()
Sets whether lines can be wrapped at white space (word boundaries) or at any
character. By default this property is false, and lines can be wrapped (if line wrapping
is turned on) at any character.
int setWrapStyleWord(boolean)
1
Lecture-5/ Programming in Java Third Class
Replaces the text between the indicated positions with the specified string.
void replaceRange(String, int, int)
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class JTextAreaTest {
public static void main(String[] args) {
JFrame mainFrame = new JFrame("Java Swing Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
JLabel headerLabel = new JLabel("");
JLabel statusLabel = new JLabel("");
headerLabel.setText("Control in action: JTextArea");
JLabel commentlabel= new JLabel("Comments: ", JLabel.RIGHT);
JTextArea commentTextArea = new JTextArea("This is a Swing tutorial " +"to
make GUI application in Java.",5,20);
JScrollPane scrollPane = new JScrollPane(commentTextArea);
JButton showButton = new JButton("Show");
showButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
statusLabel.setText( commentTextArea.getText());
}
});
2
Lecture-5/ Programming in Java Third Class
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
}
3
Lecture-5/ Programming in Java Third Class
totalGUI.add(area);
frame.setContentPane(totalGUI);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 300);
frame.setVisible(true);
}
4
Lecture-5/ Programming in Java Third Class
Homework:
1. Write a java class named "Calculator" which can perform arithmetic operations shown in
the following frame
2. Write a java program named "UpperCase.java" to design the following figure. Then, read
text in the JTextField and write it in JTextArea in uppercase when ToUpper button is
pressed. When Clear button is pressed the JTextField and JTextArea will be empty.
5
Lecture-5/ Programming in Java Third Class
JCheckBox Class
JCheckBox is a Swing component that represents an item, which shows a state of selected
or unselected. User can change this state by clicking on the check box of the component.
JCheckBox():Creates an initially unselected check box button with no text, no icon.
JCheckBox(Icon icon):Creates an initially unselected check box with an icon.
JCheckBox(Icon icon, boolean selected):Creates a check box with an icon and specifies
whether or not it is initially selected.
JCheckBox(String text): Creates an initially unselected check box with text.
JCheckBox(String text, boolean selected):Creates a check box with text and specifies
whether or not it is initially selected.
6
Lecture-5/ Programming in Java Third Class
Customizing appearance
Setting tooltip text:
checkbox.setToolTipText("If enabled, write debug in information
to log files.");
Setting font style, background color and foreground color:
checkbox.setFont(new java.awt.Font("Arial", Font.BOLD, 14));
checkbox.setBackground(Color.BLUE);
checkbox.setForeground(Color.YELLOW);
7
Lecture-5/ Programming in Java Third Class
Example:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class TestCheckBox {
JFrame frame = new JFrame("Swing JCheckBox Demo Program");
JCheckBox checkboxOne = new JCheckBox("$100");
JCheckBox checkboxTwo = new JCheckBox("$200");
JCheckBox checkboxThree = new JCheckBox("$300");
// a int sum = 0; label and a text field to display sum
JLabel labelSum = new JLabel("Sum: ");
JTextField textFieldSum = new JTextField(10);
int sum = 0; // sum of 3 numbers
TestCheckBox() {
frame.setLayout(new FlowLayout());
// add the check boxes to this frame
frame.add(checkboxOne);
frame.add(checkboxTwo);
frame.add(checkboxThree);
frame.add(labelSum);
textFieldSum.setEditable(false);
frame.add(textFieldSum);
// add action listener for the check boxes
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
JCheckBox checkbox = (JCheckBox)event.getSource();
if (checkbox.isSelected()) {
if (checkbox == checkboxOne) {
sum += 100;
} else if (checkbox == checkboxTwo) {
sum += 200;
} else if (checkbox == checkboxThree) {
8
Lecture-5/ Programming in Java Third Class
sum += 300;
}
} else {
if (checkbox == checkboxOne) {
sum -= 100;
} else if (checkbox == checkboxTwo) {
sum -= 200;
} else if (checkbox == checkboxThree) {
sum -= 300;
}
}
textFieldSum.setText(String.valueOf(sum));
}
};
checkboxOne.addActionListener(actionListener);
checkboxTwo.addActionListener(actionListener);
checkboxThree.addActionListener(actionListener);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new TestCheckBox();
}
}
JRadioButton Class
The JRadioButton class is used to create radio buttons. A set of radio buttons can be
associated as a group in which only one button at a time can be selected.
9
Lecture-5/ Programming in Java Third Class
}
}
10
Lecture-5/ Programming in Java Third Class
11
Lecture-6/ Programming in Java Third Class
Java I/O
Java I/O (Input and Output) is used to process the input and produce the output based on the input.
Java uses the concept of stream to make I/O operation fast. The java.io package contains all the classes
required for input and output operations.
Stream
A stream can be defined as a sequence of data. There are two kinds of streams:
InputStream: the InputStream is used to read data from a source, it may be a file, an array, peripheral
device or socket.
OutPutStream: the OutputStream is used for writing data to a destination, it may be a file, an array,
peripheral device or socket.
OutputStream class
OutputStream class is an abstract class. It is the superclass of all classes representing an output stream of bytes.
An output stream accepts output bytes and sends them to some sink.
Method Description
public void write(int)throws IOException is used to write a byte to the current output stream.
public void write(byte[])throws IOException is used to write an array of byte to the current
output stream.
public void close()throws IOException is used to close the current output stream.
1
Lecture-6/ Programming in Java Third Class
InputStream class
InputStream class is an abstract class.It is the superclass of all classes representing an input stream of bytes.
Method Description
public abstract int read()throws IOException: reads the next byte of data from the input stream. It
returns -1 at the end of file.
public int available()throws IOException: returns an estimate of the number of bytes that can be
read from the current input stream.
public void close()throws IOException: is used to close the current input stream.
Java byte streams are used to perform input and output of 8-bit bytes. However, there are many classes related
to byte streams but the most frequently used classes are FileInputStream and FileOutputStream.
2
Lecture-6/ Programming in Java Third Class
Java FileInputStream class: obtains input bytes from a file. It is used for reading streams of raw bytes such
as image data.
3
Lecture-6/ Programming in Java Third Class
Import java.io.*;
public class FileStreamTest{
public static void main(String args[]) {
try {
byte bWrite[] = { 2, 21, 3, 40, 5 };
FileOutputStream os = new FileOutputStream("test.txt");
for (int x = 0; x < bWrite.length; x++) {
os.write(bWrite[x]); // writes the bytes
}
os.close();
FileInputStream is = new FileInputStream("test.txt");
int size = is.available();
import java.io.*;
public class SimpleWrite {
public static void main(String args[]) throws Exception {
FileOutputStream fout = new FileOutputStream("f1.txt");
BufferedOutputStream bout = new BufferedOutputStream(fout);
String s = "Sachin is my favourite player";
byte b[] = s.getBytes();
bout.write(b);
bout.flush();
bout.close();
fout.close();
System.out.println("success");
}
}
Java BufferedInputStream class is used to read information from stream. It internally uses buffer
mechanism to make the performance fast.
Example of Java BufferedInputStream
import java.io.*;
class SimpleRead {
public static void main(String args[]) {
try {
FileInputStream fin = new FileInputStream("f1.txt");
BufferedInputStream bin = new BufferedInputStream(fin);
int i;
while ((i = bin.read()) != -1) {
System.out.print((char) i);
}
bin.close();
fin.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
5
Lecture-6/ Programming in Java Third Class
Character Streams
Java Byte streams are used to perform input and output of 8-bit bytes, where as Java Character streams are
used to perform input and output for 16-bit unicode. Though there are many classes related to character
streams but the most frequently used classes are , FileReader and FileWriter.
Constructor Description
FileWriter(File file) creates a new file. It gets file name in File object.
Method Description
import java.io.*;
class SimpleWrite {
6
Lecture-6/ Programming in Java Third Class
Java FileReader class is used to read data from the file. It returns data in byte format like FileInputStream
class.
Constructor Description
FileReader(String file) It gets filename in string. It opens the given file in read
mode. If file doesn't exist, it throws FileNotFoundException.
FileReader(File file) It gets filename in file instance. It opens the given file in read
mode. If file doesn't exist, it throws FileNotFoundException.
Method Description
public int read() returns a character in ASCII form. It returns -1 at the end of file.
Example
class SimpleRead{
public static void main(String args[])throws Exception{
FileReader fr=new FileReader("abc.txt");
int i;
while((i=fr.read())!=-1)
System.out.print((char)i);
fr.close();
}
7
Lecture-7/Programming in Java Third Class
Client/Server Communication
At a basic level, network-based systems consist of a server, client, and a media for
communication as shown in the following figure. A computer running a program that
makes a request for services is called client machine. A computer running a program
that offers requested services from one or more clients is called server machine. The
media for communication can be wired or wireless network.
1
Lecture-7/Programming in Java Third Class
2
Lecture-7/Programming in Java Third Class
3
Lecture-7/Programming in Java Third Class
dos.close();
s1out.close();
s1.close();
}
}
4
Lecture-7/Programming in Java Third Class
import java.net.*;
import java.io.*;
import javax.swing.JOptionPane;
s1.close();
dis.close();
s1In.close();
}
}
Homework
Create two java classes named "SimpleServerChat" and ""ClientChat". The simple
server chat will accept a single client connection and display everything the client says
on the screen. If the client user types ".bye", the client and the server will both quit.
5
Lecture-8/ Programming in Java Third Class
Random Numbers
There are two principal means of generating random numbers:
the Random class generates random integers, doubles, longs and so on, in various ranges.
the static method Math.random generates doubles between 0 (inclusive) and 1 (exclusive).
1
Lecture-8/ Programming in Java Third Class
Write a program that simulates rolling of two 6-sided dice until their combined result comes up as 7.
import java.util.Random;
import java.util.Random;
log("Done.");
}
2
Lecture-8/ Programming in Java Third Class
Example:
Example:
public class RandomInteger {
3
Lecture-8/ Programming in Java Third Class
Collections in Java
Collections in java is a framework that provides an architecture to store and manipulate the group of objects.
Methods of Collection interface
There are many methods declared in the Collection interface. They are as follows:
4
Lecture-8/ Programming in Java Third Class
List
A List is an ordered Collection (sometimes called a sequence). Lists may contain duplicate elements.
ArrayList
LinkedList
Vector
Set
A Set is a Collection that cannot contain duplicate elements. There are three main implementations of Set
interface:
HashSet
TreeSet
LinkedHashSet.
Map
A Map is an object that maps keys to values. A map cannot contain duplicate keys. There are three main
implementations of Map interfaces:
HashMap
TreeMap
LinkedHashMap.
5
Lecture-8/ Programming in Java Third Class
class TestCollection {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<String>();// creating arraylist
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Two ways to iterate the elements of collection in java
1. By Iterator interface.
2. By for-each loop.
In the above example, we have seen traversing ArrayList by Iterator. Let's see the example to traverse
ArrayList elements using for-each loop.
6
Lecture-8/ Programming in Java Third Class
}
public class TestCollection{
public static void main(String args[]) {
// Creating user-defined class objects
Student s1 = new Student(101, "Sonoo", 23);
Student s2 = new Student(102, "Ravi", 21);
Student s3 = new Student(103, "Hanumat", 25);
al.addAll(al2);
7
Lecture-8/ Programming in Java Third Class
class TestCollection {
public static void main(String args[]) {
al.removeAll(al2);
}
}
class TestCollection{
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<String>();
al.add("Ravi");
al.add("Vijay");
al.add("Ajay");
ArrayList<String> al2 = new ArrayList<String>();
al2.add("Ravi");
al2.add("Hanumat");
al.retainAll(al2);
8
Lecture-8/ Programming in Java Third Class
import java.util.*;
list.add("Item2");
list.add(2, "Item3");//it will add Item3 to the third position of array list
list.add("Item4");
list.add("Item2");
// Display the contents of the array list
9
Lecture-8/ Programming in Java Third Class
System.out.println("The array created after the conversion of our arraylist is: "
+ Arrays.toString(simpleArray));
Example:
Given a vector of integers v = (1, 2, 3, 4), write a Java code for inserting these numbers into an ArrayList
Homework:
Given (name, number) pairs A = {(Bob, 4), (Amber, 5), (George, 3), (Will, 7)}, write a Java class with method
for
1. inserting these pairs into a pair of ArrayList structures called NameAL and NumberAL, then
2. displaying the pairs to the screen.
10