Question

Question 1- part-1) Provide complete UML class diagrams for the Polygon and Rectangle classes, including access...

Question 1- part-1) Provide complete UML class diagrams for the Polygon and Rectangle classes, including access specifiers, parameters, and data/return types. Include the relationship between these two classes in the diagram. And then,

Question 1-part-2) Given the following reference variable declaration and object assignment:

Polygon poly = new Rectangle();

What would happen given each of the following two lines of code? Briefly explain your answer.

System.out.println(poly);
double area = poly.getArea();

Then tell me what is the major difference between abstract classes and regular classes? What does this difference prevent you from doing with abstract classes?

Then what is a functional interface? What is a lambda expression? Provide a simple example with code to define a functional interface and with a single line of code to construct the corresponding lambda expression.

And then last but not least. What are the purposes of polymorphism and interfaces in implementing real-world software applications?

You must complete all parts of question 1 in order to receive full credit, failing to do so will result in a zero. Good luck.

note for Cheg: Please do these real carefully I have uploaded questions like this before, they are very hard, and Cheg people always give me the wrong answer. Please take your time on doing this and attempt to get it correct within the first try.

public class Polygon {
      private double length;
      private double height;
      private int sides;

      public Polygon() {
            length = 1.0;
            height = Math.sqrt(3.0) / 2;
            sides = 3;
      }

      public Polygon(double length, double height, int sides) {
            this.length = length;
            this.height = height;
            this.sides = sides;
      }

      public void setLength(double length) {
            this.length = length;
      }

      public void setHeight(double height) {
            this.height = height;
      }

      public void setSides(int sides) {
            this.sides = sides;
      }

      public double getLength() {
            return length;
      }

      public double getHeight() {
            return height;
      }

      public int getSides() {
            return sides;
      }


      public String toString() {
            return "This is a polygon";
      }
}

Rectangle.java

public class Rectangle extends Polygon {
      public Rectangle() {
            this.setLength(1.0);
            this.setHeight(1.0);
            this.setSides(4);
      }

      public Rectangle(double length, double height) {
            this.setLength(1.0);
            this.setHeight(1.0);
      }

      public double getArea() {
            return this.getLength() * this.getHeight();
      }

      public String toString() {
            return "This is a rectangle";
      }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Q1) part2

       Polygon poly = new Rectangle();
      
       System.out.println(poly);

The toString method will be overrided by the Rectangle class and print the returned by the toString() method.

this is called Up casting in the oops concept.

and the second statement

double area = poly.getArea();

this will give compilation error because there is no method name getArea() declared in the Polygon class

we need to down cast this reference variable to Rectangle the getArea() method will be accessed.

double area = ((Rectangle) poly).getArea();

The Major difference in abstract class and concrete class is , we can create the object of concrete class or regular class but we can not create the object or we can't instantiate the abstract class but we can assign the object reference to its type;

For example

if we want to make Polygon class as abstract than

public abstract class Polygon {
   private double length;
   private double height;
   private int sides;

   public Polygon() {
       length = 1.0;
       height = Math.sqrt(3.0) / 2;
       sides = 3;
   }

   public Polygon(double length, double height, int sides) {
       this.length = length;
       this.height = height;
       this.sides = sides;
   }

   public void setLength(double length) {
       this.length = length;
   }

   public void setHeight(double height) {
       this.height = height;
   }

   public void setSides(int sides) {
       this.sides = sides;
   }

   public double getLength() {
       return length;
   }

   public double getHeight() {
       return height;
   }

   public int getSides() {
       return sides;
   }

   public String toString() {
       return "This is a polygon";
   }

public abstract double getArea(); //abstract method


}

now every subclass need to implement the getArea() method of Polygon class

Now the statement will not give the compilation error

double area = poly.getArea();

A functional interface is an interface that contains only one abstract method.

A functional interface can have any number of default methods.

lambda expressions can be used to represent the instance of a functional interface.

For example


interface Tripe {

   int triple(int x);
}

