Object Oriented Programming IJSE (GDSE)
Object Oriented Programming IJSE (GDSE)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q01
class Student{
String studentId;
String name;
String address;
int sub1;
int sub2;
}
class Example{
public static void main(String[] args) {
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q02
class Student{
String studentId;
String name;
String address;
int sub1;
int sub2;
}
class Example{
public static void main(String[] args) {
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q03
class Student{
String studentId;
String name;
String address;
int sub1;
int sub2;
}
class Example{
public static void main(String[] args) {
s1.studentId = "S001";
s1.name = "Student 1";
s1.address = "Panadura";
s1.sub1 = 50;
s1.sub2 = 75;
s2.studentId = "S002";
s2.name = "Student 2";
s2.address = "Galle";
s2.sub1 = 34;
s2.sub2 = 56;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q04 State as Attributes
==================
class Student{
// State as Attributes
// Start Attribute Declaration
String studentId;
String name;
String address;
int sub1;
int sub2;
// End Attribute Declaration
}
class Example{
public static void main(String[] args) {
s1.studentId = "S001";
s1.name = "Student 1";
s1.address = "Panadura";
s1.sub1 = 50;
s1.sub2 = 75;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q05 Behavior as Instance Method
==========================
class Student{
// Start Attribute Declaration
String studentId;
String name;
String address;
int sub1;
int sub2;
// End Attribute Declaration
class Example{
public static void main(String[] args) {
s1.print();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q06 Exercise
===========
class Student{
// Start Attribute Declaration
String studentId;
String name;
String address;
int sub1;
int sub2;
// End Attribute Declaration
class Example{
public static void main(String[] args) {
// s1.studentId = "S001";
// s1.name = "Student 1";
// s1.address = "Panadura";
// s1.sub1 = 50;
// s1.sub2 = 75;
s1.print();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q07 from Q06
===========
class Student{
// Start Attribute Declaration
String studentId;
String name;
String address;
int sub1;
int sub2;
// End Attribute Declaration
public void setValue(String stuId, String stuName, String stuAddress, int stuSub1, int stuSub2
){
studentId = stuId;
name = stuName;
address = stuAddress;
sub1 = stuSub1;
sub2 = stuSub2;
}
// End Method Declaration
}
class Example{
public static void main(String[] args) {
// s1.studentId = "S001";
// s1.name = "Student 1";
// s1.address = "Panadura";
// s1.sub1 = 50;
// s1.sub2 = 75;
s1.print();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q08 Exercise
============
class Student{
// Start Attribute Declaration
String studentId;
String name;
String address;
int sub1;
int sub2;
// End Attribute Declaration
class Example{
public static void main(String[] args) {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q09 from Q08
===========
class Student{
// Start Attribute Declaration
String studentId;
String name;
String address;
int sub1;
int sub2;
// End Attribute Declaration
// Start Method Declaration
public void print(){
System.out.println(studentId + " " + name + " " + address + " " + sub1 + " " + sub2);
}
public void setValue(String stuId, String stuName, String stuAddress, int stuSub1, int stuSub2
){
studentId = stuId;
name = stuName;
address = stuAddress;
sub1 = stuSub1;
sub2 = stuSub2;
}
class Example{
public static void main(String[] args) {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q10 Case 1
==========
class Box{
int length;
int width;
int height;
}
class Example{
public static void main(String[] args) {
Box b1 = new Box();
System.out.println(b1);
b1.height = 5;
b1.width = 6;
b1.length = 10;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q11 Case 2
=========
class Box{
int length; // Global Variables
int width; // Global Variables
int height; // Global Variables
b1.height = 5;
b1.width = 6;
b1.length = 10;
b1.printVolume();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q12 Case 3
==========
class Box{
int length; // Global Variables
int width; // Global Variables
int height; // Global Variables
// b1.height = 5;
// b1.width = 6;
// b1.length = 10;
b1.setValue(10, 6, 5);
b1.printVolume();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q13 Case 4
=========
class Box{
int length; // Global Variables
int width; // Global Variables
int height; // Global Variables
b1.setValue(10, 6, 5);
int volume = b1.getVolume();
System.out.println("Volume : " + volume);
}
}
Case 5 Keyword ‘this’
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q14
class Box{
int length;
int width;
int height;
b1.setValue(10, 6, 5);
int volume = b1.getVolume();
System.out.println("Volume : " + volume);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q15
class Box{
int length;
int width;
int height;
b1.setValue(10, 6, 5);
int volume = b1.getVolume();
System.out.println("Volume : " + volume);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q16
class A{
public void print(){
System.out.println(this);
}
}
class Example{
public static void main(String[] args) {
A a1 = new A();
System.out.println(a1);
a1.print();
}
}
Case 6 Constructors
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q17
class Box{
int length;
int width;
int height;
class Box{
int length;
int width;
int height;
class Box{
int length;
int width;
int height;
class Box{
int length;
int width;
int height;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q21 Constructor Overloading
=======================
class Box{
int length;
int width;
int height;
Box(){
this.length = 5;
this.width =5;
this.height = 5;
}
Box(int length){
this.width = length;
this.height = length;
this.length = length;
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q21
class Box{
int length;
int width;
int height;
Box(){
this.length = 5;
this.width =5;
this.height = 5;
}
Box(int length){
this.width = length;
this.height = length;
this.length = length;
}
b1.Box(10,20,30); // Illegal
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q22
class Box{
int length;
int width;
int height;
Box(){
this.length = 5;
this.width =5;
this.height = 5;
}
Box(int length){
this.width = length;
this.height = length;
this.length = length;
}
b1.Box(10,20,30);
b1.printVolume();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q23 Attribute Declaration Values
=========================
class Box{
int length = 10;
int width = 6;
int height = 5;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q24
class MyClass{
int a = 100;
MyClass(){
a = 200;
}
MyClass(int a){
this.a = a;
}
}
class Example{
public static void main(String[] args) {
MyClass m1 = new MyClass();
System.out.println(m1.a);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q25
class Box{
int length = 10;
int width = 6;
int height;
height = 5; // Illegal
class Account{
double balance;
Account(double balance){
this.balance = balance;
}
class Operation{
public void deposit(double amount, Account account){
account.balance+= amount;
}
operation.deposit(5000, a1);
a1.printBalance();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q27
class Account{
double balance;
Account(double balance){
this.balance = balance;
}
class Operation{
public void deposit(double amount, Account account){
account.balance+= amount;
}
class Example{
public static void main(String[] args) {
Account a1 = new Account(10000);
a1.printBalance();
Operation operation = new Operation();
operation.withdraw(3000, a1);
a1.printBalance();
operation.deposit(5000, a1);
a1.printBalance();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q28 Exercise
===========
class MyClass{
int x = 100;
}
class Example{
public static void increment(int x){
x++;
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
System.out.println(myClass.x);
increment(myClass.x);
System.out.println(myClass.x);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q29 Exercise
==========
class MyClass{
int x = 100;
}
class Example{
public static void increment(MyClass myClass){
myClass.x++;
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
System.out.println(myClass.x);
increment(myClass);
System.out.println(myClass.x);
}
}
class Box{
int length;
int width;
int height;
class Example{
public static void main(String[] args) {
Box b1 = new Box(10, 6, 5);
b1.printVolume(); //Volume : 300
Box b2 = b1.getCopy();
b2.printVolume(); //Volume : 300
class Box{
int length;
int width;
int height;
class Example{
public static void main(String[] args) {
Box b1 = new Box(10, 6, 5);
b1.printVolume(); //Volume : 300
Box b2 = b1.getCopy();
b2.printVolume(); //Volume : 300
class Example{
public static void main(String[] args) {
MyClass m1 = new MyClass();
m1.x = 100;
m1.y = 10;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q33
class MyClass{
int x;
static int y;
}
class Example{
public static void main(String[] args) {
MyClass.y = 30;
//MyClass.x = 10; // Illegal
class MyClass{
public void instanceMethod(){
System.out.println("instanceMethod");
}
class Example{
public static void main(String[] args) {
MyClass.staticMethod();
// MyClass.instanceMethod(); // Illegal
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q35 Exercise 1
============
class Example{
public static void main(String[] args) {
Date d1 = new Date();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q36 from Q35
===========
class Date{
int year;
int month;
int day;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q37 Exercise
===========
class Date{
int year;
int month;
int day;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q38 from Q37 Option 1
==================
class Date{
int year = 1980;
int month = 11;
int day = 15;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q39 from Q37 Option 2
===================
class Date{
int year;
int month;
int day;
Date(){
this.year = 1980;
this.month = 11;
this.day = 15;
}
}
class Example{
public static void main(String[] args) {
Date d1 = new Date();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q40 from Q37 Option 3
==================
class Date{
int year;
int month;
int day;
{ // Instance Block
this.year = 1980;
this.month = 11;
this.day = 15;
}
}
class Example{
public static void main(String[] args) {
Date d1 = new Date();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q41 Exercise 3
============
class Date{
int year;
int month;
int day;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q42 from Q41
===========
class Date{
int year;
int month;
int day;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q43 Exercise 4
============
class Date{
int year;
int month;
int day;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q44 from Q43
===========
class Date{
int year;
int month;
int day;
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q45 Exercise 5
===========
class Date{
int year;
int month;
int day;
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q46 from Q45
===========
class Date{
int year;
int month;
int day;
Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}
Date(int year, int month, int day){
this.year = year;
this.month = month;
this.day = day;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q47 Exercise 6
============
class Date{
int year;
int month;
int day;
Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15
d2.setDate(1991,12,14);
d2.printDate(); // 1991-12-14
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q48 from Q47
============
class Date{
int year;
int month;
int day;
Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15
d2.setDate(1991,12,14);
d2.printDate(); // 1991-12-14
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q49 Exercise 7
============
class Date{
int year;
int month;
int day;
Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15
d2.setDate(1991,12,14);
d2.printDate(); // 1991-12-14
d2.setDate(d1);
d2.printDate(); // 1980-11-15
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q50 from Q49
===========
class Date{
int year;
int month;
int day;
Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15
d2.setDate(1991,12,14);
d2.printDate(); // 1991-12-14
d2.setDate(d1);
d2.printDate(); // 1980-11-15
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q51 Exercise 8
============
class Date{
int year;
int month;
int day;
Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}
class Example{
public static void main(String[] args) {
Date d1 = Date.getInstance();
d1.printDate(); // 2000-12-17
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q52 from Q51
============
class Date{
int year;
int month;
int day;
Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}
class Example{
public static void main(String[] args) {
Date d1 = Date.getInstance();
d1.printDate(); // 2000-12-17
}
}
Access Privileges | Access Levels
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q53
////////////////////////////////////////////////////
package package1;
import package2.Car;
class Example{
public static void main(String[] args) {
Car c1 = new Car();
c1.printCar();
System.out.println(c1.name);
//System.out.println(c1.model); // Illegal
//System.out.println(c1.isStart);// Illegal
//System.out.println(c1.speed);// Illegal
}
}
////////////////////////////////////////////////////
package package2;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q54
////////////////////////////////////////////////////
package package1;
class Example{
public static void main(String[] args) {
Car c1 = new Car();
c1.printCar();
System.out.println(c1.name);
System.out.println(c1.model);
System.out.println(c1.isStart);
//System.out.println(c1.speed);// Illegal
}
}
////////////////////////////////////////////////////
package package1;
Encapsulation
● Wrapping attributes and methods together into a single unit.
● Control over the data by hiding fields and adding methods with public access to access
fields.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q55 Step 1
========
class Car{
boolean isStart;
int speed;
}
class Example{
public static void main(String[] args) {
Car c1 = new Car();
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q56 Step 2
=========
class Car{
boolean isStart;
int speed;
}
class Example{
public static void main(String[] args) {
Car c1 = new Car();
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
c1.speed = 100;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q57 Step 3
=========
class Car{
boolean isStart;
int speed;
}
class Example{
public static void main(String[] args) {
Car c1 = new Car();
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
c1.isStart = true;
c1.speed = 100;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
c1.isStart = false;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q58 Step 4
=========
class Car{
private boolean isStart;
private int speed;
}
class Example{
public static void main(String[] args) {
Car c1 = new Car();
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
c1.isStart = true;
c1.speed = 100;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
c1.isStart = false;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q59 Step 5
=========
class Car{
private boolean isStart;
private int speed;
class Example{
public static void main(String[] args) {
Car c1 = new Car();
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
c1.isStart = true;
c1.speed = 100;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
c1.isStart = false;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q60 Step 6
=========
class Car{
private boolean isStart;
private int speed;
class Example{
public static void main(String[] args) {
Car c1 = new Car();
System.out.println("Is Start : " + c1.getIsStart());
System.out.println("Speed : " + c1.getSpeed());
c1.setIsStart(true);
c1.incrementSpeed();
System.out.println("Is Start : " + c1.getIsStart());
System.out.println("Speed : " + c1.getSpeed());
c1.setIsStart(false);
System.out.println("Is Start : " + c1.getIsStart());
System.out.println("Speed : " + c1.getSpeed());
}
}
import javax.swing.*;
class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setSize(500, 400);
f1.setTitle("Test Title");
f1.setLocationRelativeTo(null);
f1.setVisible(true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q62
import javax.swing.*;
class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setSize(500, 400);
f1.setTitle("Test Title");
f1.setLocationRelativeTo(null);
f1.setVisible(true);
f2.setVisible(true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q63
import javax.swing.*;
class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
f1.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f1.setSize(500, 400);
f1.setTitle("Test Title");
f1.setLocationRelativeTo(null);
f1.setVisible(true);
}
}
// JFrame.EXIT_ON_CLOSE = 3
// JFrame.DISPOSE_ON_CLOSE = 2
// JFrame.HIDE_ON_CLOSE = 1
// JFrame.DO_NOTHING_ON_CLOSE = 0
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q64
import javax.swing.*;
class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f1.setSize(500, 400);
f1.setTitle("Frame 1");
f1.setLocationRelativeTo(null);
f1.setVisible(true);
f2.setVisible(true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q65
import javax.swing.*;
import java.awt.*;
class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
f1.setSize(500,500);
f1.setTitle("Frame 1");
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setLocationRelativeTo(null);
f1.add(button);
f1.setVisible(true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q66
import javax.swing.*;
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
c1.setSize(500,400);
c1.setTitle("Calculator");
c1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c1.setLocationRelativeTo(null);
c1.setVisible(true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q67
import javax.swing.*;
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
c1.setSize(500,400);
c1.setTitle("Calculator");
c1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c1.setLocationRelativeTo(null);
c1.setVisible(true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q68
import javax.swing.*;
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
c1.setSize(500,400);
c1.setTitle("Calculator");
c1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c1.setLocationRelativeTo(null);
c1.setVisible(true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q69
import javax.swing.*;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
Calculator c2 = new Calculator();
new Calculator();
new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q70
import javax.swing.*;
import java.awt.*;
JButton button;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q71
import javax.swing.*;
import java.awt.*;
JButton button;
JLabel label;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
Swing Layouts
Border Layout
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q72
import javax.swing.*;
import java.awt.*;
JButton button;
JLabel label;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new BorderLayout());
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q73
import javax.swing.*;
import java.awt.*;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new BorderLayout());
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
Flow Layout
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q74
import javax.swing.*;
import java.awt.*;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q75 Align Right
=============
import javax.swing.*;
import java.awt.*;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new FlowLayout(FlowLayout.RIGHT));
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q76 Align Right
import javax.swing.*;
import java.awt.*;
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
Grid Layout
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q77
import javax.swing.*;
import java.awt.*;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new GridLayout(2, 2));
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q78 Exercise
===========
import javax.swing.*;
import java.awt.*;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new GridLayout(4,4));
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q79 Exercise
==========
import javax.swing.*;
import java.awt.*;
add("Center", centerPanel);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q80 Exercise
===========
import javax.swing.*;
import java.awt.*;
JTextField textField;
JButton[] buttons = new JButton[16];
String arr[] = {"7", "8", "9", "/", "4", "5","6", "*", "1", "2", "3", "+", "0", ".", "=", "-"};
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new BorderLayout());
add("Center",buttonPanel);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
import javax.swing.*;
import java.awt.*;
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
new Notepad();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q82 Step 2
=========
import javax.swing.*;
import java.awt.*;
Notepad(){
setSize(600,400);
setLocationRelativeTo(null);
setTitle("untitled");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
new Notepad();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q83 Step 3
=========
import javax.swing.*;
import java.awt.*;
Notepad(){
setSize(600,400);
setLocationRelativeTo(null);
setTitle("untitled");
setDefaultCloseOperation(EXIT_ON_CLOSE);
add("Center", scrollPane);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
new Notepad();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q84 Step 4
=========
import javax.swing.*;
import java.awt.*;
Notepad(){
setSize(600,400);
setLocationRelativeTo(null);
setTitle("untitled");
setDefaultCloseOperation(EXIT_ON_CLOSE);
add("Center", scrollPane);
menuBar.add(fileMenu);
menuBar.add(editMenu);
setJMenuBar(menuBar);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
new Notepad();
}
}
Working with JTable
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q85
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
ViewStudent(){
setSize(600,400);
setLocationRelativeTo(null);
setTitle("View Student");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
new ViewStudent();
}
}
Event Handling
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q86 Action Event vs. Action Listener
============================
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
JButton button;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("You pressed the button...!");
}
});
add(button);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q87
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
JTextField textField;
JSlider slider;
Calculator(){
setSize(350,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int value = slider.getValue();
textField.setText(value+"");// Integer.toString(value)
}
});
add(slider);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q88 Runtime Error
===============
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
SliderFrame(){
setSize(350,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Slider Frame");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int value = slider.getValue();
displayFrame.textField.setText(Integer.toString(value));
}
});
add(slider);
setVisible(true);
}
}
DisplayFrame(){
setSize(350,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Display Frame");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
SliderFrame c1 = new SliderFrame();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q89 FRom Q88
=============
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
SliderFrame(DisplayFrame displayFrame){
this.displayFrame = displayFrame;
setSize(350,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Slider Frame");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int value = slider.getValue();
displayFrame.textField.setText(Integer.toString(value));
}
});
add(slider);
setVisible(true);
}
}
DisplayFrame(){
setSize(350,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Display Frame");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
DisplayFrame d1 = new DisplayFrame();
SliderFrame c1 = new SliderFrame(d1);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q90 Compile Error
===============
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
SliderFrame(DisplayFrame displayFrame){
this.displayFrame = displayFrame;
setSize(350,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Slider Frame");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int value = slider.getValue();
displayFrame.textField.setText(Integer.toString(value));
}
});
add(slider);
setVisible(true);
}
}
DisplayFrame(){
setSize(350,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Display Frame");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
DisplayFrame d1 = new DisplayFrame();
SliderFrame c1 = new SliderFrame(d1);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q91 from Q90
===========
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int value = slider.getValue();
displayFrame.setTextFieldValue(Integer.toString(value));
}
});
add(slider);
setVisible(true);
}
}
DisplayFrame(){
setSize(350,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Display Frame");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setVisible(true);
}
class Example{
public static void main(String[] args) {
DisplayFrame d1 = new DisplayFrame();
SliderFrame c1 = new SliderFrame(d1);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q92
class A{
public void mA(){
System.out.println("mA of A");
}
}
class B{
public void mB(){
a1.mA();
}
}
class Example{
public static void main(String[] args) {
B b = new B();
b.mB();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q93 from Q92 (Runtime Error)
========================
class A{
public void mA(){
System.out.println("mA of A");
}
}
class B{
A a1;
public void mB(){
a1.mA();
}
}
class Example{
public static void main(String[] args) {
B b = new B();
b.mB();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q94 from Q93 Option 1
==================
class A{
public void mA(){
System.out.println("mA of A");
}
}
class B{
A a1 = new A();
public void mB(){
a1.mA();
}
}
class Example{
public static void main(String[] args) {
B b = new B();
b.mB();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q95 from Q93 Option 2
==================
class A{
public void mA(){
System.out.println("mA of A");
}
}
class B{
A a1;
B(){
this.a1 = new A();
}
class Example{
public static void main(String[] args) {
B b = new B();
b.mB();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q96 from Q93 Option 3
==================
class A{
public void mA(){
System.out.println("mA of A");
}
}
class B{
A a1;
B(A a1){
this.a1 = a1;
}
public void mB(){
a1.mA();
}
}
class Example{
public static void main(String[] args) {
A a1 = new A();
B b = new B(a1);
b.mB();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q97 from Q93 Option 4
===================
class A{
public void mA(){
System.out.println("mA of A");
}
}
class B{
A a1;
class Example{
public static void main(String[] args) {
A a1 = new A();
B b = new B();
b.setA(a1);
b.mB();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q98 from Q93 Option 5
===================
class A{
public void mA(){
System.out.println("mA of A");
}
}
class B{
class Example{
public static void main(String[] args) {
A a1 = new A();
B b = new B();
b.mB(a1);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q99
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
DisplayFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Display");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
this.displayLabel = new JLabel("50");
this.displayLabel.setFont(new Font("", 1, 30));
add(displayLabel);
setVisible(true);
}
setVisible(true);
}
SplitterFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Splitter");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
this.splitterLabel = new JLabel("Off");
this.splitterLabel.setFont(new Font("", 1, 30));
add(splitterLabel);
setVisible(true);
}
WaterTankFrame(){
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Water Tank");
setLocationRelativeTo(null);
add(waterLevelSlider);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
WaterTankFrame f1 = new WaterTankFrame();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q100 from Q99
============
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
DisplayFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Display");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
this.displayLabel = new JLabel("50");
this.displayLabel.setFont(new Font("", 1, 30));
add(displayLabel);
setVisible(true);
}
setVisible(true);
}
SplitterFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Splitter");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setVisible(true);
}
WaterTankFrame(){
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Water Tank");
setLocationRelativeTo(null);
add(waterLevelSlider);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
WaterTankFrame f1 = new WaterTankFrame();
f1.setDisplayFrame(new DisplayFrame());
f1.setAlarmFrame(new AlarmFrame());
f1.setSplitterFrame(new SplitterFrame());
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q101 from Q100
=============
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
DisplayFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Display");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
this.displayLabel = new JLabel("50");
this.displayLabel.setFont(new Font("", 1, 30));
add(displayLabel);
setVisible(true);
}
setVisible(true);
}
SplitterFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Splitter");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setVisible(true);
}
class WaterTankController{
private DisplayFrame displayFrame;
private AlarmFrame alarmFrame;
private SplitterFrame splitterFrame;
WaterTankFrame(WaterTankController waterTankController){
this.waterTankController = waterTankController;
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Water Tank");
setLocationRelativeTo(null);
add(waterLevelSlider);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
WaterTankController controller = new WaterTankController();
controller.setAlarmFrame(new AlarmFrame());
controller.setDisplayFrame(new DisplayFrame());
controller.setSplitterFrame(new SplitterFrame());
WaterTankFrame f1 = new WaterTankFrame(controller);
}
}
Inheritance
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q102 Case 1
==========
import javax.swing.*;
}
class Example {
public static void main(String[] args) {
JFrame f1 = new JFrame();
f1.setSize(1000, 300);
f1.setLocationRelativeTo(null);
f1.setTitle("Frame");
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setVisible(true);
import javax.swing.*;
Calculator(){
setSize(100,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setVisible(true);
}
}
class Example {
public static void main(String[] args) {
new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q104 exercise
===========
class A{
int a;
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
public void mB(){
System.out.println("mB of B");
}
class Example {
public static void main(String[] args) {
B b = new B();
b.print();
b.printAB();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q105 Case 3
==========
class A{
int a;
static {
System.out.println("static block of A");
}
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
static {
System.out.println("static block of B");
}
public void mB(){
System.out.println("mB of B");
}
}
class Example {
public static void main(String[] args) {
new B();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q106 Exercise
============
class A{
int a;
A(){
System.out.println("constructor of A");
}
{
System.out.println("instance block of A");
}
static {
System.out.println("static block of A");
}
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
B(){
System.out.println("constructor of B");
}
{
System.out.println("instance block of B");
}
static {
System.out.println("static block of B");
}
public void mB(){
System.out.println("mB of B");
}
}
class Example {
public static void main(String[] args) {
new B();
new B();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q107 Exercise
============
class A{
int a;
static{
System.out.println("static block of A");
}
}
class B extends A{
int b;
static{
System.out.println("static block of B");
}
}
class C extends B{
int c;
static{
System.out.println("static block of C");
}
}
class D extends B{
int d;
static{
System.out.println("static block of D");
}
}
class Example {
public static void main(String[] args) {
new D();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q108 Case 4
===========
class A{
int a;
A(){
System.out.println("A()");
}
A(int i){
System.out.println("A(int)");
}
A(int i, int j){
System.out.println("A(int,int)");
}
}
class B extends A{
B(){
System.out.println("B()");
}
B(int i){
System.out.println("B(int)");
}
B(int i, int j){
System.out.println("B(int,int)");
}
int b;
}
class Example {
public static void main(String[] args) {
B b1 = new B();
System.out.println("=================");
B b2 = new B(10);
System.out.println("=================");
B b3 = new B(10, 20);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q109
class A{
int a;
A(){
System.out.println("A()");
}
A(int i){
System.out.println("A(int)");
}
A(int i, int j){
System.out.println("A(int,int)");
}
}
class B extends A{
B(){
// compiler inserts super();
System.out.println("B()");
}
B(int i){
// compiler inserts super();
System.out.println("B(int)");
}
B(int i, int j){
// compiler inserts super();
System.out.println("B(int,int)");
}
int b;
}
class Example {
public static void main(String[] args) {
B b1 = new B();
System.out.println("=================");
B b2 = new B(10);
System.out.println("=================");
B b3 = new B(10, 20);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q110 from Q109
=============
class A{
int a;
A(){
System.out.println("A()");
}
A(int i){
System.out.println("A(int)");
}
A(int i, int j){
System.out.println("A(int,int)");
}
}
class B extends A{
B(){
super();
System.out.println("B()");
}
B(int i){
super(i);
System.out.println("B(int)");
}
B(int i, int j){
super(i, j);
System.out.println("B(int,int)");
}
int b;
}
class Example {
public static void main(String[] args) {
B b1 = new B();
System.out.println("=================");
B b2 = new B(10);
System.out.println("=================");
B b3 = new B(10, 20);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q111 Example
===========
class A{
int a;
A(int i){
System.out.println("A(int)");
}
}
class B extends A{
int b;
B(){
// "super()"
super(10);
}
}
class Example {
public static void main(String[] args) {
new B();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q112 Case 5
===========
class A{
int a;
}
class B{
int b;
}
class Example {
public static void main(String[] args) {
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q113 Case 6
==========
class A{
int a;
public void printA(){
System.out.println("a : " + this.a);
}
}
class B extends A{
int b;
public void printB(){
System.out.println("b : " + b);
}
}
class Example {
public static void main(String[] args) {
B b1 = new B();
b1.a = 100;
b1.b = 200;
b1.printA();
b1.printB();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q114 Case 7
==========
class A{
int a;
public void printA(){
System.out.println("a : " + this.a);
}
}
class B extends A{
int b;
public void printB(){
System.out.println("b : " + b);
}
}
class Example {
public static void main(String[] args) {
B b1 = new B();
b1.a = 100; // legal
b1.b = 200;
b1.printA(); // legal
b1.printB();
a1.printA(); // Legal
//a1.printB(); // Illegal
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q115 Case 8 Method Overriding
=========================
class Vehicle{
public void park(){
System.out.println("Vehicle Parking");
}
}
class Example {
public static void main(String[] args) {
Car c1 = new Car();
c1.park();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q116 Method overloading vs overriding
==============================
class Vehicle{
public void park(){
System.out.println("Vehicle Parking");
}
}
public void park(int Location){ // Method Overload => Same name, Different Parameters,
Different Signatures
System.out.println("Car Parking");
}
}
class Example {
public static void main(String[] args) {
Car c1 = new Car();
c1.park();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q117 Case 9 (Dynamic method dispatch)
================================
class Vehicle{
public void park(){
System.out.println("Vehicle Parking");
}
}
class Example {
public static void main(String[] args) {
Vehicle v1 = new Car();
v1.park();
}
}
Polymorphism
(Single Interface, Many Forms)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q118
class Vehicle{
public void park(){
System.out.println("Vehicle Park");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q119 from Q101 Add SMS sender step 1
===============================
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
DisplayFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Display");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
this.displayLabel = new JLabel("50");
this.displayLabel.setFont(new Font("", 1, 30));
add(displayLabel);
setVisible(true);
}
setVisible(true);
}
SplitterFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Splitter");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setVisible(true);
}
setVisible(true);
}
class WaterTankController{
private DisplayFrame displayFrame;
private AlarmFrame alarmFrame;
private SplitterFrame splitterFrame;
WaterTankFrame(WaterTankController waterTankController){
this.waterTankController = waterTankController;
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Water Tank");
setLocationRelativeTo(null);
add(waterLevelSlider);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
WaterTankController controller = new WaterTankController();
controller.setAlarmFrame(new AlarmFrame());
controller.setDisplayFrame(new DisplayFrame());
controller.setSplitterFrame(new SplitterFrame());
WaterTankFrame f1 = new WaterTankFrame(controller);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q120 from Q119
=============
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
DisplayFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Display");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
this.displayLabel = new JLabel("50");
this.displayLabel.setFont(new Font("", 1, 30));
add(displayLabel);
setVisible(true);
}
setVisible(true);
}
SplitterFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Splitter");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setVisible(true);
}
SMSSenderFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("SMS Sender");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setVisible(true);
}
class WaterTankController{
private DisplayFrame displayFrame;
private AlarmFrame alarmFrame;
private SplitterFrame splitterFrame;
private SMSSenderFrame smsSenderFrame;
WaterTankFrame(WaterTankController waterTankController){
this.waterTankController = waterTankController;
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Water Tank");
setLocationRelativeTo(null);
add(waterLevelSlider);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
WaterTankController controller = new WaterTankController();
controller.setAlarmFrame(new AlarmFrame());
controller.setDisplayFrame(new DisplayFrame());
controller.setSplitterFrame(new SplitterFrame());
controller.setSMSSenderFrame(new SMSSenderFrame());
WaterTankFrame f1 = new WaterTankFrame(controller);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q121 Adding Polymorphism
=====================
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
DisplayFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Display");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
this.displayLabel = new JLabel("50");
this.displayLabel.setFont(new Font("", 1, 30));
add(displayLabel);
setVisible(true);
}
setVisible(true);
}
SplitterFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Splitter");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setVisible(true);
}
class WaterTankController{
private WaterLevelObserver[] observers = new WaterLevelObserver[0];
private int waterLevel;
WaterTankFrame(WaterTankController waterTankController){
this.waterTankController = waterTankController;
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Water Tank");
setLocationRelativeTo(null);
add(waterLevelSlider);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
WaterTankController controller = new WaterTankController();
controller.addWaterLevelObserver(new AlarmFrame());
controller.addWaterLevelObserver(new SplitterFrame());
controller.addWaterLevelObserver(new DisplayFrame());
WaterTankFrame f1 = new WaterTankFrame(controller);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q122 Adding SMS Window
======================
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
DisplayFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Display");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
this.displayLabel = new JLabel("50");
this.displayLabel.setFont(new Font("", 1, 30));
add(displayLabel);
setVisible(true);
}
setVisible(true);
}
SplitterFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Splitter");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
this.splitterLabel = new JLabel("Off");
this.splitterLabel.setFont(new Font("", 1, 30));
add(splitterLabel);
setVisible(true);
}
SMSSenderFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("SMS Sender");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setVisible(true);
}
class WaterTankController{
private WaterLevelObserver[] observers = new WaterLevelObserver[0];
WaterTankFrame(WaterTankController waterTankController){
this.waterTankController = waterTankController;
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Water Tank");
setLocationRelativeTo(null);
add(waterLevelSlider);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
WaterTankController controller = new WaterTankController();
controller.addWaterLevelObserver(new AlarmFrame());
controller.addWaterLevelObserver(new SplitterFrame());
controller.addWaterLevelObserver(new DisplayFrame());
controller.addWaterLevelObserver(new SMSSenderFrame());
WaterTankFrame f1 = new WaterTankFrame(controller);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q123
class Vehicle{
public void park(){
System.out.println("Vehicle Parking");
}
}
class Example{
public static void main(String[] args) {
Car c1 = new Car();
c1.callPark();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q124
class Vehicle{
int year = 2000;
public void park(){
System.out.println("Vehicle Parking");
}
}
class Example{
public static void main(String[] args) {
Vehicle v1 = new Car();
v1.park(); // compile from Vehicle, run from Car
System.out.println("year : " + v1.year); // compile from Vehicle, run from Vehicle
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q125
class Vehicle{
int year = 2000;
public void park(){
System.out.println("Vehicle Parking");
}
}
class Example{
public static void main(String[] args) {
Car c1 = new Car();
c1.printYear();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q126
class Vehicle{
public void park(){
System.out.println("Vehicle Parking");
}
class Example{
public static void main(String[] args) {
Car c1 = new Car();
c1.callPark();
}
}
Class “Object”
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q127
class Example {
public static void main(String[] args) {
Customer c1 = new Customer();
c1.hashCode(); // Legal
c1.toString();
c1.equals(c1);
c1.getClass();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q128
Customer(){
super(); // Legal
}
class Example {
public static void main(String[] args) {
Customer c1 = new Customer();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q129
class Customer{
}
class Example {
public static void main(String[] args) {
int[] ar = new int[10];
ar.toString();
ar.equals(ar);
ar.getClass();
ar.hashCode();
String s1 = "ABC";
s1.toString();
s1.equals(s1);
s1.getClass();
s1.hashCode();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q130
import java.util.Random;
import javax.swing.JFrame;
class Customer{
class Example {
public static void main(String[] args) {
Object obj;
class Customer{
private int code;
private String name;
class Example {
public static void main(String[] args) {
Customer c1 = new Customer(1001, "Customer 1");
System.out.println(c1.hashCode());
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q132
class Customer{
private int code;
private String name;
class Example {
public static void main(String[] args) {
Customer c1 = new Customer(1001, "Customer 1");
System.out.println(c1);
int hashCode = c1.hashCode();
System.out.println(hashCode);
System.out.println(Integer.toHexString(hashCode));
}
}
class Customer{
private int code;
private String name;
class Example {
public static void main(String[] args) {
Customer c1 = new Customer(1001, "Customer 1");
}
}
class Customer{
private int code;
private String name;
class Example {
public static void main(String[] args) {
Customer c1 = new Customer(1001, "Customer 1");
System.out.println(c1.toString());
System.out.println(c1); // compiler inserts "toString()"
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q135
class Customer{
private int code;
private String name;
class Example {
public static void main(String[] args) {
String s1 = "ABC";
System.out.println(s1.toString());
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q136 Exercise
============
class Customer{
private int code;
private String name;
class Example {
public static void main(String[] args) {
Customer c1 = new Customer(1001, "Customer 1");
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q137 form Q136
==============
class Customer{
private int code;
private String name;
class Example {
public static void main(String[] args) {
Customer c1 = new Customer(1001, "Customer 1");
class Customer{
private int code;
private String name;
class Example {
public static void main(String[] args) {
Customer c1 = new Customer(1001, "Customer 1");
Customer c2 = new Customer(1001, "Customer 1");
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q139
class Customer{
private int code;
private String name;
class Example {
public static void main(String[] args) {
Customer c1 = new Customer(1001, "Customer 1");
Customer c2 = new Customer(1001, "Customer 1");
Customer c3 = new Customer(1002, "Customer 2");
Customer c4 = c1;
class Customer{
private int code;
private String name;
class Example {
public static void main(String[] args) {
Customer c1 = new Customer(1001, "Customer 1");
Customer c2 = new Customer(1001, "Customer 1");
Customer c3 = new Customer(1002, "Customer 2");
Customer c4 = c1;
class Vehicle{
public void park(){
System.out.println("Vehicle Parking");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q142 Rule 1 (Static Methods)
=======================
Case 1
=====
class Vehicle{
public static void park(){
System.out.println("Vehicle Parking");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q143 Case 2
===========
class Vehicle{
public void park(){
System.out.println("Vehicle Parking");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q144 Case 3
==========
class Vehicle{
public static void park(){
System.out.println("Vehicle Parking");
}
}
class Vehicle{
private void park(){
System.out.println("Vehicle Parking");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q146 Rule 3 “final”
==============
class Vehicle{
public final void park(){
System.out.println("Vehicle Parking");
}
}
class Vehicle{
public void park(){
System.out.println("Vehicle Parking");
}
}
class Vehicle{
public void park(){ // Logical Issue
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q149 from Q148
=============
class Vehicle{
public void park(); // Illegal
}
class Car extends Vehicle{
public void park(){
System.out.println("Car Parking");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q150
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q151 From Q122
=========
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
DisplayFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Display");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
this.displayLabel = new JLabel("50");
this.displayLabel.setFont(new Font("", 1, 30));
add(displayLabel);
setVisible(true);
}
setVisible(true);
}
SplitterFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Splitter");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setVisible(true);
}
SMSSenderFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("SMS Sender");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setVisible(true);
}
class WaterTankController{
private WaterLevelObserver[] observers = new WaterLevelObserver[0];
WaterTankFrame(WaterTankController waterTankController){
this.waterTankController = waterTankController;
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Water Tank");
setLocationRelativeTo(null);
add(waterLevelSlider);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
WaterTankController controller = new WaterTankController();
controller.addWaterLevelObserver(new AlarmFrame());
controller.addWaterLevelObserver(new SplitterFrame());
controller.addWaterLevelObserver(new DisplayFrame());
controller.addWaterLevelObserver(new SMSSenderFrame());
WaterTankFrame f1 = new WaterTankFrame(controller);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q152 Case 1
==========
class Vehicle{
public void park(); // Illegal
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q153 Case 2
==========
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q154 Case 3
===========
class Vehicle{
abstract public void park(); //Illegal
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q155 Case 4
==========
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q156 Case 5
==========
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q157 Case 6
==========
class Example{
public static void main(String[] args) {
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q158 Case 7
==========
class Example{
public static void main(String[] args) {
Vehicle v1; // Legal
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q159 Case 8
===========
class Example{
public static void main(String[] args) {
Vehicle v1; // Legal
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q160 Case 9
==========
int year;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q161 Case 10
===========
int year;
Vehicle(){
//
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q161 Case 11
===========
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q162 Case 12
============
class Animal{
public void eat(){
}
}
Java interfaces
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q163
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q164
interface Vehicle{
void park();
}
class Example{
public static void main(String[] args) {
Vehicle v1 = new Car();
v1.park();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q165 Case 1
==========
interface Vehicle{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q167 Case 3
==========
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q168 Case 4
==========
interface Vehicle{
void park(); // Implicitly public
public void start(); // Implicitly public
protected void stop(); // Illegal
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q169 Case 5
==========
interface Vehicle{
void park(){ // Illegal
// body
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q170 Case 6
==========
interface Vehicle{
void park();
void start();
}
class Example{
public static void main(String[] args) {
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q171 Case 7
==========
interface Vehicle{
void park();
}
class Example{
public static void main(String[] args) {
Vehicle v1; // Legal
v1 = new Vehicle(); // Illegal
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q172 Case 8
==========
interface Vehicle{
void park();
}
class Example{
public static void main(String[] args) {
Vehicle v1; // Legal
v1 = new Car(); // Legal
v1.park();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q173 Case 9
===========
interface Vehicle{
int year = 2000; // implicitly public, final, static
void park();
}
class Example{
public static void main(String[] args) {
Vehicle.year = 2005; // Illegal
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q174 Case 10
==============
interface Vehicle{
int year = 2000;
Vehicle(){ // Illegal
void park();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q175 Exercise from Q150
====================
interface Vehicle{
void park();
}
class Example {
public static void main(String[] args) {
Vehicle[] vehicles = {new Car(), new Van(), new Bus()};
for (Vehicle vehicle : vehicles) {
vehicle.park();
}
}
}
Blueprint of a Class
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q176
interface Date{
void setDate(int year, int month, int day);
void setYear(int year);
void setMonth(int month);
void setDay(int day);
void printDate();
int getYear();
int getMonth();
int getDay();
}
class Example {
public static void main(String[] args) {
Date date = new DateImpl();
date.setDate(2024, 7, 31);
date.printDate();
}
}
class Animal{
interface Lion{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q178 from Q151
=============
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
interface WaterLevelObserver{
void update(int waterLevel);
}
DisplayFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Display");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
this.displayLabel = new JLabel("50");
this.displayLabel.setFont(new Font("", 1, 30));
add(displayLabel);
setVisible(true);
}
setVisible(true);
}
SplitterFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Splitter");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setVisible(true);
}
SMSSenderFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("SMS Sender");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setVisible(true);
}
class WaterTankController{
private WaterLevelObserver[] observers = new WaterLevelObserver[0];
WaterTankFrame(WaterTankController waterTankController){
this.waterTankController = waterTankController;
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Water Tank");
setLocationRelativeTo(null);
add(waterLevelSlider);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
WaterTankController controller = new WaterTankController();
controller.addWaterLevelObserver(new AlarmFrame());
controller.addWaterLevelObserver(new SplitterFrame());
controller.addWaterLevelObserver(new DisplayFrame());
controller.addWaterLevelObserver(new SMSSenderFrame());
WaterTankFrame f1 = new WaterTankFrame(controller);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q179 Case 12
===========
interface Lion{}
interface Fox extends Lion{}
interface Cat{}
interface Dog extends Lion, Cat{}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q180 Case 13
===========
class Lion{}
interface Cat extends Lion{} // Illegal
interface Fox implements Lion{} // Illegal
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q181 Case 14
===========
class Dog{}
interface Lion{}
interface Cat{}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q182 Case 15
===========
interface Vehicle{
void park();
class Example{
public static void main(String[] args) {
Vehicle v1 = new Car();
v1.park();
v1.start();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q183 Case 16
============
interface Vehicle{
void park();
class Example{
public static void main(String[] args) {
Vehicle v1 = new Car();
v1.park();
v1.start();
Vehicle.stop();
}
}
Interface as Contract
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q184
import java.awt.FlowLayout;
import javax.swing.*;
Calculator(){
setSize(400,400);
setLocationRelativeTo(null);
setTitle("Calculator");
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
class MyClass{
public void myMethod(){
System.out.println("You pressed the Button");
}
}
class Example{
public static void main(String[] args) {
new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q185 ActionListener & ActionEvent
===========================
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
Calculator(){
setSize(400,400);
setLocationRelativeTo(null);
setTitle("Calculator");
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
// interface ActionListener{
// void actionPerformed(ActionEvent e);
// }
class Example{
public static void main(String[] args) {
new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q186 Exercise
===========
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
Calculator() {
setSize(400, 400);
setLocationRelativeTo(null);
setTitle("Calculator");
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
// interface MouseListener {
// public void mouseClicked(MouseEvent e);
// public void mouseEntered(MouseEvent e);
// public void mouseExited(MouseEvent e);
// public void mousePressed(MouseEvent e);
// public void mouseReleased(MouseEvent e);
// }
class MyClass {
class Example {
public static void main(String[] args) {
new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q187 from Q186 MouseListener vs MouseEvent
=====================================
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
Calculator() {
setSize(400, 400);
setLocationRelativeTo(null);
setTitle("Calculator");
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
// interface MouseListener {
// public void mouseClicked(MouseEvent e);
// public void mouseEntered(MouseEvent e);
// public void mouseExited(MouseEvent e);
// public void mousePressed(MouseEvent e);
// public void mouseReleased(MouseEvent e);
// }
class Example {
public static void main(String[] args) {
new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q188
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
Calculator() {
setSize(400, 400);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
textField = new JTextField(15);
textField.setFont(new Font("", 1, 25));
add(textField);
setVisible(true);
}
}
class Example {
public static void main(String[] args) {
new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q189 Option 1
============
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
Calculator() {
setSize(400, 400);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
MyClass(Calculator calculator) {
this.calculator = calculator;
}
public void actionPerformed(ActionEvent e) {
String title = calculator.textField.getText();
calculator.setTitle(title);
}
}
class Example {
public static void main(String[] args) {
new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q190 Option 2
============
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class Calculator extends JFrame implements ActionListener{
JTextField textField;
JButton button;
Calculator() {
setSize(400, 400);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
class Example {
public static void main(String[] args) {
new Calculator();
}
}
Inner Class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
Calculator() {
setSize(400, 400);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
class Example {
public static void main(String[] args) {
new Calculator();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
Calculator() {
setSize(400, 400);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
button.addActionListener(new MyClass());
add(button);
setVisible(true);
}
}
class Example {
public static void main(String[] args) {
new Calculator();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
Calculator() {
setSize(400, 400);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
class Example {
public static void main(String[] args) {
new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q194 Option 6 from Option 5
======================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
Calculator() {
setSize(400, 400);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
class Example {
public static void main(String[] args) {
new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q195 Exercise
===========
interface Vehicle{
void park();
}
class Example {
public static void main(String[] args) {
Vehicle v1 = new Vehicle(){
public void park(){
System.out.println("Car parking");
}
};
v1.park();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q196 Exercise
============
interface Vehicle{
void park();
}
class Example {
public static void main(String[] args) {
Vehicle v1 = new Vehicle(){
public void park(){
System.out.println("Car parking");
}
}//; // Illegal
v1.park();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q197 Option 7
============
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
Calculator() {
setSize(400, 400);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
button.addActionListener(ob);
add(button);
setVisible(true);
}
}
class Example {
public static void main(String[] args) {
new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q198 Option 8
============
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
Calculator() {
setSize(400, 400);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
button.addActionListener((e) -> {
String title = textField.getText();
setTitle(title);
});
add(button);
setVisible(true);
}
}
class Example {
public static void main(String[] args) {
new Calculator();
}
}
Lambda Expression
(Since JDK 1.8)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q199
interface Vehicle{
public void park();
}
class Example {
public static void main(String[] args) {
Vehicle car = new Vehicle(){
public void park(){
System.out.println("Car Parking");
}
};
car.park();
jeep.park();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q200 Case 1
==========
interface Vehicle{
public void park();
}
class Example {
public static void main(String[] args) {
Vehicle v1 = () -> System.out.println("Car Parking");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q201 Case 2
==========
interface Vehicle{
public void park();
}
class Example {
public static void main(String[] args) {
Vehicle v1 = () -> {
System.out.println("Statement 1");
System.out.println("Statement 1");
};
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q202 Case 3
==========
interface Vehicle{
public void park();
}
class Example {
public static void main(String[] args) {
Vehicle v1 = () -> {
System.out.println("Statement 1");
System.out.println("Statement 1");
}//; // Illegal
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q203 Case 4
==========
interface Vehicle{
public void park(int location);
}
class Example {
public static void main(String[] args) {
Vehicle v1 = (int location) -> {
System.out.println("Car parking : " + location);
};
v1.park(1000);
v2.park(1001);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q204 Case 5
==========
interface Vehicle{
public boolean park();
}
class Example {
public static void main(String[] args) {
Vehicle v1 = () -> true;
//Vehicle v2 = () -> return true; // Illegal
Vehicle v3 = ()-> {
///
///
return true;
};
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q205 Case 6
==========
interface Vehicle{
public boolean park();
public boolean start();
}
class Example {
public static void main(String[] args) {
Vehicle v1 = ()-> {
};
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q206 Case 7
===========
interface Vehicle{
public void park();
default public void start(){
System.out.println("Default Implementation");
}
}
class Example {
public static void main(String[] args) {
Vehicle v1 = ()-> {
System.out.println("Car Parking");
};
v1.park();
v1.start();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q207 Exercise
===========
interface Math{
public int operation(int num1, int num2);
}
class Example {
public static void main(String[] args) {
int a = 100, b = 50;
Addition addition = new Addition();
Substraction substraction = new Substraction();
Division division = new Division();
Multiplication multiplication = new Multiplication();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q208 (Local Inner)
===============
interface Math{
public int operation(int num1, int num2);
}
class Example {
public static void main(String[] args) {
int a = 100, b = 50;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q209 Anonymous Inner Class
========================
interface Math{
public int operation(int num1, int num2);
}
class Example {
public static void main(String[] args) {
int a = 100, b = 50;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q210 Lambda Expression
====================
interface Math{
public int operation(int num1, int num2);
}
class Example {
public static void main(String[] args) {
int a = 100, b = 50;
Garbage Collector
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q211
class MyClass{
long[] ar = new long[1000000];
}
class Example {
public static void main(String[] args) {
System.out.println("Start Main");
for(int i = 0; i < 100000; i++){
System.out.println("i : " + i);
new MyClass();
}
System.out.println("End Main");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q212
class MyClass{
long[] ar = new long[1000000];
}
class Example {
public static void main(String[] args) {
System.out.println("Start Main");
MyClass[] myClasses = new MyClass[100000];
for(int i = 0; i < 100000; i++){
System.out.println("i : " + i);
myClasses[i] = new MyClass();
}
System.out.println("End Main");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q213 Case 1
==========
class MyClass{
long[] ar = new long[10000000];
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q214 Case 2
==========
class MyClass{
long[] ar = new long[1000000];
System.out.println("End Main");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q215 Case 3
==========
class MyClass{
long[] ar = new long[1000000];
System.gc();
System.out.println("End Main");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q216 Case 4 (Null Referencing)
=========================
class MyClass{
long[] ar = new long[1000000];
c1 = null;
System.gc();
System.out.println("End Main");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q217 Case 5 (Reassigning)
=====================
class MyClass{
long[] ar = new long[1000000];
System.gc();
System.out.println("End Main");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q218 Case 6
==========
class MyClass{
long[] ar = new long[1000000];
new MyClass().myMethod();
System.gc();
System.out.println("End Main");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q219 Case 7 Local Scope
====================
class MyClass{
long[] ar = new long[1000000];
System.gc();
}
}
class A{
int a;
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
public void mB(){
System.out.println("mB of B");
}
}
class Example {
public static void main(String[] args) {
A a1 = new B();
a1.a = 100; // Legal
a1.mA(); // Legal
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q221
class A{
int a;
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
public void mB(){
System.out.println("mB of B");
}
}
class Example {
public static void main(String[] args) {
A a1 = new B();
a1.a = 100; // Legal
a1.mA(); // Legal
// call MB
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q222
class A{
int a;
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
public void mB(){
System.out.println("mB of B");
}
}
class Example {
public static void main(String[] args) {
A a1 = new B();
a1.a = 100; // Legal
a1.mA(); // Legal
// call MB
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q223
class A{
int a;
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
public void mB(){
System.out.println("mB of B");
}
}
class Example {
public static void main(String[] args) {
A a1 = new B();
a1.a = 100; // Legal
a1.mA(); // Legal
// call MB
B b1 = (B)a1; // Legal
b1.b = 200;
b1.mB();;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q224 Case 1
==========
class A{
int a;
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
public void mB(){
System.out.println("mB of B");
}
}
class Example {
public static void main(String[] args) {
A a1 = new A();
a1.a = 100; // Legal
a1.mA(); // Legal
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q225
class A {
int a;
class B extends A {
int b;
class Example {
public static void main(String[] args) {
A a1 = new A();
a1.a = 100; // Legal
a1.mA(); // Legal
if (a1 instanceof B) {
B b1 = (B) a1;
b1.b = 200;
b1.mB();
} else{
System.out.println("Not a instance of B");
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q226 Exercise
===========
class A {}
class B extends A {}
class C extends B {}
class Example {
public static void main(String[] args) {
A a1 = new A();
A a2 = new B();
A a3 = new C();
B b1 = new B();
B b2 = new C();
C c1 = new C();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q227 Case 3
==========
class A{}
class B{}
class Example {
public static void main(String[] args) {
A a1 = new A();
B b1 = new B();
a1 = (A) b1;
b1 = (B) a1;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q228
class A{}
class B{}
class Example {
public static void main(String[] args) {
A a1 = new A();
B b1 = new B();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q229 Case 4
==========
interface A{}
class B{}
class Example {
public static void main(String[] args) {
A a1 = null;
B b1 = null;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q230 Case 5
===========
interface A{}
final class B{}
class Example {
public static void main(String[] args) {
A a1 = null;
B b1 = null;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q232 Step 2 Throw Exception
=======================
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q233 Step 3 Create Connection
=========================
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q234 Step 4 Execute Query
=====================
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
while (resultSet.next()) {
System.out.println(resultSet.getString("ItemCode") + " " +
resultSet.getString("Description") + " " +
resultSet.getString("PackSize") + " " +
resultSet.getDouble("UnitPrice") + " " +
resultSet.getInt("QtyOnHand"));
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q235 Exercise
============
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
while (rst.next()) {
System.out.println(rst.getString(1) + " " + rst.getString(2) + " " + rst.getString(3));
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q236 Step 5 (Execute Update)
========================
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
String sql = "INSERT INTO Item VALUES ('P048', 'Sunlight', '100g', 100.00, 250)";
Statement statement = connection.createStatement();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q237
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Supermarket", "root", "mysql");
String sql = "INSERT INTO Customer VALUES ('" + code + "', '" + title + "' , '" + name + "', '"
+ dob + "', "
+ salary + ", '" + address + "', '" + city + "', '" + province + "', '" + zip + "')";
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q238 (Prepared Statement - Insert)
===========================
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Supermarket", "root", "mysql");
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q239 (Prepared Statement - Select)
============================
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Supermarket", "root", "mysql");
while (rst.next()) {
System.out.println(rst.getString(1) + " " + rst.getString(2) + " " + rst.getString(3) );
}
}
}
Singleton Design Pattern
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q240 Step 1
==========
class A{
public A(){}
A a2 = new A();
a2.myMethod();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q241 Step 2
==========
class A{
private static A a;
public A(){}
return a;
}
A a2 = new A();
a2.myMethod();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q242 Step 3
==========
class A{
private static A a;
private A(){}
return a;
}
A a2 = A.getInstance();
a2.myMethod();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q243 Step 7
==========
// App.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
while (rst.next()) {
System.out.println(rst.getString(1) + " " + rst.getString(2) + " " + rst.getString(3) );
}
}
}
// DBConnection.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
return dbConnection;
}
Enumerations
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q244
// class Color{
// public static final String RED = "RED";
// public static final String GREEN = "GREEN";
// public static final String BLACK = "BLACK";
// public static final String BLUE = "BLUE";
// }
enum Color{
RED, GREEN, BLACK, BLUE; // Enum Objects
}
class Example {
public static void main(String[] args) {
System.out.println(Color.RED);
Color c1 = Color.RED;
System.out.println(c1.toString());
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q245 Case 1
==========
enum Color{
RED, GREEN, BLACK, BLUE; // Enum Objects
}
class Example {
public static void main(String[] args) {
Color c1 = new Color(); // Illegal
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q246 Case 2
==========
enum Color{
RED, GREEN, BLACK, BLUE; // Enum Objects
}
class Example {
public static void main(String[] args) {
Color [] colors = Color.values();
for (Color color : colors) {
System.out.println(color);// color.toString();
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q247 Case 3
==========
enum Color {
RED, GREEN, BLACK, BLUE; // Enum Objects
}
class Example {
public static void main(String[] args) {
Color c1 = Color.RED;
switch (c1) {
case RED:
break;
case BLACK:
break;
case BLUE:
break;
case GREEN:
break;
default:
break;
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q248 Case 4
==========
enum Color {
RED, GREEN, BLACK, BLUE; // Enum Objects
}
class Example {
public static void main(String[] args) {
Color c1 = Color.valueOf("RED");
System.out.println(c1);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q249 Case 5
==========
enum Color {
RED, GREEN, BLACK, BLUE; // Enum Objects
Color(){
System.out.println("Color()");
}
}
class Example {
public static void main(String[] args) {
Color c1 = Color.RED;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q250
enum Color {
RED(1001), GREEN(1002), BLACK(1003), BLUE(1004); // Enum Objects
Color(int code){
this.code = code;
}
class Example {
public static void main(String[] args) {
Color c1 = Color.RED;
System.out.println(c1.getCode());
c1.setCode(2001);
System.out.println(c1.getCode());
}
}
Generics In Java
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q251
class List{
private int[] items = new int[10];
private int count = 0;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q252
class List{
private int[] items = new int[10];
private int count = 0;
class Customer{
}
class CustomerList{
private Customer[] items = new Customer[10];
private int count = 0;
class Example {
public static void main(String[] args) {
List list = new List();
list.add(100);
System.out.println(list.get(0));
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q253 from Q252
=============
class List{
private Object[] items = new Object[10];
private int count = 0;
class Customer{
}
class Example {
public static void main(String[] args) {
List list = new List();
list.add(100); // -> Integer.valueOf(100); Wrapper Class
System.out.println(list.get(0));
list.add(new Customer());
System.out.println(list.get(1));
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q254 from Q253
=============
class List{
private Object[] items = new Object[10];
private int count = 0;
class Customer{
class Example {
public static void main(String[] args) {
List list = new List();
list.add(10);
list.add("ABC");
list.add(new Customer());
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q255
class Customer{
class Example {
public static void main(String[] args) {
List<Integer> intList = new List();
intList.add(100);
intList.add(200);
// intList.add("ABC"); // Illegal
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q256
class Customer{
class Example {
public static void main(String[] args) {
List<Integer> intList = new List();
List<Byte> byteList = new List();
}
}
Collection Framework
Class ArrayList
As list
Type Safe
Indexing, Fast random Access
Insertion Order
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q257
import java.util.ArrayList;
class Customer {
private int code;
private String name;
class Example {
public static void main(String[] args) {
ArrayList<Customer> customers = new ArrayList<>();
customers.add(new Customer(1001, "Customer 1"));
customers.add(new Customer(1002, "Customer 2"));
customers.add(new Customer(1003, "Customer 3"));
customers.add(new Customer(1004, "Customer 4"));
customers.add(new Customer(1005, "Customer 5"));
System.out.println(customers);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q258
import java.util.ArrayList;
class Customer {
private int code;
private String name;
System.out.println(customers);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q259
import java.util.ArrayList;
class Customer {
private int code;
private String name;
class Example {
public static void main(String[] args) {
ArrayList<Customer> customers = new ArrayList<>();
customers.add(new Customer(1001, "Customer 1"));
customers.add(new Customer(1002, "Customer 2"));
customers.add(new Customer(1003, "Customer 3"));
customers.add(new Customer(1004, "Customer 4"));
customers.add(new Customer(1005, "Customer 5"));
customers.forEach(e->{
System.out.println(e);
} );
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q260
import java.util.ArrayList;
class Customer {
private int code;
private String name;
class Example {
public static void main(String[] args) {
ArrayList<String> stringList = new ArrayList<>();
stringList.add("A");
stringList.add("B");
stringList.add("C");
stringList.add("D");
stringList.add("E");
System.out.println(stringList);
boolean b = stringList.contains("B");
System.out.println(b);
b = stringList.contains("X");
System.out.println(b);
stringList.remove(new String("C"));
System.out.println(stringList);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q261 Exercise
============
import java.util.ArrayList;
class Customer {
private int code;
private String name;
class Example {
public static void main(String[] args) {
ArrayList<Customer> customers = new ArrayList<>();
customers.add(new Customer(1001, "Customer 1"));
customers.add(new Customer(1002, "Customer 2"));
customers.add(new Customer(1003, "Customer 3"));
customers.add(new Customer(1004, "Customer 4"));
customers.add(new Customer(1005, "Customer 5"));
System.out.println(customers);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q262 from Q261
=============
import java.util.ArrayList;
class Customer {
private int code;
private String name;
class Example {
public static void main(String[] args) {
ArrayList<Customer> customers = new ArrayList<>();
customers.add(new Customer(1001, "Customer 1"));
customers.add(new Customer(1002, "Customer 2"));
customers.add(new Customer(1003, "Customer 3"));
customers.add(new Customer(1004, "Customer 4"));
customers.add(new Customer(1005, "Customer 5"));
System.out.println(customers);
Class LinkedList
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q263
import java.util.LinkedList;
class Customer {
private int code;
private String name;
class Example {
public static void main(String[] args) {
LinkedList<Customer> customers = new LinkedList<>();
customers.add(new Customer(1001, "Customer 1"));
customers.add(new Customer(1002, "Customer 2"));
customers.add(new Customer(1003, "Customer 3"));
customers.add(new Customer(1004, "Customer 4"));
customers.add(new Customer(1005, "Customer 5"));
System.out.println(customers);
LinkedList as Stack
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q264
import java.util.LinkedList;
class Customer {
private int code;
private String name;
class Example {
public static void main(String[] args) {
LinkedList<Customer> customers = new LinkedList<>();
customers.add(new Customer(1001, "Customer 1"));
customers.add(new Customer(1002, "Customer 2"));
customers.add(new Customer(1003, "Customer 3"));
customers.add(new Customer(1004, "Customer 4"));
customers.add(new Customer(1005, "Customer 5"));
System.out.println(customers);
Customer topCustomer = customers.removeLast();
System.out.println(topCustomer);
System.out.println(customers);
topCustomer = customers.getLast();
System.out.println(topCustomer);
System.out.println(customers);
}
}
LinkedList as Queue
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q265
import java.util.LinkedList;
class Customer {
private int code;
private String name;
class Example {
public static void main(String[] args) {
LinkedList<Customer> customers = new LinkedList<>();
customers.add(new Customer(1001, "Customer 1"));
customers.add(new Customer(1002, "Customer 2"));
customers.add(new Customer(1003, "Customer 3"));
customers.add(new Customer(1004, "Customer 4"));
customers.add(new Customer(1005, "Customer 5"));
System.out.println(customers);
Customer topCustomer = customers.remove();
System.out.println(topCustomer);
System.out.println(customers);
topCustomer = customers.getFirst();
System.out.println(topCustomer);
System.out.println(customers);
}
}
Exception Handling
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q266 ArrayIndexOutOfBoundsException
================================
class Example {
public static void main(String[] args) {
int [] ar = new int[5];
ar[10] = 20;
System.out.println(ar.length);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q267 NullPointerException
=====================
class A{
public void mA(){
System.out.println("mA of A");
}
}
class Example {
private A a;
public static void main(String[] args) {
new Example().callMethod();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q268 ClassNotFoundException
========================
class Example {
public static void main(String[] args) {
Class.forName("com.mysql.cj.jdbc.Driver");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q269 OutOfMemoryError
=====================
class MyClass{
long[] arr= new long[1000000];
}
class Example {
public static void main(String[] args) {
MyClass [] myClassAr = new MyClass[1000000];
for(int i = 0; i < myClassAr.length; i++){
myClassAr[i] = new MyClass();
System.out.println(i);
}
}
}
class Example {
public static void main(String[] args) {
int [] ar = new int[5];
try{
ar[10] = 20;
} catch (ArrayIndexOutOfBoundsException e){
System.out.println("Array does not have index");
}
System.out.println(ar.length);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q271
class A{
public void mA(){
System.out.println("mA of A");
}
}
class Example {
private A a;
public static void main(String[] args) {
new Example().callMethod();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q272
class Example {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("JDBC Driver not found");
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q273
class A{
public void mA(){
System.out.println("mA of A");
}
}
class Example {
private A a;
public static void main(String[] args) {
new Example().callMethod();
System.out.println("Final Statement");
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q274 Case 1
==========
class Example {
public static void main(String[] args) {
try{
// Code
} // Illegal
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q275 Case 2
==========
class Example {
public static void main(String[] args) {
try{
// Code
} catch (Exception e){
// handling Exception
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q276 Case 3
==========
class Example {
public static void main(String[] args) {
try{
// Code
} catch (NullPointerException e){
// handling Exception
} catch (NumberFormatException e){
// handling Exception
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q277 from Q276
=============
class Example {
private String s1;
public static void main(String[] args) {
try{
Example e = new Example();
System.out.println(e.s1.length());
Integer num = Integer.parseInt(e.s1);
} catch (NullPointerException e){
System.out.println("S1 is Null");
} catch (NumberFormatException e){
System.out.println("S1 is not integer");
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q278 Case 5
==========
class Example {
public static void main(String[] args) {
try{
try {
} catch (Exception e) {
}
} catch (Exception e){
try {
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q279
class Example {
private String s1 = "ABC";
public static void main(String[] args) {
try{
Example example = new Example();
System.out.println(example.s1.length());
try {
Integer num = Integer.parseInt(example.s1);
} catch (Exception e) {
System.out.println("S1 is not an Integer");
}
} catch (NullPointerException e){
System.out.println("S1 is null");
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q280 Exercise
============
class Example {
private String s1;
public static void main(String[] args) {
try{
Example e = new Example();
System.out.println(e.s1.length());
Integer num = Integer.parseInt(e.s1);
System.out.println(e.s1);
} catch (NullPointerException e){
System.out.println("S1 is Null");
} catch (NumberFormatException e){
System.out.println("S1 is not integer");
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q281 from Q380
=============
class Example {
private String s1 = "ABC";
public static void main(String[] args) {
try{
Example e = new Example();
System.out.println(e.s1.length());
try{
Integer num = Integer.parseInt(e.s1);
} catch (NumberFormatException ex){
System.out.println("S1 is not an integer");
}
System.out.println(e.s1);
} catch (NullPointerException e){
System.out.println("S1 is Null");
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q282
class A {
public void mA() {
System.out.println("mA of A");
}
}
class Example {
private A a;
System.out.println("Final Statement");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q283 Case 6
===========
class A {
public void mA() {
System.out.println("mA of A");
}
}
class Example {
private A a;
System.out.println("Final Statement");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q284 Case 7
==========
class Example {
public static void main(String[] args) {
try{
} finally{
System.out.println("Finally block");
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q285
class Example {
public static void main(String[] args) {
try{
} catch(Exception e){
} finally{
System.out.println("Finally block");
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q286 Case 8
===========
class Example {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q288
class Car {
public void park(){
System.out.println("Car Parking");
}
}
class Example {
public static void main(String[] args) {
Thread t1 = new Thread(new MyThread1());
t1.start();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q290
class Car {
public void park(){
System.out.println("Car Parking");
}
}
class Example {
public static void main(String[] args) {
Thread t1 = new Thread(new MyThread1());
t1.start();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q291 Thread Lifecycle
=================
class Example {
public static void main(String[] args) {
MyThread myThread = new MyThread();
System.out.println(myThread.getState());
myThread.start();
System.out.println(myThread.getState());
try { Thread.sleep(500); } catch (Exception e) {}
System.out.println(myThread.getState());
try { Thread.sleep(2000); } catch (Exception e) {}
System.out.println(myThread.getState());
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q292 Thread Pool
==============
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class Example {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(5);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q293 Thread Join
==============
class Example {
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
MyThread t3 = new MyThread();
t1.start();
try {
t1.join();
} catch (Exception e) {}
t2.start();
try {
t2.join();
} catch (Exception e) {}
t3.start();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q294 Thread Priority
================
class Example {
public static void main(String[] args) {
MyThread1 myThread1 = new MyThread1();
MyThread2 myThread2 = new MyThread2();
MyThread3 myThread3 = new MyThread3();
MyThread4 myThread4 = new MyThread4();
myThread1.setPriority(Thread.MAX_PRIORITY);
myThread2.setPriority(Thread.MIN_PRIORITY);
myThread3.setPriority(Thread.NORM_PRIORITY);
myThread4.setPriority(6);
myThread1.start();
myThread2.start();
myThread3.start();
myThread4.start();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q295 Daemon Thread
==================
class Example {
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
t1.setDaemon(true);
t1.start();
t2.start();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q296 Using Lambda Expression
=========================
class Example {
public static void main(String[] args) {
new Thread(() -> {
for(int i = 0; i < 10; i++){
System.out.println("Thread : " + i);
};
}).start();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q297 Synchronization
==================
class MyClass{
public void printData(String threadName){
for(int i = 0; i < 10; i++){
try {
Thread.sleep(500);
} catch (Exception e) {}
System.out.println(threadName + " + " + i );
}
}
}
MyThread1(MyClass myClass){
this.myClass = myClass;
}
MyThread2(MyClass myClass){
this.myClass = myClass;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q298 from Q297
=============
class MyClass{
synchronized public void printData(String threadName){
for(int i = 0; i < 10; i++){
try {
Thread.sleep(500);
} catch (Exception e) {}
System.out.println(threadName + " + " + i );
}
}
}
MyThread1(MyClass myClass){
this.myClass = myClass;
}
MyThread2(MyClass myClass){
this.myClass = myClass;
}
class MyClass{
synchronized public void myMethod1(MyClass myClass){
System.out.println("myMethod 1 - start");
try {
Thread.sleep(1000);
} catch (Exception e) {}
myClass.myMethod2();
System.out.println("myMethod 1 - end");
}
class Example {
public static void main(String[] args) {
MyClass myClass1 = new MyClass();
MyClass myClass2 = new MyClass();
t1.start();
t2.start();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q300
class MyClass{
synchronized public void myMethod1(MyClass myClass){
System.out.println("myMethod 1 - start");
try {
Thread.sleep(1000);
} catch (Exception e) {}
myClass.myMethod2();
System.out.println("myMethod 1 - end");
}
class Example {
public static void main(String[] args) {
MyClass myClass1 = new MyClass();
MyClass myClass2 = new MyClass();
t1.start();
try {
t1.join();
} catch (Exception e) {}
t2.start();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
END
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////