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();
		
		myTriObj.runIt();
	
		

		
		
	}

}

Object:

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);
		
		
	}

}

ok,
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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.