Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Instructions Start NetBeans. Create a new project called Lab7. Create a Java main class file using the class name YourlastnameLab7 with your actual last name.

Instructions

  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.
    2. 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.
    3. Add an accessor method for the number of sides.
    4. 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.
    2. Pass the object to System.out.println() to display it.
  1. the program to make sure it works. The output should look something like this:
  1. Polygon constructor Number of sides: 5
  1. Create a Java class file for a RegularPolygon class.
  2. Implement the RegularPolygon class.
    1. Modify the class header to make RegularPolygon a subclass of Polygon.
    2. Add a private instance variable to represent the length of a side (a double).
    3. 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.
    4. Add an accessor method and a mutator method for the length of a side.
    5. 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.
    6. 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.
    7. 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.
  3. Add code to the main method of your main class to do the following:
    1. Create a RegularPolygon object and display it.
    2. Call the side length mutator, and display the object again.
  1. the program to make sure it works. The output should look something like this:
  1. Polygon constructor Number of sides: 4 RegularPolygon constructor Number of sides: 5 Side length: 1.0 Perimeter: 5.0 side length mutator Number of sides: 5 Side length: 2.0 Perimeter: 10.0
  1. Create a Java class file for a RegularTriangle class.
  2. Implement the RegularTriangle class.
    1. Modify the class header to make RegularTriangle a subclass of RegularPolygon.
    2. Add a private instance variable for the height of the triangle (a double).
    3. 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.)
    4. Override the side length mutator so it calls the RegularPolygon mutator to initialize the side length, then sets the height to 3side/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.
    5. 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 2height/3.
    6. Write a method called getArea that calculates the area of the triangle using the formula area=1/2sideheight and returns it.
    7. Override the toString method to add information about the height and area.
  3. Add code to the main method of your main class to do the following:
    1. Create a RegularTriangle object and display it.
    2. Call the height mutator, and display the object.
    3. Call the side length mutator, and display the object.
  1. the program to make sure it works. The output should look something like this:
  1. Polygon constructor Number of sides: 4 RegularPolygon constructor Number of sides: 5 Side length: 1.0 Perimeter: 5.0 side length mutator Number of sides: 5 Side length: 2.0 Perimeter: 10.0 RegularTriangle constructor Number of sides: 3 Side length: 4.0 Perimeter: 12.0 Height: 3.4641016151377544 Area: 6.928203230275509 height mutator Number of sides: 3 Side length: 3.464101615137755 Perimeter: 10.392304845413264 Height: 3.0 Area: 5.196152422706632 side length mutator Number of sides: 3 Side length: 4.0 Perimeter: 12.0 Height: 3.4641016151377544 Area: 6.928203230275509
  1. Hand in the source files, YourlastnameLab7.java, Polygon.java, RegularPolygon.java, and RegularTriangle.java to the D2L assignment dropbox called Lab 7. The comments in each file should contain your name, CSCI 2011 Lab 7, and a description of what the class defined in the file does.

**PLEASE MAKE SURE YOU GO THROUGH THE STEPS CAREFULLY, ENSURING THE CODE WILL GIVE THE DESIRED OUTPUT. THANK YOU :)**

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions