I got the first part correct but how do I put the get height into the second part? Like would I just repeat everything the base did.. like "private double height" " public void setHeight " etc...


The design is this:
RightTriangle
variables: base, height
methods:
setBase - changes the base
setHeight - changes the height
getBase - returns the base
getHeight - returns the height
area - returns the rea of the triangle(1/2bh)

The output is suppose to be:
The base of the right triangle is 4.0
the height of the right triangle is 5.0
The area of the right triable is 10.0

My code so far is:

public class RightTriangle { 
  
  public static void main(String[] args) { 
    Triangle shape = new Triangle(); 
    
    shape.setBase(4.0);  
    shape.setHeight(5.0); 

    
    System.out.println("The base of the right triangle is " + shape.getBase()); 
    System.out.println("The height of the right triangle is " + shape.getHeight()); 
    System.out.println("The area of the right triangle is " + shape.area()); 
  } 
}

The class code is:

public class Triangle {  
  private double base; 
  

  public Triangle() { 
    base = 1;           
  } 

  public void setBase(double newBase) { 
    base = newBase;
  }
  

  public double area() { 
    double triangleArea; 
    
    triangleArea = 1/2 * base * height; 
    return(triangleArea);
  } 
  
  
  public double getBase() { 
    return(base); 
  } 
}

It look to me like you have all the code you need in front of you.

Copy the 'setBase(double newBase)' method in the Triangle class and redo it so that it sets the height. as you are already passing in a value for height in the main.

So basically like just copy all the code that setBase had, and put it in with height? I don't mean replacing it I need the two methods. But when I put them together like im pretty much repeating the codes accept one is for base and one is for height. It gives me a error. could u give me an example.

So basically like just copy all the code that setBase had, and put it in with height? I don't mean replacing it I need the two methods. But when I put them together like im pretty much repeating the codes accept one is for base and one is for height. It gives me a error. could u give me an example.

You already have an example in your code. If you look at the Triangle class, you/your teacher (whoever wrote it) declared a variable called 'base'. Then, you declared a method called setBase. If you want to declare a method called setHeight, you would need a variable called 'height' in your Triangle class. Then you would need a method that returns nothing (void) and sets the height. I'll give you one more example. . .

public class Person{
public String age;


public void setAge(String theAge){
age = theAge;
}

}
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.