Question

In: Computer Science

0. Introduction. In this lab assignment, you will extend some simple Java classes that represent plane...

0. Introduction.

In this lab assignment, you will extend some simple Java classes that represent plane figures from elementary geometry. The object of this assignment is not to do anything useful, but rather to demonstrate how methods can be inherited by extending classes.

1. Theory.

A polygon is a closed plane figure with three or more sides, all of which are line segments. The perimeter of a polygon is the sum of the lengths of its sides. A rectangle is a polygon with exactly four sides that meet at 90° angles. Like a polygon, it has a perimeter. It also has an area, the product of its base and height. A square is a rectangle whose sides are all the same length. Like a rectangle, it has a perimeter and an area.
      Polygons, rectangles, and squares make up an is-a hierarchy. The hierarchy gets its name because a square is-a rectangle, and a rectangle is-a polygon. Is-a hierarchies can be easily modeled by Java classes using the extends keyword.

2. Implementation.

The following is the source code for a Java class whose instances represent polygons. The file Polygon.java on Canvas contains a copy of this source code. You will need it to complete the laboratory assignment.

class Polygon
{
  private int[] sideLengths;

  public Polygon(int sides, int ... lengths)
  {
    int index = 0;
    sideLengths = new int[sides];
    for (int length: lengths)
    {
      sideLengths[index] = length;
      index += 1;
    }
  }

  public int side(int number)
  {
    return sideLengths[number];
  }

  public int perimeter()
  {
    int total = 0;
    for (int index = 0; index < sideLengths.length; index += 1)
    {
      total += side(index);
    }
    return total;
  }
}

The class Polygon uses a private array called sideLengths to store the lengths of a polygon’s sides. The array’s length, sideLengths.length, is the number of sides that the polygon has. The class Polygon also has a has a public constructor and two public methods. To keep things simple, they do not check their arguments for correctness, as they would if Polygon was part of a real program.
      The constructor takes four or more arguments and returns an instance of Polygon that represents a polygon. The first argument is the number of sides that the polygon has. The remaining arguments are the lengths of those sides. For example, the Java statement:

Polygon triangle = new Polygon(3, 3, 4, 5);

declares the variable triangle and sets it to an instance of Polygon that represents a triangle (because the first argument says it has 3 sides). The lengths of the triangle’s sides are 3, 4, and 5.
      The three dots ‘...’ in the constructor mean that it can take zero or more extra integer arguments after its first argument. The for-loop with the colon visits the extra arguments one at a time. Don’t worry if those parts of Java are unfamiliar. You don’t have to know how the constructor works, only how to call it.
      The method side returns the length of a polygon’s side. Sides are numbered starting from 0. For example, the expression triangle.side(0) returns 3, the expression triangle.side(1) returns 4, and the expression triangle.side(2) returns 5.
      The method perimeter returns a polygon’s perimeter, the sum of the lengths of its sides. For example, the expression triangle.perimeter() returns 3 + 4 + 5 = 12.
      For this assignment, you must write a class called Rectangle. As its name suggests, each instance of Rectangle must represent a rectangle. Along with a constructor, Rectangle must provide two methods, called area and perimeter. The method area must return the integer area of the rectangle, and the method perimeter must return the integer perimeter of the rectangle.
      You must also write another class, called Square. As its name suggests, each instance of Square must represent a square. Along with a constructor, Square must provide two methods, called area and perimeter. The method area must return the integer area of the square, and the method perimeter must return the integer perimeter of the square.
      The following driver program shows examples of how the constructors and methods of Rectangle and Square must work.

class Shapes
{
  public static void main(String[] args)
  {
    Rectangle wreck = new Rectangle(3, 5);  //  Make a 3 × 5 rectangle.
    System.out.println(wreck.area());       //  Print its area, 15.
    System.out.println(wreck.perimeter());  //  Print its perimeter, 16.

    Square nerd = new Square(7);            //  Make a 7 × 7 square.
    System.out.println(nerd.area());        //  Print its area, 49.
    System.out.println(nerd.perimeter()     //  Print its perimeter, 28.
  }
}

Your classes Rectangle and Square must use the extends keyword, so they will inherit methods from other classes. Also, each class must inherit as many of its methods as possible from those other classes. YOU WILL LOSE POINTS FOR DEFINING A METHOD INSIDE A CLASS, IF IT COULD HAVE BEEN INHERITED FROM ANOTHER CLASS!
      Here’s a hint about how to write the constructors for Rectangle and Square. Suppose that a class Triangle extends the class Polygon. Then Polygon is the superclass of Triangle. The keyword super can be used to call the constructor that belongs to a superclass. For example, Triangle’s constructor, which takes the lengths of a triangle’s three sides, might look like this.

public Triangle(int a, int b, int c)
{
  super(3, a, b, c);
}

It uses Polygon’s constructor to make a polygon with 3 sides, whose lengths are a, b, and c. If super is used in this way, then it must be the first statement in the constructor. You don’t have to write Triangle—this was only an example!

3. Deliverables.

The file tests6.java contains a driver class whose a main method performs 12 public tests, worth 1 point each. Each public test is a call to println, along with a comment that shows what it must print. To grade your work, the TA’s will run the public tests using your Rectangle and Square classes. If a public test behaves exactly as it should, then you will receive 1 point for it.
      In addition, the TA’s will do 5 private tests on your Rectangle and Square classes. You will not be told what the private tests are, but they are worth 2 points each, and they determine if Rectangle and Square have inherited as many methods as possible. The TA’s will do the same private tests for all students, and these tests will be made public only after all the work for this lab has been graded.
      Your score for this lab is the sum of the points you get for the public tests, and the points you get for the private tests, for a maximum of 22 points. Here is what you must turn in.

