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

Lab 1 Solution

The document contains Java code defining an abstract class hierarchy for shapes, including 2D and 3D shapes. It features classes for Circle and Cube, with methods for setting and getting dimensions, calculating area, and volume. The design emphasizes abstraction and encapsulation, allowing for extensibility in shape types.

Uploaded by

Saeedul Karim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab 1 Solution

The document contains Java code defining an abstract class hierarchy for shapes, including 2D and 3D shapes. It features classes for Circle and Cube, with methods for setting and getting dimensions, calculating area, and volume. The design emphasizes abstraction and encapsulation, allowing for extensibility in shape types.

Uploaded by

Saeedul Karim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab 1 Solution

package shape;

/**
*
* @author abdurrahmanjalil
*/
public abstract class Shape {

private int x;
private int y;

//constructor
public Shape( int x, int y){
this.x = x;
this.y = y;
}

//setX()
public void setX(int x){
this.x=x;
}

//setY()
public void setY(int y){
this.y=y;
}

//getX()
public int getX(){
return x;
}
//getY()
public int getY(){
return y;
}

//abstract method getName()


public abstract String getName();

public String toString(){


return String.format("(%d, %d)", getX(), getY());
}

package shape;

/**
*
* @author abdurrahmanjalil
*/
public abstract class TwoDimensionalShape extends Shape{

private int dimension1;


private int dimension2;

//constructor
public TwoDimensionalShape(int x, int y, int d1, int d2){
super(x,y);
dimension1 =d1;
dimension2 =d2;

2
}

//setDimension1
public void setD1(int d1){
this.dimension1=d1;
}

//setDimension2
public void setD2(int d2){
this.dimension2=d2;
}

//getDimension1
public int getD1(){
return dimension1;
}

//getDimension2
public int getD2(){
return dimension2;
}

//getArea()
public abstract int getArea();

package shape;

/**
*
* @author abdurrahmanjalil

3
*/
public class Circle extends TwoDimensionalShape {

//constructor
public Circle (int x, int y, int radius){
super(x, y, radius, radius);

//getName()
public String getName(){
return "Circle";
}

//getArea()
public int getArea(){
return (int)(Math.PI * getRadius() * getRadius());

//
public void setRadius(int radius){
setD1(radius);
setD2(radius);
}

public int getRadius(){


return getD1();
}

public String toString(){


return String.format("%s %s: %d\n", super.toString(), "radius",
getRadius());

4
}//end of class Circle

package shape;

/**
*
* @author abdurrahmanjalil
*/
public abstract class ThreeDimensionalShape extends Shape{

private int dimension1;


private int dimension2;
private int dimension3;

//constructor
public ThreeDimensionalShape(int x, int y, int d1, int d2, int d3){
super(x, y);
dimension1 =d1;
dimension2 =d2;
dimension3 =d2;
}

//setDimension1
public void setD1(int d1){
dimension1 = d1;
}

//setDimension2
public void setD2(int d2){
dimension2 = d2;
}
//setDimension3

5
public void setD3(int d3){
dimension3 = d3;
}

public int getD1(){


return dimension1;
}

public int getD2(){


return dimension2;
}

public int getD3(){


return dimension3;
}

//getArea()
public abstract int getArea();

//getVolume()
public abstract int getVolume();
}

package shape;

/**
*
* @author abdurrahmanjalil
*/
public class Cube extends ThreeDimensionalShape {

6
public Cube(int x, int y, int d1, int d2, int d3){
super(x, y, d1, d2, d3);
}

public String getName(){

return "Cube";
}

public int getArea(){

return getD1()*getD2()*6;
}

public int getVolume(){

return getD1()*getD2()*getD3();
}

You might also like