+ 1

Java error with arguments differing in length

I’m trying to write a code that determines the area of a triangle. I have a separate class that sets the triangles height and base from a scanner object. The scanner looks for a double. The program then determines the area with the given information. Here’s the problem. When it runs it says that the triangle.setBase() and triangle.setHeight() says that the actual and formal argument lists differ in length. What does this mean and how can I fix it?

10th Jul 2019, 2:43 AM
Tyler
3 Respostas
+ 1
import java.util.Scanner; public class TriangleArea { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); Triangle triangle1 = new Triangle(); Triangle triangle2 = new Triangle(); double base, height; base = scnr.nextDouble(); height = scnr.nextDouble(); // Read and set base and height for triangle1 (use setBase() and setHeight()) triangle1.setBase(); triangle1.setHeight(); // Read and set base and height for triangle2 (use setBase() and setHeight()) // Determine larger triangle (use getArea()) triangle1.getArea(); System.out.println("Triangle with larger area:"); // Output larger triangle's info (use printInfo()) triangle1.printInfo(); } } //This is the next class public class Triangle { private double base; private double height; public void setBase(double userBase){ base = userBase; } public void setHeight(double userHeight) { height = userHeight; } public double getArea() { double area = 0.5 * base * height; return area; } public void printInfo() { System.out.println("Base: " + base); System.out.println("Height: " + height); System.out.println("Area: " + getArea()); } }
10th Jul 2019, 2:48 AM
Tyler
+ 1
never mind, i figured that out...but the next problem I am running into is that i have to conpare another triangle using different number inputs with the same information. Do I just re-scan again with the same information?
10th Jul 2019, 2:55 AM
Tyler
0
yes you must do it twice //first ... //second base = scnr.nextDouble(); height = scnr.nextDouble(); triangle2.setBase(base); triangle2.setHeight(height);
10th Jul 2019, 12:16 PM
zemiak