Search code examples
javamethodsconstructorrectangles

Lab constructing a rectangle in Java


I am new to programming and recently started on my computer science degree. Due to my son being sick I missed labs yesterday for CS-140/Java and am having a little trouble with my lab assignment from home.

  1. I am told to Construct an empty Rectangle called box.

  2. Figure out how to change the width of box to 50, its height to 60 and top-left corner to [100,50]. (You need to call appropriate methods on box to achieve this.)

  3. Figure out how to compute the area of box. You have to get the height and width of box, by calling appropriate methods to compute the area.

  4. Then print the value returned by calling a method on box and then we print a message that describes the value we expect to see.

I have searched through stack exchange and found some useful information but I still cannot figure out the bugs in the code and why it is not working. I am assuming the 63 errors are from something like a curly bracket in the beginning either missing or too many curly brackets.

I have 63 errors somehow the code I have entered so far is the following:

 import java.awt.Rectangle;
public class Rectangle {
    public double x;
    public double y;
    public double width, height;

    public Rectangle(double x, double y, double w, double h) {{
           this.x = x;
           this.y = y;
           width = w;
           height = h;
   }
   public static void main(String[] args) {
       Rectangle box = new Rectangle (100, 50, 50, 60);
       System.out.println(box);
    }
       System.out.println("Part 1:");
       System.out.println("-------");
       System.out.println("CREATING AN EMPTY Rectangle, SETTING ITS width TO 50, " +
                          "ITS height TO 60, AND ITS TOP LEFT CORNER TO (100, 50)");
       // Code for Part 1 goes here


        }

       System.out.println("Part 2:");
       System.out.println("-------");
       System.out.println("TESTING THE PERIMETER OF THE RECTANGLE "
                        + "CREATED IN PART 1 ABOVE");
       // Code for Part 2 goes here.  Use the same Rectangle
       // you used Part1

       // define a String reference called river and initialize it
       // to Mississippi.  Read the API to figure out which method 
       // to use to get the desired effect.
       System.out.println("Part 3:");
       System.out.println("-------");
       System.out.println("INITIALIZING river to Mississippi AND REPLACING "
                        + "EACH i to ! AND EACH s TO $");
       // code for Part 3 goes here


       System.out.println("Part 4:");
       System.out.println("-------");
       System.out.println("CONSTRUCTING A StringBuilder OBJECT INITIALIZED "
                        + "TO desserts AND REVERSING IT");
       // code for Part 4 goes here


       System.out.println("Part 5:");
       System.out.println("-------");
       System.out.println("CONSTRUCTING A Random OBJECT TO PLAY LOTTERY BY GENERATING ");
       System.out.println("6 RANDOM INTEGERS BETWEEN 1 and 49 (BOTH INCLUSIVE)");
       //code for Part 5 goes here

       System.out.println("Part 6:");
       System.out.println("-------");
       System.out.println("ADDING ONE DAY TO 2/28/2019, 2/28/2020, 2/28/2021 " +
                          "AND 2/28/2022 AND PRINTING THEM");
       // code for Part 6 goes here
    }
}

Any help is appreciated. I am excited to join the community of StackOverflow and hopefully be able to contribute someday.


Solution

  • Here is the cleanup of your code:

    //import java.awt.Rectangle; // you probably autocompleted this line, since you created your own Rectangle class for console outputs
    public class Rectangle {
        public double x;
        public double y;
        public double width, height;
    
        public Rectangle(double x, double y, double w, double h) {
               this.x = x;
               this.y = y;
               width = w;
               height = h;
       }
       public static void main(String[] args) {
           Rectangle box = new Rectangle (100, 50, 50, 60);
           System.out.println(box);
    
           System.out.println("Part 1:");
           System.out.println("-------");
           System.out.println("CREATING AN EMPTY Rectangle, SETTING ITS width TO 50, " +
                              "ITS height TO 60, AND ITS TOP LEFT CORNER TO (100, 50)");
           // Code for Part 1 goes here
    
    
    
    
           System.out.println("Part 2:");
           System.out.println("-------");
           System.out.println("TESTING THE PERIMETER OF THE RECTANGLE "
                            + "CREATED IN PART 1 ABOVE");
           // Code for Part 2 goes here.  Use the same Rectangle
           // you used Part1
    
           // define a String reference called river and initialize it
           // to Mississippi.  Read the API to figure out which method 
           // to use to get the desired effect.
           System.out.println("Part 3:");
           System.out.println("-------");
           System.out.println("INITIALIZING river to Mississippi AND REPLACING "
                            + "EACH i to ! AND EACH s TO $");
           // code for Part 3 goes here
    
    
           System.out.println("Part 4:");
           System.out.println("-------");
           System.out.println("CONSTRUCTING A StringBuilder OBJECT INITIALIZED "
                            + "TO desserts AND REVERSING IT");
           // code for Part 4 goes here
    
    
           System.out.println("Part 5:");
           System.out.println("-------");
           System.out.println("CONSTRUCTING A Random OBJECT TO PLAY LOTTERY BY GENERATING ");
           System.out.println("6 RANDOM INTEGERS BETWEEN 1 and 49 (BOTH INCLUSIVE)");
           //code for Part 5 goes here
    
           System.out.println("Part 6:");
           System.out.println("-------");
           System.out.println("ADDING ONE DAY TO 2/28/2019, 2/28/2020, 2/28/2021 " +
                              "AND 2/28/2022 AND PRINTING THEM");
           // code for Part 6 goes here
       }
    }
    

    The assignments are as follows:

          //assignment 1
          Rectangle box = new Rectangle(0,0,0,0);
          //assignment 2
          box.width = 50;
          box.height = 60;
          box.x = 100;
          box.y = 150;
          //assignment 3
          int area = box.height*box.width;
          //assignment 4
          System.out.println("Area: "+area);
    

    I'm sure you can integrate this into your own code and make the appropriate methods as well as getters and setter if needed.