Tuesday, June 9, 2015

Project 5 / Area and Color




/*
Our program uses many kinds of shapes, such as triangle, rectangle and so on. You should design a
super class called Shape, which defines the public interface/class (or behaviours) of all the shapes.
 And, we would like all the shapes to have a method called getArea(), which returns the area of that
  particular shape. Define a Shape class as mentioned in the above class diagram.
*/

public class MainActivity
{
public static void main(String[] args)
{
Ractangle r = new Ractangle(666, 10, 10);
r.toString();
r.getArea();

System.out.println();

Traingle t = new Traingle(999, 10, 10);
t.toString();
t.getArea();
}
}

------------------------------------------------------------------


public class Shape
{
int color ; 
public Shape(int color) 
{
this.color = color;
}
@Override
public String toString() 
{
System.out.println("The color of the shape is: "+color);
return super.toString();
}
public void getArea() 
{

}
}

--------------------------------------------------------------------------


public class Ractangle extends Shape{

double length, breadth;

public Ractangle(int color, double length , double breadth)
{
super(color);
this.length = length;
this.breadth = breadth;
}
@Override
public void getArea() 
{
super.getArea();
double result = length * breadth;
System.out.println("The area of Rectangle is: "+result);
}
}

--------------------------------------------------------------------------------


public class Traingle extends Shape{

double base, height;
public Traingle(int color, double base, double height)
{
super(color);
this.base = base;
this.height = height;
}

@Override
public void getArea() 
{
double result = 0.5 * base*height;
System.out.println("The area is: "+ result);
super.getArea();
}
}

---------------------------------------------------------------------------------------



No comments:

Post a Comment