0% found this document useful (0 votes)
4 views

GDSE 71 - Object Oriented Programming

Uploaded by

yehara nethsilu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

GDSE 71 - Object Oriented Programming

Uploaded by

yehara nethsilu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 73

Introduction to Object Oriented Programming

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q01

class Student{
String studentId;
String name;
String address;
int sub1;
int sub2;
}

class Example{
public static void main(String[] args) {

Student s1 = new Student();


System.out.println("s1 Id: " + s1.studentId);
System.out.println("s1 Name: " + s1.name);
System.out.println("s1 Address: " + s1.address);
System.out.println("s1 Sub1: " + s1.sub1);
System.out.println("s1 Sub2: " + s1.sub2);

}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q02

class Student{
String studentId;
String name;
String address;
int sub1;
int sub2;
}

class Example{
public static void main(String[] args) {

Student s1; // Create Reference Veriable


s1 = new Student(); // Create Object "student" and assign to s1
s1.studentId = "S001";
s1.name = "Student 1";
s1.address = "Panadura";
s1.sub1 = 50;
s1.sub2 = 75;

System.out.println("s1 Id: " + s1.studentId);


System.out.println("s1 Name: " + s1.name);
System.out.println("s1 Address: " + s1.address);
System.out.println("s1 Sub1: " + s1.sub1);
System.out.println("s1 Sub2: " + s1.sub2);

}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q03

class Student{
String studentId;
String name;
String address;
int sub1;
int sub2;
}

class Example{
public static void main(String[] args) {

Student s1; // Create Reference Variable


s1 = new Student(); // Create Object "student" and assign to s1

s1.studentId = "S001";
s1.name = "Student 1";
s1.address = "Panadura";
s1.sub1 = 50;
s1.sub2 = 75;

System.out.println("s1 Id: " + s1.studentId);


System.out.println("s1 Name: " + s1.name);
System.out.println("s1 Address: " + s1.address);
System.out.println("s1 Sub1: " + s1.sub1);
System.out.println("s1 Sub2: " + s1.sub2);

Student s2 = new Student();

s2.studentId = "S002";
s2.name = "Student 2";
s2.address = "Galle";
s2.sub1 = 34;
s2.sub2 = 56;

System.out.println("s2 Id: " + s2.studentId);


System.out.println("s2 Name: " + s2.name);
System.out.println("s2 Address: " + s2.address);
System.out.println("s2 Sub1: " + s2.sub1);
System.out.println("s2 Sub2: " + s2.sub2);
}
}

State and Behavior of an Object


● Real world objects can be described by two components.
○ State
○ Behavior

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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) {

Student s1; // Create Reference Variable


s1 = new Student(); // Create Object "student" and assign to s1

s1.studentId = "S001";
s1.name = "Student 1";
s1.address = "Panadura";
s1.sub1 = 50;
s1.sub2 = 75;

System.out.println("s1 Id: " + s1.studentId);


System.out.println("s1 Name: " + s1.name);
System.out.println("s1 Address: " + s1.address);
System.out.println("s1 Sub1: " + s1.sub1);
System.out.println("s1 Sub2: " + s1.sub2);
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q05 Behavior as Instance Method
==========================

class Student{
// Start Attribute Declaration
String studentId;
String name;
String address;
int sub1;
int sub2;
// End Attribute Declaration

// Start Method Declaration


public void print(){
System.out.println(studentId + " " + name + " " + address + " " + sub1 + " " + sub2);
}
// End Method Declaration
}

class Example{
public static void main(String[] args) {

Student s1; // Create Reference Variable


s1 = new Student(); // Create Object "student" and assign to s1
s1.studentId = "S001";
s1.name = "Student 1";
s1.address = "Panadura";
s1.sub1 = 50;
s1.sub2 = 75;

// System.out.println("s1 Id: " + s1.studentId);


// System.out.println("s1 Name: " + s1.name);
// System.out.println("s1 Address: " + s1.address);
// System.out.println("s1 Sub1: " + s1.sub1);
// System.out.println("s1 Sub2: " + s1.sub2);

s1.print();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q06 Exercise
===========

class Student{
// Start Attribute Declaration
String studentId;
String name;
String address;
int sub1;
int sub2;
// End Attribute Declaration

// Start Method Declaration


public void print(){
System.out.println(studentId + " " + name + " " + address + " " + sub1 + " " + sub2);
}
// End Method Declaration
}

class Example{
public static void main(String[] args) {

Student s1; // Create Reference Variable


s1 = new Student(); // Create Object "student" and assign to s1

// s1.studentId = "S001";
// s1.name = "Student 1";
// s1.address = "Panadura";
// s1.sub1 = 50;
// s1.sub2 = 75;

s1.setValue("S001", "Student 1", "Panadura",50, 75);

// System.out.println("s1 Id: " + s1.studentId);


// System.out.println("s1 Name: " + s1.name);
// System.out.println("s1 Address: " + s1.address);
// System.out.println("s1 Sub1: " + s1.sub1);
// System.out.println("s1 Sub2: " + s1.sub2);

s1.print();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q07 from Q06
===========

class Student{
// Start Attribute Declaration
String studentId;
String name;
String address;
int sub1;
int sub2;
// End Attribute Declaration

// Start Method Declaration


public void print(){
System.out.println(studentId + " " + name + " " + address + " " + sub1 + " " + sub2);
}

public void setValue(String stuId, String stuName, String stuAddress, int stuSub1, int stuSub2
){
studentId = stuId;
name = stuName;
address = stuAddress;
sub1 = stuSub1;
sub2 = stuSub2;
}
// End Method Declaration
}

class Example{
public static void main(String[] args) {

Student s1; // Create Reference Variable


s1 = new Student(); // Create Object "student" and assign to s1

// s1.studentId = "S001";
// s1.name = "Student 1";
// s1.address = "Panadura";
// s1.sub1 = 50;
// s1.sub2 = 75;

s1.setValue("S001", "Student 1", "Panadura",50, 75);

// System.out.println("s1 Id: " + s1.studentId);


// System.out.println("s1 Name: " + s1.name);
// System.out.println("s1 Address: " + s1.address);
// System.out.println("s1 Sub1: " + s1.sub1);
// System.out.println("s1 Sub2: " + s1.sub2);

s1.print();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q08 Exercise
============

class Student{
// Start Attribute Declaration
String studentId;
String name;
String address;
int sub1;
int sub2;
// End Attribute Declaration

// Start Method Declaration


public void print(){
System.out.println(studentId + " " + name + " " + address + " " + sub1 + " " + sub2);
}
public void setValue(String stuId, String stuName, String stuAddress, int stuSub1, int stuSub2
){
studentId = stuId;
name = stuName;
address = stuAddress;
sub1 = stuSub1;
sub2 = stuSub2;
}
// End Method Declaration
}

class Example{
public static void main(String[] args) {

Student s1 = new Student();

s1.setValue("S001", "Student 1", "Panadura",50, 75);


s1.print();
System.out.println("Total : " + s1.total());
System.out.println("Average : " + s1.average());

Student s2 = new Student();

s2.setValue("S002", "Student 2", "Galle",45, 65);


s2.print();
System.out.println("Total : " + s2.total());
System.out.println("Average : " + s2.average());
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q09 from Q08
===========

class Student{
// Start Attribute Declaration
String studentId;
String name;
String address;
int sub1;
int sub2;
// End Attribute Declaration
// Start Method Declaration
public void print(){
System.out.println(studentId + " " + name + " " + address + " " + sub1 + " " + sub2);
}

public void setValue(String stuId, String stuName, String stuAddress, int stuSub1, int stuSub2
){
studentId = stuId;
name = stuName;
address = stuAddress;
sub1 = stuSub1;
sub2 = stuSub2;
}

public int total(){


int total = sub1+ sub2;
return total;
}

public double average(){


double avg;
//avg = (sub1 + sub1) / 2.0;
//avg = (sub1 + sub1) / (double) 2;
//avg = (double)(sub1 + sub1) / (double) 2;
avg = total() / (double) 2;
return avg;
}
// End Method Declaration
}

class Example{
public static void main(String[] args) {

Student s1 = new Student();

s1.setValue("S001", "Student 1", "Panadura",50, 75);


s1.print();
System.out.println("Total : " + s1.total());
System.out.println("Average : " + s1.average());

Student s2 = new Student();

s2.setValue("S002", "Student 2", "Galle",45, 65);


s2.print();
System.out.println("Total : " + s2.total());
System.out.println("Average : " + s2.average());
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q10 Case 1
==========

class Box{
int length;
int width;
int height;
}
class Example{
public static void main(String[] args) {
Box b1 = new Box();
System.out.println(b1);

b1.height = 5;
b1.width = 6;
b1.length = 10;
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q11 Case 2
=========

class Box{
int length; // Global Variables
int width; // Global Variables
int height; // Global Variables

public void printVolume(){


int volume; // Local Variables
volume = length * width * height;
System.out.println("Volume : " + volume);
}
}
class Example{
public static void main(String[] args) {
Box b1 = new Box();
System.out.println(b1);

b1.height = 5;
b1.width = 6;
b1.length = 10;

b1.printVolume();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q12 Case 3
==========

class Box{
int length; // Global Variables
int width; // Global Variables
int height; // Global Variables

public void setValue(int l, int w, int h){


length = l;
height = h;
width = w;
}

public void printVolume(){


int volume; // Local Variables
volume = length * width * height;
System.out.println("Volume : " + volume);
}
}
class Example{
public static void main(String[] args) {
Box b1 = new Box();
System.out.println(b1);

// b1.height = 5;
// b1.width = 6;
// b1.length = 10;

b1.setValue(10, 6, 5);
b1.printVolume();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q13 Case 4
=========

class Box{
int length; // Global Variables
int width; // Global Variables
int height; // Global Variables

public void setValue(int l, int w, int h){


length = l;
height = h;
width = w;
}

public void printVolume(){


int volume; // Local Variables
volume = length * width * height;
System.out.println("Volume : " + volume);
}

public int getVolume(){


int volume = length * width* height;
return volume;
}
}
class Example{
public static void main(String[] args) {
Box b1 = new Box();
System.out.println(b1);

b1.setValue(10, 6, 5);
int volume = b1.getVolume();
System.out.println("Volume : " + volume);
}
}
Case 5 Keyword ‘this’
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q14

class Box{
int length;
int width;
int height;

public void setValue(int length, int width, int height){


length = length;
height = height;
width = width;
}

public void printVolume(){


int volume;
volume = length * width * height;
System.out.println("Volume : " + volume);
}

public int getVolume(){


int volume = length * width* height;
return volume;
}
}
class Example{
public static void main(String[] args) {
Box b1 = new Box();
System.out.println(b1);

b1.setValue(10, 6, 5);
int volume = b1.getVolume();
System.out.println("Volume : " + volume);
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q15

class Box{
int length;
int width;
int height;

public void setValue(int length, int width, int height){


this.length = length;
this.height = height;
this.width = width;
}

public void printVolume(){


int volume;
volume = length * width * height;
System.out.println("Volume : " + volume);
}

public int getVolume(){


int volume = length * width* height;
return volume;
}
}
class Example{
public static void main(String[] args) {
Box b1 = new Box();
System.out.println(b1);

b1.setValue(10, 6, 5);
int volume = b1.getVolume();
System.out.println("Volume : " + volume);
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q16
class A{
public void print(){
System.out.println(this);
}
}
class Example{
public static void main(String[] args) {
A a1 = new A();
System.out.println(a1);
a1.print();
}
}
Case 6 Constructors
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q17

class Box{
int length;
int width;
int height;

// ---- Compiler Inserts


/*Box(){
length = 0;
width = 0;
height = 0;
}*/

public void setValue(int length, int width, int height){


this.length = length;
this.height = height;
this.width = width;
}

public void printVolume(){


int volume;
volume = length * width * height;
System.out.println("Volume : " + volume);
}

public int getVolume(){


int volume = length * width* height;
return volume;
}
}
class Example{
public static void main(String[] args) {
Box b1 = new Box();
System.out.println(b1);

System.out.println("Length : " + b1.length);


System.out.println("Height : " + b1.height);
System.out.println("Width : " + b1.width);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q18 Default Constructor
===================

class Box{
int length;
int width;
int height;

// ---- Compiler Inserts | Default Constructor


Box(){ // No Return type | Must be class name
length = 10;
width = 5;
height = 6;
}

public void setValue(int length, int width, int height){


this.length = length;
this.height = height;
this.width = width;
}

public void printVolume(){


int volume;
volume = length * width * height;
System.out.println("Volume : " + volume);
}

public int getVolume(){


int volume = length * width* height;
return volume;
}
}
class Example{
public static void main(String[] args) {
Box b1 = new Box();
System.out.println(b1);

System.out.println("Length : " + b1.length);


System.out.println("Height : " + b1.height);
System.out.println("Width : " + b1.width);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q19 Parameterized Constructor
=========================

class Box{
int length;
int width;
int height;

Box(int length, int width, int height){


this.length = length;
this.width = width;
this.height = height;
}

public void setValue(int length, int width, int height){


this.length = length;
this.height = height;
this.width = width;
}

public void printVolume(){


int volume;
volume = length * width * height;
System.out.println("Volume : " + volume);
}

public int getVolume(){


int volume = length * width* height;
return volume;
}
}
class Example{
public static void main(String[] args) {
Box b1 = new Box(10,6,5);
System.out.println(b1);

System.out.println("Length : " + b1.length);


System.out.println("Height : " + b1.height);
System.out.println("Width : " + b1.width);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q20

class Box{
int length;
int width;
int height;

Box(int length, int width, int height){


this.length = length;
this.width = width;
this.height = height;
}

public void setValue(int length, int width, int height){


this.length = length;
this.height = height;
this.width = width;
}

public void printVolume(){


int volume;
volume = length * width * height;
System.out.println("Volume : " + volume);
}

public int getVolume(){


int volume = length * width* height;
return volume;
}
}
class Example{
public static void main(String[] args) {
Box b1 = new Box(10,6,5);
System.out.println(b1);
b1.printVolume();

Box b2 = new Box(); // Illegal

}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q21 Constructor Overloading
=======================

class Box{
int length;
int width;
int height;

Box(){
this.length = 5;
this.width =5;
this.height = 5;
}

Box(int length){
this.width = length;
this.height = length;
this.length = length;
}

Box(int length, int width, int height){


this.length = length;
this.width = width;
this.height = height;
}

public void setValue(int length, int width, int height){


this.length = length;
this.height = height;
this.width = width;
}

public void printVolume(){


int volume;
volume = length * width * height;
System.out.println("Volume : " + volume);
}

public int getVolume(){


int volume = length * width* height;
return volume;
}
}
class Example{
public static void main(String[] args) {
Box b1 = new Box(10,6,5);
System.out.println(b1);
b1.printVolume();

Box b2 = new Box(); //


b2.printVolume();

Box b3 = new Box(10);


b3.printVolume();

}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q21

class Box{
int length;
int width;
int height;

Box(){
this.length = 5;
this.width =5;
this.height = 5;
}

Box(int length){
this.width = length;
this.height = length;
this.length = length;
}

Box(int length, int width, int height){


this.length = length;
this.width = width;
this.height = height;
}

public void setValue(int length, int width, int height){


this.length = length;
this.height = height;
this.width = width;
}

public void printVolume(){


int volume;
volume = length * width * height;
System.out.println("Volume : " + volume);
}

public int getVolume(){


int volume = length * width* height;
return volume;
}
}
class Example{
public static void main(String[] args) {
Box b1 = new Box();
b1.printVolume();

b1.Box(10,20,30); // Illegal
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q22

class Box{
int length;
int width;
int height;

Box(){
this.length = 5;
this.width =5;
this.height = 5;
}

Box(int length){
this.width = length;
this.height = length;
this.length = length;
}

void Box(int length, int width, int height){ // Method


this.length = length;
this.width = width;
this.height = height;
}

public void setValue(int length, int width, int height){


this.length = length;
this.height = height;
this.width = width;
}

public void printVolume(){


int volume;
volume = length * width * height;
System.out.println("Volume : " + volume);
}

public int getVolume(){


int volume = length * width* height;
return volume;
}
}
class Example{
public static void main(String[] args) {
Box b1 = new Box();
b1.printVolume();

b1.Box(10,20,30);
b1.printVolume();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q23 Attribute Declaration Values
=========================

class Box{
int length = 10;
int width = 6;
int height = 5;

public void printVolume(){


int volume;
volume = length * width * height;
System.out.println("Volume : " + volume);
}
}
class Example{
public static void main(String[] args) {
Box b1 = new Box();
b1.printVolume();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q24

class MyClass{
int a = 100;

MyClass(){
a = 200;
}

MyClass(int a){
this.a = a;
}
}
class Example{
public static void main(String[] args) {
MyClass m1 = new MyClass();
System.out.println(m1.a);

MyClass m2 = new MyClass(300);


System.out.println(m2.a);
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q25

class Box{
int length = 10;
int width = 6;
int height;
height = 5; // Illegal

public void printVolume(){


int volume;
volume = length * width * height;
System.out.println("Volume : " + volume);
}
}
class Example{
public static void main(String[] args) {
Box b1 = new Box();
b1.printVolume();
}
}

Case 7 Passing Object to Method


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q26

class Account{
double balance;

Account(double balance){
this.balance = balance;
}

public void printBalance(){


System.out.println("Balance : " + balance);
}
}

class Operation{
public void deposit(double amount, Account account){
account.balance+= amount;
}

public void withdraw(double amount, Account account){


account.balance -= amount;
}
}
class Example{
public static void main(String[] args) {
Account a1 = new Account(10000);
a1.printBalance();

Operation operation = new Operation();


operation.withdraw(3000, a1);
a1.printBalance();

operation.deposit(5000, a1);
a1.printBalance();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q27

class Account{
double balance;

Account(double balance){
this.balance = balance;
}

public void printBalance(){


System.out.println("Balance : " + balance);
}
}

class Operation{
public void deposit(double amount, Account account){
account.balance+= amount;
}

public void withdraw(double amount, Account account){


account.balance -= amount;
}
}

class Example{
public static void main(String[] args) {
Account a1 = new Account(10000);
a1.printBalance();
Operation operation = new Operation();
operation.withdraw(3000, a1);
a1.printBalance();

operation.deposit(5000, a1);
a1.printBalance();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q28 Exercise
===========

class MyClass{
int x = 100;
}

class Example{
public static void increment(int x){
x++;
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
System.out.println(myClass.x);
increment(myClass.x);
System.out.println(myClass.x);
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q29 Exercise
==========

class MyClass{
int x = 100;
}

class Example{
public static void increment(MyClass myClass){
myClass.x++;
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
System.out.println(myClass.x);
increment(myClass);
System.out.println(myClass.x);
}
}

Case 8 Returning an Object


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q30 Exercise
==========

class Box{
int length;
int width;
int height;

Box(int length, int width, int height){


this.length = length;
this.height = height;
this.width = width;
}

public void printVolume(){


int volume;
volume = length * width * height;
System.out.println("Volume : " + volume);
}
}

class Example{
public static void main(String[] args) {
Box b1 = new Box(10, 6, 5);
b1.printVolume(); //Volume : 300

Box b2 = b1.getCopy();
b2.printVolume(); //Volume : 300

System.out.println(b1 == b2); // false


}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q31 from Q30
===========

class Box{
int length;
int width;
int height;

Box(int length, int width, int height){


this.length = length;
this.height = height;
this.width = width;
}

public void printVolume(){


int volume;
volume = length * width * height;
System.out.println("Volume : " + volume);
}

public Box getCopy(){


Box temp = new Box(length, width, height);
return temp;
}
}

class Example{
public static void main(String[] args) {
Box b1 = new Box(10, 6, 5);
b1.printVolume(); //Volume : 300

Box b2 = b1.getCopy();
b2.printVolume(); //Volume : 300

System.out.println(b1 == b2); // false


}
}

Case 9 Static attributes and methods


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q32
class MyClass{
int x;
static int y;
}

class Example{
public static void main(String[] args) {
MyClass m1 = new MyClass();
m1.x = 100;
m1.y = 10;

MyClass m2 = new MyClass();


m2.x = 200;
m2.y = 20;

MyClass m3 = new MyClass();


m3.x = 300;
m3.y = 30;

System.out.println(m1.x + " " + m1.y); // 100 30


System.out.println(m2.x + " " + m2.y); // 200 30
System.out.println(m3.x + " " + m3.y); // 300 30
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q33

class MyClass{
int x;
static int y;
}

class Example{
public static void main(String[] args) {
MyClass.y = 30;
//MyClass.x = 10; // Illegal

MyClass m1 = new MyClass();


m1.y = 30;
m1.x = 10;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q34

class MyClass{
public void instanceMethod(){
System.out.println("instanceMethod");
}

public static void staticMethod(){


System.out.println("staticMethod");
}
}

class Example{
public static void main(String[] args) {
MyClass.staticMethod();
// MyClass.instanceMethod(); // Illegal

MyClass m1 = new MyClass();


m1.staticMethod();
m1.instanceMethod();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q35 Exercise 1
============

class Example{
public static void main(String[] args) {
Date d1 = new Date();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q36 from Q35
===========

class Date{
int year;
int month;
int day;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q37 Exercise
===========

class Date{
int year;
int month;
int day;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q38 from Q37 Option 1
==================

class Date{
int year = 1980;
int month = 11;
int day = 15;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q39 from Q37 Option 2
===================

class Date{
int year;
int month;
int day;

Date(){
this.year = 1980;
this.month = 11;
this.day = 15;
}
}
class Example{
public static void main(String[] args) {
Date d1 = new Date();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q40 from Q37 Option 3
==================

class Date{
int year;
int month;
int day;

{ // Instance Block
this.year = 1980;
this.month = 11;
this.day = 15;
}
}
class Example{
public static void main(String[] args) {
Date d1 = new Date();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q41 Exercise 3
============

class Date{
int year;
int month;
int day;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q42 from Q41
===========

class Date{
int year;
int month;
int day;

Date(int year, int month, int day){


this.year = year;
this.month = month;
this.day = day;
}
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q43 Exercise 4
============

class Date{
int year;
int month;
int day;

Date(int year, int month, int day){


this.year = year;
this.month = month;
this.day = day;
}
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q44 from Q43
===========

class Date{
int year;
int month;
int day;

Date(int year, int month, int day){


this.year = year;
this.month = month;
this.day = day;
}

public void printDate(){


System.out.println(this.year + "-" + this.month + "-" + this.day);
}
}

class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q45 Exercise 5
===========

class Date{
int year;
int month;
int day;

Date(int year, int month, int day){


this.year = year;
this.month = month;
this.day = day;
}

public void printDate(){


System.out.println(this.year + "-" + this.month + "-" + this.day);
}
}

class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15

Date d2 = new Date();


d2.printDate(); // 2000-12-17
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q46 from Q45
===========

class Date{
int year;
int month;
int day;

Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}
Date(int year, int month, int day){
this.year = year;
this.month = month;
this.day = day;
}

public void printDate(){


System.out.println(this.year + "-" + this.month + "-" + this.day);
}
}

class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15

Date d2 = new Date();


d2.printDate(); // 2000-12-17
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q47 Exercise 6
============

class Date{
int year;
int month;
int day;

Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}

Date(int year, int month, int day){


this.year = year;
this.month = month;
this.day = day;
}
public void printDate(){
System.out.println(this.year + "-" + this.month + "-" + this.day);
}
}

class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15

Date d2 = new Date();


d2.printDate(); // 2000-12-17

d2.setDate(1991,12,14);
d2.printDate(); // 1991-12-14
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q48 from Q47
============

class Date{
int year;
int month;
int day;

Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}

Date(int year, int month, int day){


this.year = year;
this.month = month;
this.day = day;
}

public void printDate(){


System.out.println(this.year + "-" + this.month + "-" + this.day);
}
public void setDate(int year, int month, int day){
this.year = year;
this.month = month;
this.day = day;
}
}

class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15

Date d2 = new Date();


d2.printDate(); // 2000-12-17

d2.setDate(1991,12,14);
d2.printDate(); // 1991-12-14
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q49 Exercise 7
============

class Date{
int year;
int month;
int day;

Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}

Date(int year, int month, int day){


this.year = year;
this.month = month;
this.day = day;
}

public void printDate(){


System.out.println(this.year + "-" + this.month + "-" + this.day);
}
public void setDate(int year, int month, int day){
this.year = year;
this.month = month;
this.day = day;
}
}

class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15

Date d2 = new Date();


d2.printDate(); // 2000-12-17

d2.setDate(1991,12,14);
d2.printDate(); // 1991-12-14

d2.setDate(d1);
d2.printDate(); // 1980-11-15
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q50 from Q49
===========

class Date{
int year;
int month;
int day;

Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}

Date(int year, int month, int day){


this.year = year;
this.month = month;
this.day = day;
}
public void printDate(){
System.out.println(this.year + "-" + this.month + "-" + this.day);
}

public void setDate(int year, int month, int day){


this.year = year;
this.month = month;
this.day = day;
}

public void setDate(Date date){


this.year = date.year;
this.month = date.month;
this.day = date.day;
}
}

class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15

Date d2 = new Date();


d2.printDate(); // 2000-12-17

d2.setDate(1991,12,14);
d2.printDate(); // 1991-12-14

d2.setDate(d1);
d2.printDate(); // 1980-11-15
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q51 Exercise 8
============

class Date{
int year;
int month;
int day;
Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}

Date(int year, int month, int day){


this.year = year;
this.month = month;
this.day = day;
}

public void printDate(){


System.out.println(this.year + "-" + this.month + "-" + this.day);
}

public void setDate(int year, int month, int day){


this.year = year;
this.month = month;
this.day = day;
}

public void setDate(Date date){


this.year = date.year;
this.month = date.month;
this.day = date.day;
}
}

class Example{
public static void main(String[] args) {
Date d1 = Date.getInstance();
d1.printDate(); // 2000-12-17

}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q52 from Q51
============

class Date{
int year;
int month;
int day;

Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}

Date(int year, int month, int day){


this.year = year;
this.month = month;
this.day = day;
}

public void printDate(){


System.out.println(this.year + "-" + this.month + "-" + this.day);
}

public void setDate(int year, int month, int day){


this.year = year;
this.month = month;
this.day = day;
}

public void setDate(Date date){


this.year = date.year;
this.month = date.month;
this.day = date.day;
}

public static Date getInstance(){


Date d1 = new Date();
return d1;
}
}

class Example{
public static void main(String[] args) {
Date d1 = Date.getInstance();
d1.printDate(); // 2000-12-17

}
}
Access Privileges | Access Levels

Access Modifier In class In the package In the sub class In other


packages

public Access Allowed Access Allowed Access Allowed Access Allowed

protected Access Allowed Access Allowed Access Allowed Access Denied

default Access Allowed Access Allowed Access Denied Access Denied

private Access Allowed Access Denied Access Denied Access Denied

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q53
////////////////////////////////////////////////////

package package1;

import package2.Car;

class Example{
public static void main(String[] args) {
Car c1 = new Car();
c1.printCar();
System.out.println(c1.name);
//System.out.println(c1.model); // Illegal
//System.out.println(c1.isStart);// Illegal
//System.out.println(c1.speed);// Illegal
}
}

////////////////////////////////////////////////////

package package2;

public class Car {


public String name = "Nissan";
protected String model = "FB15";
boolean isStart = true;
private int speed = 60;

public void printCar(){


System.out.println(this.name + " " + this.model+ " " + this.isStart + " " + this.speed);
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q54

////////////////////////////////////////////////////
package package1;

class Example{
public static void main(String[] args) {
Car c1 = new Car();
c1.printCar();
System.out.println(c1.name);
System.out.println(c1.model);
System.out.println(c1.isStart);
//System.out.println(c1.speed);// Illegal
}
}
////////////////////////////////////////////////////
package package1;

public class Car {


public String name = "Nissan";
protected String model = "FB15";
boolean isStart = true;
private int speed = 60;

public void printCar(){


System.out.println(this.name + " " + this.model+ " " + this.isStart + " " + this.speed);
}
}

Encapsulation
● Wrapping attributes and methods together into a single unit.
● Control over the data by hiding fields and adding methods with public access to access
fields.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q55 Step 1
========
class Car{
boolean isStart;
int speed;
}

class Example{
public static void main(String[] args) {
Car c1 = new Car();
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q56 Step 2
=========

class Car{
boolean isStart;
int speed;
}

class Example{
public static void main(String[] args) {
Car c1 = new Car();
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);

c1.speed = 100;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q57 Step 3
=========

class Car{
boolean isStart;
int speed;
}

class Example{
public static void main(String[] args) {
Car c1 = new Car();
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);

c1.isStart = true;
c1.speed = 100;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);

c1.isStart = false;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q58 Step 4
=========

class Car{
private boolean isStart;
private int speed;
}

class Example{
public static void main(String[] args) {
Car c1 = new Car();
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);

c1.isStart = true;
c1.speed = 100;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);

c1.isStart = false;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q59 Step 5
=========

class Car{
private boolean isStart;
private int speed;

public void setIsStart(boolean isStart){


if(!isStart){
this.speed = 0;
}
this.isStart = isStart;
}

public void incrementSpeed(){


if(this.isStart){
this.speed+= 10;
}
}

public boolean getIsStart(){


return this.isStart;
}

public int getSpeed(){


return this.speed;
}
}

class Example{
public static void main(String[] args) {
Car c1 = new Car();
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);

c1.isStart = true;
c1.speed = 100;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);

c1.isStart = false;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q60 Step 6
=========

class Car{
private boolean isStart;
private int speed;

public void setIsStart(boolean isStart){


if(!isStart){
this.speed = 0;
}
this.isStart = isStart;
}

public void incrementSpeed(){


if(this.isStart){
this.speed+= 10;
}
}

public boolean getIsStart(){


return this.isStart;
}

public int getSpeed(){


return this.speed;
}
}

class Example{
public static void main(String[] args) {
Car c1 = new Car();
System.out.println("Is Start : " + c1.getIsStart());
System.out.println("Speed : " + c1.getSpeed());

c1.setIsStart(true);
c1.incrementSpeed();
System.out.println("Is Start : " + c1.getIsStart());
System.out.println("Speed : " + c1.getSpeed());

c1.setIsStart(false);
System.out.println("Is Start : " + c1.getIsStart());
System.out.println("Speed : " + c1.getSpeed());

}
}

Java Swing Application


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q61

import javax.swing.*;

class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setSize(500, 400);
f1.setTitle("Test Title");
f1.setLocationRelativeTo(null);

f1.setVisible(true);
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q62

import javax.swing.*;

class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setSize(500, 400);
f1.setTitle("Test Title");
f1.setLocationRelativeTo(null);
f1.setVisible(true);

JFrame f2 = new JFrame();


f2.setSize(600, 300);
f2.setTitle("Frame 2");
f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f2.setVisible(true);
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q63

import javax.swing.*;

class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
f1.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f1.setSize(500, 400);
f1.setTitle("Test Title");
f1.setLocationRelativeTo(null);

f1.setVisible(true);
}
}

// JFrame.EXIT_ON_CLOSE = 3
// JFrame.DISPOSE_ON_CLOSE = 2
// JFrame.HIDE_ON_CLOSE = 1
// JFrame.DO_NOTHING_ON_CLOSE = 0

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q64

import javax.swing.*;

class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f1.setSize(500, 400);
f1.setTitle("Frame 1");
f1.setLocationRelativeTo(null);

f1.setVisible(true);

JFrame f2 = new JFrame();


f2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f2.setSize(700, 500);
f2.setTitle("Frame 2");
f2.setLocationRelativeTo(null);

f2.setVisible(true);
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q65

import javax.swing.*;
import java.awt.*;

class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
f1.setSize(500,500);
f1.setTitle("Frame 1");
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setLocationRelativeTo(null);

Font font = new Font("", 1, 25);

JButton button = new JButton();


button.setFont(font);
button.setText("Button");

f1.add(button);

f1.setVisible(true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q66

import javax.swing.*;

class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
c1.setSize(500,400);
c1.setTitle("Calculator");
c1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c1.setLocationRelativeTo(null);

c1.setVisible(true);
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q67

import javax.swing.*;

class Calculator extends JFrame{

class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
c1.setSize(500,400);
c1.setTitle("Calculator");
c1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c1.setLocationRelativeTo(null);

c1.setVisible(true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q68

import javax.swing.*;

class Calculator extends JFrame{

class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
c1.setSize(500,400);
c1.setTitle("Calculator");
c1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c1.setLocationRelativeTo(null);

c1.setVisible(true);
}
}

// JFrame -> Super Class


// Calculator -> Sub Class
// Calculator IS-A JFrame

// Inheritance -> Code reusability

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q69

import javax.swing.*;

class Calculator extends JFrame{

Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);

setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
Calculator c2 = new Calculator();
new Calculator();
new Calculator();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q70

import javax.swing.*;
import java.awt.*;

class Calculator extends JFrame{

JButton button;

Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);

button = new JButton();


button.setText("Button");
button.setFont(new Font("", 1, 25));
add(button);

setVisible(true);
}
}

class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q71

import javax.swing.*;
import java.awt.*;

class Calculator extends JFrame{

JButton button;
JLabel label;

Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);

button = new JButton();


button.setText("Button");
button.setFont(new Font("", 1, 25));
add(button);

label = new JLabel();


label.setText("This is a Label");
label.setFont(new Font("", 1, 25));
add(label);

setVisible(true);
}
}

class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
Swing Layouts

Border Layout
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q72

import javax.swing.*;
import java.awt.*;

class Calculator extends JFrame{

JButton button;
JLabel label;

Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new BorderLayout());

button = new JButton();


button.setText("Button");
button.setFont(new Font("", 1, 25));
add("North", button);

label = new JLabel();


label.setText("This is a Label");
label.setFont(new Font("", 1, 25));
add("South", label);

setVisible(true);
}
}

class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q73

import javax.swing.*;
import java.awt.*;

class Calculator extends JFrame{

JButton northButton, eastButton, southButton, westButton, centerButton;

Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new BorderLayout());

Font font = new Font("", 1, 25);

northButton = new JButton();


northButton.setText("North");
northButton.setFont(font);
add("North", northButton);

southButton = new JButton();


southButton.setText("South");
southButton.setFont(font);
add("South", southButton);

eastButton = new JButton();


eastButton.setText("East");
eastButton.setFont(font);
add("East", eastButton);

westButton = new JButton();


westButton.setText("West");
westButton.setFont(font);
add("West", westButton);

centerButton = new JButton();


centerButton.setText("Center");
centerButton.setFont(font);
add("Center", centerButton);
setVisible(true);
}
}

class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}

Flow Layout
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q74

import javax.swing.*;
import java.awt.*;

class Calculator extends JFrame{

JButton northButton, eastButton, southButton, westButton, centerButton;

Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new FlowLayout());

Font font = new Font("", 1, 25);

northButton = new JButton();


northButton.setText("North");
northButton.setFont(font);
add( northButton);

southButton = new JButton();


southButton.setText("South");
southButton.setFont(font);
add( southButton);

eastButton = new JButton();


eastButton.setText("East");
eastButton.setFont(font);
add(eastButton);

westButton = new JButton();


westButton.setText("West");
westButton.setFont(font);
add( westButton);

centerButton = new JButton();


centerButton.setText("Center");
centerButton.setFont(font);
add( centerButton);

setVisible(true);
}
}

class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q75 Align Right
=============

import javax.swing.*;
import java.awt.*;

class Calculator extends JFrame{

JButton northButton, eastButton, southButton, westButton, centerButton;

Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new FlowLayout(FlowLayout.RIGHT));

Font font = new Font("", 1, 25);

northButton = new JButton();


northButton.setText("North");
northButton.setFont(font);
add( northButton);

southButton = new JButton();


southButton.setText("South");
southButton.setFont(font);
add( southButton);

eastButton = new JButton();


eastButton.setText("East");
eastButton.setFont(font);
add(eastButton);

westButton = new JButton();


westButton.setText("West");
westButton.setFont(font);
add( westButton);

centerButton = new JButton();


centerButton.setText("Center");
centerButton.setFont(font);
add( centerButton);

setVisible(true);
}
}

class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q76 Align Right

import javax.swing.*;
import java.awt.*;

class Calculator extends JFrame{

JButton northButton, eastButton, southButton, westButton, centerButton;


Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new FlowLayout(FlowLayout.LEFT));

Font font = new Font("", 1, 25);

northButton = new JButton();


northButton.setText("North");
northButton.setFont(font);
add( northButton);

southButton = new JButton();


southButton.setText("South");
southButton.setFont(font);
add( southButton);

eastButton = new JButton();


eastButton.setText("East");
eastButton.setFont(font);
add(eastButton);

westButton = new JButton();


westButton.setText("West");
westButton.setFont(font);
add( westButton);

centerButton = new JButton();


centerButton.setText("Center");
centerButton.setFont(font);
add( centerButton);

setVisible(true);
}
}

class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
Grid Layout
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q77

import javax.swing.*;
import java.awt.*;

class Calculator extends JFrame{

JButton button1, button2, button3, button4;

Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new GridLayout(2, 2));

Font font = new Font("", 1, 25);

button1 = new JButton();


button1.setText("1");
button1.setFont(font);
add(button1);

button2 = new JButton();


button2.setText("2");
button2.setFont(font);
add(button2);

button3 = new JButton();


button3.setText("3");
button3.setFont(font);
add(button3);

button4 = new JButton();


button4.setText("4");
button4.setFont(font);
add(button4);

setVisible(true);
}
}

class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q78 Exercise
===========

import javax.swing.*;
import java.awt.*;

class Calculator extends JFrame{

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 GridLayout(4,4));

Font font = new Font("", 1, 25);

for(int i = 0; i < 16; i++){


buttons[i] = new JButton();
buttons[i].setText(arr[i]);
buttons[i].setFont(font);
add(buttons[i]);
}
setVisible(true);
}
}

class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q79 Exercise
==========

import javax.swing.*;
import java.awt.*;

class Calculator extends JFrame{


JButton headerButton, footerButton, rSideNavButton, lSideNavButton,
button1, button2, button3, button4;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);

Font font = new Font("", 1, 25);

headerButton = new JButton("Header");


headerButton.setFont(font);
add("North", headerButton);

footerButton = new JButton("Footer");


footerButton.setFont(font);
add("South", footerButton);

rSideNavButton = new JButton("Side Nav");


rSideNavButton.setFont(font);
add("East", rSideNavButton);

lSideNavButton = new JButton("Side Nav");


lSideNavButton.setFont(font);
add("West", lSideNavButton);

JPanel centerPanel = new JPanel();


centerPanel.setLayout(new GridLayout(2, 2));

button1 = new JButton("1");


button1.setFont(font);
centerPanel.add(button1);

button2 = new JButton("2");


button2.setFont(font);
centerPanel.add(button2);
button3 = new JButton("3");
button3.setFont(font);
centerPanel.add(button3);

button4 = new JButton("4");


button4.setFont(font);
centerPanel.add(button4);

add("Center", centerPanel);

setVisible(true);
}
}

class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q80 Exercise
===========

import javax.swing.*;
import java.awt.*;

class Calculator extends JFrame{

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());

Font font = new Font("", 1, 25);


textField = new JTextField();
textField.setFont(font);
add("North", textField);

JPanel buttonPanel = new JPanel();


buttonPanel.setLayout(new GridLayout(4, 4));

for(int i = 0; i < 16; i++){


buttons[i] = new JButton();
buttons[i].setText(arr[i]);
buttons[i].setFont(font);
buttonPanel.add(buttons[i]);
}

add("Center",buttonPanel);

setVisible(true);
}
}

class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}

JMenuBar, JMenu, JMenuItem


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q81 Step1
========

import javax.swing.*;
import java.awt.*;

class Notepad extends JFrame{


Notepad(){
setSize(600,400);
setLocationRelativeTo(null);
setTitle("untitled");
setDefaultCloseOperation(EXIT_ON_CLOSE);

setVisible(true);
}
}

class Example{
public static void main(String[] args) {
new Notepad();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q82 Step 2
=========

import javax.swing.*;
import java.awt.*;

class Notepad extends JFrame{


private JTextArea textArea;

Notepad(){
setSize(600,400);
setLocationRelativeTo(null);
setTitle("untitled");
setDefaultCloseOperation(EXIT_ON_CLOSE);

textArea = new JTextArea();


textArea.setFont(new Font("", 1, 25));
add("Center", textArea);

setVisible(true);
}
}

class Example{
public static void main(String[] args) {
new Notepad();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q83 Step 3
=========
import javax.swing.*;
import java.awt.*;

class Notepad extends JFrame{


private JTextArea textArea;

Notepad(){
setSize(600,400);
setLocationRelativeTo(null);
setTitle("untitled");
setDefaultCloseOperation(EXIT_ON_CLOSE);

textArea = new JTextArea();


textArea.setFont(new Font("", 1, 25));

JScrollPane scrollPane = new JScrollPane(textArea);

add("Center", scrollPane);

setVisible(true);
}
}

class Example{
public static void main(String[] args) {
new Notepad();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q84 Step 4
=========

import javax.swing.*;
import java.awt.*;

class Notepad extends JFrame{


private JTextArea textArea;

private JMenuBar menuBar;


private JMenu fileMenu, editMenu, viewMenu;
private JMenuItem item1, item2, item3, item4, item5, item6;

Notepad(){
setSize(600,400);
setLocationRelativeTo(null);
setTitle("untitled");
setDefaultCloseOperation(EXIT_ON_CLOSE);

textArea = new JTextArea();


textArea.setFont(new Font("", 1, 25));

JScrollPane scrollPane = new JScrollPane(textArea);

add("Center", scrollPane);

menuBar = new JMenuBar();

fileMenu = new JMenu("File");


item1 = new JMenuItem("New");
item2 = new JMenuItem("Open");
fileMenu.add(item1);
fileMenu.add(item2);

editMenu = new JMenu("Edit");


item3 = new JMenuItem("Undo");
item4 = new JMenuItem("Redo");
editMenu.add(item3);
editMenu.add(item4);

menuBar.add(fileMenu);
menuBar.add(editMenu);

setJMenuBar(menuBar);

setVisible(true);
}
}

class Example{
public static void main(String[] args) {
new Notepad();
}
}
Working with JTable
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q85

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.*;

class ViewStudent extends JFrame{

private JTable table;


private DefaultTableModel defaultTableModel;

ViewStudent(){
setSize(600,400);
setLocationRelativeTo(null);
setTitle("View Student");
setDefaultCloseOperation(EXIT_ON_CLOSE);

String columns[] = {"Id", "Name", "Address", "Mobile"};


defaultTableModel = new DefaultTableModel(columns, 0);

String[] rowData = {"S001", "Student 1", "Panadura", "0778987765"};


defaultTableModel.addRow(rowData);

table = new JTable(defaultTableModel);

JScrollPane scrollPane = new JScrollPane(table);


add(scrollPane);

setVisible(true);
}
}

class Example{
public static void main(String[] args) {
new ViewStudent();
}
}
Event Handling
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q86 Action Event vs. Action Listener
============================

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class Calculator extends JFrame{

JButton button;

Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new FlowLayout());

button = new JButton();


button.setText("Button");
button.setFont(new Font("", 1, 25));

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("You pressed the button...!");
}
});

add(button);

setVisible(true);
}
}

class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q87

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class Calculator extends JFrame{

JTextField textField;
JSlider slider;

Calculator(){
setSize(350,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new FlowLayout());

textField = new JTextField(20);


textField.setFont(new Font("", 1, 18));
add(textField);

slider = new JSlider(JSlider.VERTICAL, 0, 100, 50);


slider.setMajorTickSpacing(10);
slider.setPaintLabels(true);

slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int value = slider.getValue();
textField.setText(value+"");// Integer.toString(value)
}
});

add(slider);
setVisible(true);
}
}

class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q88

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q89

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q90

You might also like