ADVANCED OBJECT ORIENTED PROGRAMMING
TASK#1:
Create a class called computers and two classes MyComputer and YourComputer which
inherits computer class. Define appropriate fields for the three classes. Create another class
processor as a composite class of computer. Write a method which prints the differences
between the processors of two computers.
CODE:
class Computers{
int a,b,c,d,e;
String comp;
void spec(int a, int b , int c , int d , int e ){
this.a=a;
this.b=b;
this.c=c;
this.d=d;
this.e=e;
System.out.println("RAM: "+a);
System.out.println("Prosessor: "+b);
System.out.println("Hard Disk: "+c);
System.out.println("Graphic Card: "+d);
System.out.println("SSD; "+e);
void price(int b){
System.out.println("Price: "+b);
void company(String comp){
this.comp=comp;
System.out.println("Company: "+comp);
}}
class MyComputer extends Computers{
@Override
ADVANCED OBJECT ORIENTED PROGRAMMING
void spec(int a, int b , int c, int d, int e){
System.out.println("\t\tMY COMPUTER");
System.out.println("RAM: "+a);
System.out.println("Prosessor: "+b);
System.out.println("Hard Disk: "+c);
System.out.println("Graphic Card: "+d);
System.out.println("SSD; "+e);
@Override
void price(int a){
System.out.println("Price: "+a);
@Override
void company(String comp){
System.out.println("Cmpany: "+comp);
class YourComputer extends Computers{
@Override
void spec(int a, int b , int c, int d, int e){
System.out.println("\t\tYOUR COMPUTER");
System.out.println("RAM: "+a);
System.out.println("Prosessor: "+b);
System.out.println("Hard Disk: "+c);
System.out.println("Graphic Card: "+d);
System.out.println("SSD; "+e);
@Override
ADVANCED OBJECT ORIENTED PROGRAMMING
void price(int a){
System.out.println("Price: "+a)
@Override
void company(String comp){
System.out.println("Comapny: "+comp);
public class H1 {
public static void main(String[] args) {
Computers obj1= new MyComputer();
Computers obj2= new YourComputer();
obj1.spec(16,7,500,2,125);
obj1.price(8000000);
obj1.company("dell");
obj2.spec(12,5,500,4,125);
obj2.price(1000000);
obj2.company("hp");
}}
OUTPUT:
ADVANCED OBJECT ORIENTED PROGRAMMING
TASK#2:
Create a class to print the area of a rectangle. The class has two methods with the
same name but different number of parameters. The method for printing area of
rectangle has two parameters which are length and breadth.
CODE:
public class Area {
void printArea(int length,int breadth)
{
int Area=length*breadth;
System.out.println("Area of rectangle is:"+Area);
}
void printArea(double length,double breadth){
double Area=length*breadth;
System.out.println(" Area of rectangle is:"+Area);
}
public static void main(String[] args) {
Area obj1=new Area();
Area obj2=new Area();
obj1.printArea(10,20);
obj2.printArea(5,6);
}}
OUTPUT:
ADVANCED OBJECT ORIENTED PROGRAMMING
TASK#3:
Define an abstract base class SHAPE that includes protected data members for the (x,y)
position of a shape, a public method to move a shape , and a public abstract method show() to
output a shape .Derive subclasses for lines, circles and rectangles. Also define the class
POLYLINE with SHAPE as the base class. You can represent a line as two points, a circle as
a center and a radius. A rectangle as two points on diagonally opposite corners. Implement the
toString() method for each class.Test the classes by selecting ten random objects of the
derived classes, then invoking the show() method for each class.Use the toString() method in
the derived classes.
CODE:
abstract class shape{
protected int a,b;
public void moveShape(int a,int b) {
this.a = a;
this.b=b;
}
public abstract void show();
}
class circle extends shape{
int center,radius;
@Override
public void moveShape(int center , int radius) {
this.center = center;
this.radius= radius;
}
@Override
public void show() {
String cen = Integer.toString(this.center);
String rad = Integer.toString(this.radius);
System.out.println("Radius of Circle is: "+ radius+" and center of circle is: "+ center);}}
class lines extends shape{
ADVANCED OBJECT ORIENTED PROGRAMMING
int x,y;
@Override
public void moveShape(int m1 , int m2) {
this.x =m1 ;
this.y= m2;
}
@Override
public void show() {
String s1= Integer.toString(this.x);
String s2 = Integer.toString(this.y);
System.out.println(" point of line one is "+ s1+" and point of line two is: "+ y);
}
}
class rectangle extends shape{
int rec1,rec2;
@Override
public void moveShape(int rec1 , int rec2) {
this.rec1 = rec1;
this.rec2 = rec2;
}
@Override
public void show() {
String r = Integer.toString(this.rec1);
String p = Integer.toString(this.rec2);
System.out.println("Opposite points of rectangle "+ rec1+" and the main point of rectangle "+rec2);}}
class polyline extends shape{
@Override
public void show() {
}
}
ADVANCED OBJECT ORIENTED PROGRAMMING
public class T3 {
public static void main(String[] args) {
// TODO code application logic here
circle c[] = {
new circle(),new circle(),new circle()
};
lines l[] = {
new lines(),new lines(),new lines()
};
rectangle r[] = {
new rectangle(), new rectangle(), new
rectangle()
};
c[0].moveShape(3, 8);
c[0].show();
c[1].moveShape(9, 5);
c[1].show();
c[2].moveShape(40,34);
c[2].show();
l[0].moveShape(32, 12);
l[0].show();
l[1].moveShape(344, 120);
l[1].show();
l[2].moveShape(6, 12);
l[2].show();
r[0].moveShape(125, 74);
r[0].show();
r[1].moveShape(20, 14);
r[1].show();
r[2].moveShape(21, 144);
ADVANCED OBJECT ORIENTED PROGRAMMING
r[2].show();
}
}
OUTPUT: