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:

public class TriMain {

	public static void main(String[] args) {
		TriObj myTriObj = new TriObj();




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: ");
		System.out.println("Enter height of triange: ");
		System.out.println("The area of the triangle is: "+ area);


first, the get and set methods (also called Accessors and Mutators) are used to access attributes of an object and change them. like this:

class x{
  int age;
  String name;

  public void setName(String n){
    name = n;

  public String getName(){
    return name;
  Similarly goes for variable age

in your case, I guess you're required to read values in the main method, use the set methods to set values of base and height, and use a get method that returns area. Hope I did not complicate it, good luck.

Edited 7 Years Ago by striker3344: n/a

This article has been dead for over six months. Start a new discussion instead.