Include a call in the main method
Hi,
I have created a program call 'Fruits' and have added another class call 'FruitOrder'.
How do I call a method by including a call in the main method?
Code for FruitOrder is:
public class FruitOrder {
private char fruitOrder;
private double weight;
private String message;
FruitOrder (char fruitCode, double weight) {
this.fruitCode = fruitCode;
this.weight = weight;
}
public String calculate() {
double price = 0;
String fruitType = "";
boolean isValidCode = true;
switch (friutCode) {
case 'A':
fruitType ="Apple";
price = weight * 1.75;
break;
case 'B':
fruitType ="Banana";
price = weight * 2.65;
break;
default:
isValidCode = false;
message = "iInvalid fruit cade entered";
break;
}
if (isValidCode) {
message = "You are buying " + weight + "Kgs of " + fruitType + " for $" + price;
}
return message;
}
}
This code is entered in the 'Fruits' class:
public static void case1() {
FruitOrder fo = new FruitOrder ('A', .5);
System.out.println ("case 1: " + "\n" + fo.calculate());
}
This is my question? how do I call this method by including a call in the main method as follow:
case1();
Re: Include a call in the main method
Quote:
how do I call this method
To call a static method in a class use the classname dot the method name: ClassName.methodName();
Re: Include a call in the main method
Re: Include a call in the main method
Quote:
Originally Posted by
Andyroxxx
This is my question? how do I call this method by including a call in the main method as follow:
case1();
Try this:
you call this case1() method into main method like below...
Code:
Fruits fr=new Fruits();
fr.case1();
Re: Include a call in the main method
It's a static method.
You don't need (and shouldn't use) an instance of Fruit to call it.
is all you should use.