public class FuntionalTest {

   public static void main(String[] args) {

       int a = 5;

       // lambda expression to define the triple method
       Tripe t = (int x) -> 3 * x;

       int result = t.triple(a);
       System.out.println(result);
   }
}

The purpose of interface and polymorphism is to provide multiple behavior by single reference variable as its subclass type object. and Programmer can easily understand the code and what should be implemented in the subclass .

Please refer the Polygon class to understand this concept.

Please let me know if you have any doubt or modify the answer, Thanks :)

Add a comment
Know the answer?
Add Answer to:
Question 1- part-1) Provide complete UML class diagrams for the Polygon and Rectangle classes, including access...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Modify the code to create a class called box, wherein you find the area. I...

    JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width;       public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...

  • The current code I have is the following: package uml; public class uml {        public...

    The current code I have is the following: package uml; public class uml {        public static void main(String[] args) {              // TODO Auto-generated method stub        } } class Account { private String accountID; public Account(String accountID) { this.accountID = accountID; } public String getAccountID() { return accountID; } public void setAccountID(String accountID) { this.accountID = accountID; } @Override public String toString() { return "Account [accountID=" + accountID + "]"; } } class SuppliesAccount extends Account { private...

  • Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they...

    Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they both say: constructor Shape in class Shape cannot be applied to given types; required: no arguments found: String reason: actual and formal argument lists differ in length The directions say: The files Shape.java and TestArea.java have been finished already, YOU DONT ADD ANYTHING TO THESE TWO. According to the requirement, you need to modify in Square.java and Rectangle.java, respectively a. Implementing constructor with no...

  • Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override...

    Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override the toString method for Rectangle. Override the equals method for Rectangle. Implement the comparable Interface for Rectangle (Compare by area) Rectangle class: public class Rectangle {       private double length;    private double width;       public Rectangle(double l, double w)    {        length = l;        width = w;    }       public double getLength()    {   ...

  • Question: Modify the Rectangle and Box classes that are below, so that they implement the Comparable...

    Question: Modify the Rectangle and Box classes that are below, so that they implement the Comparable interface. The compareTo() method shall compare rectangles and boxes by areas and volumes. In main(), create an ArrayList of rectangles and an ArrayList of boxes. Randomly generate ten rectangles and ten boxes adding to the two ArrayLists. Use Collections.sort() to sort the two ArrayLists, respectively. Print the ArrayLists before and after being sorted. Three classes that need to be edited are below: Main class,...

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

  • I need help with a java error Question: Consider a graphics system that has classes for...

    I need help with a java error Question: Consider a graphics system that has classes for various figures—say, rectangles, boxes, triangles, circles, and so on. For example, a rectangle might have data members’ height, width, and center point, while a box and circle might have only a center point and an edge length or radius, respectively. In a well-designed system, these would be derived from a common class, Figure. You are to implement such a system. The class Figure is...

  • I need help with a java error Question: Consider a graphics system that has classes for...

    I need help with a java error Question: Consider a graphics system that has classes for various figures—say, rectangles, boxes, triangles, circles, and so on. For example, a rectangle might have data members’ height, width, and center point, while a box and circle might have only a center point and an edge length or radius, respectively. In a well-designed system, these would be derived from a common class, Figure. You are to implement such a system. The class Figure is...

  • Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g...

    Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g GeometricObject.java GeometricObject.java:92: error: class, interface, or enum expected import java.util.Comparator; ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. 20.21 Please code using Java IDE. Please DO NOT use Toolkit. You can use a class for GeometricObject. MY IDE does not have access to import ToolKit.Circle;import. ToolKit.GeometricObject;.import ToolKit.Rectangle. Can you code this without using the ToolKit? Please show an...

  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() {    this.numOffspring = 0; } public Person (int numOffspring) {    this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) {    super(name, birthYear, weight, height, gender, numCarryOn);       if(numOffspring < 0) {        this.numOffspring = 0;    }    this.numOffspring = numOffspring; } public int getNumOffspring() {   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT