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) {
[Link] = "S001";
[Link] = "Student 1";
[Link] = "Panadura";
s1.sub1 = 50;
s1.sub2 = 75;
[Link] = "S002";
[Link] = "Student 2";
[Link] = "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) {
[Link] = "S001";
[Link] = "Student 1";
[Link] = "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) {
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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) {
// [Link] = "S001";
// [Link] = "Student 1";
// [Link] = "Panadura";
// s1.sub1 = 50;
// s1.sub2 = 75;
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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) {
// [Link] = "S001";
// [Link] = "Student 1";
// [Link] = "Panadura";
// s1.sub1 = 50;
// s1.sub2 = 75;
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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(){
[Link](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();
[Link](b1);
[Link] = 5;
[Link] = 6;
[Link] = 10;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q11 Case 2
=========
class Box{
int length; // Global Variables
int width; // Global Variables
int height; // Global Variables
[Link] = 5;
[Link] = 6;
[Link] = 10;
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q12 Case 3
==========
class Box{
int length; // Global Variables
int width; // Global Variables
int height; // Global Variables
// [Link] = 5;
// [Link] = 6;
// [Link] = 10;
[Link](10, 6, 5);
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q13 Case 4
=========
class Box{
int length; // Global Variables
int width; // Global Variables
int height; // Global Variables
[Link](10, 6, 5);
int volume = [Link]();
[Link]("Volume : " + volume);
}
}
Case 5 Keyword ‘this’
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q14
class Box{
int length;
int width;
int height;
[Link](10, 6, 5);
int volume = [Link]();
[Link]("Volume : " + volume);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q15
class Box{
int length;
int width;
int height;
[Link](10, 6, 5);
int volume = [Link]();
[Link]("Volume : " + volume);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q16
class A{
public void print(){
[Link](this);
}
}
class Example{
public static void main(String[] args) {
A a1 = new A();
[Link](a1);
[Link]();
}
}
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(){
[Link] = 5;
[Link] =5;
[Link] = 5;
}
Box(int length){
[Link] = length;
[Link] = length;
[Link] = length;
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q21
class Box{
int length;
int width;
int height;
Box(){
[Link] = 5;
[Link] =5;
[Link] = 5;
}
Box(int length){
[Link] = length;
[Link] = length;
[Link] = length;
}
[Link](10,20,30); // Illegal
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q22
class Box{
int length;
int width;
int height;
Box(){
[Link] = 5;
[Link] =5;
[Link] = 5;
}
Box(int length){
[Link] = length;
[Link] = length;
[Link] = length;
}
[Link](10,20,30);
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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();
[Link](m1.a);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q25
class Box{
int length = 10;
int width = 6;
int height;
height = 5; // Illegal
class Account{
double balance;
Account(double balance){
[Link] = balance;
}
class Operation{
public void deposit(double amount, Account account){
[Link]+= amount;
}
[Link](5000, a1);
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q27
class Account{
double balance;
Account(double balance){
[Link] = balance;
}
class Operation{
public void deposit(double amount, Account account){
[Link]+= amount;
}
class Example{
public static void main(String[] args) {
Account a1 = new Account(10000);
[Link]();
Operation operation = new Operation();
[Link](3000, a1);
[Link]();
[Link](5000, a1);
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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();
[Link](myClass.x);
increment(myClass.x);
[Link](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();
[Link](myClass.x);
increment(myClass);
[Link](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);
[Link](); //Volume : 300
Box b2 = [Link]();
[Link](); //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);
[Link](); //Volume : 300
Box b2 = [Link]();
[Link](); //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(){
[Link]("instanceMethod");
}
class Example{
public static void main(String[] args) {
[Link]();
// [Link](); // 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(){
[Link] = 1980;
[Link] = 11;
[Link] = 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
[Link] = 1980;
[Link] = 11;
[Link] = 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);
[Link](); // 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);
[Link](); // 1980-11-15
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q46 from Q45
===========
class Date{
int year;
int month;
int day;
Date(){
[Link] = 2000;
[Link] = 12;
[Link] = 17;
}
Date(int year, int month, int day){
[Link] = year;
[Link] = month;
[Link] = day;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
[Link](); // 1980-11-15
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q47 Exercise 6
============
class Date{
int year;
int month;
int day;
Date(){
[Link] = 2000;
[Link] = 12;
[Link] = 17;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
[Link](); // 1980-11-15
[Link](1991,12,14);
[Link](); // 1991-12-14
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q48 from Q47
============
class Date{
int year;
int month;
int day;
Date(){
[Link] = 2000;
[Link] = 12;
[Link] = 17;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
[Link](); // 1980-11-15
[Link](1991,12,14);
[Link](); // 1991-12-14
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q49 Exercise 7
============
class Date{
int year;
int month;
int day;
Date(){
[Link] = 2000;
[Link] = 12;
[Link] = 17;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
[Link](); // 1980-11-15
[Link](1991,12,14);
[Link](); // 1991-12-14
[Link](d1);
[Link](); // 1980-11-15
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q50 from Q49
===========
class Date{
int year;
int month;
int day;
Date(){
[Link] = 2000;
[Link] = 12;
[Link] = 17;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
[Link](); // 1980-11-15
[Link](1991,12,14);
[Link](); // 1991-12-14
[Link](d1);
[Link](); // 1980-11-15
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q51 Exercise 8
============
class Date{
int year;
int month;
int day;
Date(){
[Link] = 2000;
[Link] = 12;
[Link] = 17;
}
class Example{
public static void main(String[] args) {
Date d1 = [Link]();
[Link](); // 2000-12-17
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q52 from Q51
============
class Date{
int year;
int month;
int day;
Date(){
[Link] = 2000;
[Link] = 12;
[Link] = 17;
}
class Example{
public static void main(String[] args) {
Date d1 = [Link]();
[Link](); // 2000-12-17
}
}
Access Privileges | Access Levels
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q53
////////////////////////////////////////////////////
package package1;
import [Link];
class Example{
public static void main(String[] args) {
Car c1 = new Car();
[Link]();
[Link]([Link]);
//[Link]([Link]); // Illegal
//[Link]([Link]);// Illegal
//[Link]([Link]);// Illegal
}
}
////////////////////////////////////////////////////
package package2;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q54
////////////////////////////////////////////////////
package package1;
class Example{
public static void main(String[] args) {
Car c1 = new Car();
[Link]();
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
//[Link]([Link]);// 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();
[Link]("Is Start : " + [Link]);
[Link]("Speed : " + [Link]);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q56 Step 2
=========
class Car{
boolean isStart;
int speed;
}
class Example{
public static void main(String[] args) {
Car c1 = new Car();
[Link]("Is Start : " + [Link]);
[Link]("Speed : " + [Link]);
[Link] = 100;
[Link]("Is Start : " + [Link]);
[Link]("Speed : " + [Link]);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q57 Step 3
=========
class Car{
boolean isStart;
int speed;
}
class Example{
public static void main(String[] args) {
Car c1 = new Car();
[Link]("Is Start : " + [Link]);
[Link]("Speed : " + [Link]);
[Link] = true;
[Link] = 100;
[Link]("Is Start : " + [Link]);
[Link]("Speed : " + [Link]);
[Link] = false;
[Link]("Is Start : " + [Link]);
[Link]("Speed : " + [Link]);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q58 Step 4
=========
class Car{
private boolean isStart;
private int speed;
}
class Example{
public static void main(String[] args) {
Car c1 = new Car();
[Link]("Is Start : " + [Link]);
[Link]("Speed : " + [Link]);
[Link] = true;
[Link] = 100;
[Link]("Is Start : " + [Link]);
[Link]("Speed : " + [Link]);
[Link] = false;
[Link]("Is Start : " + [Link]);
[Link]("Speed : " + [Link]);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q59 Step 5
=========
class Car{
private boolean isStart;
private int speed;
class Example{
public static void main(String[] args) {
Car c1 = new Car();
[Link]("Is Start : " + [Link]);
[Link]("Speed : " + [Link]);
[Link] = true;
[Link] = 100;
[Link]("Is Start : " + [Link]);
[Link]("Speed : " + [Link]);
[Link] = false;
[Link]("Is Start : " + [Link]);
[Link]("Speed : " + [Link]);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q60 Step 6
=========
class Car{
private boolean isStart;
private int speed;
class Example{
public static void main(String[] args) {
Car c1 = new Car();
[Link]("Is Start : " + [Link]());
[Link]("Speed : " + [Link]());
[Link](true);
[Link]();
[Link]("Is Start : " + [Link]());
[Link]("Speed : " + [Link]());
[Link](false);
[Link]("Is Start : " + [Link]());
[Link]("Speed : " + [Link]());
}
}
import [Link].*;
class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
[Link](JFrame.EXIT_ON_CLOSE);
[Link](500, 400);
[Link]("Test Title");
[Link](null);
[Link](true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q62
import [Link].*;
class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
[Link](JFrame.EXIT_ON_CLOSE);
[Link](500, 400);
[Link]("Test Title");
[Link](null);
[Link](true);
[Link](true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q63
import [Link].*;
class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
[Link](JFrame.DO_NOTHING_ON_CLOSE);
[Link](500, 400);
[Link]("Test Title");
[Link](null);
[Link](true);
}
}
// JFrame.EXIT_ON_CLOSE = 3
// JFrame.DISPOSE_ON_CLOSE = 2
// JFrame.HIDE_ON_CLOSE = 1
// JFrame.DO_NOTHING_ON_CLOSE = 0
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q64
import [Link].*;
class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
[Link](JFrame.DISPOSE_ON_CLOSE);
[Link](500, 400);
[Link]("Frame 1");
[Link](null);
[Link](true);
[Link](true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q65
import [Link].*;
import [Link].*;
class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
[Link](500,500);
[Link]("Frame 1");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
[Link](button);
[Link](true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q66
import [Link].*;
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
[Link](500,400);
[Link]("Calculator");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
[Link](true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q67
import [Link].*;
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
[Link](500,400);
[Link]("Calculator");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
[Link](true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q68
import [Link].*;
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
[Link](500,400);
[Link]("Calculator");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
[Link](true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q69
import [Link].*;
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 [Link].*;
import [Link].*;
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 [Link].*;
import [Link].*;
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 [Link].*;
import [Link].*;
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 [Link].*;
import [Link].*;
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 [Link].*;
import [Link].*;
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 [Link].*;
import [Link].*;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new FlowLayout([Link]));
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q76 Align Right
import [Link].*;
import [Link].*;
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
Grid Layout
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q77
import [Link].*;
import [Link].*;
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 [Link].*;
import [Link].*;
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 [Link].*;
import [Link].*;
add("Center", centerPanel);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q80 Exercise
===========
import [Link].*;
import [Link].*;
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 [Link].*;
import [Link].*;
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
new Notepad();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q82 Step 2
=========
import [Link].*;
import [Link].*;
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 [Link].*;
import [Link].*;
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 [Link].*;
import [Link].*;
Notepad(){
setSize(600,400);
setLocationRelativeTo(null);
setTitle("untitled");
setDefaultCloseOperation(EXIT_ON_CLOSE);
add("Center", scrollPane);
[Link](fileMenu);
[Link](editMenu);
setJMenuBar(menuBar);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
new Notepad();
}
}
Working with JTable
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q85
import [Link].*;
import [Link];
import [Link].*;
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 [Link].*;
import [Link].*;
import [Link];
import [Link];
JButton button;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link]("You pressed the button...!");
}
});
add(button);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q87
import [Link].*;
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
JTextField textField;
JSlider slider;
Calculator(){
setSize(350,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
[Link](new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int value = [Link]();
[Link](value+"");// [Link](value)
}
});
add(slider);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q88 Runtime Error
===============
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
SliderFrame(){
setSize(350,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Slider Frame");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
[Link](new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int value = [Link]();
[Link]([Link](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 [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
SliderFrame(DisplayFrame displayFrame){
[Link] = displayFrame;
setSize(350,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Slider Frame");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
[Link](new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int value = [Link]();
[Link]([Link](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 [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
SliderFrame(DisplayFrame displayFrame){
[Link] = displayFrame;
setSize(350,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Slider Frame");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
[Link](new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int value = [Link]();
[Link]([Link](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 [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
[Link](new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int value = [Link]();
[Link]([Link](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(){
[Link]("mA of A");
}
}
class B{
public void mB(){
[Link]();
}
}
class Example{
public static void main(String[] args) {
B b = new B();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q93 from Q92 (Runtime Error)
========================
class A{
public void mA(){
[Link]("mA of A");
}
}
class B{
A a1;
public void mB(){
[Link]();
}
}
class Example{
public static void main(String[] args) {
B b = new B();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q94 from Q93 Option 1
==================
class A{
public void mA(){
[Link]("mA of A");
}
}
class B{
A a1 = new A();
public void mB(){
[Link]();
}
}
class Example{
public static void main(String[] args) {
B b = new B();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q95 from Q93 Option 2
==================
class A{
public void mA(){
[Link]("mA of A");
}
}
class B{
A a1;
B(){
this.a1 = new A();
}
class Example{
public static void main(String[] args) {
B b = new B();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q96 from Q93 Option 3
==================
class A{
public void mA(){
[Link]("mA of A");
}
}
class B{
A a1;
B(A a1){
this.a1 = a1;
}
public void mB(){
[Link]();
}
}
class Example{
public static void main(String[] args) {
A a1 = new A();
B b = new B(a1);
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q97 from Q93 Option 4
===================
class A{
public void mA(){
[Link]("mA of A");
}
}
class B{
A a1;
class Example{
public static void main(String[] args) {
A a1 = new A();
B b = new B();
[Link](a1);
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q98 from Q93 Option 5
===================
class A{
public void mA(){
[Link]("mA of A");
}
}
class B{
class Example{
public static void main(String[] args) {
A a1 = new A();
B b = new B();
[Link](a1);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q99
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
DisplayFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Display");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
[Link] = new JLabel("50");
[Link](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());
[Link] = new JLabel("Off");
[Link](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 [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
DisplayFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Display");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
[Link] = new JLabel("50");
[Link](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();
[Link](new DisplayFrame());
[Link](new AlarmFrame());
[Link](new SplitterFrame());
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q101 from Q100
=============
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
DisplayFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Display");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
[Link] = new JLabel("50");
[Link](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){
[Link] = 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();
[Link](new AlarmFrame());
[Link](new DisplayFrame());
[Link](new SplitterFrame());
WaterTankFrame f1 = new WaterTankFrame(controller);
}
}
Inheritance
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q102 Case 1
==========
import [Link].*;
}
class Example {
public static void main(String[] args) {
JFrame f1 = new JFrame();
[Link](1000, 300);
[Link](null);
[Link]("Frame");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
import [Link].*;
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(){
[Link]("mA of A");
}
}
class B extends A{
int b;
public void mB(){
[Link]("mB of B");
}
class Example {
public static void main(String[] args) {
B b = new B();
[Link]();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q105 Case 3
==========
class A{
int a;
static {
[Link]("static block of A");
}
public void mA(){
[Link]("mA of A");
}
}
class B extends A{
int b;
static {
[Link]("static block of B");
}
public void mB(){
[Link]("mB of B");
}
}
class Example {
public static void main(String[] args) {
new B();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q106 Exercise
============
class A{
int a;
A(){
[Link]("constructor of A");
}
{
[Link]("instance block of A");
}
static {
[Link]("static block of A");
}
public void mA(){
[Link]("mA of A");
}
}
class B extends A{
int b;
B(){
[Link]("constructor of B");
}
{
[Link]("instance block of B");
}
static {
[Link]("static block of B");
}
public void mB(){
[Link]("mB of B");
}
}
class Example {
public static void main(String[] args) {
new B();
new B();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q107 Exercise
============
class A{
int a;
static{
[Link]("static block of A");
}
}
class B extends A{
int b;
static{
[Link]("static block of B");
}
}
class C extends B{
int c;
static{
[Link]("static block of C");
}
}
class D extends B{
int d;
static{
[Link]("static block of D");
}
}
class Example {
public static void main(String[] args) {
new D();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q108 Case 4
===========
class A{
int a;
A(){
[Link]("A()");
}
A(int i){
[Link]("A(int)");
}
A(int i, int j){
[Link]("A(int,int)");
}
}
class B extends A{
B(){
[Link]("B()");
}
B(int i){
[Link]("B(int)");
}
B(int i, int j){
[Link]("B(int,int)");
}
int b;
}
class Example {
public static void main(String[] args) {
B b1 = new B();
[Link]("=================");
B b2 = new B(10);
[Link]("=================");
B b3 = new B(10, 20);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q109
class A{
int a;
A(){
[Link]("A()");
}
A(int i){
[Link]("A(int)");
}
A(int i, int j){
[Link]("A(int,int)");
}
}
class B extends A{
B(){
// compiler inserts super();
[Link]("B()");
}
B(int i){
// compiler inserts super();
[Link]("B(int)");
}
B(int i, int j){
// compiler inserts super();
[Link]("B(int,int)");
}
int b;
}
class Example {
public static void main(String[] args) {
B b1 = new B();
[Link]("=================");
B b2 = new B(10);
[Link]("=================");
B b3 = new B(10, 20);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q110 from Q109
=============
class A{
int a;
A(){
[Link]("A()");
}
A(int i){
[Link]("A(int)");
}
A(int i, int j){
[Link]("A(int,int)");
}
}
class B extends A{
B(){
super();
[Link]("B()");
}
B(int i){
super(i);
[Link]("B(int)");
}
B(int i, int j){
super(i, j);
[Link]("B(int,int)");
}
int b;
}
class Example {
public static void main(String[] args) {
B b1 = new B();
[Link]("=================");
B b2 = new B(10);
[Link]("=================");
B b3 = new B(10, 20);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q111 Example
===========
class A{
int a;
A(int i){
[Link]("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(){
[Link]("a : " + this.a);
}
}
class B extends A{
int b;
public void printB(){
[Link]("b : " + b);
}
}
class Example {
public static void main(String[] args) {
B b1 = new B();
b1.a = 100;
b1.b = 200;
[Link]();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q114 Case 7
==========
class A{
int a;
public void printA(){
[Link]("a : " + this.a);
}
}
class B extends A{
int b;
public void printB(){
[Link]("b : " + b);
}
}
class Example {
public static void main(String[] args) {
B b1 = new B();
b1.a = 100; // legal
b1.b = 200;
[Link](); // legal
[Link]();
[Link](); // Legal
//[Link](); // Illegal
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q115 Case 8 Method Overriding
=========================
class Vehicle{
public void park(){
[Link]("Vehicle Parking");
}
}
class Example {
public static void main(String[] args) {
Car c1 = new Car();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q116 Method overloading vs overriding
==============================
class Vehicle{
public void park(){
[Link]("Vehicle Parking");
}
}
public void park(int Location){ // Method Overload => Same name, Different Parameters,
Different Signatures
[Link]("Car Parking");
}
}
class Example {
public static void main(String[] args) {
Car c1 = new Car();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q117 Case 9 (Dynamic method dispatch)
================================
class Vehicle{
public void park(){
[Link]("Vehicle Parking");
}
}
class Example {
public static void main(String[] args) {
Vehicle v1 = new Car();
[Link]();
}
}
Polymorphism
(Single Interface, Many Forms)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q118
class Vehicle{
public void park(){
[Link]("Vehicle Park");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q119 from Q101 Add SMS sender step 1
===============================
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
DisplayFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Display");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
[Link] = new JLabel("50");
[Link](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){
[Link] = 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();
[Link](new AlarmFrame());
[Link](new DisplayFrame());
[Link](new SplitterFrame());
WaterTankFrame f1 = new WaterTankFrame(controller);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q120 from Q119
=============
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
DisplayFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Display");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
[Link] = new JLabel("50");
[Link](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){
[Link] = 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();
[Link](new AlarmFrame());
[Link](new DisplayFrame());
[Link](new SplitterFrame());
[Link](new SMSSenderFrame());
WaterTankFrame f1 = new WaterTankFrame(controller);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q121 Adding Polymorphism
=====================
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
DisplayFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Display");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
[Link] = new JLabel("50");
[Link](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){
[Link] = 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();
[Link](new AlarmFrame());
[Link](new SplitterFrame());
[Link](new DisplayFrame());
WaterTankFrame f1 = new WaterTankFrame(controller);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q122 Adding SMS Window
======================
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
DisplayFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Display");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
[Link] = new JLabel("50");
[Link](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());
[Link] = new JLabel("Off");
[Link](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){
[Link] = 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();
[Link](new AlarmFrame());
[Link](new SplitterFrame());
[Link](new DisplayFrame());
[Link](new SMSSenderFrame());
WaterTankFrame f1 = new WaterTankFrame(controller);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q123
class Vehicle{
public void park(){
[Link]("Vehicle Parking");
}
}
class Example{
public static void main(String[] args) {
Car c1 = new Car();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q124
class Vehicle{
int year = 2000;
public void park(){
[Link]("Vehicle Parking");
}
}
class Example{
public static void main(String[] args) {
Vehicle v1 = new Car();
[Link](); // compile from Vehicle, run from Car
[Link]("year : " + [Link]); // compile from Vehicle, run from Vehicle
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q125
class Vehicle{
int year = 2000;
public void park(){
[Link]("Vehicle Parking");
}
}
class Example{
public static void main(String[] args) {
Car c1 = new Car();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q126
class Vehicle{
public void park(){
[Link]("Vehicle Parking");
}
class Example{
public static void main(String[] args) {
Car c1 = new Car();
[Link]();
}
}
Class “Object”
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q127
class Example {
public static void main(String[] args) {
Customer c1 = new Customer();
[Link](); // Legal
[Link]();
[Link](c1);
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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];
[Link]();
[Link](ar);
[Link]();
[Link]();
String s1 = "ABC";
[Link]();
[Link](s1);
[Link]();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q130
import [Link];
import [Link];
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");
[Link]([Link]());
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q132
class Customer{
private int code;
private String name;
class Example {
public static void main(String[] args) {
Customer c1 = new Customer(1001, "Customer 1");
[Link](c1);
int hashCode = [Link]();
[Link](hashCode);
[Link]([Link](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");
[Link]([Link]());
[Link](c1); // compiler inserts "toString()"
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q135
class Customer{
private int code;
private String name;
class Example {
public static void main(String[] args) {
String s1 = "ABC";
[Link]([Link]());
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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(){
[Link]("Vehicle Parking");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q142 Rule 1 (Static Methods)
=======================
Case 1
=====
class Vehicle{
public static void park(){
[Link]("Vehicle Parking");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q143 Case 2
===========
class Vehicle{
public void park(){
[Link]("Vehicle Parking");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q144 Case 3
==========
class Vehicle{
public static void park(){
[Link]("Vehicle Parking");
}
}
class Vehicle{
private void park(){
[Link]("Vehicle Parking");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q146 Rule 3 “final”
==============
class Vehicle{
public final void park(){
[Link]("Vehicle Parking");
}
}
class Vehicle{
public void park(){
[Link]("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(){
[Link]("Car Parking");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q150
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q151 From Q122
=========
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
DisplayFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Display");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
[Link] = new JLabel("50");
[Link](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){
[Link] = 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();
[Link](new AlarmFrame());
[Link](new SplitterFrame());
[Link](new DisplayFrame());
[Link](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();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q173 Case 9
===========
interface Vehicle{
int year = 2000; // implicitly public, final, static
void park();
}
class Example{
public static void main(String[] args) {
[Link] = 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) {
[Link]();
}
}
}
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();
[Link](2024, 7, 31);
[Link]();
}
}
class Animal{
interface Lion{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q178 from Q151
=============
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
interface WaterLevelObserver{
void update(int waterLevel);
}
DisplayFrame(){
setSize(300, 300);
setLocationRelativeTo(null);
setTitle("Display");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
[Link] = new JLabel("50");
[Link](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){
[Link] = 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();
[Link](new AlarmFrame());
[Link](new SplitterFrame());
[Link](new DisplayFrame());
[Link](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();
[Link]();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q183 Case 16
============
interface Vehicle{
void park();
class Example{
public static void main(String[] args) {
Vehicle v1 = new Car();
[Link]();
[Link]();
[Link]();
}
}
Interface as Contract
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q184
import [Link];
import [Link].*;
Calculator(){
setSize(400,400);
setLocationRelativeTo(null);
setTitle("Calculator");
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
class MyClass{
public void myMethod(){
[Link]("You pressed the Button");
}
}
class Example{
public static void main(String[] args) {
new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q185 ActionListener & ActionEvent
===========================
import [Link];
import [Link];
import [Link];
import [Link].*;
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 [Link];
import [Link];
import [Link];
import [Link].*;
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 [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
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 [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
Calculator() {
setSize(400, 400);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
textField = new JTextField(15);
[Link](new Font("", 1, 25));
add(textField);
setVisible(true);
}
}
class Example {
public static void main(String[] args) {
new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q189 Option 1
============
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
Calculator() {
setSize(400, 400);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
MyClass(Calculator calculator) {
[Link] = calculator;
}
public void actionPerformed(ActionEvent e) {
String title = [Link]();
[Link](title);
}
}
class Example {
public static void main(String[] args) {
new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q190 Option 2
============
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
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 [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
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 [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
Calculator() {
setSize(400, 400);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
[Link](new MyClass());
add(button);
setVisible(true);
}
}
class Example {
public static void main(String[] args) {
new Calculator();
}
}
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
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 [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
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(){
[Link]("Car parking");
}
};
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q196 Exercise
============
interface Vehicle{
void park();
}
class Example {
public static void main(String[] args) {
Vehicle v1 = new Vehicle(){
public void park(){
[Link]("Car parking");
}
}//; // Illegal
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q197 Option 7
============
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
Calculator() {
setSize(400, 400);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
[Link](ob);
add(button);
setVisible(true);
}
}
class Example {
public static void main(String[] args) {
new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q198 Option 8
============
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
Calculator() {
setSize(400, 400);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
[Link]((e) -> {
String title = [Link]();
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(){
[Link]("Car Parking");
}
};
[Link]();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q200 Case 1
==========
interface Vehicle{
public void park();
}
class Example {
public static void main(String[] args) {
Vehicle v1 = () -> [Link]("Car Parking");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q201 Case 2
==========
interface Vehicle{
public void park();
}
class Example {
public static void main(String[] args) {
Vehicle v1 = () -> {
[Link]("Statement 1");
[Link]("Statement 1");
};
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q202 Case 3
==========
interface Vehicle{
public void park();
}
class Example {
public static void main(String[] args) {
Vehicle v1 = () -> {
[Link]("Statement 1");
[Link]("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) -> {
[Link]("Car parking : " + location);
};
[Link](1000);
[Link](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(){
[Link]("Default Implementation");
}
}
class Example {
public static void main(String[] args) {
Vehicle v1 = ()-> {
[Link]("Car Parking");
};
[Link]();
[Link]();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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) {
[Link]("Start Main");
for(int i = 0; i < 100000; i++){
[Link]("i : " + i);
new MyClass();
}
[Link]("End Main");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q212
class MyClass{
long[] ar = new long[1000000];
}
class Example {
public static void main(String[] args) {
[Link]("Start Main");
MyClass[] myClasses = new MyClass[100000];
for(int i = 0; i < 100000; i++){
[Link]("i : " + i);
myClasses[i] = new MyClass();
}
[Link]("End Main");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q213 Case 1
==========
class MyClass{
long[] ar = new long[10000000];
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q214 Case 2
==========
class MyClass{
long[] ar = new long[1000000];
[Link]("End Main");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q215 Case 3
==========
class MyClass{
long[] ar = new long[1000000];
[Link]();
[Link]("End Main");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q216 Case 4 (Null Referencing)
=========================
class MyClass{
long[] ar = new long[1000000];
c1 = null;
[Link]();
[Link]("End Main");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q217 Case 5 (Reassigning)
=====================
class MyClass{
long[] ar = new long[1000000];
[Link]();
[Link]("End Main");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q218 Case 6
==========
class MyClass{
long[] ar = new long[1000000];
new MyClass().myMethod();
[Link]();
[Link]("End Main");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q219 Case 7 Local Scope
====================
class MyClass{
long[] ar = new long[1000000];
[Link]();
}
}
class A{
int a;
public void mA(){
[Link]("mA of A");
}
}
class B extends A{
int b;
public void mB(){
[Link]("mB of B");
}
}
class Example {
public static void main(String[] args) {
A a1 = new B();
a1.a = 100; // Legal
[Link](); // Legal
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q221
class A{
int a;
public void mA(){
[Link]("mA of A");
}
}
class B extends A{
int b;
public void mB(){
[Link]("mB of B");
}
}
class Example {
public static void main(String[] args) {
A a1 = new B();
a1.a = 100; // Legal
[Link](); // Legal
// call MB
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q222
class A{
int a;
public void mA(){
[Link]("mA of A");
}
}
class B extends A{
int b;
public void mB(){
[Link]("mB of B");
}
}
class Example {
public static void main(String[] args) {
A a1 = new B();
a1.a = 100; // Legal
[Link](); // Legal
// call MB
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q223
class A{
int a;
public void mA(){
[Link]("mA of A");
}
}
class B extends A{
int b;
public void mB(){
[Link]("mB of B");
}
}
class Example {
public static void main(String[] args) {
A a1 = new B();
a1.a = 100; // Legal
[Link](); // Legal
// call MB
B b1 = (B)a1; // Legal
b1.b = 200;
[Link]();;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q224 Case 1
==========
class A{
int a;
public void mA(){
[Link]("mA of A");
}
}
class B extends A{
int b;
public void mB(){
[Link]("mB of B");
}
}
class Example {
public static void main(String[] args) {
A a1 = new A();
a1.a = 100; // Legal
[Link](); // 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
[Link](); // Legal
if (a1 instanceof B) {
B b1 = (B) a1;
b1.b = 200;
[Link]();
} else{
[Link]("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 [Link];
import [Link];
import [Link];
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q234 Step 4 Execute Query
=====================
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
while ([Link]()) {
[Link]([Link]("ItemCode") + " " +
[Link]("Description") + " " +
[Link]("PackSize") + " " +
[Link]("UnitPrice") + " " +
[Link]("QtyOnHand"));
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q235 Exercise
============
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
while ([Link]()) {
[Link]([Link](1) + " " + [Link](2) + " " + [Link](3));
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q236 Step 5 (Execute Update)
========================
import [Link];
import [Link];
import [Link];
import [Link];
String sql = "INSERT INTO Item VALUES ('P048', 'Sunlight', '100g', 100.00, 250)";
Statement statement = [Link]();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q237
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]("[Link]");
Connection connection =
[Link]("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 [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]("[Link]");
Connection connection =
[Link]("jdbc:mysql://localhost:3306/Supermarket", "root", "mysql");
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q239 (Prepared Statement - Select)
============================
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]("[Link]");
Connection connection =
[Link]("jdbc:mysql://localhost:3306/Supermarket", "root", "mysql");
while ([Link]()) {
[Link]([Link](1) + " " + [Link](2) + " " + [Link](3) );
}
}
}
Singleton Design Pattern
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q240 Step 1
==========
class A{
public A(){}
A a2 = new A();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q241 Step 2
==========
class A{
private static A a;
public A(){}
return a;
}
A a2 = new A();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q242 Step 3
==========
class A{
private static A a;
private A(){}
return a;
}
A a2 = [Link]();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q243 Step 7
==========
// [Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
while ([Link]()) {
[Link]([Link](1) + " " + [Link](2) + " " + [Link](3) );
}
}
}
// [Link]
import [Link];
import [Link];
import [Link];
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) {
[Link]([Link]);
Color c1 = [Link];
[Link]([Link]());
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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 = [Link]();
for (Color color : colors) {
[Link](color);// [Link]();
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q247 Case 3
==========
enum Color {
RED, GREEN, BLACK, BLUE; // Enum Objects
}
class Example {
public static void main(String[] args) {
Color c1 = [Link];
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 = [Link]("RED");
[Link](c1);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q249 Case 5
==========
enum Color {
RED, GREEN, BLACK, BLUE; // Enum Objects
Color(){
[Link]("Color()");
}
}
class Example {
public static void main(String[] args) {
Color c1 = [Link];
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q250
enum Color {
RED(1001), GREEN(1002), BLACK(1003), BLUE(1004); // Enum Objects
Color(int code){
[Link] = code;
}
class Example {
public static void main(String[] args) {
Color c1 = [Link];
[Link]([Link]());
[Link](2001);
[Link]([Link]());
}
}
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();
[Link](100);
[Link]([Link](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();
[Link](100); // -> [Link](100); Wrapper Class
[Link]([Link](0));
[Link](new Customer());
[Link]([Link](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();
[Link](10);
[Link]("ABC");
[Link](new Customer());
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q255
class Customer{
class Example {
public static void main(String[] args) {
List<Integer> intList = new List();
[Link](100);
[Link](200);
// [Link]("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 [Link];
class Customer {
private int code;
private String name;
class Example {
public static void main(String[] args) {
ArrayList<Customer> customers = new ArrayList<>();
[Link](new Customer(1001, "Customer 1"));
[Link](new Customer(1002, "Customer 2"));
[Link](new Customer(1003, "Customer 3"));
[Link](new Customer(1004, "Customer 4"));
[Link](new Customer(1005, "Customer 5"));
[Link](customers);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q258
import [Link];
class Customer {
private int code;
private String name;
[Link](customers);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q259
import [Link];
class Customer {
private int code;
private String name;
class Example {
public static void main(String[] args) {
ArrayList<Customer> customers = new ArrayList<>();
[Link](new Customer(1001, "Customer 1"));
[Link](new Customer(1002, "Customer 2"));
[Link](new Customer(1003, "Customer 3"));
[Link](new Customer(1004, "Customer 4"));
[Link](new Customer(1005, "Customer 5"));
[Link](e->{
[Link](e);
} );
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q260
import [Link];
class Customer {
private int code;
private String name;
class Example {
public static void main(String[] args) {
ArrayList<String> stringList = new ArrayList<>();
[Link]("A");
[Link]("B");
[Link]("C");
[Link]("D");
[Link]("E");
[Link](stringList);
boolean b = [Link]("B");
[Link](b);
b = [Link]("X");
[Link](b);
[Link](new String("C"));
[Link](stringList);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q261 Exercise
============
import [Link];
class Customer {
private int code;
private String name;
class Example {
public static void main(String[] args) {
ArrayList<Customer> customers = new ArrayList<>();
[Link](new Customer(1001, "Customer 1"));
[Link](new Customer(1002, "Customer 2"));
[Link](new Customer(1003, "Customer 3"));
[Link](new Customer(1004, "Customer 4"));
[Link](new Customer(1005, "Customer 5"));
[Link](customers);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q262 from Q261
=============
import [Link];
class Customer {
private int code;
private String name;
class Example {
public static void main(String[] args) {
ArrayList<Customer> customers = new ArrayList<>();
[Link](new Customer(1001, "Customer 1"));
[Link](new Customer(1002, "Customer 2"));
[Link](new Customer(1003, "Customer 3"));
[Link](new Customer(1004, "Customer 4"));
[Link](new Customer(1005, "Customer 5"));
[Link](customers);
Class LinkedList
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q263
import [Link];
class Customer {
private int code;
private String name;
class Example {
public static void main(String[] args) {
LinkedList<Customer> customers = new LinkedList<>();
[Link](new Customer(1001, "Customer 1"));
[Link](new Customer(1002, "Customer 2"));
[Link](new Customer(1003, "Customer 3"));
[Link](new Customer(1004, "Customer 4"));
[Link](new Customer(1005, "Customer 5"));
[Link](customers);
LinkedList as Stack
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q264
import [Link];
class Customer {
private int code;
private String name;
class Example {
public static void main(String[] args) {
LinkedList<Customer> customers = new LinkedList<>();
[Link](new Customer(1001, "Customer 1"));
[Link](new Customer(1002, "Customer 2"));
[Link](new Customer(1003, "Customer 3"));
[Link](new Customer(1004, "Customer 4"));
[Link](new Customer(1005, "Customer 5"));
[Link](customers);
Customer topCustomer = [Link]();
[Link](topCustomer);
[Link](customers);
topCustomer = [Link]();
[Link](topCustomer);
[Link](customers);
}
}
LinkedList as Queue
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q265
import [Link];
class Customer {
private int code;
private String name;
class Example {
public static void main(String[] args) {
LinkedList<Customer> customers = new LinkedList<>();
[Link](new Customer(1001, "Customer 1"));
[Link](new Customer(1002, "Customer 2"));
[Link](new Customer(1003, "Customer 3"));
[Link](new Customer(1004, "Customer 4"));
[Link](new Customer(1005, "Customer 5"));
[Link](customers);
Customer topCustomer = [Link]();
[Link](topCustomer);
[Link](customers);
topCustomer = [Link]();
[Link](topCustomer);
[Link](customers);
}
}
Exception Handling
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q266 ArrayIndexOutOfBoundsException
================================
class Example {
public static void main(String[] args) {
int [] ar = new int[5];
ar[10] = 20;
[Link]([Link]);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q267 NullPointerException
=====================
class A{
public void mA(){
[Link]("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) {
[Link]("[Link]");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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 < [Link]; i++){
myClassAr[i] = new MyClass();
[Link](i);
}
}
}
class Example {
public static void main(String[] args) {
int [] ar = new int[5];
try{
ar[10] = 20;
} catch (ArrayIndexOutOfBoundsException e){
[Link]("Array does not have index");
}
[Link]([Link]);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q271
class A{
public void mA(){
[Link]("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 {
[Link]("[Link]");
} catch (ClassNotFoundException e) {
[Link]("JDBC Driver not found");
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q273
class A{
public void mA(){
[Link]("mA of A");
}
}
class Example {
private A a;
public static void main(String[] args) {
new Example().callMethod();
[Link]("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();
[Link]([Link]());
Integer num = [Link](e.s1);
} catch (NullPointerException e){
[Link]("S1 is Null");
} catch (NumberFormatException e){
[Link]("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();
[Link]([Link]());
try {
Integer num = [Link](example.s1);
} catch (Exception e) {
[Link]("S1 is not an Integer");
}
} catch (NullPointerException e){
[Link]("S1 is null");
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q280 Exercise
============
class Example {
private String s1;
public static void main(String[] args) {
try{
Example e = new Example();
[Link]([Link]());
Integer num = [Link](e.s1);
[Link](e.s1);
} catch (NullPointerException e){
[Link]("S1 is Null");
} catch (NumberFormatException e){
[Link]("S1 is not integer");
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q281 from Q380
=============
class Example {
private String s1 = "ABC";
public static void main(String[] args) {
try{
Example e = new Example();
[Link]([Link]());
try{
Integer num = [Link](e.s1);
} catch (NumberFormatException ex){
[Link]("S1 is not an integer");
}
[Link](e.s1);
} catch (NullPointerException e){
[Link]("S1 is Null");
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q282
class A {
public void mA() {
[Link]("mA of A");
}
}
class Example {
private A a;
[Link]("Final Statement");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q283 Case 6
===========
class A {
public void mA() {
[Link]("mA of A");
}
}
class Example {
private A a;
[Link]("Final Statement");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q284 Case 7
==========
class Example {
public static void main(String[] args) {
try{
} finally{
[Link]("Finally block");
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q285
class Example {
public static void main(String[] args) {
try{
} catch(Exception e){
} finally{
[Link]("Finally block");
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q286 Case 8
===========
class Example {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q288
class Car {
public void park(){
[Link]("Car Parking");
}
}
class Example {
public static void main(String[] args) {
Thread t1 = new Thread(new MyThread1());
[Link]();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q290
class Car {
public void park(){
[Link]("Car Parking");
}
}
class Example {
public static void main(String[] args) {
Thread t1 = new Thread(new MyThread1());
[Link]();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q291 Thread Lifecycle
=================
class Example {
public static void main(String[] args) {
MyThread myThread = new MyThread();
[Link]([Link]());
[Link]();
[Link]([Link]());
try { [Link](500); } catch (Exception e) {}
[Link]([Link]());
try { [Link](2000); } catch (Exception e) {}
[Link]([Link]());
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q292 Thread Pool
==============
import [Link];
import [Link];
class Example {
public static void main(String[] args) {
ExecutorService executorService = [Link](5);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q293 Thread Join
==============
class Example {
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
MyThread t3 = new MyThread();
[Link]();
try {
[Link]();
} catch (Exception e) {}
[Link]();
try {
[Link]();
} catch (Exception e) {}
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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();
[Link](Thread.MAX_PRIORITY);
[Link](Thread.MIN_PRIORITY);
[Link](Thread.NORM_PRIORITY);
[Link](6);
[Link]();
[Link]();
[Link]();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q295 Daemon Thread
==================
class Example {
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
[Link](true);
[Link]();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q296 Using Lambda Expression
=========================
class Example {
public static void main(String[] args) {
new Thread(() -> {
for(int i = 0; i < 10; i++){
[Link]("Thread : " + i);
};
}).start();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q297 Synchronization
==================
class MyClass{
public void printData(String threadName){
for(int i = 0; i < 10; i++){
try {
[Link](500);
} catch (Exception e) {}
[Link](threadName + " + " + i );
}
}
}
MyThread1(MyClass myClass){
[Link] = myClass;
}
MyThread2(MyClass myClass){
[Link] = myClass;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q298 from Q297
=============
class MyClass{
synchronized public void printData(String threadName){
for(int i = 0; i < 10; i++){
try {
[Link](500);
} catch (Exception e) {}
[Link](threadName + " + " + i );
}
}
}
MyThread1(MyClass myClass){
[Link] = myClass;
}
MyThread2(MyClass myClass){
[Link] = myClass;
}
class MyClass{
synchronized public void myMethod1(MyClass myClass){
[Link]("myMethod 1 - start");
try {
[Link](1000);
} catch (Exception e) {}
myClass.myMethod2();
[Link]("myMethod 1 - end");
}
class Example {
public static void main(String[] args) {
MyClass myClass1 = new MyClass();
MyClass myClass2 = new MyClass();
[Link]();
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q300
class MyClass{
synchronized public void myMethod1(MyClass myClass){
[Link]("myMethod 1 - start");
try {
[Link](1000);
} catch (Exception e) {}
myClass.myMethod2();
[Link]("myMethod 1 - end");
}
class Example {
public static void main(String[] args) {
MyClass myClass1 = new MyClass();
MyClass myClass2 = new MyClass();
[Link]();
try {
[Link]();
} catch (Exception e) {}
[Link]();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
END
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////