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

My Document

Uploaded by

Bikram Dhibar
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)
6 views

My Document

Uploaded by

Bikram Dhibar
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/ 17

GOVERNMENT COLLEGE OF ENGINEERING

AND TEXTILE TECHNOLOGY, SERAMPORE

LAB COPY
Name: SOUMILI POLLEY
Bikram Dhibar
Roll no.: 11000222033
11000222010
Year: 3rd
Sem: 5th
Subject: Object Oriented Programming Lab
Subject code: PCC-CS593
Stream: Information Technology (IT)
Assignment no: 01
Problem statement: Implement salary calculations of different types of
faculties using abstract class.
Implementation:
abstract class Salary
{
final static double hra=0.15, ma=0.12, da=0.2;
abstract void calSal(double basic,int yoe);
}
class Professor extends Salary
{
void calSal(double basic,int yoe)
{
double totSal;
basic=basic+yoe*1000.01;
totSal=basic+basic*da+basic*hra+basic*ma;
System.out.println("Total Salary= "+totSal);
}
}
class AsstProfessor extends Salary
{
void calSal(double basic,int yoe)
{
double totSal;
basic=basic+yoe*800.01;
totSal=basic+basic*da+basic*hra+basic*ma;
System.out.println("Total Salary= "+totSal);
}
}
class Lecturer extends Salary
{
void calSal(double basic,int yoe)
{
double totSal;
basic=basic+yoe*600.01;
totSal=basic+basic*da+basic*hra+basic*ma;
System.out.println("Total Salary= "+totSal);
}
}
class TestAbstractClass
{
public static void main(String a[])
{
new Professor().calSal(60000.0,5);
new AsstProfessor().calSal(50000.0,5);
new Lecturer().calSal(45000.0,5);
}
}

1|Page
Output:

Discussion:
First we define an abstract base class called ‘Faculty’. This class should contain an abstract
method, ‘calculate_salary()’, that outlines the structure for salary calculation without
implementation. It will inherit from ‘Faculty’ and provide concrete implementations of the
‘calculate_salary()’ method. Each derived class will have its own logic for calculating salaries
based on factors like base pay, da, ma, hra. By using an abstract class, the code ensures a
consistent interface for salary calculations while allowing flexibility for different
salary structures.

Teacher signature with date

Assignment no: 02
Problem statement: Implement salary calculations of different types of
faculties using interface.
Implementation:
interface Salary
{
double hra=0.15, ma=0.12, da=0.2;
void calSal(double basic,int yoe);
}
class Professor implements Salary
{
public void calSal(double basic,int yoe)
{
double totSal;
basic=basic+yoe*1000.01;
totSal=basic+basic*da+basic*hra+basic*ma;
System.out.println("Total Salary= "+totSal);
}
}
class AsstProfessor implements Salary
{
public void calSal(double basic,int yoe)
{
double totSal;
basic=basic+yoe*800.01;
totSal=basic+basic*da+basic*hra+basic*ma;
System.out.println("Total Salary= "+totSal);
}
}

2|Page
class Lecturer implements Salary
{
public void calSal(double basic,int yoe)
{
double totSal;
basic=basic+yoe*600.01;
totSal=basic+basic*da+basic*hra+basic*ma;
System.out.println("Total Salary= "+totSal);
}
}
class TestAbstractClass
{
public static void main(String a[])
{
new Professor().calSal(60000.0,5);
new AsstProfessor().calSal(50000.0,5);
new Lecturer().calSal(45000.0,5);
}
}
Output:

Discussion:
Interface is implicitly ‘public’. And as the subclass can not access strong access modifier then
the superclass, so the inherited subclasses are must be public (as ‘default’ is strong then
’public’). So by using ‘implements’ keyword the interface Salary is inherited by ‘Professor’,
‘AsstProfessor’, ‘Lecturer’ classes. And ‘public access modifier is also ued in the methods.

Teacher signature with date

