In: Computer Science
6. Add code to the main method of your main class to do the following:
Run the program to make sure it works. The output should look something like this:
Testing Polygon constructor
Number of sides: 5
7. Create a Java class file for a RegularPolygon class.
8. Implement the RegularPolygon class.
9. Add code to the main method of your main class to do the following:
Run the program to make sure it works. The output should look something like this:
Testing Polygon constructor
Number of sides: 4
Testing RegularPolygon constructor
Number of sides: 5
Side length: 1.0
Perimeter: 5.0
Testing side length mutator
Number of sides: 5
Side length: 2.0
Perimeter: 10.0
10. Create a Java class file for a RegularTriangle class.
11. Implement the RegularTriangle class.
RegularPolygon.
12. Add code to the main method of your main class to do the following:
Run the program to make sure it works. The output should look something like this:
Testing Polygon constructor
Number of sides: 4
Testing RegularPolygon constructor
Number of sides: 5
Side length: 1.0
Perimeter: 5.0
Testing side length mutator
Number of sides: 5
Side length: 2.0
Perimeter: 10.0
Testing RegularTriangle constructor
Number of sides: 3
Side length: 4.0
Perimeter: 12.0
Height: 3.4641016151377544
Area: 6.928203230275509
Testing height mutator
Number of sides: 3
Side length: 3.464101615137755
Perimeter: 10.392304845413264
Height: 3.0
Area: 5.196152422706632
Testing side length mutator
Number of sides: 3
Side length: 4.0
Perimeter: 12.0
Height: 3.4641016151377544
Area: 6.928203230275509
I will be typing all the classes here. I am sure that you will be able to make seperate class files for each of them.
1. The Polygon class :
public class Polygon {
private int sides; //number of sides of polygon
//constructor with no parameters
Polygon() {
sides = 3; // any default Polyggon will have 3 sides
}
//constructor with sides as parameter
Polygon(int sides) {
if(sides < 3 ) {
System.out.println("No polygon can be created with number of sides less than 3");
System.exit(0);
}
this.sides = sides;
}
// accessor for number of sides
public int getSides() {
return sides;
}
// Overriden to string method
@Override
public String toString() {
return "Number of sides: " + getSides();
}
}
2. The RegularPolygon class:
public class RegularPolygon extends Polygon {
private double length; //length of the regular polygon
//constructor with no parameter
RegularPolygon() {
super();
length = 1.0; // any defualt regular polygon will have length 1.0 unit
}
//constructor with sides and length as parameter
RegularPolygon(int sides, double length) {
super(sides);
this.setLength(length);
}
//accessor for length of sides
public double getLength() {
return length;
}
//mutator for length of sides
public void setLength(double length) {
if(length <= 0) {
System.out.println("Length of sides of a regular polygon must be greater than 0");
System.exit(1);
}
this.length = length;
}
//method to calculate perimeter
public double getPerimeter() {
return getSides() * getLength();
}
// Overriden to string method
@Override
public String toString() {
return super.toString() + "\nSide length: " + getLength() + "\n" + "Perimeter: " + getPerimeter();
}
}
3. The RegularTriangle class:
public class RegularTriangle extends RegularPolygon {
private double height; //height of a triangle
//constructor with no parameter
RegularTriangle() {
super();
height = (Math.sqrt(3.0)/2);
}
//construstor with length of side as parameter
RegularTriangle(double length) {
super(3, length);
}
//overrided mutator
@Override
public void setLength(double length) {
super.setLength(length);
height = (Math.sqrt(3.0)/2)*length;
}
//accessor for height
public double getHeight() {
return height;
}
//mutator for height
public void setHeight(double height) {
if(height <= 0) {
System.out.println("Height of a polygon can not be 0.");
System.exit(2);
}
this.height = height;
super.setLength((2/Math.sqrt(3.0))*height);
}
//method to get area
public double getArea() {
return 0.5 * getLength() * getHeight();
}
//Overriden to string method
@Override
public String toString() {
return super.toString() + "\nHeight: " + getHeight() + "\n" + "Area: " + getArea();
}
}
4. The Tester class containing the main method:
public class YourlastnameLab7 {
public static void main(String[] args) {
//1
System.out.println("Testing Polygon construnctor");
Polygon p1 = new Polygon(4);
System.out.println(p1);
//2
System.out.println("Testing RegularPolygon construnctor");
Polygon p2 = new RegularPolygon(5, 2.0);
System.out.println(p2);
//3
System.out.println("Testing RegularTriangle construnctor");
RegularTriangle t1 = new RegularTriangle(4.0);
System.out.println(t1);
//4
System.out.println("Testing height mutator");
t1.setHeight(3.0);
System.out.println(t1);
//5
System.out.println("Testing side length mutator");
t1.setLength(4.0);
System.out.println(t1);
}
}