Reputation: 17
I am currently working on a school project and I am suppose to be able to call for my program to call for a non-static method that would still move my current number forward and backwards. I am at a loss. What exactly should I do? Here is my coding thus far and everything works minus implementing the non-static options.
import java.util.Scanner;
public class PictureViewer {
final static int MAX_NUMBER = 8;
final static int MIN_NUMBER = 1;
public static void main(String[] args) {
showMenu();
}
public static void showMenu() {
Scanner input = new Scanner(System.in);
int current_number = MIN_NUMBER;
boolean Continuation = true;
while (Continuation) {
System.out.println("Select One of the Options");
System.out.println("Option 1: Forward");
System.out.println("Option 2: Backward");
System.out.println("Option 3: CreateFileName");
System.out.println("Option 4: CreateRandomName");
System.out.println("Option 5: Non-Static Forward");
int Menu_Option = input.nextInt();
switch (Menu_Option) {
case 1:
current_number = forward(current_number);
System.out.println("The Current Number is: " + current_number);
break;
case 2:
current_number = backward(current_number);
System.out.println("The Current Number is: " + current_number);
break;
case 3:
String fileName = createFileName(current_number);
System.out.println("File Name: " + fileName);
break;
case 4:
fileName = createRandomName(current_number);
System.out.println("File Name: " + fileName);
break;
}
}
}
public static int forward(int current_number) {
if (current_number >= MAX_NUMBER) {
System.out.println(MIN_NUMBER);
current_number = MIN_NUMBER;
} else {
System.out.println(current_number++);
}
return current_number;
}
public static int backward(int current_number) {
if (current_number <= MIN_NUMBER) {
System.out.println(current_number);
current_number = MIN_NUMBER;
} else current_number --; {
}
return current_number;
}
public static String createFileName(int current_number) {
return "Picture" + current_number + ".gif";
}
public static String createRandomName(int current_number) {
current_number = MIN_NUMBER + (int) (Math.random() * MAX_NUMBER);
return "Picture" + current_number + ".gif";
}
public void forward () {
// overloaded method, use global variable as input and output
}
public void backward () {
// overloaded method, use global variable as input and output
}
}
Upvotes: 0
Views: 75
Reputation: 1
You don't need to make all methods static just keep only Main() static. Using this trick you will be able to access non-static methods easily and also this will save you from Critical Section problem.Follpwing are steps to implement:
1. Remove static keyword from all methods except main().
2.Replace
showOptions()
With
new PictureViewer().showOptions();
Hope this solves your problem :)
Upvotes: 0
Reputation: 623
OK now is almost everything, please elaborate better your questions like someone willing to learn, not only " is not working", just a free advice ;-)
import java.util.Scanner;
public class PictureViewer {
final static int MAX_NUMBER = 8;
final static int MIN_NUMBER = 1;
private int currentNumber;
public static void main(String[] args) {
showMenu();
}
public static void showMenu() {
Scanner input = new Scanner(System.in);
int current_number = MIN_NUMBER;
boolean Continuation = true;
while (Continuation) {
System.out.println("Select One of the Options");
System.out.println("Option 1: Forward");
System.out.println("Option 2: Backward");
System.out.println("Option 3: CreateFileName");
System.out.println("Option 4: CreateRandomName");
System.out.println("Option 5: Non-Static Forward");
int Menu_Option = input.nextInt();
switch (Menu_Option) {
case 1:
current_number = forward(current_number);
System.out.println("The Current Number is: " + current_number);
break;
case 2:
current_number = backward(current_number);
System.out.println("The Current Number is: " + current_number);
break;
case 3:
String fileName = createFileName(current_number);
System.out.println("File Name: " + fileName);
break;
case 4:
fileName = createRandomName(current_number);
System.out.println("File Name: " + fileName);
break;
case 5:
PictureViewer pv = new PictureViewer();
pv.setCurrentNumber(current_number);
pv.forward();
current_number = pv.getCurrentNumber();
System.out.println("The Current Number is: " + current_number);
break;
}
}
}
public static int forward(int current_number) {
if (current_number >= MAX_NUMBER) {
System.out.println(MIN_NUMBER);
current_number = MIN_NUMBER;
} else {
System.out.println(current_number++);
}
return current_number;
}
public static int backward(int current_number) {
if (current_number <= MIN_NUMBER) {
System.out.println(current_number);
current_number = MIN_NUMBER;
} else
current_number--;
{
}
return current_number;
}
public static String createFileName(int current_number) {
return "Picture" + current_number + ".gif";
}
public static String createRandomName(int current_number) {
current_number = MIN_NUMBER + (int) (Math.random() * MAX_NUMBER);
return "Picture" + current_number + ".gif";
}
public void forward() {
// overloaded method, use global variable as input and output
this.currentNumber = this.currentNumber +1;
}
public void backward() {
// overloaded method, use global variable as input and output
}
public int getCurrentNumber() {
return currentNumber;
}
public void setCurrentNumber(int currentNumber) {
this.currentNumber = currentNumber;
}
}
Upvotes: 0
Reputation: 30849
You don't need to have all the methods defined as static
in order to be called from main
. You can create an object in main
and call the same method. This is what I would do:
main
) non static (i.e. remove static
keyword)Change main
method implementation to create an object of PictureViewer
class and call showMenu
, e.g.
public static void main(String[] args) {
new PictureViewer().showMenu();
}
Upvotes: 2