Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Newbie

Options
  • 30-09-2006 1:42pm
    #1
    Closed Accounts Posts: 1,080 ✭✭✭


    Hi, Iv just started 1st year college and im having trouble with this code and I dont really understand what is wrong. It has to be in for Monday can anyone tell me how to fix it?
    public class Rectangle
    {
     int width;
     int height;
     int x;
     int y;
    
     public Rectangle()
     {
      width = 0;
      height = 0;
      x = 0;
      y = 0;
      }
    
      public void Rectangle(int width_in, int height_in, int x_in, int y_in)
      {
       width = width_in;
       height = height_in;
       x = x_in;
       y = y_in;
      }
        public int getwidth()
        {
        return width;
         }
    
        public int getheight()
        {
          return height;
        }
    
        public int getx()
        {
         return x;
        }
    
        public int gety()
        {
         return y;
        }
    
         public void setwidth(int width_int)
         {
          width=width_in;
         }
    
         public void setheight(int height_int)
         {
          height=height_in;
         }
    
         public void setx(int x_in)
         {
          x=x_int;
         }
    
         public void sety(int y_in)
         {
          y=y_in;
         }
         }
        }
       }
      }
     }
    


Comments

  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    I've never done Java but to satisfy my own curiousity is that script not just setting a class called rectangle, specifying the dimensions in integers(as zero I might add, essentially creating a non-existant shape :confused:), equating the variables to the integers, returning the integers, and then setting them again??


  • Registered Users Posts: 4,188 ✭✭✭pH


    eamoss wrote:
    Hi, Iv just started 1st year college and im having trouble with this code and I dont really understand what is wrong. It has to be in for Monday can anyone tell me how to fix it?
    public class Rectangle
    {
     int width;
     int height;
     int x;
     int y;
    
     public Rectangle()
     {
      width = 0;
      height = 0;
      x = 0;
      y = 0;
      }
    
      public [highlight]void[/highlight] Rectangle(int width_in, int height_in, int x_in, int y_in)
      {
       width = width_in;
       height = height_in;
       x = x_in;
       y = y_in;
      }
      public int get[B]W[/B]idth()
        {
        return width;
         }
    
        public int get[B]H[/B]eight()
        {
          return height;
        }
    
        public int get[B]X[/B]()
        {
         return x;
        }
    
        public int get[B]Y[/B]()
        {
         return y;
        }
    
         public void set[B]W[/B]idth(int width_in[highlight]t[/highlight])
         {
          width=width_in;
         }
    
         public void set[B]H[/B]eight(int height_in[highlight]t[/highlight])
         {
          height=height_in;
         }
    
         public void set[B]X[/B](int x_in)
         {
          x=x_in[highlight]t[/highlight];
         }
    
         public void set[B]Y[/B](int y_in)
         {
          y=y_in;
         }
         }
        }
       }
      }
     }
    

    Remove stuff in [highlight]RED[/highlight] change BOLD

    I didn't compile it, see what errors you have now


  • Closed Accounts Posts: 1,080 ✭✭✭eamoss


    Still get the same errors its to do with the '}' down the bottom of the code.


  • Registered Users Posts: 742 ✭✭✭easyontheeye


    hey OP, can you post the error you receive when compiling


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Why are there so many }'s at the end of the code?


  • Advertisement
  • Closed Accounts Posts: 1,080 ✭✭✭eamoss




  • Registered Users Posts: 742 ✭✭✭easyontheeye


    should you not finish with this

    public void setY(int y_in)
    {
    y=y_in;
    }


    }


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Things would be much easier if you used an indenting editor. Then you could align opening and closing braces much more easily. By the looks of it you're using notepad or something similar :p I'm only familiar with emacs, but i'm sure people could recommend you better ones.

    EDIT: Ok, so it looks like you're not using notepad... but does your editor not support auto-indenting of opening and closing braces and of your code?


  • Registered Users Posts: 9,480 ✭✭✭projectmayhem


    there's like 4 extra } brackets at the end of the code though...


  • Registered Users Posts: 742 ✭✭✭easyontheeye


    text pad is free and handy


  • Advertisement
  • Closed Accounts Posts: 307 ✭✭Idgeitman


    *seconds texpad*

    Handy & quick


    ~ Idgeitman


  • Registered Users Posts: 6,557 ✭✭✭GrumPy


    there's like 4 extra } brackets at the end of the code though...

    Like you'd know :p


  • Registered Users Posts: 1,466 ✭✭✭Smoggy


    There does seem to be a lot of extra bracketing going on !
    On a side note if java supports the equivelant of .net properties you may want to use them to make ur code a little mopre readable.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Lets see..

    Your first constructor is pointless because primatives are auto initalised if they are global to the class and as it does nothing else you can get rid of it. Unless you need a no arg constructor in which case you can just make a blank one (as adding the second constructor will disable it being automatically made)

    Your second constructor isn't a constructor at all as it returns a void.

    Some variables have spelling mistakes and you appear to have 4 too many brackets at the end.

    If you loaded this up in Eclipse you would of seen all this in a few seconds.

    To further nitpick, you appear to be creating a bean but not sticking to the bean standards (which is to make the class variables private).


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    btw if you don't understand it then it begs the questions.

    1. Did you write it to begin with?

    2. Are you supposed to tell the teacher what is wrong with it?


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Meh bored.
    /**
     * You are free to use this code only if you can describe in detail to your
     * teacher exactly what everything does. 
     * @author Hobbes
     *
     */
    public class Rectangle {
    	private int _width;
    	private int _height;
    	private int _x;
    	private int _y;
    
    	/** 
    	 * No arg constructor.
    	 */
    	public Rectangle() {
    	}
    
    	/**
    	 * Constructor.
    	 * @param width		description.
    	 * @param height	description.
    	 * @param x			description.
    	 * @param y			description.
    	 */
    	public Rectangle(int width, int height, int x, int y) {
    		_width = width;
    		_height = height;
    		_x = x;
    		_y = y;
    	}
    
    	/**
    	 * description
    	 * @return	description
    	 */
    	public int getWidth() {
    		return _width;
    	}
    
    	/**
    	 * description
    	 * @return	description
    	 */
    	public int getHeight() {
    		return _height;
    	}
    
    	/**
    	 * description
    	 * @return	description
    	 */
    	public int getX() {
    		return _x;
    	}
    
    	/**
    	 * description
    	 * @return	description
    	 */
    	public int getY() {
    		return _y;
    	}
    
    	/**
    	 * description
    	 * @return	description
    	 */
    	public void setWidth(int width) {
    		_width = width;
    	}
    
    	/**
    	 * description
    	 * @return	description
    	 */
    	public void setHeight(int height) {
    		_height = height;
    	}
    
    	/**
    	 * description
    	 * @return	description
    	 */
    	public void setX(int x) {
    		_x = x;
    	}
    
    	/**
    	 * description
    	 * @return	description
    	 */
    	public void setY(int y) {
    		_y = y;
    	}
    }
    

    [edit] onoz!


  • Closed Accounts Posts: 362 ✭✭information


    good few errors in this bean
    Hobbes wrote:
    Meh bored.
    /**
     * You are free to use this code only if you can describe in detail to your
     * teacher exactly what everything does. 
     * @author Hobbes
     *
     */
    public class Rectangle {
    	private int _width;
    	private int _height;
    	private int _x;
    	private int _y;
    
    	/** 
    	 * No arg constructor.
    	 */
    	public Rectangle() {
    		super();
    	}
    
    	/**
    	 * Constructor.
    	 * @param width		description.
    	 * @param height	description.
    	 * @param x			description.
    	 * @param y			description.
    	 */
    	public Rectangle(int width, int height, int x, int y) {
    		_width = width;
    		_height = height;
    		_x = x;
    		_y = y;
    	}
    
    	/**
    	 * description
    	 * @return	description
    	 */
    	public int getwidth() {
    		return _width;
    	}
    
    	/**
    	 * description
    	 * @return	description
    	 */
    	public int getheight() {
    		return _height;
    	}
    
    	/**
    	 * description
    	 * @return	description
    	 */
    	public int getx() {
    		return _x;
    	}
    
    	/**
    	 * description
    	 * @return	description
    	 */
    	public int gety() {
    		return _y;
    	}
    
    	/**
    	 * description
    	 * @return	description
    	 */
    	public void setwidth(int width) {
    		_width = width;
    	}
    
    	/**
    	 * description
    	 * @return	description
    	 */
    	public void setheight(int height) {
    		_height = height;
    	}
    
    	/**
    	 * description
    	 * @return	description
    	 */
    	public void setx(int x) {
    		_x = x;
    	}
    
    	/**
    	 * description
    	 * @return	description
    	 */
    	public void sety(int y) {
    		_y = y;
    	}
    }
    


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    good few errors in this bean

    isBrainActive(false);

    *DOH* :D Should of spotted that. Although in fairness I was just using the previous code.

    I'd also go as far as to remove the "super();" as the compiler adds that.


Advertisement