Assignment no: 03
Problem statement: Create two new threads in addition of main thread under
all three threads will display value from 1 to 100. Write the code in such a way
so that 2nd thread can displays values from 50 to 100, at the last without any
intervention.
Implementation:
class A extends Thread
{
public void run()
{
int i;

3|Page
for(i=1;i<=100;i++)
{
System.out.println("Thread 1, i= "+i);
if(i==50)
{
try
{
sleep(5000);
}
catch(InterruptedException ie)
{
}
}
}
}
}
class B extends Thread
{
public void run()
{
int i;
for(i=1;i<=100;i++)
{
System.out.println("Thread 2, i= "+i);
}
}
}
class threadTest
{
public static void main(String a[])
{
A p = new A();
p.start();
B q = new B();
q.start();
int i;
for(i=1;i<=100;i++)
{
System.out.println("Main thread i= "+i);
}
}
}

4|Page
Output:

5|Page
Discussion:
The given code demonstrates multithreading in Java using two classes, A and B, which extend
the Thread class. Both override the run() method to perform a loop printing values from 1 to
100. In class A, when i reaches 50, the thread sleeps for 5000 milliseconds (5 seconds) using
sleep(). The main() method in the threadTest class creates and starts threads for both A and B
and also executes a loop in the main thread. Thus, three threads run concurrently, printing their
respective outputs. The sleep in thread A introduces a delay at i=50.

Teacher signature with date

6|Page
Assignment no: 04
Problem statement: Create a package and test with one interface Salary with
three constants da=0.2, hra=0.15, ma=0.12 and one method calSalary() with two
parameters basic and year of experience.
Import this package in another program TestPackage.java where calculate the
salaries of Lecturer, Assistant Professor & Professor using the interface defined
in package test. Consider the yearly salary increment of lecturer, assistant
professor & professor as 800, 1200, 2000 respectively.
Note: Salary calculation formula will be –
totSal = basic + basic*da + basic*hra + basic*ma + yearly increment value
+ year of experience
Implementation:
test.java file:
package test;
public interface Salary{
public double hra=0.15, ma=0.12, da=0.2;
void calSal(double basic,int yoe);
}
TestPackage.java file:
import test.*;
class Lecturer implements Salary
{
public double calSal(double basic, int yoe)
{
double totSal;
totSal=basic+basic*da+basic*hra+basic*ma+800.0*yoe;
return totSal;
}
}
class AssisProfessor implements Salary
{
public double calSal(double basic, int yoe)
{
double totSal;
totSal=basic+basic*da+basic*hra+basic*ma+1200.0*yoe;
return totSal;
}
}
class Professor implements Salary{
public double calSal(double basic, int yoe)
{
double totSal;
totSal=basic+basic*da+basic*hra+basic*ma+2000.0*yoe;
return totSal;
}
}

7|Page
class Professor implements Salary {
public double calSal(double basic, int yoe)
{
double totSal;
totSal=basic+basic*da+basic*hra+basic*ma+2000.0*yoe; return totSal;
}
}
class testInter
{
public static void main(String a[])
{
System.out.println("the salary of Professor:"+new Professor().calSal(60000.0,6));
System.out.println("the salary of Assis Professor:"+new
AssisProfessor().calSal(50000.0,6));
System.out.println("the salary of lecturer:"+new Lecturer().calSal(45000.0,6));
}
}

Output:

Discussion:
This code defines an interface Salary in a package test and implements it across three classes
(Lecturer, AssisProfessor, and Professor) to calculate the total salary based on a basic salary
and years of experience (YOE). Each role has a different formula for calculating the total salary,
incorporating constants for various allowances. The code also has a main class, testInter, which
tests the salary calculations for each role.
This contains constants for HRA (House Rent Allowance), MA (Medical Allowance), and DA
(Dearness Allowance) as double values.
The "Lecturer", "AssisProfessor" and "Professor" implements Salary and calculates
the total salary.

Teacher signature with date


