Results 1 to 5 of 5
  1. #1
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default Help with get and set methods

    Hey everyone. I am supposed to create a program that finds the area of a triangle...this i can do fine. My only problem is i was told that i need to use a get and set method to calculate the area. I dont exactly know how to use a get and set method with an object...would anybody show me how to do this or set me in the right direction? here is what i have, which works:

    Main class:
    Java Code:
    public class TriMain {
    
    	public static void main(String[] args) {
    		
    		TriObj myTriObj = new TriObj();
    		
    		myTriObj.runIt();
    	
    		
    
    		
    		
    	}
    
    }

    Object:
    Java Code:
    import java.util.Scanner;
    public class TriObj {
    	
    	public void runIt()
    	{
    		
    		Scanner input = new Scanner (System.in);
    		
    		int base;
    		int height;
    		int area;
    
    		
    		System.out.println("Enter base of triange: ");
    		base=input.nextInt();
    		System.out.println("Enter height of triange: ");
    		height=input.nextInt();
    	
    		area=(base*height/2);
    	
    		
    		System.out.println("The area of the triangle is: "+ area);
    		
    		
    	}
    
    }

  2. #2
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    I suppose that what you should do is declare base, height and area outside the method, that is, as fields of the object. Then you can have the accesor methods (get) and the mutator methods (set) like this:

    Java Code:
    public void setBase(int base) {
        this.base=base;
    }
    
    public int getArea() {
        return area;
    }

    ... and so on. Therefore, I would recommend you to take out from TriObj the input reading and don't use set methods. You should read the input in the Main and after that create an object like this:
    Java Code:
    TriObj myTriObj = new TriObj(base, height);

    using the constructor:
    Java Code:
    public Triobject(int base, int height) {
        this.base=base;
        this.height=height;
    }

    At the end it would be something like this (I didn't compile it so be careful):
    Java Code:
    import java.util.Scanner;
    
    public class TriMain {
    
    	public static void main(String[] args) {
    
                    int base, height, area;
    		Scanner input = new Scanner (System.in);
    		
    		System.out.println("Enter base of triange: ");
    		base=input.nextInt();
    		System.out.println("Enter height of triange: ");
    		height=input.nextInt();
    
    		TriObj myTriObj = new TriObj(base, height);
    
                    area=myTriObj.getArea();
                    System.out.println("The area of the triangle is: "+ area);
    
    	}
    }
    Java Code:
    public class TriObj {
    	
    	private int base;
            private int height;
            private int area;
    
    	public Triobject(int base, int height) {
                   this.base=base;
                   this.height=height;
                   this.area=(base*height/2);
             }
    		
    	public int getArea() {
                   return area;
            }	
    }
    Last edited by sky; 11-23-2009 at 05:32 PM.

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Possibly you might also want a getHeight(), getBase(), and reCompute(int, int) method. (reCompute is going overboard, but hey, I like to be thorough)

    Java Code:
    public int getHeight(){
         return height;
    }
    public int getBase(){
         return base;
    }
    public void reCompute(int height, int base){
         this.base = base;
         this.height = height;
         this.area = base*height/2;
    }
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    Yes, I agree with Singing Boyo, but I only see useful the method reCompute() if you also define the methods setHeight() and setBase()

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Yes, but then you need to do the re-computation in those methods to, or you end up with problems. Or you just change getArea to this...

    Java Code:
    public int getArea(){
         return base*height/2;
    }
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Help with Methods Please :-)
    By jkhamler in forum New To Java
    Replies: 22
    Last Post: 11-10-2009, 08:54 PM
  3. Replies: 4
    Last Post: 04-03-2009, 06:36 AM
  4. JSP methods example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-30-2008, 10:00 AM
  5. Methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 08:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •