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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Square Question

  • 13-05-2012 5:36pm
    #1
    Registered Users, Registered Users 2 Posts: 620 ✭✭✭


    Hi guys i'm studying for an exam and there's the following question
    public class Square {
    	
    	public static void main(String[] args){
    		
    	}
    
    		private int sideLength;
    		private int area; 
    		
    		public Square(int initiallength)
    		{
    			sideLength=initiallength;
    			area= sideLength*sideLength;
    		}
    		
    		public int getArea(){
    				return area;
    		
    		}
    		public void grow(){ 
    			sideLength=2*sideLength;
    		}
    }
    
    What error does this class have?How would you fix it?

    Now iv written the code and compiled it to no errors. Also as the question says "error" i'm presuming it sone large error!The only thing i noticed is that in the class square it computes the area of the square and therefore the getArea() method is pointless. Is there something i'm missing?Any help would be great


Comments

  • Registered Users, Registered Users 2 Posts: 5,246 ✭✭✭conor.hogan.2


    Logical error. The main has nothing in it, so it does nothing.

    Usually you would not have a main unless you plain to do something in that main. Either take out the main or put something in it.

    GetArea() is fine it is just a getter method for the private variable.


  • Registered Users, Registered Users 2 Posts: 620 ✭✭✭Laika1986


    Logical error. The main has nothing in it, so it does nothing.

    Usually you would not have a main unless you plain to do something in that main. Either take out the main or put something in it.

    GetArea() is fine it is just a getter method for the private variable.

    Cheers for the reply Conor,I think i understand what you mean. Should the code be like this then?
    public class Square {
    	
    	public static void main(String[] args){
    		
    	}
    		Square newSquare= new Square(5);
    		//call methods here 
    		
    		//below here should be defined in seperate classes?
    		private int sideLength;
    		private int area; 
    		
    		public Square(int initiallength)
    		{
    			sideLength=initiallength;
    			area= sideLength*sideLength;
    		}
    		
    		public int getArea(){
    				return area;
    		
    		}
    		public void grow(){ 
    			sideLength=2*sideLength;
    		}
    }
    

    The methods square getArea and grow should be just blank and defined in seperate classes?


  • Registered Users, Registered Users 2 Posts: 5,246 ✭✭✭conor.hogan.2


    No.
    public class Square{
    
    	public static void main(String[] args){
    
                Square newSquare= new Square(5); //you had this outside of main
    		
                // yes call other methods here, like below for example
           	    System.out.println(newSquare.getArea());
    
            } //main is closed here not below
    		
    // Here is the meat of the class, stuff that is usually used above in main
    // or in some other file/class
    
    	    private int sideLength;
           	    private int area; 
    		
    	    public Square(int initiallength){
    	        sideLength=initiallength;
    	        area= sideLength*sideLength;
    	    }
    		
    	    public int getArea(){
                    return area;
    	    }
    
    	    public void grow(){ 
    	        sideLength=2*sideLength;
    	    }
    
    }
    

    Either that or removing the main declaration and using that class as an import.


  • Registered Users, Registered Users 2 Posts: 620 ✭✭✭Laika1986


    Ah i see sorry i should have copped that really but iv been looking at stuff all day my head is fried!Thanks for the help


  • Registered Users, Registered Users 2 Posts: 5,246 ✭✭✭conor.hogan.2


    Laika1986 wrote: »
    Ah i see sorry i should have copped that really but iv been looking at stuff all day my head is fried!Thanks for the help

    No problem. What is the exam for if you don't mind me asking. You don't have to say specifics.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 620 ✭✭✭Laika1986


    No problem. What is the exam for if you don't mind me asking. You don't have to say specifics.

    Data Structures and Algorithms tomorrow at 3!


  • Registered Users, Registered Users 2 Posts: 5,246 ✭✭✭conor.hogan.2


    Laika1986 wrote: »
    Data Structures and Algorithms tomorrow at 3!

    Which college, if that is not too specific. Is it a 1st or second year course?


  • Registered Users, Registered Users 2 Posts: 620 ✭✭✭Laika1986


    Ask away mate!UCD its a second year subject im on a kinda back to college initiative thing only got 6 subjects. Love programming but I've only scratched the surface really!


  • Registered Users, Registered Users 2 Posts: 5,246 ✭✭✭conor.hogan.2


    Laika1986 wrote: »
    Ask away mate!UCD its a second year subject im on a kinda back to college initiative thing only got 6 subjects. Love programming but I've only scratched the surface really!

    This one? http://www.csi.ucd.ie/content/data-structures-and-algorithms-i-comp20010

    Is this the final exam?


  • Registered Users, Registered Users 2 Posts: 620 ✭✭✭Laika1986


    Yeah that's the one, online though..you in the class?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 620 ✭✭✭Laika1986


    That's not it exactly my code is Comp20210


  • Registered Users, Registered Users 2 Posts: 5,246 ✭✭✭conor.hogan.2


    Laika1986 wrote: »
    Yeah that's the one, online though..you in the class?

    No. I did it last semester. The online course looks a fair bit different to the physical one then. The exam we did was more about various DS/Algorithms and you demonstrated them through diagrams/java/pseudocode.

    Ah you are in Springboard. It seems to be a different course but loosely based on the same module.


  • Registered Users, Registered Users 2 Posts: 620 ✭✭✭Laika1986


    Yeah Iv looked over your past papers, we only have one past paper for this module. Yours seems to have alot about the Big O notation whereas only one question on this paper. Ours looks far easier to be honest


  • Registered Users, Registered Users 2 Posts: 5,246 ✭✭✭conor.hogan.2


    Laika1986 wrote: »
    Yeah Iv looked over your past papers, we only have one past paper for this module. Yours seems to have alot about the Big O notation whereas only one question on this paper. Ours looks far easier to be honest

    Ah ok so it has changed. I would say it has changed for everybody tbh including the full time 2nd years as most of them seemed to be struggling with that course big time.

    Is there much on queues, deques, stacks, vectors etc? Do you have to know the pseudocode and/or java code for them?

    Anyway, good luck.


  • Registered Users, Registered Users 2 Posts: 620 ✭✭✭Laika1986


    Thanks again!


  • Registered Users, Registered Users 2 Posts: 2,781 ✭✭✭amen


    I'm not a Java Programmer and I hate to reopen a solved question but

    [PHP] public Square(int initiallength){
    sideLength=initiallength;
    area= sideLength*sideLength;
    }

    public int getArea(){
    return area;
    }

    public void grow(){
    sideLength=2*sideLength;
    }[/PHP]

    1:if you call grow and the call getArea then the area is incorrect
    2:I would have move the area calculation out of in the Object initialisation and into the getArea function. That way when you either call getArea on a new object or after you grow an object you will always get the correct area or move the area calculation into grow but then you have two calculations the same.


  • Registered Users, Registered Users 2 Posts: 5,246 ✭✭✭conor.hogan.2


    Good spot Amen*. I would do it this way or "way2" in comments.

    [PHP]public class Square {

    public static void main(String[] args){
    Square newSquare= new Square(5);
    //newSquare.calcArea(); way2
    System.out.println(newSquare.getArea());
    newSquare.grow();
    System.out.println(newSquare.getArea());
    }

    private int sideLength;
    private int area;

    public Square(int initiallength)
    {
    sideLength=initiallength;
    calcArea(); // area = sideLength*sideLength;
    //possibly not needed and just call in main, way2
    }

    // method is private internal helper method. public if way2
    private void calcArea(){
    area = sideLength*sideLength;
    }

    public int getArea(){ return area; } //space, don't indent like this

    public void grow(){
    sideLength=2*sideLength;
    calcArea(); // call here as can only change here.
    }
    }[/PHP]

    *Also good to know PHP code also colors the code too. Indentation fixed.


  • Registered Users, Registered Users 2 Posts: 620 ✭✭✭Laika1986


    Well lads thanks for the help!pretty much the exact same question came up!delighted


  • Registered Users, Registered Users 2 Posts: 3,532 ✭✭✭Unregistered.


    The code may also run the risk of integer overflow.
    area = sideLength*sideLength;
    

    and
    [COLOR=#000000][COLOR=#007700][/COLOR][/COLOR]
    sideLength=2*sideLength
    


    area and sideLength should be of type long or some big-int class if you want to get fancy.


  • Registered Users, Registered Users 2 Posts: 5,246 ✭✭✭conor.hogan.2


    True, rare you would be getting the area of a square with sides longer than 10,000 (roughly, likely more) though but true.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,532 ✭✭✭Unregistered.


    True, rare you would be getting the area of a square with sides longer than 10,000 (roughly, likely more) though but true.

    Not at all rare! It's only an exam question, however, you can never make assumptions unless you've been given the full spec :P. The exam question may look like a task to spot the programming error (eg, area not being updated when by the grow method) when actually it's asking you to analyse the problem and take in to consideration some test cases which might cause the code to behave differently!


  • Registered Users, Registered Users 2 Posts: 896 ✭✭✭moycullen14


    From a design perspective, what stands out here is that a Square is mutable (ie it can change, compare - say - with a java.lang.String). Also there is redundancy as the Square has only one attribute: the length of a side, but a derived attribute (area) is stored. What this means is that you are storing (sorry!) up a potential maintenance nightmare because when you change the length, the area MUST change as well. What if someone changes the area? Should the length change then too? Always be thinking of possible extensions: What if you needed to calculate diagonal length, perimeter length, etc - would you store those as instance variables too? How about abstracting the Square into a Rectangle, Quadrilateral, Parallelogram, etc?

    Good practice would be to get rid of the area as an instance variable and just calculate it when you need it. It's a good idea to keep classes as minimal as possible, delaying bindings and calculations as much as possible.


  • Registered Users, Registered Users 2 Posts: 3,532 ✭✭✭Unregistered.


    From a design perspective, what stands out here is that a Square is mutable (ie it can change, compare - say - with a java.lang.String). Also there is redundancy as the Square has only one attribute: the length of a side, but a derived attribute (area) is stored. What this means is that you are storing (sorry!) up a potential maintenance nightmare because when you change the length, the area MUST change as well. What if someone changes the area? Should the length change then too?

    Always be thinking of possible extensions: What if you needed to calculate diagonal length, perimeter length, etc - would you store those as instance variables too? How about abstracting the Square into a Rectangle, Quadrilateral, Parallelogram, etc?


    You can't change the are with the code he was given. There is no method which supports it. Everything else if good in practice but out of scope for the exam question!


Advertisement