Assignment no: 05
Problem statement: Show the concept of Thread synchronization using at
least two extra Threads.
Implementation:
class T1 extends Thread
{
Sclass p;
T1(Sclass r){
p=r;
}
public void run()

8|Page
{
p.display(1);
}
}
class T2 extends Thread
{
Sclass p;
T2(Sclass r){
p=r;
}
public void run()
{
p.display(2);
}
}
class Sclass
{
synchronized void display(int id)
{
int i;
for(i=0;i<5;i++){
System.out.println("Thread "+id+" is here");
}
}
}
class TestSynchronize
{
public static void main(String a[])
{
Sclass o1= new Sclass();
T1 t1=new T1(o1);
T2 t2=new T2(o1);
t1.start();
t2.start();
}
}

Output:

9|Page
Discussion:
The provided code demonstrates the concept of Thread Synchronization in Java, using two
threads (T1 and T2) to access a shared resource (Sclass). The Sclass contains a synchronized
method display(int id) that runs a loop five times, printing the id of the thread accessing it. The
synchronized keyword ensures that only one thread can execute this method at a time on the
same Sclass instance.
When a thread (either T1 or T2) calls display, it locks the Sclass instance, preventing the other
thread from entering display until the current thread exits the method.

Teacher signature with date


Assignment no: 06
Problem statement: Implement Applet calculator application using 3 text
fields.
Implementation:
JAVA file:-
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
class Calculator extends Applet implements ActionListener{
TextField t1,t2,t3;
Label l1,l2,l3;
Button b1,b2,b3;
public void init(){
b1=new Button("+");
b2=new Button("-");
l1= new Label("No1");
l2= new Label("No2");
l3= new Label("Result");
t1= new TextField(10);
t2= new TextField(10);
t3= new TextField(10);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String val1=t1.getText();
String val2=t2.getText();

10 | P a g e
if(ae.getSource()==b1)
{
int r =Integer.parseInt(val1)+Integer.parseInt(val2);
t3.setText(String.valueOf(r));
}
else{
int r =Integer.parseInt(val1)-Integer.parseInt(val2);
t3.setText(String.valueOf(r));
}
}
}

HTML file:
<html>
<body>
<applet code = "Calculator.class"
width="300"
helght="300">
</applet>
</body>
</html>

Output:

Discussion:
The Java code provided implements a simple calculator using an applet with three text fields
for input and output. This applet allows users to perform addition and subtraction.
There are three text fields (t1, t2, and t3), used for input and output. Labels (l1, l2, and l3) are
used to identify each text field. Two buttons (b1 and b2) are provided for performing operations.
The init() method initializes the applet components and adds them to the applet interface. Here,
the text fields, labels, and buttons are created and added to the applet layout.
The actionPerformed() method handles the actions performed when buttons are clicked. And
to implement ActionListener interface java.awt.event package is imported.

Teacher signature with date

11 | P a g e
Assignment no: 07
Problem statement: Show the mouse co-ordinates on the Applet window.
Implementation:
JAVA file:-
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class TestMouse extends Applet implements MouseMotionListener{
String str;
public void init()
{
addMouseMotionListener(this);
}
public void mouseDragged(MouseEvent m){}
public void mouseMoved(MouseEvent m)
{
int x=m.getX();
int y=m.getY();
str="("+String.valueOf(x)+","+String.valueOf(y)+")";
repaint();
}
public void paint(Graphics g) {
g.drawString(str,100,100);
}
}
HTML file:
<html>
<body>
<applet code = "TestMouse.class"
width="300"
helght="300">
</applet>
</body>
</html>

Output:

12 | P a g e
Discussion:
The Java code implements a simple applet named TestMouse, which listens for mouse
movements and displays the cursor coordinates in the applet window.
A string variable str is declared to store the coordinates of the mouse cursor. In the init()
method, the addMouseMotionListener(this) call registers the applet as a listener for mouse
motion events.The mouseMoved method, however, captures the cursor's x and y coordinates
when it moves. It then updates str to display the coordinates in (x, y) format and calls repaint()
to refresh the applet display. As the user moves the mouse over the applet area, the
mouseMoved method captures the current x and y positions and updates the display by
repainting the screen with the new coordinates. Each time the mouse moves, paint is called to
update the display with the latest coordinates, providing real-time feedback on the
cursor's location.

