I have been at this for days and I just can't wrap my finger around it. I am in a basic 100 level college class and this just seems to advanced for someone just getting into Java. I am trying to figure this out but as you can see, not going over too well. Can someone please point me in the right direction as to what I am doing wrong? This is what needs to be done:
Write a Java class definition for class Rectangle. Each object of this class has the following variables:
h : an integer value representing height.
w : an integer value representing width.
Your class should also define the following methods:
* A constructor that accepts no parameter. This must set the h and w to 3 by default.
* A constructor that accepts two integer parameters. First parameter is for height, second parameter is for width. If any parameter is not in the range of [1, 10] inclusive, then it must be set to 3 by default.
* setHW method that accepts no parameter. This method asks user to enter height and width. If any parameter is not in the range of [1, 10] inclusive, then it must ask user again to enter in the range [1, 10]. This must continue until user enters a valid number, that is in [1, 10]. See sample output below.
* setHW method that accepts two integer parameters. First parameter is for height, second parameter is for width. If any parameter is not in the range of [1, 10] inclusive, then it must be set to 3 by default.
* draw method. This will draw the rectangle on the screen. For the edges of the rectangle, use ‘#’ character. For the inside of rectangle, use space character.
Have the main method in the Rectangle class. Put this main method as it is. Do not make any change on this method. Below is the main method which tests the class;
Code:
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
r1.draw();
r1.setHW(4, 4);
r1.draw();
r1.setHW();
r1.draw();
Rectangle r2 = new Rectangle(-5, 4);
r2.draw();
}
This is what I have so far:
Code:
public class Rectangle {
private int H, W;
Rectangle(){
H = 3;
W = 3;
}
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
r1.draw();
r1.setHW(4, 4);
r1.draw();
r1.setHW();
r1.draw();
Rectangle r2 = new Rectangle(-5, 4);
r2.draw();
}
Rectangle(int height, int width){
H = height;
W = width;
System.out.println("Height:");
{
public height (int i){
if (i <1 || i>10){
System.out.println("Height must be in [1,10]. Please enter Height again:");
}
System.out.println("Width:");
public width (int l){
if (l < 1 || l >10){
System.out.println("Width must be in [1,10]. Please enter Width again:");
}
public void draw(
int height,
int width);
}
Thanks in advance!