I’m very new in Java and I have a small question. I believe it is due to some misunderstanding of the concepts.
So, I have main class menu:
/**
* menu.java
*/
public class menu {
public void run() {
println ("1. Option#1.");
println ("2. Option#2.");
println ("============");
int choose = readInt("Enter a choice:");
if (choose == 1) {
// QUESTION>>>>> // ### how can I call class option1.java here?
}
}
/**
* option1.java
*/
public class option1 {
public void scriepedos () {
setFont("Times New Roman-24");
while (true) {
String str = readLine("Please enter a string: ");
if (str.equals("")) break;
String rev = reverseString(str);
println(rev);
}
}
private String reverseString(String str) {
String result = "";
for (int i=0; i<str.length();i++){
result=str.charAt(i)+result;
}
return result.toLowerCase();
}
}
Many thanks in advance. Leo
You need an instance of option1 to call upon e.g.
Alternatively you can make the method
static. That means that you don’t need the corresponding instance of the object e.g. inoption1.javathen in
main.javaThe above isn’t very OO. You’re now making use of the fact that you can have an object encapsulating state etc. and is a much more procedural style.
Notes:
Option1,Main