Question
I currenly have my code written, its either the overdose of caffeine or lack of sleep but i cant see why my code isnt working:
I currenly have my code written, its either the overdose of caffeine or lack of sleep but i cant see why my code isnt working:
Assignment:
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.
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. However, you will need some new methods in your MySquare class. Specifically:
public MySquare()
public MySquare(int x, int y, int side)
// Constructors to allow new objects to be created. x and y are the // location coordinates and size is the side length
public String toString()
// Redefining toString(). See output for effect.
public void setSize(int w, int h)
// Redefining setSize(). This must be done because the inherited version
// allows the width and height to differ 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 that size of the square. Think about
// how this will be implemented using the existing inherited instance vars.
Your MySquare class must run with the Lab8.java program with no changes. To see how your output should look see the file lab8out.txt. Note that there are some methods that are utilized in Lab8.java which are inherited and which you should not redefine in your MySquare class. See details in the comments in Lab8.java.
Lab8:
public class Lab8
{
// 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());
}
}
MyRectangle:
public class MyRectangle
{
// Declare instance variables here. See toString() method below for names.
protected int width, height, startX ,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.
// Be careful to get the order correct!
startX = x;
startY = y;
width = w;
height = h;
}
public int area()
{
return(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)
{
if((x >= startX) && (x <= (startX + width))&&(y >= startY && y <= (startY + height)))
{
return true;
}
return false;
// 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
width = w;
height = h;
}
public void setPosition(int x, int y)
{
// Update startX and startY as shown
startX = x;
startY = y;
}
}
MySquare:
public class MySquare extends MyRectangle{
public MySquare() { super(); }
public MySquare(int width, int height, int side) { super(width, height, side); }
public void setSize(int w, int h){ if(w != h) System.out.println("Error. Width and height are not same"); else{ width = w; height = h; } }
public void setSide(int side){ this.side = side; }
public String toString(){ return "Width: " + width + ", Height: " + height + ", Side: " + side; }
}
Expected Output:
S1: Side: 25 X: 10 Y: 20 Area: 625 S2: Side: 0 X: 0 Y: 0 Area: 0 S3: Side: 150 X: 0 Y: 0 Area: 22500 -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- --------------------++++++++++++++++++++++++++---------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- S1: Side: 15 X: 0 Y: 0 Area: 225 ++++++++++++++++---------------------------------------------------------------- ++++++++++++++++---------------------------------------------------------------- ++++++++++++++++---------------------------------------------------------------- ++++++++++++++++---------------------------------------------------------------- ++++++++++++++++---------------------------------------------------------------- ++++++++++++++++---------------------------------------------------------------- ++++++++++++++++---------------------------------------------------------------- ++++++++++++++++---------------------------------------------------------------- ++++++++++++++++---------------------------------------------------------------- ++++++++++++++++---------------------------------------------------------------- ++++++++++++++++---------------------------------------------------------------- ++++++++++++++++---------------------------------------------------------------- ++++++++++++++++---------------------------------------------------------------- ++++++++++++++++---------------------------------------------------------------- ++++++++++++++++---------------------------------------------------------------- ++++++++++++++++---------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- Sides must be equal. 20 != 40 so no action taken S1: Side: 20 X: 0 Y: 0 Area: 400
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started