Why Am I Getting the Error "The Method Is Undefined for the Type"

Why am I getting the error "the Method Is Undefined for the type"?

getCost method is defined in order class and not in ShopCLI class. So your code:

ord.add(new Order(1, inputOuter, inputInner, inputSauce, 0));
System.out.println("You Made a " + inputInner + " with " + inputSauce + " Sandwich on " + inputOuter);
System.out.println("This Will Cost " + getCost());

Should be changed to

Order order = new Order(1, inputOuter, inputInner, inputSauce, 0);
ord.add(order);
System.out.println("You Made a " + inputInner + " with " + inputSauce + " Sandwich on " + inputOuter);
System.out.println("This Will Cost " + order.getCost());
^^^^^

The method ____ is undefined for the type ____

In java the only top level "things" are classes (and similar stuff such as interfaces and enums). Functions are not top level "things". They can exist only inside a class. Thus to call it you need to go through that class, or through an object of that class.

From the code you have written it seems that test is a non static method. In that case you need to create an object from that class, and run the method on it :

landEnclosure l = new landEnclosure();
l.test();

However, it seems that your intention is for 'test' to be a static method. In that case, declare it static and call it that way :

landEnclosure.test();

On a side note, the convention in Java is to name classes with an upper case first :

class LandEnclosure {

Trouble with Method undefined for a type Java

The error is that your method createTriangle() doesn't have a return type. Since you are returning a Triangle, you need to add that.

public static Triangle createTriangle() {

And continue with your normal code.

Also, a good catch from @JO3-W3B-D3V, the side1 and side2 are not globally accessible in the class, so you need to do:

public static double getHypotenuse(double side1, double side2) {
return Math.sqrt(Math.pow(side1, 2) + Math.pow(side2, 2));
}

So, your complete createTriangle() function becomes:

public static Triangle createTriangle(){
double side1 = 90;
double side2 = 80;
double hypotenuse = getHypotenuse(side1, side2);
Triangle thisTriangle = new Triangle(side1, side2, hypotenuse);
return thisTriangle;
}

The method is undefined for the type (object)

Option 1) Move the method into FoodEater. Need to also declare as static (for reasons....)

public static Food findLowestCalories(Food f1, Food f2, Food f3) {

Option 2) Declare an instance of the class so that you can call that method.

FoodMeasure measure = new FoodMeasure();
System.out.println(measure.findLowestCalories(hamburger, spaghetti, sausage));

Option 3) No external methods. Move all the logic of findLowestCalories into the main method.

(Array of objects) The method is undefined for the type

set_no is a method of Book, so you need to call it on an instance of Book, in this case b[i].

So, write b[i].set_no(sc.nextInt()) (and likewise for other methods).

Could you help me with this error "The method in undefined for the type"?

If you have already assigned a value to a variable, then use this variable. The variable already contains what you have written to it.

    BankAccount cc = new BankAccount(1115);
BankAccount dd = new BankAccount(1234);
BankAccount ee = new BankAccount(3333);
ArrayList<BankAccount> accounts
= new ArrayList<BankAccount>(Arrays.asList(cc, dd, ee));

Or:

   ArrayList<BankAccount> accounts
= new ArrayList<BankAccount>(Arrays.asList(
new BankAccount(1234);
new BankAccount(2222);
new BankAccount(3333);
));

asList() method fills in either list you have created or another list that has already been created.

For example:

 ArrayList<BankAccount> accounts
= new ArrayList<BankAccount>
(Arrays.asList(name_of_another_list));


Related Topics



Leave a reply



Submit