create a simple text-based console game that implements at least three (3) interfaces. code example
Example: create a simple text-based console game that implements at least three (3) interfaces.
import java.lang.*;
import java.util.*;
interface Name { //THIS IS THE FIRST INTERFACE
public void user_name();
}
interface Choice extends Name{ //THIS IS THE SECOND INTERFACE
public void choice_by_user();
}
interface nextStep extends Choice{ //THIS IS THE THIRD INTERFACE
public void next_Step();
}
//THE extends KEYWORD IS USED TO IMPLEMENT MULTIPLE INTERFACES
//AND AS WE HAVE 3 INTERFACES HERE WE ARE USING extends KEYWORD
public class input implements nextStep {
//THIS CLASS IMPLEMENTS ALL THE 3 INTERFACES AND THEIR METHODS.
public void user_name() {
Scanner sc=new Scanner(System.in);
System.out.println("Enter your name: ");
//ASKING THE USER INPUT AS GIVEN IN QUESTION
String name=sc.next();
System.out.println("Press 1 or 2 to select your game mode.\n1-Story\n2-Survival\n");
//PRINTING MESSAGES AS ASKED IN THE QUESTION
System.out.println("Press P to start Playing "+name);
//TAKING INPUT FROM THE USER
char c = sc.next().charAt(0);
if(c=='P') System.out.println("YOUR INPUT IS "+c);
//DISPLAYING THE USER OUTPUT
}
public void choice_by_user() {
//THIS METHOD IS FROM THE 2ND INTERFACE
//WE IMPLEMENT THIS METHOD AND DISPLAY APPROPRIATE MESSAGES
System.out.println("Thanks for input.");
System.out.println("The game is starting.....");
for(int i=0;i<5;i++)
System.out.println(".....");
}
public void next_Step() {
//THIS METHOD IMPLEMENTS THE LAST INTERFACE
System.out.println("\nNow We will go the game scene");
System.out.println("The game will start at the easy level \nand \nthen will move to higher level of difficulty.");
}
public static void main(String args[]) {
input obj = new input(); //WE CREATE THE OBJECT FOR THE INPUT CLASS WHICH IMPLEMENTS ALL THE 3 INTERFACES
/* obj IS USED TO CALL THE METHODS wHICH IMPLEMENTS ALL THE INTERFACES. */
obj.user_name(); //CALLING user_name() METHOD USING obj
obj.choice_by_user(); //CALLING choice_by_user() METHOD USING obj
obj.next_Step(); //CALLING next_step() METHOD USING obj
}
}