Browse Source

problem 2 done

master
Raphael Roberts 7 years ago
parent
commit
037de0fac8
  1. 35
      Circle.java
  2. 46
      Rectangle.java
  3. 37
      Shape.java
  4. 2
      TestParseString.java
  5. 8
      TestShape.java

35
Circle.java

@ -0,0 +1,35 @@
public class Circle extends Shape{
private double radius;
public Circle() {
super();
this.radius = 1.0;
}
public Circle(double radius) {
super();
this.radius = radius;
}
public Circle(double radius,boolean isFilled,String color){
super(isFilled, color);
this.radius = radius;
}
public void setRadius(double radius){
this.radius = radius;
}
public double getRadius() {
return this.radius;
}
public String toString() {
String ret = "";
ret += "Radius: " + this.radius + "\n";
double area = Math.PI * Math.pow(this.radius,2);
ret += "Area: " + area + "\n";
ret += super.toString();
return ret;
}
}

46
Rectangle.java

@ -0,0 +1,46 @@
public class Rectangle extends Shape{
private double width;
private double length;
public Rectangle() {
super();
this.width = 1.0;
this.length = 2.0;
}
public Rectangle(double width, double length){
super();
setLW(width,length);
}
public Rectangle(double width, double length,boolean isFilled,String color) {
super(isFilled, color);
setLW(width,length);
}
public double getWidth() {
return this.width;
}
public double getLength() {
return this.length;
}
public void setLW(double x, double y){
this.length = Math.max(x,y);
this.width = Math.min(x,y);
}
public double getArea() {
return this.length * this.width;
}
public String toString() {
String ret = "";
ret += "Width: " + this.width + "\n";
ret += "Length: " + this.length + "\n";
ret += "Area: " + getArea() + "\n";
ret += super.toString();
return ret;
}
}

37
Shape.java

@ -0,0 +1,37 @@
public class Shape{
private boolean isFilled;
private String color;
public Shape() {
this.isFilled = true;
this.color = "Green";
}
public Shape(boolean isFilled,String color){
this.isFilled = isFilled;
this.color = color;
}
public void setColor(String color) {
this.color = color;
}
public void setIsFilled(boolean isFilled){
this.isFilled = isFilled;
}
public String getColor() {
return this.color;
}
public boolean getIsFilled(){
return this.isFilled;
}
public String toString() {
String ret = "";
ret += "Filled: "+this.isFilled + "\n";
ret += "Color: "+this.color + "\n";
return ret;
}
}

2
TestParseString.java

@ -7,7 +7,7 @@ public class TestParseString
System.out.println("Sum of all number in \"" + s1 + "\" is: ");
System.out.println(ps1.sumOfAll());
System.out.println();
String s2 = "120,8 7.3,6.4 0.1,8.0";
ParseString ps2 = new ParseString(s2);
System.out.println("Sum of all number in \"" + s2 + "\" is: ");

8
TestShape.java

@ -2,26 +2,24 @@ public class TestShape
{
public static void main(String[] args)
{
/*
Circle c1 = new Circle(2.67);
System.out.println("c1: ");
System.out.println(c1.toString());
System.out.println();
Circle c2 = new Circle(3, false, "Red");
System.out.println("c2: ");
System.out.println(c2.toString());
System.out.println();
Rectangle r1 = new Rectangle(3, 2, true, "Blue");
System.out.println("r1: ");
System.out.println(r1.toString());
System.out.println();
Rectangle r2 = new Rectangle(3.2, 4, false, "Red");
System.out.println("r2: ");
System.out.println(r2.toString());
*/
}
}

Loading…
Cancel
Save