  1. Source code for the class Rectangle. Its instances must provide the methods side, area and perimeter. These methods are not necessarily defined in Rectangle: some or all may be inherited.

  2. Source code for the class Square. Its instances must provide the methods side, area and perimeter. These methods are not necessarily defined in Square: some or all may be inherited.

Solutions

Expert Solution

Here are the implemented classes, Rectangle extends Polygon class and Square extends Rectangle class.

I ran the Shapes driver class and the output is coming as expected,

Let me know for any questions or changes. Will be happy to assist you !!

===================================================================

public class Rectangle extends Polygon {
    public Rectangle(int length, int width) {
        super(4, length, width,length,width);
    }


    public int area() {
        return side(0) * side(1);
    }
}

===================================================================

public class Square extends Rectangle {


    public Square(int length){
        super(length,length);
    }


}

===================================================================


Related Solutions

For this lab, you will design classes to represent three simple geometric shapes: circles, rectangles, and...
For this lab, you will design classes to represent three simple geometric shapes: circles, rectangles, and triangles. Implement the classes for these three shapes in Java, each in a separate class file, according to the guidelines given below: The classes must include instance fields (also known as attributes or data members) for the information necessary to represent objects of these classes. Recall that the instance fields represent the properties of objects. The most fundamental property of a circle is its...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
For this assignment you will create a set of simple classes that model a cookbook. The...
For this assignment you will create a set of simple classes that model a cookbook. The cookbook will consist of set or recipes. Each recipe will consist of a set of ingredients and instructions. Your submission will consist of the below three classes and a test driver (the only class with a main()) that exercises the functionality of your system. You will need to implement the following classes based on the description of their attributes and operations: Ingredient name -...
Assignment 1: JAVA Classes, Objects, and Constructors The goal of this assignment is to get you...
Assignment 1: JAVA Classes, Objects, and Constructors The goal of this assignment is to get you familiar with some of the most used JAVA syntax, as well as constructors objects, objects calling other objects, class and instance attributes and methods. You will create a small program consisting of Musician and Song classes, then you will have Musicians perform the Songs. Included is a file “Assignment1Tester.java”. Once you have done the assignment and followed all instructions, you should be able to...
Lab 3 Java Classes and Memory Management Multi-file Project In this lab you will gain experience...
Lab 3 Java Classes and Memory Management Multi-file Project In this lab you will gain experience using classes and arrays of classes. Use the following as the beginning of a student abstract data type. public class Student { private String fName ; private String lName ; private double[] grades; } This class should be in the package com.csc241. Write a program that prompts a user for a total number of students and dynamically allocates memory for just that number of...
#2 Extend Employee and Manager classes created in lab#3. § Replace field year by the field...
#2 Extend Employee and Manager classes created in lab#3. § Replace field year by the field hireDate of type java.util.Date § Your classes should implement Comparable interface. (Employee1 > Employee2 if its salary is more than the salary of Employee2, the same for managers, but if their salaries are equal, compare by bonus). (java oop)-> laboratory work lab3# Create a class called Employee whose objects are records for an employee. This class will be a derived class of the class...
*JAVA* For this assignment you have been given two classes, a Main.java and a Coin.java. The...
*JAVA* For this assignment you have been given two classes, a Main.java and a Coin.java. The coin class represents a coin. Any object made from it will have a 1) name, 2) weight and 3) value. As of now, the instance variables in Coin.java are all public, and the main function is calling these variables directly for the one coin made in it. Your goal is to enforce information hiding principles in this project. Take Coin.java, make all instance variables...
I need this in java A6 – Shipping Calculator Assignment Introduction In this part, you will...
I need this in java A6 – Shipping Calculator Assignment Introduction In this part, you will solve a problem described in English. Although you may discuss ideas with your classmates, etc., everyone must write and submit their own version of the program. Do NOT use anyone else’s code, as this will result in a zero for you and the other person! Shipping Calculator Speedy Shipping Company will ship your package based on the weight and how far you are sending...
Programmed In Java In this assignment you will create a simple game. You will put all...
Programmed In Java In this assignment you will create a simple game. You will put all of your code in a class called “Game”. You may put all of your code in the main method. An example of the “game” running is provided below. Y ou will start by welcoming the user. 1. You should print "Welcome! Your starting coordinates are (0, 0).” 2. On the next line, you will tell the user the acceptable list of commands. This should...
Background: introduction to "Wrapper" classes, this is a tool or concept provided in the Java Programming...
Background: introduction to "Wrapper" classes, this is a tool or concept provided in the Java Programming Language that gives us a way to utilize native datatypes as Objects with attributes and methods. A Wrapper class in Java is the type of class which contains or the primitive data types. When a wrapper class is created a new field is created and in that field, we store the primitive data types. It also provides the mechanism to covert primitive into object...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT