You are on page 1of 10

LAB 2

Inheritance :
1. Write a superclass Worker and subclasses HourlyWorker and SalariedWorker.
Every worker
has a name and a salary rate. Write a method computePay(int hours) that
computes the
weekly pay for every worker. An hourly worker gets paid the hourly wage for the
actual
number of hours worked, if hours is at most 40. If the hourly worker worked more
than 40
hours, the excess is paid at time and a half. The salaried worker gets paid the
hourly wage for
40 hours, no matter what the actual number of hours is. Supply a test program
that uses
polymorphism to test these classes and methods.

Program :
import java.util.*;
class Worker
{
String name;
int rate;
double salary=55;
};
class HourlyWorker extends Worker
{
HourlyWorker(String str,int rate)
{
name = str;
super.rate = rate;
}
void computePay()
{
if(rate<=40)
{
salary *= rate;
}
else
{
int temp = rate-40;
int c = (int)salary;
salary *=40;
salary += temp*((int)(c/2));
}
}
};
class SalariedWorker extends Worker
{
SalariedWorker(String str,int rate)
{
name = str;
super.rate = rate;
}
void computePay()
{
if(rate<=40)
{
salary *= rate;
}
else
{
salary*=40;
}
}
};
class Main
{
public static void main(String...args)
{
HourlyWorker h = new HourlyWorker("Kanish",44);
SalariedWorker s = new SalariedWorker("Dhoni",50);
h.computePay();
s.computePay();
System.out.println("Hourly worker :
"+h.name+"\t"+"Salary : "+h.salary);
System.out.println("Salariedworker :
"+s.name+"\t"+"Salary : "+s.salary);
}
}
Interface :
1. Design an interface MoveableShape that can be used as a generic
mechanism for animating a
shape. A moveable shape must have two methods: move and draw.
Write a generic
AnimationPanel that paints and moves any MoveableShape or array list
of MoveableShape
objects. Supply moveable rectangle and car shapes.

Program :
import java.awt.*;
interface MoveableShape
{
void move();
void drawi();
}
class Shape
{
/*void drawi(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.drawRect(0,0,100,200);
}*/
public static void main(String...args)
{
Shape sp = new Shape();
Frame f = new Frame("Frame");
f.setSize(500,500);
f.setVisible(true);
/*void paint(Graphics g)
{
sp.drawi(g);
}*/
}
}

PACKAGE
Write a java program to find the roots of a quadratic equation using
interface
and packages.
Program :
E:\JAVA_LAB\LAB2\packages\Quad2\QuadInterface.java
package Quad1;
public interface QuadInterface
{
public abstract void printRoots();
}

E:\JAVA_LAB\LAB2\packages\Quad2\Quadratic.java
package Quad2;
import Quad1.*;

public class Quadratic implements QuadInterface


{
public Quadratic(){ }
public static void printRoots(double a,double b,double c)
{
double root1,root2;
double determine = b*b - 4*a*c;
if(determine > 0)
{
//2 real and dinsct roots
root1 = (-b + Math.sqrt(determine)) / (2*a); //-b
+ or - sqrt(b^2-4ac)/2a
root2 = (-b - Math.sqrt(determine)) / (2*a);
System.out.printf("Root1 : \t%.3f\n",root1);
System.out.printf("Root2 : \t%.3f\n",root2);
}
else if(determine==0)
{
//2 real and equal roots
root1 = root2 = -b/(2*a);
System.out.printf("Root1 = Root2 :
\t%.3f\n",root1);
}
else
{
//roots are complex distnct
root1 = -b /(2*a);
root2 = Math.sqrt(-determine)/(2*a);
System.out.printf("Root1 : \t%.2f +
%.2fi",root1,root2);
System.out.printf("\nRoot1 : \t%.2f -
%.2fi",root1,root2);
}
}
};

MAINCLASS program :
E:\JAVA_LAB\LAB2\packages\Package.java
import java.util.*;
import Quad1.*;
import Quad2.*;
class Package
{
public static void main(String...args)
{
double a=2.3,b=4,c=5.6;
Quadratic.printRoots(a,b,c);
}
}

You might also like