Results 1 to 4 of 4

Thread: Get and Set methods

  1. #1
    Join Date
    May 2008
    Posts
    248

    Get and Set methods

    Hi
    I am trying to create a program that finds the area of a triangle. I can easily find the area of the triangle but the problem is that I have been said to use the set and get method to calculate the area. I have no idea of how to use get and set method with an object. Any help or advice. I have posted my code below.
    Code:
    public class TriObj {
    	public void runIt()
    	{
    		Scanner in = new Scanner (System.in);
    		int b;
    		int h;
    		int a;
    		System.out.println("Enter b of triange: ");
    		b=in.nextInt();
    		System.out.println("Enter h of triange: ");
    		h=in.nextInt();
    		a=(b*h/2);
    		System.out.println("The a of the triangle is: "+ a);
    	}
    }

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Get and Set methods

    Hello
    I think it would be great if you declare base, height and area outside the method. I have created a sample example see if it help's you.
    Code:
    public void sb(int b) {
        this.b=b;
    }
    
    public int ga() {
        return area;
    }
    And your constructor should look somewhat like this
    Code:
    public Trio(int b, int height) {
        this.b=b;
        this.height=height;
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Get and Set methods

    Hi
    If you need then you can also use gh(), gb() and recomp(x,x) methods. I have shown it in the code below. Check it out.
    Code:
    public int gh(){
         return height;
    }
    public int gb(){
         return b;
    }
    public void reCompute(int height, int b){
         this.b = b;
         this.height = height;
         this.area = b*height/2;
    }
    Hope this may increase your options. Enjoy programming

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: Get and Set methods

    Hi
    Yes it is correct you can do this as above posted but then you need to do the re-computation in those methods. Or you can change your code like this.
    Code:
    public int ga(){
         return b*height/2;
    }

Similar Threads

  1. Extension methods in C# 3.0
    By Daniela007 in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 05:21 AM
  2. What is Abstract methods?
    By ScarFace 01 in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 11:59 AM
  3. What are the different methods used by a Firewall
    By Abhibhava in forum Networking & Security
    Replies: 5
    Last Post: 23-12-2009, 01:30 AM
  4. use of methods and constructor
    By beelow in forum Software Development
    Replies: 4
    Last Post: 14-11-2009, 01:37 AM
  5. Methods of debugging in PHP
    By Ninad23 in forum Tips & Tweaks
    Replies: 1
    Last Post: 21-11-2008, 02:31 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,908,246.93054 seconds with 16 queries