Results 1 to 5 of 5
Thread: Help with get and set methods
- 11-23-2009, 04:27 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 31
- Rep Power
- 0
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); } }
- 11-23-2009, 05:20 PM #2
Member
- Join Date
- Nov 2009
- Posts
- 96
- Rep Power
- 0
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.
- 11-23-2009, 07:52 PM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 11-23-2009, 07:59 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 96
- Rep Power
- 0
Yes, I agree with Singing Boyo, but I only see useful the method reCompute() if you also define the methods setHeight() and setBase()
- 11-23-2009, 08:04 PM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
Help with Methods Please :-)
By jkhamler in forum New To JavaReplies: 22Last Post: 11-10-2009, 08:54 PM -
How to get methods to see variables in other methods
By ejs7597 in forum New To JavaReplies: 4Last Post: 04-03-2009, 06:36 AM -
JSP methods example
By Java Tip in forum Java TipReplies: 0Last Post: 01-30-2008, 10:00 AM -
Methods
By Java Tip in forum Java TipReplies: 0Last Post: 12-01-2007, 08:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks