-3

I just want to make sure I'm correct here. I am trying to add methods to

  • Change height
  • Change width
  • Change coordinates
  • Calculate perimeter
  • Calculate area

    public class MyRectangle {
    
    public int width;
    public int height;
    public int y;
    public int x;
    
    public MyRectangle()
    {
        width=10;
        height=10;
        y=10;
        x=10;
    
    public int MyRectangle;
    
    public MyRectangle(int width, int height, int y, int x, int MyRectangle) {
        this.width = width;
        this.height = height;
        this.y = y;
        this.x = x;
        this.MyRectangle = MyRectangle;
    }
    }
    

and I'm also getting a illegal start of expression error on my method.

Sunny Dhillon
  • 61
  • 1
  • 1
  • 8
  • 1
    what is the question? and why are you setting a variable with the same name as the class, please first change that name so your class may appear more readbale – Tarik Feb 07 '15 at 01:54
  • You forgot a close `}` on your first constructor. Please check your code for syntax errors. – k_g Feb 07 '15 at 01:55
  • You can't declare `public int MyRectangle;` within a method nor can you declare a method within a method –  Feb 07 '15 at 01:56
  • 1
    Anyone here can easily give you a code with the methods that changes width, height.... but you have to make some effort, you didn't even try to compile your code – Tarik Feb 07 '15 at 01:59

1 Answers1

1

This is your problem, you can't have methods within a method. But this was due to you not closing your brackets for your methods. I fixed your code and added the methods you wanted:

public class MyRectangle {

    //Best to group your variables up here
    public int MyRectangle;
    public int width;
    public int height;
    public int y;
    public int x;

    public MyRectangle() {
        width  = 10;
        height = 10;
        y      = 10;
        x      = 10;
    }//Make sure to close this method with the bracket

    public MyRectangle(int width, int height, int y, int x, int MyRectangle) {
        this.width = width;
        this.height = height;
        this.y = y;
        this.x = x;
        this.MyRectangle = MyRectangle;
    }

    /**
     * Changes the current height to the given new height
     * @param newHeight
     */
    public final void changeHeight(int newHeight) {
        height = newHeight;
    }

    /**
     * Changes the current width to the given new width
     * @param newWidth
     */
    public final void changeWidth (int newWidth) {
        width = newWidth;
    }

    /**
     * Calculates the current perimeter based on the width and height
     * @return parameter ofd the rectangle
     */
    public final int getPerimeter() {
        return ((2 * width) + (2 * height));
    }

    /**
     * Calculates the area based on the width and height
     * @return area of the rectangle
     */
    public final int getArea() {
        return (width * height);
    }

    public final void changesXCoordinate(int newX){
        x = newX;
    }

    public final void changesYCoordinate(int newY){
        y = newY;
    }

    public final void changesCoordinate(int newX, int newY) {
        x = newX;
        y = newY;
    }
}

I will explain more soon, just wanted to post the correct code first :P

As it stands, it's kinda hard to understand what else you are looking for.

If this is what you are looking for, please mark this as the correct answer :D

  • Am wondering why I received -1, can you please explains so that I can improve my answer and for future reference –  Feb 07 '15 at 02:03
  • Not the downvoter, but maybe because you are answering while you don't understand what he is looking for – Tarik Feb 07 '15 at 02:07
  • Thanks Tarik! But I simply fixed the error he was asking about... Am sorry if I put this as a answer, I thought it would help him ;( I lost rep trying to help him –  Feb 07 '15 at 02:09
  • don't worry, that's not much rep :) you can easily get +2 rep by correctly editing someone's question ;) – Tarik Feb 07 '15 at 02:11
  • I think I pretty much covered anything else he would had asked about this, added methods for change in height, width, area and Perimeter –  Feb 07 '15 at 02:13
  • This is a wrong code. You have declared `public int MyRectangle;` in your MyRectangle class. Can you tell what does `int MyRectangle` do here? – omerfarukdogan Feb 07 '15 at 02:46
  • yeah i think that is what i need thank you will try it and find out ig its correct – Sunny Dhillon Feb 07 '15 at 03:34
  • the myrectangle has the default values in it – Sunny Dhillon Feb 07 '15 at 05:53
  • I personally do not know what MyRectangle does (the int). I think it might had been Sunny's way of associated a index/id value to the rectangle to be able to find easily. But I do recommend renaming that variable to something else that wouldn't be easily confused with the class –  Feb 07 '15 at 12:57