Commit 815c90ea authored by lui.bingham's avatar lui.bingham

Exercise 2

parent 89aec285
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
public class App { public class App {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
Shape shape1 = new Triangle(3); Shape shape1 = new Rectangle(5, 6);
shape1.draw(); shape1.draw();
} }
} }
public class Rectangle extends Shape{
// An equilateral triangle
public Rectangle() {
this.width = 3;
this.height = 4;
}
public Rectangle(int a, int b) {
this.width = a;
this.height = b;
}
public Rectangle(int b) {
this.width = 3;
this.height = b;
}
@Override
public void draw() {
System.out.print(" ");
for (int loop1=0; loop1<width; loop1++){
System.out.print("__");
}
System.out.println("");
for (int loop2=0; loop2<height; loop2++){ // Loops 3 times
System.out.print("|");
for (int loop3=0; loop3<width; loop3++){
if (loop2 == (height - 1))
{
System.out.print("__");
}
else{
System.out.print(" ");
}
}
System.out.println("|");
}
System.out.println("");
}
@Override
public float area() {
float area = width * height;
return area;
}
@Override
public float circumference() {
float circ = (width + height) * 2;
return circ;
}
}
abstract public class Shape { abstract public class Shape {
protected int size; protected int size;
protected int width;
protected int height;
abstract public void draw(); abstract public void draw();
abstract public float area(); abstract public float area();
...@@ -8,6 +10,10 @@ abstract public class Shape { ...@@ -8,6 +10,10 @@ abstract public class Shape {
public int getSize() { return size; } public int getSize() { return size; }
public void setSize(int size) { this.size = size; } public void setSize(int size) { this.size = size; }
public int getWidth() { return width; }
public void setWidth(int width) { this.width = width; }
public int getHeight() { return height; }
public void setHeight(int height) { this.height = height; }
public Shape(){ public Shape(){
this.size = 3; this.size = 3;
......
public class Square extends Shape{
// An equilateral triangle
public Square() {
super(3);
}
public Square(int i) {
super(i);
}
@Override
public void draw() {
System.out.print(" ");
for (int loop1=0; loop1<size; loop1++){
System.out.print("__");
}
System.out.println("");
for (int loop2=0; loop2<size; loop2++){ // Loops 3 times
System.out.print("|");
for (int loop3=0; loop3<size; loop3++){
if (loop2 == (size - 1))
{
System.out.print("__");
}
else{
System.out.print(" ");
}
}
System.out.println("|");
}
System.out.println("");
}
@Override
public float area() {
float fSize = size;
float area = fSize * fSize;
return area;
}
@Override
public float circumference() {
float fSize = size;
float circ = fSize + fSize + fSize + fSize;
return circ;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment