GDSE 71 - Object Oriented Programming
GDSE 71 - Object Oriented Programming
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q01
class Student{
String studentId;
String name;
String address;
int sub1;
int sub2;
}
class Example{
public static void main(String[] args) {
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q02
class Student{
String studentId;
String name;
String address;
int sub1;
int sub2;
}
class Example{
public static void main(String[] args) {
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q03
class Student{
String studentId;
String name;
String address;
int sub1;
int sub2;
}
class Example{
public static void main(String[] args) {
s1.studentId = "S001";
s1.name = "Student 1";
s1.address = "Panadura";
s1.sub1 = 50;
s1.sub2 = 75;
s2.studentId = "S002";
s2.name = "Student 2";
s2.address = "Galle";
s2.sub1 = 34;
s2.sub2 = 56;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q04 State as Attributes
==================
class Student{
// State as Attributes
// Start Attribute Declaration
String studentId;
String name;
String address;
int sub1;
int sub2;
// End Attribute Declaration
}
class Example{
public static void main(String[] args) {
s1.studentId = "S001";
s1.name = "Student 1";
s1.address = "Panadura";
s1.sub1 = 50;
s1.sub2 = 75;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q05 Behavior as Instance Method
==========================
class Student{
// Start Attribute Declaration
String studentId;
String name;
String address;
int sub1;
int sub2;
// End Attribute Declaration
class Example{
public static void main(String[] args) {
s1.print();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q06 Exercise
===========
class Student{
// Start Attribute Declaration
String studentId;
String name;
String address;
int sub1;
int sub2;
// End Attribute Declaration
class Example{
public static void main(String[] args) {
// s1.studentId = "S001";
// s1.name = "Student 1";
// s1.address = "Panadura";
// s1.sub1 = 50;
// s1.sub2 = 75;
s1.print();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q07 from Q06
===========
class Student{
// Start Attribute Declaration
String studentId;
String name;
String address;
int sub1;
int sub2;
// End Attribute Declaration
public void setValue(String stuId, String stuName, String stuAddress, int stuSub1, int stuSub2
){
studentId = stuId;
name = stuName;
address = stuAddress;
sub1 = stuSub1;
sub2 = stuSub2;
}
// End Method Declaration
}
class Example{
public static void main(String[] args) {
// s1.studentId = "S001";
// s1.name = "Student 1";
// s1.address = "Panadura";
// s1.sub1 = 50;
// s1.sub2 = 75;
s1.print();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q08 Exercise
============
class Student{
// Start Attribute Declaration
String studentId;
String name;
String address;
int sub1;
int sub2;
// End Attribute Declaration
class Example{
public static void main(String[] args) {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q09 from Q08
===========
class Student{
// Start Attribute Declaration
String studentId;
String name;
String address;
int sub1;
int sub2;
// End Attribute Declaration
// Start Method Declaration
public void print(){
System.out.println(studentId + " " + name + " " + address + " " + sub1 + " " + sub2);
}
public void setValue(String stuId, String stuName, String stuAddress, int stuSub1, int stuSub2
){
studentId = stuId;
name = stuName;
address = stuAddress;
sub1 = stuSub1;
sub2 = stuSub2;
}
class Example{
public static void main(String[] args) {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q10 Case 1
==========
class Box{
int length;
int width;
int height;
}
class Example{
public static void main(String[] args) {
Box b1 = new Box();
System.out.println(b1);
b1.height = 5;
b1.width = 6;
b1.length = 10;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q11 Case 2
=========
class Box{
int length; // Global Variables
int width; // Global Variables
int height; // Global Variables
b1.height = 5;
b1.width = 6;
b1.length = 10;
b1.printVolume();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q12 Case 3
==========
class Box{
int length; // Global Variables
int width; // Global Variables
int height; // Global Variables
// b1.height = 5;
// b1.width = 6;
// b1.length = 10;
b1.setValue(10, 6, 5);
b1.printVolume();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q13 Case 4
=========
class Box{
int length; // Global Variables
int width; // Global Variables
int height; // Global Variables
b1.setValue(10, 6, 5);
int volume = b1.getVolume();
System.out.println("Volume : " + volume);
}
}
Case 5 Keyword ‘this’
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q14
class Box{
int length;
int width;
int height;
b1.setValue(10, 6, 5);
int volume = b1.getVolume();
System.out.println("Volume : " + volume);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q15
class Box{
int length;
int width;
int height;
b1.setValue(10, 6, 5);
int volume = b1.getVolume();
System.out.println("Volume : " + volume);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q16
class A{
public void print(){
System.out.println(this);
}
}
class Example{
public static void main(String[] args) {
A a1 = new A();
System.out.println(a1);
a1.print();
}
}
Case 6 Constructors
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q17
class Box{
int length;
int width;
int height;
class Box{
int length;
int width;
int height;
class Box{
int length;
int width;
int height;
class Box{
int length;
int width;
int height;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q21 Constructor Overloading
=======================
class Box{
int length;
int width;
int height;
Box(){
this.length = 5;
this.width =5;
this.height = 5;
}
Box(int length){
this.width = length;
this.height = length;
this.length = length;
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q21
class Box{
int length;
int width;
int height;
Box(){
this.length = 5;
this.width =5;
this.height = 5;
}
Box(int length){
this.width = length;
this.height = length;
this.length = length;
}
b1.Box(10,20,30); // Illegal
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q22
class Box{
int length;
int width;
int height;
Box(){
this.length = 5;
this.width =5;
this.height = 5;
}
Box(int length){
this.width = length;
this.height = length;
this.length = length;
}
b1.Box(10,20,30);
b1.printVolume();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q23 Attribute Declaration Values
=========================
class Box{
int length = 10;
int width = 6;
int height = 5;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q24
class MyClass{
int a = 100;
MyClass(){
a = 200;
}
MyClass(int a){
this.a = a;
}
}
class Example{
public static void main(String[] args) {
MyClass m1 = new MyClass();
System.out.println(m1.a);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q25
class Box{
int length = 10;
int width = 6;
int height;
height = 5; // Illegal
class Account{
double balance;
Account(double balance){
this.balance = balance;
}
class Operation{
public void deposit(double amount, Account account){
account.balance+= amount;
}
operation.deposit(5000, a1);
a1.printBalance();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q27
class Account{
double balance;
Account(double balance){
this.balance = balance;
}
class Operation{
public void deposit(double amount, Account account){
account.balance+= amount;
}
class Example{
public static void main(String[] args) {
Account a1 = new Account(10000);
a1.printBalance();
Operation operation = new Operation();
operation.withdraw(3000, a1);
a1.printBalance();
operation.deposit(5000, a1);
a1.printBalance();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q28 Exercise
===========
class MyClass{
int x = 100;
}
class Example{
public static void increment(int x){
x++;
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
System.out.println(myClass.x);
increment(myClass.x);
System.out.println(myClass.x);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q29 Exercise
==========
class MyClass{
int x = 100;
}
class Example{
public static void increment(MyClass myClass){
myClass.x++;
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
System.out.println(myClass.x);
increment(myClass);
System.out.println(myClass.x);
}
}
class Box{
int length;
int width;
int height;
class Example{
public static void main(String[] args) {
Box b1 = new Box(10, 6, 5);
b1.printVolume(); //Volume : 300
Box b2 = b1.getCopy();
b2.printVolume(); //Volume : 300
class Box{
int length;
int width;
int height;
class Example{
public static void main(String[] args) {
Box b1 = new Box(10, 6, 5);
b1.printVolume(); //Volume : 300
Box b2 = b1.getCopy();
b2.printVolume(); //Volume : 300
class Example{
public static void main(String[] args) {
MyClass m1 = new MyClass();
m1.x = 100;
m1.y = 10;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q33
class MyClass{
int x;
static int y;
}
class Example{
public static void main(String[] args) {
MyClass.y = 30;
//MyClass.x = 10; // Illegal
class MyClass{
public void instanceMethod(){
System.out.println("instanceMethod");
}
class Example{
public static void main(String[] args) {
MyClass.staticMethod();
// MyClass.instanceMethod(); // Illegal
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q35 Exercise 1
============
class Example{
public static void main(String[] args) {
Date d1 = new Date();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q36 from Q35
===========
class Date{
int year;
int month;
int day;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q37 Exercise
===========
class Date{
int year;
int month;
int day;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q38 from Q37 Option 1
==================
class Date{
int year = 1980;
int month = 11;
int day = 15;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q39 from Q37 Option 2
===================
class Date{
int year;
int month;
int day;
Date(){
this.year = 1980;
this.month = 11;
this.day = 15;
}
}
class Example{
public static void main(String[] args) {
Date d1 = new Date();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q40 from Q37 Option 3
==================
class Date{
int year;
int month;
int day;
{ // Instance Block
this.year = 1980;
this.month = 11;
this.day = 15;
}
}
class Example{
public static void main(String[] args) {
Date d1 = new Date();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q41 Exercise 3
============
class Date{
int year;
int month;
int day;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q42 from Q41
===========
class Date{
int year;
int month;
int day;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q43 Exercise 4
============
class Date{
int year;
int month;
int day;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q44 from Q43
===========
class Date{
int year;
int month;
int day;
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q45 Exercise 5
===========
class Date{
int year;
int month;
int day;
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q46 from Q45
===========
class Date{
int year;
int month;
int day;
Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}
Date(int year, int month, int day){
this.year = year;
this.month = month;
this.day = day;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q47 Exercise 6
============
class Date{
int year;
int month;
int day;
Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15
d2.setDate(1991,12,14);
d2.printDate(); // 1991-12-14
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q48 from Q47
============
class Date{
int year;
int month;
int day;
Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15
d2.setDate(1991,12,14);
d2.printDate(); // 1991-12-14
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q49 Exercise 7
============
class Date{
int year;
int month;
int day;
Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15
d2.setDate(1991,12,14);
d2.printDate(); // 1991-12-14
d2.setDate(d1);
d2.printDate(); // 1980-11-15
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q50 from Q49
===========
class Date{
int year;
int month;
int day;
Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}
class Example{
public static void main(String[] args) {
Date d1 = new Date(1980, 11, 15);
d1.printDate(); // 1980-11-15
d2.setDate(1991,12,14);
d2.printDate(); // 1991-12-14
d2.setDate(d1);
d2.printDate(); // 1980-11-15
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q51 Exercise 8
============
class Date{
int year;
int month;
int day;
Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}
class Example{
public static void main(String[] args) {
Date d1 = Date.getInstance();
d1.printDate(); // 2000-12-17
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q52 from Q51
============
class Date{
int year;
int month;
int day;
Date(){
this.year = 2000;
this.month = 12;
this.day = 17;
}
class Example{
public static void main(String[] args) {
Date d1 = Date.getInstance();
d1.printDate(); // 2000-12-17
}
}
Access Privileges | Access Levels
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q53
////////////////////////////////////////////////////
package package1;
import package2.Car;
class Example{
public static void main(String[] args) {
Car c1 = new Car();
c1.printCar();
System.out.println(c1.name);
//System.out.println(c1.model); // Illegal
//System.out.println(c1.isStart);// Illegal
//System.out.println(c1.speed);// Illegal
}
}
////////////////////////////////////////////////////
package package2;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q54
////////////////////////////////////////////////////
package package1;
class Example{
public static void main(String[] args) {
Car c1 = new Car();
c1.printCar();
System.out.println(c1.name);
System.out.println(c1.model);
System.out.println(c1.isStart);
//System.out.println(c1.speed);// Illegal
}
}
////////////////////////////////////////////////////
package package1;
Encapsulation
● Wrapping attributes and methods together into a single unit.
● Control over the data by hiding fields and adding methods with public access to access
fields.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q55 Step 1
========
class Car{
boolean isStart;
int speed;
}
class Example{
public static void main(String[] args) {
Car c1 = new Car();
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q56 Step 2
=========
class Car{
boolean isStart;
int speed;
}
class Example{
public static void main(String[] args) {
Car c1 = new Car();
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
c1.speed = 100;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q57 Step 3
=========
class Car{
boolean isStart;
int speed;
}
class Example{
public static void main(String[] args) {
Car c1 = new Car();
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
c1.isStart = true;
c1.speed = 100;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
c1.isStart = false;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q58 Step 4
=========
class Car{
private boolean isStart;
private int speed;
}
class Example{
public static void main(String[] args) {
Car c1 = new Car();
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
c1.isStart = true;
c1.speed = 100;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
c1.isStart = false;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q59 Step 5
=========
class Car{
private boolean isStart;
private int speed;
class Example{
public static void main(String[] args) {
Car c1 = new Car();
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
c1.isStart = true;
c1.speed = 100;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
c1.isStart = false;
System.out.println("Is Start : " + c1.isStart);
System.out.println("Speed : " + c1.speed);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q60 Step 6
=========
class Car{
private boolean isStart;
private int speed;
class Example{
public static void main(String[] args) {
Car c1 = new Car();
System.out.println("Is Start : " + c1.getIsStart());
System.out.println("Speed : " + c1.getSpeed());
c1.setIsStart(true);
c1.incrementSpeed();
System.out.println("Is Start : " + c1.getIsStart());
System.out.println("Speed : " + c1.getSpeed());
c1.setIsStart(false);
System.out.println("Is Start : " + c1.getIsStart());
System.out.println("Speed : " + c1.getSpeed());
}
}
import javax.swing.*;
class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setSize(500, 400);
f1.setTitle("Test Title");
f1.setLocationRelativeTo(null);
f1.setVisible(true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q62
import javax.swing.*;
class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setSize(500, 400);
f1.setTitle("Test Title");
f1.setLocationRelativeTo(null);
f1.setVisible(true);
f2.setVisible(true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q63
import javax.swing.*;
class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
f1.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f1.setSize(500, 400);
f1.setTitle("Test Title");
f1.setLocationRelativeTo(null);
f1.setVisible(true);
}
}
// JFrame.EXIT_ON_CLOSE = 3
// JFrame.DISPOSE_ON_CLOSE = 2
// JFrame.HIDE_ON_CLOSE = 1
// JFrame.DO_NOTHING_ON_CLOSE = 0
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q64
import javax.swing.*;
class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f1.setSize(500, 400);
f1.setTitle("Frame 1");
f1.setLocationRelativeTo(null);
f1.setVisible(true);
f2.setVisible(true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q65
import javax.swing.*;
import java.awt.*;
class Example{
public static void main(String[] args) {
JFrame f1 = new JFrame();
f1.setSize(500,500);
f1.setTitle("Frame 1");
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setLocationRelativeTo(null);
f1.add(button);
f1.setVisible(true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q66
import javax.swing.*;
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
c1.setSize(500,400);
c1.setTitle("Calculator");
c1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c1.setLocationRelativeTo(null);
c1.setVisible(true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q67
import javax.swing.*;
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
c1.setSize(500,400);
c1.setTitle("Calculator");
c1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c1.setLocationRelativeTo(null);
c1.setVisible(true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q68
import javax.swing.*;
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
c1.setSize(500,400);
c1.setTitle("Calculator");
c1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c1.setLocationRelativeTo(null);
c1.setVisible(true);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q69
import javax.swing.*;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
Calculator c2 = new Calculator();
new Calculator();
new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q70
import javax.swing.*;
import java.awt.*;
JButton button;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q71
import javax.swing.*;
import java.awt.*;
JButton button;
JLabel label;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
Swing Layouts
Border Layout
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q72
import javax.swing.*;
import java.awt.*;
JButton button;
JLabel label;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new BorderLayout());
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q73
import javax.swing.*;
import java.awt.*;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new BorderLayout());
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
Flow Layout
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q74
import javax.swing.*;
import java.awt.*;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q75 Align Right
=============
import javax.swing.*;
import java.awt.*;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new FlowLayout(FlowLayout.RIGHT));
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q76 Align Right
import javax.swing.*;
import java.awt.*;
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
Grid Layout
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q77
import javax.swing.*;
import java.awt.*;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new GridLayout(2, 2));
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q78 Exercise
===========
import javax.swing.*;
import java.awt.*;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new GridLayout(4,4));
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q79 Exercise
==========
import javax.swing.*;
import java.awt.*;
add("Center", centerPanel);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q80 Exercise
===========
import javax.swing.*;
import java.awt.*;
JTextField textField;
JButton[] buttons = new JButton[16];
String arr[] = {"7", "8", "9", "/", "4", "5","6", "*", "1", "2", "3", "+", "0", ".", "=", "-"};
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new BorderLayout());
add("Center",buttonPanel);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
import javax.swing.*;
import java.awt.*;
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
new Notepad();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q82 Step 2
=========
import javax.swing.*;
import java.awt.*;
Notepad(){
setSize(600,400);
setLocationRelativeTo(null);
setTitle("untitled");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
new Notepad();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q83 Step 3
=========
import javax.swing.*;
import java.awt.*;
Notepad(){
setSize(600,400);
setLocationRelativeTo(null);
setTitle("untitled");
setDefaultCloseOperation(EXIT_ON_CLOSE);
add("Center", scrollPane);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
new Notepad();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q84 Step 4
=========
import javax.swing.*;
import java.awt.*;
Notepad(){
setSize(600,400);
setLocationRelativeTo(null);
setTitle("untitled");
setDefaultCloseOperation(EXIT_ON_CLOSE);
add("Center", scrollPane);
menuBar.add(fileMenu);
menuBar.add(editMenu);
setJMenuBar(menuBar);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
new Notepad();
}
}
Working with JTable
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q85
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
ViewStudent(){
setSize(600,400);
setLocationRelativeTo(null);
setTitle("View Student");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
new ViewStudent();
}
}
Event Handling
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q86 Action Event vs. Action Listener
============================
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
JButton button;
Calculator(){
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("You pressed the button...!");
}
});
add(button);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q87
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
JTextField textField;
JSlider slider;
Calculator(){
setSize(350,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int value = slider.getValue();
textField.setText(value+"");// Integer.toString(value)
}
});
add(slider);
setVisible(true);
}
}
class Example{
public static void main(String[] args) {
Calculator c1 = new Calculator();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q88
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q89
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q90