Question

In: Computer Science

Start NetBeans. Create a new project called Lab7. Create a Java main class file using the...

  1. Start NetBeans.
  2. Create a new project called Lab7.
  3. Create a Java main class file using the class name YourlastnameLab7 with your actual last name.
  4. Create a Java class file for a Polygon class.
  5. Implement the Polygon class.
    1. Add a private instance variable representing the number of sides in the polygon.
    1. Add a constructor that takes a single argument and uses it to initialize the number of sides. If the value of the argument is less than three, display an error message and exit the program.
    1. Add an accessor method for the number of sides.
    1. Override the toString method of the Object class to return a String containing the number of sides.

6. Add code to the main method of your main class to do the following:

    1. Create a Polygon object.
    1. Pass the object to System.out.println() to display it.

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.

    1. Modify the class header to make RegularPolygon a subclass of Polygon.
    1. Add a private instance variable to represent the length of a side (a double).
    1. Add a constructor that takes an int and a double and uses them to initialize the private instance variables. If the length of the side is less than or equal to zero, display an error message, and exit the program.
    1. Add an accessor method and a mutator method for the length of a side.
    1. Move the code from the constructor that does the input validation and initialization of the length of a side to the mutator method. Replace the constructor code you just moved with a call to the mutator.
    1. Add a method called getPerimeter that calculates the perimeter of the polygon. Remember that all of the sides of a regular polygon have the same length.
    1. Override the toString method from the Polygon class to include the side length and perimeter along with the number of sides. Use the toString method from the base class to construct the first part of the string.

9. Add code to the main method of your main class to do the following:

    1. Create a RegularPolygon object and display it.
    1. Call the side length mutator, and display the object again.

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.

  1. Modify the class header to make RegularTriangle a subclass of

RegularPolygon.

    1. Add a private instance variable for the height of the triangle (a double).
    1. Add a constructor that takes a double representing the length of a side and uses the RegularPolygon constructor to initialize the number of sides and the side length. You do not need to initialize the height (you will see why in the next step.)
    1. Override the side length mutator so it calls the RegularPolygon mutator to initialize the side length, then sets the height to √3 × /2. This will take care of initializing the height since the constructor calls the RegularPolygon constructor, which calls this version of the side length mutator.
    1. Write an accessor method and a mutator method for the height. The mutator should display an error message and exit the program if the value provided is less than or equal to zero. It should also set the side length to 2 × ℎ      ℎ /√3.
    1. Write a method called getArea that calculates the area of the triangle using the formula = 1/2 × × ℎ      ℎ and returns it.
    1. Override the toString method to add information about the height and area.

12. Add code to the main method of your main class to do the following:

    1. Create a RegularTriangle object and display it.
    1. Call the height mutator, and display the object.
    1. Call the side length mutator, and display the object.

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

Solutions

Expert Solution

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);

    }

}


Related Solutions

in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑ Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. ☑ Add a method addToCollection. In this method,...
Part A Java netbeans ☑ Create a project and in it a class with a main....
Part A Java netbeans ☑ Create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class , after the package statement, paste import java.util.Scanner; As the first line inside your main method, paste Scanner scan = new Scanner(System.in); Remember when you are getting a value from the user, first print a request, then use the Scanner to read the right type...
Create a Netbeans project with a Java main class. (It does not matter what you name...
Create a Netbeans project with a Java main class. (It does not matter what you name your project or class.) Your Java main class will have a main method and a method named "subtractTwoNumbers()". Your subtractTwoNumbers() method should require three integers to be sent from the main method. The second and third numbers should be subtracted from the first number. The answer should be returned to the main method and then displayed on the screen. Your main() method will prove...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In my case the project would be called rghanbarPart1. Select the option to create a main method. Create a new class called Vehicle. In the Vehicle class write the code for: • Instance variables that store the vehicle’s make, model, colour, and fuel type • A default constructor, and a second constructor that initialises all the instance variables • Accessor (getters) and mutator (setters) methods...
android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called...
android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called DataBaseManager as in Lecture 5 and create a database table in SQLite, called StudentInfo. The fields for the StudentInfo table include StudentID, FirstName, LastName, YearOfBirth and Gender. Include functions for adding a row to the table and for retrieving all rows, similar to that shown in lecture 5. No user interface is required for this question, t -Continuing from , follow the example in...
Create a new Java project using NetBeans, giving it the name L-14. In java please Read...
Create a new Java project using NetBeans, giving it the name L-14. In java please Read and follow the instructions below, placing the code statements needed just after each instruction. Leave the instructions as given in the code. Declare and create (instantiate) an integer array called List1 that holds 10 places that have the values: 1,3,4,5,2,6,8,9,2,7. Declare and create (instantiate) integer arrays List2 and List3 that can hold 10 values. Write a method called toDisplay which will display the contents...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT