Question

In: Computer Science

Given the following UML class diagram, implement the class as described by the model. Use your...

Given the following UML class diagram, implement the class as described by the model. Use your best judgment when implementing the code inside of each method.

+---------------------------------------------------+
| Polygon |
+---------------------------------------------------+
| - side_count : int = 3 |
| - side_length : double = 1.0 |
+---------------------------------------------------+
| + Polygon() |
| + Polygon(side_count : int) |
| + Polygon(side_count : int, side_length : double) |
| + getSides() : int |
| + setSides(side_count : int) |
| + getSideLength() : double |
| + setSideLength(side_length : double) |
| + perimeter() : double |
+---------------------------------------------------+

The following information might also be useful:

- The perimeter of a polygon is the sum of it's side lengths.
- Side counts of less than 3 are meaningless for a polygon, and should not be accepted.
- Side lengths of less than 0 are meaningless for a polygon, and should not be accepted.

Solutions

Expert Solution


class Polygon {
   private int side_count;
   private double side_length;

   public Polygon() {

       side_count = 3;
       side_length = 1;
   }

   public Polygon(int s) {
       //checking if values are valid
       if (s >= 3)
           side_count = s;
       side_length = 1;
   }

   public Polygon(int s, double l) {
       //checking if values are valid
       if (s >= 3 && l >= 1) {
           side_count = s;
           side_length = l;
       }
   }

   public int getSides() {
       return side_count;
   }

   public void setSides(int s) {
       //checking if values are valid
       if (s >= 3)
           side_count = s;
   }

   public double getSideLength() {
       return side_length;
   }

   public void setSideLength(double l) {
       //checking if values are valid
       if (l >= 1)
           side_length = l;
   }

   //sides * length
   public double perimeter() {
       return side_count * side_length;
   }
}

public class TestPolygon {
   public static void main(String[] args) {
       Polygon p= new Polygon(5, 15);
       System.out.println("Perimeter : "+p.perimeter());
   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

Given the following UML class diagram, implement the class as described by the model. Use your best judgement when implementing the code inside of each method.
In java Given the following UML class diagram, implement the class as described by the model. Use your best judgement when implementing the code inside of each method.+------------------------------+| Circle |+------------------------------+| - radius : double = 1.0 |+------------------------------+| + Circle(radius : double) || + getRadius() : double || + setRadius(radius : double) || + area() : double || + perimeter() : double |+------------------------------+The following information might also be useful:Formula for area of a circle with radius r:       A = πr^2Formula for...
create the UML Diagram to model a Movie/TV viewing site. Draw a complete UML class diagram...
create the UML Diagram to model a Movie/TV viewing site. Draw a complete UML class diagram which shows: Classes (Only the ones listed in bold below) Attributes in classes (remember to indicate privacy level, and type) No need to write methods Relationships between classes (has is, is a, ...) Use a program like Lucid Cart and then upload your diagram. Each movie has: name, description, length, list of actors, list of prequals and sequals Each TV Show has: name, description,...
Write two classes, ToDoList and Driver ToDoList should implement the following UML class diagram: ToDoList -list:String[]...
Write two classes, ToDoList and Driver ToDoList should implement the following UML class diagram: ToDoList -list:String[] -size:int +ToDoList() +add(String item):void +remove(String item): boolean +toString():String add(String item) should add the item as the last element in the list, updating size. remove(String item) should remove the item and return true, or, if the item was not in the list, return false.   To remove the item, check every list element from 0 to size-1, and if that item is equal to the parameter...
Given the following specification, design a class diagram using PlantUML. To design the class diagram, use...
Given the following specification, design a class diagram using PlantUML. To design the class diagram, use abstract, static, package, namespace, association, and generalization on PlantUML Specification: A school has a principal, many students, and many teachers. Each of these persons has a name, birth date, and may borrow and return books. The book class must contain a title, abstract, and when it is available. Teachers and the principal are both paid a salary. A school has many playgrounds and rooms....
Complete the following class UML design class diagram by filling in all the sections based on...
Complete the following class UML design class diagram by filling in all the sections based on the information given below.         The class name is Boat, and it is a concrete entity class. All three attributes are private strings with initial null values. The attribute boat identifier has the property of “key.” The other attributes are the manufacturer of the boat and the model of the boat. Provide at least two methods for this class. Class Name: Attribute Names: Method...
Draw a UML diagram for the classes. Code for UML: // Date.java public class Date {...
Draw a UML diagram for the classes. Code for UML: // Date.java public class Date {       public int month;    public int day;    public int year;    public Date(int month, int day, int year) {    this.month = month;    this.day = day;    this.year = year;    }       public Date() {    this.month = 0;    this.day = 0;    this.year = 0;    } } //end of Date.java // Name.java public class Name...
For this assignment, create a complete UML class diagram of this program. You can use Lucidchart...
For this assignment, create a complete UML class diagram of this program. You can use Lucidchart or another diagramming tool. If you can’t find a diagramming tool, you can hand draw the diagram but make sure it is legible. Points to remember: • Include all classes on your diagram. There are nine of them. • Include all the properties and methods on your diagram. • Include the access modifiers on the diagram. + for public, - for private, ~ for...
A UML class diagram has the following description about a member of the class: + getHeaderField(name:String):String...
A UML class diagram has the following description about a member of the class: + getHeaderField(name:String):String Which of the following is a correct declaration of the member in Java? 1 public String getHeaderField(String name) 2 public String getHeaderField; 3 public String getHeaderField(String) 4 public static String getHeaderField( name) ------------------------------------------------------------------------------------------------------------------------------------- Note: all answers are case sensitive. Inside a class, the declaration of a constructor looks like the following: public JButton(String text) 1. From the constructor, you infer that the name of...
1: (Passing Objects to Methods) From the following UML Class Diagram, define the Circle class in...
1: (Passing Objects to Methods) From the following UML Class Diagram, define the Circle class in Java. Circle Circle Radius: double Circle() Circle(newRadius: double) getArea(): double setRadius(newRadius: double): void getRadius(): double After creating the Circle class, you should test this class from the main() method by passing objects of this class to a method “ public static void printAreas(Circle c, int times)” You should display the area of the circle object 5 times with different radii.
1: (Passing Objects to Methods) From the following UML Class Diagram, define the Circle class in...
1: (Passing Objects to Methods) From the following UML Class Diagram, define the Circle class in Java. Circle Circle Radius: double Circle() Circle(newRadius: double) getArea(): double setRadius(newRadius: double): void getRadius(): double After creating the Circle class, you should test this class from the main() method by passing objects of this class to a method “ public static void printAreas(Circle c, int times)” You should display the area of the circle object 5 times with different radii.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT