Question

In Lab 5, you completed the MyRectangle class, which is a simple representation of a rectangle....

In Lab 5, you completed the MyRectangle class, which is a simple representation of a rectangle. Review Lab 5 before completing this lab and retrieve the MyRectangle class that you completed for this lab, since you will need it again in this lab. Before starting this lab, edit your MyRectangle class in the following way:

• change the declaration of your instance variables from private to protected

This will enable access of these variables by your MySquare subclass.

Here is my rectangle code :

// CS 401 Lab 05
// Fill in the indicated code sections to complete this class.  Then test it
// by compiling and executing Lab05.java.

public class MyRectangle
{
        // Declare instance variables here.  See toString() method below for names.

        public MyRectangle()
        {
                // Default constructor -- initialize all instance variables
                // to 0
        }

        public MyRectangle(int x, int y, int w, int h)
        {
                // Initialize instance variables based on parameters shown.
                // Be careful to get the order correct!
        }

        public int area()
        {
                // Return the area of this Rectangle
        }

        // I have written this method for you.  Note how a StringBuilder is
        // utilized, since (as we discussed in lecture) it can be modified
        // without having to create a new object each time (unlike a String).
        public String toString()
        {
                StringBuilder S = new StringBuilder();
                S.append("Width: " + width);
                S.append(" Height: " + height);
                S.append(" X: " + startX);
                S.append(" Y: " + startY);
                return S.toString();
        }

        public boolean isInside(int x, int y)
        {
                // This is the trickiest of the methods to write.  This should
                // return true if point (x,y) is anywhere within the borders of the
                // current MyRectangle (including the borders themselves).  Use a
                // pencil and paper to figure out how this can be determined with
                // just a few simple relational operations.
        }

        public void setSize(int w, int h)
        {
                // Update width and height as shown
        }

        public void setPosition(int x, int y)
        {
                // Update startX and startY as shown
        }

// rest of code

public class Lab05
{
        public static void testInside(MyRectangle R, int x, int y)
        {
                System.out.print("Point (" + x + "," + y + ")");
                if (R.isInside(x, y))
                        System.out.println(" is INSIDE " + R);
                else
                        System.out.println(" is OUTSIDE " + R);
        }

        public static void main(String [] args)
        {
                MyRectangle R1, R2, R3;

                R1 = new MyRectangle(100, 50, 80, 20);
                R2 = new MyRectangle();
                R3 = new MyRectangle(60, 60, 100, 100);
 
                // In Java, when Objects are printed (as shown below), the toString()
                // method is implicitly called.  Thus the statements below will call
                // toString() for each of the three MyRectangle objects
                System.out.println("R1: " + R1 + " Area: " + R1.area());
                System.out.println("R2: " + R2 + " Area: " + R2.area());
                System.out.println("R3: " + R3 + " Area: " + R3.area());

                int x1 = 120, y1 = 70;
                int x2 = 130, y2 = 110;

                // Verify with a pencil and paper which of these should be true and
                // which should be false.
                testInside(R1, x1, y1);
                testInside(R3, x1, y1);
                testInside(R1, x2, y2);
                testInside(R3, x2, y2);

                R1.setSize(120, 240);
                R3.setPosition(400, 350);
                testInside(R1, x2, y2);
                testInside(R3, x2, y2);
        }
}

What to Do?

In this lab, you will implement a new class with the following header:

public class MySquare extends MyRectangle

Your MySquare class should NOT have any new instance variables. If you think about it, you

will see that the instance variables already present in MyRectangle are sufficient. Note that

since you just changed the declarations of your instance variables to protected, a

subclass (MySquare) can have a direct access to the instance variables of the superclass

(MyRectangle). For this lab, you will need some new methods in your MySquare class as follows:

• public MySquare(int x, int y, int side): This is a constructor that allows new objects

to be created. x and y are the location coordinates and side is the side length.

• public MySquare(): This is a constructor that sets location (x,y) to (0,0) and side to 0.

• public String toString(): See output for effect of the toString() method.

• public void setSize(int w, int h): Redefine the setSize() method. This must be done

because the inherited version allows the width and height to difer but in a square they must

be the same. In this version, if the width and height are not the same, the method should

output a message and not change anything.

• public void setSide(int side): This is a new method that updates the size of the square.

Think about how this will be implemented using the existing inherited instance variables.

Your MySquare class must run with the Lab9.java program with no changes. To see how your

output should look, see the file lab9out.txt. Note that there are some methods that are utilized

in Lab9.java which are inherited and which you should not redefine in your MySquare class. See

details in the comments in Lab9.java.

// CS 0401 Lab 9 Driver Program
// Compile and run this program to test your MySquare class.
// Compare the output of your program with the provided file 
// lab8out.txt

public class Lab9
{
        // Note that this method has a MyRectangle parameter.   This should allow
        // it to work with the MyRectangle class or the MySquare class (because
        // MySquare ISA MyRectangle).  In other words, it only works with MySquare
        // because MySquare is a subclass of MyRectangle.  Finally, note that the
        // isInside() method can be used as is from MyRectangle -- you do not have
        // to redefine it for MySquare
        public static void showRec(MyRectangle R, int width, int height)
        {
                for (int i = 0; i < height; i++)
                {
                        for (int j = 0; j < width; j++)
                        {
                                if (R.isInside(i, j))
                                        System.out.print("+");
                                else
                                        System.out.print("-");
                        }
                        System.out.println();
                }
        }
        
        public static void main(String [] args)
        {
                MySquare S1, S2, S3;

                // Note the MySquare constructors.  You must write these.
                S1 = new MySquare(10, 20, 25);
                S2 = new MySquare();
                S3 = new MySquare(0, 0, 150);
 
                // The area() should be inherited -- you should not redefine it.  However
                // you must redefine the toString() method so that it outputs properly.
                System.out.println("S1: " + S1 + " Area: " + S1.area());
                System.out.println("S2: " + S2 + " Area: " + S2.area());
                System.out.println("S3: " + S3 + " Area: " + S3.area());

                showRec(S1, 80, 50);
                
                System.out.println();
                S1.setSide(15);                 // new method in MySquare class
                S1.setPosition(0, 0);   // inherited method
                
                System.out.println("S1: " + S1 + " Area: " + S1.area());
                showRec(S1, 80, 50);
                
                // The setSize() method is inherited from MyRectangle but as given it is
                // incorrect since the width and height can be assigned to different values.
                // Thus you should redefine the same method (with the same header) in the
                // MySquare class so that it makes the assignment only if the width and height
                // are the same (since the object is a square).  This is called method
                // overriding and we will discuss it soon in lecture.
                
                S1.setSize(20, 40);
                S1.setSize(20, 20);
                System.out.println("S1: " + S1 + " Area: " + S1.area());
        }
}

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

Solution:

MyRectangle.java

public class MyRectangle {
        // Declare instance variables here.  See toString() method below for names.
        protected double width;
        protected double height;
        protected double startX;
        protected double startY;

        public MyRectangle()
        {
            // Default constructor -- initialize all instance variables to 0
            width = 0;
            height = 0;
            startX = 0;
            startY = 0;
        }

        public MyRectangle(int x, int y, int w, int h)
        {
            // Initialize instance variables based on parameters shown.
            startX = x;
            startY = y;
            width = w;
            height = h;
        }

        public int area()
        {
            return (int)(width * height);
        }

        // I have written this method for you.  Note how a StringBuilder is
        // utilized, since (as we discussed in lecture) it can be modified
        // without having to create a new object each time (unlike a String).
        public String toString()
        {
            StringBuilder S = new StringBuilder();
            S.append("Width: " + width);
            S.append(" Height: " + height);
            S.append(" X: " + startX);
            S.append(" Y: " + startY);
            return S.toString();
        }

        public boolean isInside(int x, int y)
        {
            // This is the trickiest of the methods to write.  This should
            // return true if point (x,y) is anywhere within the borders of the
            // current MyRectangle (including the borders themselves).  Use a
            // pencil and paper to figure out how this can be determined with
            // just a few simple relational operations.

            if(x > startX && x < width && y > startY && y < height)
                return true;

            return false;
        }

        public void setSize(int w, int h)
        {
            // Update width and height as shown
            width = w;
            height = h;
        }

        public void setPosition(int x, int y)
        {
            // Update startX and startY as shown
            startX = x;
            startY = y;
        }
}

Lab05.java

public class Lab05 {
    public static void testInside(MyRectangle R, int x, int y)
    {
        System.out.print("Point (" + x + "," + y + ")");
        if (R.isInside(x, y))
            System.out.println(" is INSIDE " + R);
        else
            System.out.println(" is OUTSIDE " + R);
    }

    public static void main(String [] args)
    {
        MyRectangle R1, R2, R3;

        R1 = new MyRectangle(100, 50, 80, 20);
        R2 = new MyRectangle();
        R3 = new MyRectangle(60, 60, 100, 100);

        // In Java, when Objects are printed (as shown below), the toString()
        // method is implicitly called.  Thus the statements below will call
        // toString() for each of the three MyRectangle objects
        System.out.println("R1: " + R1 + " Area: " + R1.area());
        System.out.println("R2: " + R2 + " Area: " + R2.area());
        System.out.println("R3: " + R3 + " Area: " + R3.area());

        int x1 = 120, y1 = 70;
        int x2 = 130, y2 = 110;

        // Verify with a pencil and paper which of these should be true and
        // which should be false.
        testInside(R1, x1, y1);
        testInside(R3, x1, y1);
        testInside(R1, x2, y2);
        testInside(R3, x2, y2);

        R1.setSize(120, 240);
        R3.setPosition(400, 350);
        testInside(R1, x2, y2);
        testInside(R3, x2, y2);
    }
}

Output:

RI: Width: 80.0 Height: 20.0 X: ї00 0 Y: 50.0 Area : 1600 R2 : Width: 0.0 Height : 0,0 X: 0.0 Y: 0.0 Area: 0 R3: Width: 100.0

MySquare.java

public class MySquare extends MyRectangle{

    public MySquare(int x, int y, int side){
        this.startX = x;
        this.startY = y;
        this.width = side;
        this.height = side;
    }

    public MySquare(){
        startX = 0;
        startY = 0;
        width = 0;
        height = 0;
    }

    public String toString(){
        StringBuilder S = new StringBuilder();
        S.append("Width: " + width);
        S.append(" Height: " + height);
        S.append(" X: " + startX);
        S.append(" Y: " + startY);
        return S.toString();
    }

    public void setSize(int w, int h){
        if(w != h){
            System.out.printf("\n\n*****Value of 'w' and 'h' must be same******\n\n");
        }
        else {
            width = w;
            height = h;
        }
    }

    public void setSide(int side){
        width = side;
        height = side;
    }
}

Lab9.java

public class Lab9 {
    // Note that this method has a MyRectangle parameter.   This should allow
    // it to work with the MyRectangle class or the MySquare class (because
    // MySquare ISA MyRectangle).  In other words, it only works with MySquare
    // because MySquare is a subclass of MyRectangle.  Finally, note that the
    // isInside() method can be used as is from MyRectangle -- you do not have
    // to redefine it for MySquare
    public static void showRec(MyRectangle R, int width, int height)
    {
        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                if (R.isInside(i, j))
                    System.out.print("+");
                else
                    System.out.print("-");
            }
            System.out.println();
        }
    }

    public static void main(String [] args)
    {
        MySquare S1, S2, S3;

        // Note the MySquare constructors.  You must write these.
        S1 = new MySquare(10, 20, 25);
        S2 = new MySquare();
        S3 = new MySquare(0, 0, 150);

        // The area() should be inherited -- you should not redefine it.  However
        // you must redefine the toString() method so that it outputs properly.
        System.out.println("S1: " + S1 + " Area: " + S1.area());
        System.out.println("S2: " + S2 + " Area: " + S2.area());
        System.out.println("S3: " + S3 + " Area: " + S3.area());

        showRec(S1, 80, 50);

        System.out.println();
        S1.setSide(15);                 // new method in MySquare class
        S1.setPosition(0, 0);   // inherited method

        System.out.println("S1: " + S1 + " Area: " + S1.area());
        showRec(S1, 80, 50);

        // The setSize() method is inherited from MyRectangle but as given it is
        // incorrect since the width and height can be assigned to different values.
        // Thus you should redefine the same method (with the same header) in the
        // MySquare class so that it makes the assignment only if the width and height
        // are the same (since the object is a square).  This is called method
        // overriding and we will discuss it soon in lecture.

        S1.setSize(20, 40);
        S1.setSize(20, 20);
        System.out.println("S1: " + S1 + " Area: " + S1.area());
    }
}

Output:

S1: Width: 25.0 Height: 25.0 X: 10.0 Y: 20.0 Area: 625
S2: Width: 0.0 Height: 0.0 X: 0.0 Y: 0.0 Area: 0
S3: Width: 150.0 Height: 150.0 X: 0.0 Y: 0.0 Area: 22500
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
---------------------++++-------------------------------------------------------
---------------------++++-------------------------------------------------------
---------------------++++-------------------------------------------------------
---------------------++++-------------------------------------------------------
---------------------++++-------------------------------------------------------
---------------------++++-------------------------------------------------------
---------------------++++-------------------------------------------------------
---------------------++++-------------------------------------------------------
---------------------++++-------------------------------------------------------
---------------------++++-------------------------------------------------------
---------------------++++-------------------------------------------------------
---------------------++++-------------------------------------------------------
---------------------++++-------------------------------------------------------
---------------------++++-------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

S1: Width: 15.0 Height: 15.0 X: 0.0 Y: 0.0 Area: 225
--------------------------------------------------------------------------------
-++++++++++++++-----------------------------------------------------------------
-++++++++++++++-----------------------------------------------------------------
-++++++++++++++-----------------------------------------------------------------
-++++++++++++++-----------------------------------------------------------------
-++++++++++++++-----------------------------------------------------------------
-++++++++++++++-----------------------------------------------------------------
-++++++++++++++-----------------------------------------------------------------
-++++++++++++++-----------------------------------------------------------------
-++++++++++++++-----------------------------------------------------------------
-++++++++++++++-----------------------------------------------------------------
-++++++++++++++-----------------------------------------------------------------
-++++++++++++++-----------------------------------------------------------------
-++++++++++++++-----------------------------------------------------------------
-++++++++++++++-----------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


*****Value of 'w' and 'h' must be same******

S1: Width: 20.0 Height: 20.0 X: 0.0 Y: 0.0 Area: 400

Add a comment
Know the answer?
Add Answer to:
In Lab 5, you completed the MyRectangle class, which is a simple representation of a rectangle....
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
  • For this question you must write a java class called Rectangle and a client class called...

    For this question you must write a java class called Rectangle and a client class called RectangleClient. The partial Rectangle class is given below. (For this assignment, you will have to submit 2 .java files: one for the Rectangle class and the other one for the RectangleClient class and 2 .class files associated with these .java files. So in total you will be submitting 4 files for part b of this assignment.) // A Rectangle stores an (x, y) coordinate...

  • Making a rectangle class in java write a simple class that will represent a rectangle. Later...

    Making a rectangle class in java write a simple class that will represent a rectangle. Later on, we will see how to do this with graphics, but for now, we will just have to use our imaginations for the visual representations ofWthe rectangles. Instance Variables Recall that an object is constructed using a class as its blueprint. So, think about an rectangle on your monitor screen. To actually draw a rectangle on your monitor screen, we need a number of...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • package rectangle; public class Rectangle {    private int height;    private int width;    public...

    package rectangle; public class Rectangle {    private int height;    private int width;    public Rectangle(int aHeight, int aWidth) {    super();    height = aHeight;    width = aWidth;    }    public int getHeight() {    return height;    }    public int getWidth() {    return width;    }    public void setHeight(int aHeight) {    height = aHeight;    }    public void setWidth(int aWidth) {    width = aWidth;    }    public int...

  • 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...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams...

    In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams and print it to the terminal via the driver class: import java.util.*; public class Racer implements Comparable { private final String name; private final int year; private final int topSpeed;    public Racer(String name, int year, int topSpeed){    this.name = name; this.year = year; this.topSpeed = topSpeed;    }    public String toString(){    return name + "-" + year + ", Top...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • Objectives: GUI Tasks: In Lab 4, you have completed a typical function of music player --...

    Objectives: GUI Tasks: In Lab 4, you have completed a typical function of music player -- sorting song lists. In this assignment, you are asked to design a graphic user interface (GUI) for this function. To start, create a Java project named CS235A4_YourName. Then, copy the class Singer and class Song from finished Lab 4 and paste into the created project (src folder). Define another class TestSongGUI to implement a GUI application of sorting songs. Your application must provide the...

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
ADVERTISEMENT