Teacher signature with date


Assignment no: 08
Problem statement: Implement Digital Stopwatch with start and stop
button.
Implementation:
JAVA code:-
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
class StopWatch extends Applet implements Runnable, ActionListener
{
Thread t1;
String str;
Button b1,b2;
public void init(){
b1=new Button("start");
b2=new Button("stop");
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1)
{
t1= new Thread(this);
t1.start();
}
else
t1.stop();
}
public void run()
{

13 | P a g e
int i;
for(i=0;i<=60;i++)
{
if(i<10)
str="0:0"+String.valueOf(i);
else if (i>=10 && i<60)
str="0:"+ String.valueOf(i);
else
str="1:00";
repaint();
try {
t1.sleep(1000);
}
catch(InterruptedException ie){}
}
}
public void paint(Graphics g) {
g.drawString(str,100,100);
}
}
HTML code:
<html>
<body>
<applet code = "StopWatch.class"
width="300"
helght="300">
</applet>
</body>
</html>

Output:

Discussion:
This code represents a simple digital stopwatch Java applet with a start and stop button,
displaying seconds on the screen. It uses the java.awt package for graphical components,
java.applet for applet functionality, and java.awt.event for handling button actions. The
stopwatch increments seconds every second up to 60 seconds.
The init() method initializes the applet. b1 and b2 buttons are created for "start" and "stop", and
added to the applet’s display.

14 | P a g e
The actionPerformed method determines which button is clicked. If the start button (b1) is
clicked, a new thread is created and started, allowing the stopwatch to begin counting. If the
stop button (b2) is clicked, the thread is stopped.
The run() method, part of the Runnable interface, uses a loop to count seconds up to 60,
updating the str variable each second.
If the count (i) is less than 10, it formats str to "0:0x" (e.g., "0:01" for one second). If the count
reaches 10 or above but less than 60, str is formatted to "0 ". If the count reaches 60, str becomes
"1:00", indicating one minute.

Teacher signature with date


Assignment no: 09
Problem statement: Implement Frame calculator application using 3 text
fields.
Implementation:
import java.awt.*;
import java.awt.event.*;
class Calculator extends Frame implements ActionListener{
TextField t1,t2,t3;
Label l1,l2,l3;
Button b1,b2,b3;
Calculator(){
setLayout(new FlowLayout());
b1=new Button("+");
b2=new Button("-");
l1= new Label("No1");
l2= new Label("No2");
l3= new Label("Result");
t1= new TextField(10);
t2= new TextField(10);
t3= new TextField(10);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public static void main(String a[]){
Calculator t = new Calculator();
t.setSize(200,300);
t.setVisible(true);

15 | P a g e
}
public void actionPerformed(ActionEvent ae)
{
String val1=t1.getText();
String val2=t2.getText();
if(ae.getSource()==b1)
{
int r =Integer.parseInt(val1)+Integer.parseInt(val2);
t3.setText(String.valueOf(r));
}
else{
int r =Integer.parseInt(val1)-Integer.parseInt(val2);
t3.setText(String.valueOf(r));
}
}
}
Output:

Discussion:
This Java program demonstrates a simple calculator application using AWT (Abstract Window
Toolkit) to create a graphical interface. The application consists of three text fields, two buttons
for addition and subtraction, and three labels to guide the user.
The constructor Calculator() sets up the layout (using FlowLayout), initializes the components,
and adds them to the frame. The text fields t1, t2, and t3 are created with a width of 10
characters.
Each component is added to the frame using add(), including labels, text fields, and buttons.
Action listeners are added to b1 and b2 to handle click events. The actionPerformed method
checks the source of the event to determine whether the addition or subtraction button was
clicked.
Depending on which button is pressed, the program either adds or subtracts the two numbers.
The result is converted to a string using String.valueOf(r) and displayed in t3.

Teacher signature with date

16 | P a g e

You might also like