I am attempting to create two players in the program I am writing. The issue is that there are three types of players that can be created but in order to reference them later I want them named as player one and player two. The three classes: "Human", "Computer", and "Karen" are derived classes of "Player". I am wondering how to create an array Player that I can then put player1 and player2 objects in. Is this possible to do?
int playerType = 0;
int highestScore = 0;
Player[] player = new Player[2]; // This is the array of type Player, the master class
Scanner userInput = new Scanner (System.in);
for (int i = 1; i < 3; i++)
{
System.out.println ("Choose a type of player:");
System.out.println ("1: Human");
System.out.println ("2: Computer (Threshold = 20)");
System.out.println ("3. Computer (Custom)");
System.out.print ("? : ");
playerType = Integer.valueOf(userInput.nextLine()); // gets input choice from user
switch (playerType)
{
case 1:
Human player[i] = new Human();
case 2:
Computer player[i] = new Computer();
case 3:
Karen player[i] = new Karen();
}
}
There is another method I tried, using an if statement to make an object playerOne or playerTwo based on what iteration of the for loop we were on (i). Unfortunately it doesn't look like java will compile it because there are multiple declarations of the same variable, even though only one of the declarations should run.
if(playerNumber == 1){
switch (playerType)
{
case 1:
Human playerOne = new Human();
case 2:
Computer playerOne = new Computer();
case 3:
Karen playerOne = new Karen();
}
}
if(playerNumber == 2){
switch (playerType)
{
case 1:
Human playerTwo = new Human();
case 2:
Computer playerTwo = new Computer();
case 3:
Karen playerTwo = new Karen();
}
}
The solution suggested by OldProgrammer is below, it worked perfectly. However, I am now having an issue calling methods of: Human, Computer, and Karen. I get the following error when trying to execute:
System.out.println(player[i].getName() + ", your turn");
Error Msg:
Main.java:38: error: cannot find symbol
System.out.println(player[i].getName() + ", your turn");
^
symbol: method getName()
location: class Player
1 error
The method in each class looks like this:
public class Karen extends Player {
private String playerName = "Karen";
public String getName(){
return playerName;
}
}
Thanks to OldProgrammer:
Change "Human player[i] = new Human();" to "player[i] = new Human();", etc.
Understanding how derived classes is really important and I'm glad this is how it works. Thank you very much and I hope this might help someone in the future. Here is my updated code for full clarification:
int highestScore = 0;
Player[] player = new Player[2];
Scanner userInput = new Scanner (System.in);
for (int i = 0; i < 2; i++)
{
System.out.println ("Choose a type of player:");
System.out.println ("1: Human");
System.out.println ("2: Computer (Threshold = 20)");
System.out.println ("3. Computer (Custom)");
System.out.print ("? : ");
playerType = Integer.valueOf(userInput.nextLine());
switch (playerType)
{
case 1:
player[i] = new Human();
case 2:
player[i] = new Computer();
case 3:
player[i] = new Karen();
}
}
Related
I have designed the battleship game to only have one ship hidden and now I have to implement another ship into the game. I am new to Java and was wondering if anybody could offer me a simple way to go about this. Do I need to create another method or just adjust the display river method?
public static void displayRiver(int[] river, boolean showShip) {
System.out.println();
System.out.print("|");
for (int val : river) {
switch (val) {
case -1: // No Ship
System.out.print("x");
break;
case 0: // Unknown
System.out.print(" ");
break;
case 1: // Ship Found
System.out.print(showShip ? "Y" : " ");
break;
}//switch
System.out.print("|");
}//for
System.out.println();
System.out.println();
}//displayRiver
// main method
public static void main(String[] arg) {
int riverLength = promptForInt("Please, enter the river lenght");
int [] shipArray = new int[riverLength];
int randomBattleshipLocation = new Random().nextInt(riverLength);
shipArray[randomBattleshipLocation] = 1;
boolean showShip = false ;
int userGuess;
do
{
displayRiver (shipArray, false);
userGuess = promptForInt(String.format("Guess, enter a location from 1 to " + riverLength));
userGuess = userGuess -1;
if(shipArray[userGuess] == 1)
{
System.out.println("Boom! ");
showShip = true;
displayRiver(shipArray, true);
}
else if(shipArray[userGuess] == -1)
{
System.out.println("Location was already hit, try again! ");
}
else if(shipArray[userGuess] == 0)
{
System.out.println("Splash...");
shipArray[userGuess] = -1 ;
}
} while(!showShip);
System.exit(0);
}
}
Your logic seems to be that an 1 in the array indicates a ship, and your ships apparently are never more than one in width.
You currently use the following to create one ship
int randomBattleshipLocation = new Random().nextInt(riverLength);
shipArray[randomBattleshipLocation] = 1;
So you could turn that into a method that creates a battleship, then call that as many times as you want for multiple ships. Just make sure that you don't assign a ship on top of another ship, or make another logical error (like trying to put 5 ships into a river of size 4, and it will loop forever trying to find space for ships).
Pseudo-code and not-so-pseudo-code:
for(int i = 0;i < shipsToAdd; i++) {
addShip(shipArray);
}
// Use a shared class-level Random object, don't do new Random().nextInt();
private static Random rnd = new Random();
private static addShip(int[] array) {
// Here you should loop to check if the array is already full of ships
// otherwise it's a design flaw that will result in an infinite loop with bad input
// loop until we find a shipless array index
int index = rnd.nextInt(array);
while(array[index] == 1)
index = rnd.nextInt(array);
array[index] = 1;
}
I am trying to make a movie database for some homework. Getting stuck adding deleting functionality. I get stuck trying when trying to re-add another movie because of my moivecount variable. If I delete movie3 if I have 4 movies then moviecount == 3 and when I try to add another movie it asks for movie4 again when I need to add movie3.
import java.util.*;
public class Interface {
private void run()
{
Scanner console = new Scanner(System.in);
Movie m1;
Movie m2;
Movie m3;
Movie m4;
String name, director;
int size, duration, option;
int moviecount = 0;
m1 = new Movie();
m2 = new Movie();
m3 = new Movie();
m4 = new Movie();
do {
System.out.println("Import new movie:(0), Delete movie from Database (2), View Movies: (3), Exit (9): ");
option = console.nextInt();
switch(option)
{
case 0:
if(moviecount == 0)
{
System.out.print("First Movie: ");
name = console.next();
m1.setName (name);
System.out.print("Director: ");
director = console.next();
m1.setDirector (director);
System.out.print("Size in MB: ");
size = console.nextInt();
m1.setSize(size);
System.out.print("Duration in minutes: ");
duration = console.nextInt();
m1.setDuration(duration);
moviecount++;
break;
}
else if(moviecount == 1)
{
System.out.print("Second Movie: ");
name = console.next();
m2.setName (name);
System.out.print("Director: ");
director = console.next();
m2.setDirector (director);
System.out.print("Size in MB: ");
size = console.nextInt();
m2.setSize(size);
System.out.print("Duration in minutes: ");
duration = console.nextInt();
m2.setDuration(duration);
moviecount++;
break;
}
else if(moviecount == 2)
{
System.out.print("Third Movie: ");
name = console.next();
m3.setName (name);
System.out.print("Director: ");
director = console.next();
m3.setDirector (director);
System.out.print("Size in MB: ");
size = console.nextInt();
m3.setSize(size);
System.out.print("Duration in minutes: ");
duration = console.nextInt();
m3.setDuration(duration);
moviecount++;
break;
}
else if(moviecount == 3)
{
System.out.print("Fourth Movie: ");
name = console.next();
m4.setName (name);
System.out.print("Director: ");
director = console.next();
m4.setDirector (director);
System.out.print("Size in MB: ");
size = console.nextInt();
m4.setSize(size);
System.out.print("Duration in minutes: ");
duration = console.nextInt();
m4.setDuration(duration);
moviecount++;
break;
}
else
{
System.out.print("Too many movies currently stored \n" );
break;
}
case 2: System.out.print("Which movie to delete? (1), (2), (3), (4) \n");
option = console.nextInt();
switch(option)
{
case 1: m1.setName(null);
m1.setDirector(null);
m1.setSize(0);
m1.setDuration(0);
System.out.print("Movie 1 deleted! ");
moviecount--;
break;
case 2: m2.setName(null);
m2.setDirector(null);
m2.setSize(0);
m2.setDuration(0);
moviecount--;
break;
case 3: m3.setName(null);
m3.setDirector(null);
m3.setSize(0);
m3.setDuration(0);
moviecount--;
break;
case 4: m4.setName(null);
m4.setDirector(null);
m4.setSize(0);
m4.setDuration(0);
moviecount--;
break;
}
case 3: System.out.print("Movies stored in database: \n");
System.out.print(m1.getName()+"\n");
System.out.print(m2.getName()+"\n");
System.out.print(m3.getName()+"\n");
System.out.print(m4.getName()+"\n");
}
}
while(option!=9);
}
public static void main(String[] args){
Interface intFace = new Interface();
intFace.run();
}
}
The problem lies in your design. You are running into this problem because you are not actually storing your movies anywhere. You are just creating 4 different movie objects and printing them out. If you changed the order of your print statements, they would be in a different order.
You are going to need to add in some sort of data structure - any array, a list, or some other sort of collection to store your movies in.
For example you might create a List to store your movie data.
//create new List called movieList
List movieList = new ArrayList();
//add movies to the list
movieList.add(m1);
movieList.add(m2);
movieList.add(m3);
movieList.add(m4);
//remove movies from the list by position
movieList.remove(0);
movieList.remove(1);
movieList.remove(2);
movieList.remove(3);
You can add your movies to the list using the Lists's add() method, and remove them using the remove() method. Keep in mind that remove removes the element at the index you provide, and the index starts at 0.
So, for example to remove the first movie, you would call movieList.remove(0).
The second movie would be at index 1, etc.
The answer to your specific implementation: you can't use that moviecount variable to determine in which slot you want to add a new move!
Instead: just walk over your movie objects. And push data into the first one that is empty.
Meaning: you could define a "isEmpty()" method on the Movie class that returns true if the name is null.
But just for the record: having and using these setter methods is a bad approach. Normally you would want to set all these values using the constructor of the movie class, as you don't want to allow to change that information later on.
And then you would simply do
movie1 = new...
when creating a new movie and
movie1 = null
to delete it later on. And then you can also easily check if a slot is taken:
if (movie1 == null) {...
I want to place the array questions in a loop in a method.
class QuizMethods{
String [] questions = new String [20];
String [] answers = new String [20];
String [] correctAnswers = new String [20];
int score;
void fillArrays(){
questions[0] = " Who is the lead guitarist of Led Zeppelin?";
correctAnswers[0] = "Jimmy Page";
questions[1] = " Who is the lead singer of Led Zeppelin?";
correctAnswers[1]= "Robert Plant";
questions[2] = " Who is the lead guitarist of Pink Floyd?";
correctAnswers[2] = "David Gilmour";
questions[3] = " Who is the bassist and main lyricist of Pink Floyd?";
correctAnswers[3]= "Roger Waters";
questions[4] = " Under which category of music are Pink Floyd entitled too?";
correctAnswers[4] = "Soft Rock";
questions[5] = " Under which category of music are Metallica entitled too?";
correctAnswers[5]= "Trash Metal";
questions[6] = " Who is the lead guitarist of Metallica?";
correctAnswers[6] = "Kirk Hammet";
questions[7] = " Who is the lead singer and rhythm guitarist of Metallica?";
correctAnswers[7]= "James Hetfield";
questions[8] = " Who is the lead guitarist of Guns n Roses?";
correctAnswers[8] = "Slash";
questions[9] = " Who is the lead singer of Guns n Roses?";
correctAnswers[9]= "Axl Rose";
questions[13] = " Under which category of music are Guns n Roses entitled too?";
correctAnswers[13]= "Hard Rock";
questions[11] = " Who is the bassist of Guns n Roses?";
correctAnswers[11]= "Duff McKagan";
questions[12] = " Name the biggest and most sold album of Pink Floyd";
correctAnswers[12] = "The Wall";
questions[13] = " Under which category of music are ZZ Top entitled too?";
correctAnswers[13]= "Blues Rock";
questions[14] = " Who is the lead guitarist and vocalist of ZZ Top?";
correctAnswers[14] = "Billy Gibbons";
questions[15] = " Who is considered as the king of blues?";
correctAnswers[15]= "BB King";
questions[16] = " Who was the lead singer for the popular band Queen?";
correctAnswers[16] = "Freddie Mercury";
questions[17] = " Who was the lead singer for the Heavy Metal band Black Sabbath?";
correctAnswers[17]= "Ozzy Osbourne";
questions[18] = " Who is the lead guitarist of Dire Straits?";
correctAnswers[18] = "Mark Knopfler";
questions[19] = " Complete the sentence.Alphaville released the song named _______ _______ in September 1984.";
correctAnswers[19]= "Forever Young";
}
void takeQuiz(){
// loop using arrays in this method.
}
void menu(){
System.out.println("1. Take Quiz");
System.out.println("2. Quiz Results");
System.out.println("3. Performance Comment");
System.out.println("4. Exit");
System.out.println("Choose from the above");
byte menu = Keyboard.readByte();
switch(menu){
case 1 :
takeQuiz();
case 2 :
quizResults();
case 3 :
performanceComment();
case 4 :
exit();
}
}
}
I used the menu for the user to choose what he wants to do.
All I need to know is how can I place the array in a loop in the method.
Any help is appreciated.
You can loop over an array using while or for-loop. Here I have used the for-loop to print the quiz question and store the user response in answers array.
Also it will be good if you use List interface implementations for your program.
void takeQuiz(){
Scanner keyboard = new Scanner(System.in);
for(int i=0;i<questions.length;i++)
{
System.out.println(questions[i]);
answers[i]= keyboard.nextLine();
}
}
You need a for loop. Because the index is important, you cannot use enhanced for loops.
void takeQuiz()
{
Scanner in = new Scanner(System.in);
String answer;
for(int i = 0; i < questions.size(); i++)
{
System.out.print(questions[i] + "\nAnswer: ");
answer = in.next();
answers[i] = answer;
if(answer.equalsIgnoreCase(correctAnswers[i])
{
score++;
}
else
{
score--;
}
}
}
Your switch statement also needs breaks to get out of the switch
switch(menu)
{
case 1 :
takeQuiz();
break;
case 2 :
quizResults();
break;
case 3 :
performanceComment();
break;
case 4 :
exit();
break; // Do not need break if exit() used `System.exit(0)`
case default: // Switch statements should include a `default` declaration for what to do when no cases are valid
exit();
break;
}
This question already has answers here:
If-else working, switch not [duplicate]
(9 answers)
Closed 6 years ago.
I started to learn Java some time ago and I thought that making a calculator that works in the terminal. Lately I added an Array list to store the history, and then something went wrong.
Calculator program:
import java.util.Scanner;
import java.util.ArrayList;
public class calc_case {
public static void main(String[] args) {
System.out.println("Welcom to The Calculator!");
double a;
double b;
double c;
Scanner input0;
int input = 0;
ArrayList<Double> history = new ArrayList<Double>();
while (input != 6) {
try {Thread.sleep(2000);} catch(InterruptedException ex) {Thread.currentThread().interrupt();}
a = 0; b = 0; c = 0; input = 0;
System.out.println("#################################################");
System.out.println("How can I help you?");
System.out.println("1-Add\n2-Subtrackt\n3-Devide\n4-Multiply\n5-Show history\n6-Exit");
input0 = new Scanner(System.in);
input = input0.nextInt();
switch (input) {
case 1: //add
System.out.println("Input two numbers:");
input0 = new Scanner(System.in);
a = input0.nextDouble();
input0 = new Scanner(System.in);
b = input0.nextDouble();
c = a + b;
System.out.println("Calculating... \nThe answer is: " + c );
break;
case 2: //subtrackt
System.out.println("Input two numbers:");
input0 = new Scanner(System.in);
a = input0.nextDouble();
input0 = new Scanner(System.in);
b = input0.nextDouble();
c = a - b;
System.out.println("Calculating... \nThe answer is: " + c );
break;
case 3: //devide
System.out.println("Input two numbers:");
input0 = new Scanner(System.in);
a = input0.nextDouble();
input0 = new Scanner(System.in);
b = input0.nextDouble();
c = a/b;
System.out.println("Calculating... \nThe answer is: " + c );
break;
case 4: //multiply
System.out.println("Input two numbers:");
input0 = new Scanner(System.in);
a = input0.nextDouble();
input0 = new Scanner(System.in);
b = input0.nextDouble();
c = a*b;
System.out.println("Calculating... \nThe answer is: " + c );
break;
case 5: //history
for (int x = 0; x < history.size(); x++) {
System.out.println(history.get(x));
}
case 6: //exit
System.out.println("Goodbye!\n Killing process... " + " OK");
default:
history.add(c);
}
}
}
}
After adding case 5 as 'Show history' and moving case 5 'Exit' to case 6 'Exit' after choosing option 5 ('Show history') I get this:
Goodbye!
Killing process... OK
I tried recompiling the program, deleting the class file, and pasting the program to a new file. What is wrong with my program?
OS: Ubuntu 16.04 Java version:
Java version: openjdk version "1.8.0_91"
Two things:
You forget the break after case 5; so you always fall through to the next case. So does the next case 6.
Your history is printed; but you never add a value to that history! There is not much sense in keeping a history, if that variable is only read from, but never written to.
And from a "clean coding" perspective: read about the "single layer of abstraction" principle. You really do not want to have so much code in one method; for example: each of your case blocks ... should go into its own method. You want to create small "units" that can be understood with one glance. Something that goes over 10 lines or more always requires more work than something smaller!
And you know; when each code would like this:
...
case 3:
divide();
break;
case 4:
multiply();
break;
dont you think you would have spotted your problem yourself?!
You do not have break in case 5, that is why it executes case 6 after finishing case 5
You are missing a break after your for loop in case 5:, so it falls through into case 6:.
Also, case 6: appears to be missing a break after the System.out statement.
This question already has answers here:
Switch without break
(7 answers)
Closed 6 years ago.
I am trying to create a very simple game in which several players of different types tries to guess a number in a range. I made a function to initialize an array of players, and I used switch-case to do it.
For some reason, if I put a number, it goes over all the cases after it, even if there are not enough cells in the array. For example, when entered 2, the value for type HUMAN, it did also the cases for 3 and 4- and players of types COMPUTER and GUMBLER are created.
Here is the code:
String name;
int count = 0;
System.out.println("How many players will participate?");
players = new Player[reader.nextByte()];
for (Player player: players)
{
count++;
System.out.print("\n1)Name of the player: ");
name = reader.next();
System.out.println("What will be its type? WRITE A NUMBER\n1- Monley\n2-Human\n3-Gumbler\n4-Computer");
switch (reader.nextInt())
{
case 1:
player = new Monkey (name, MAXIMUM, MINIMUM);
System.out.println("A moneky was created");
case 2:
player = new Human (name);
System.out.println("A human was created");
case 3:
player = new Gumbler (name, MAXIMUM, MINIMUM);
System.out.println("A gummbler was created");
case 4:
player = new Computer (name, MAXIMUM, MINIMUM);
System.out.println("A computer was created");
}
}
MAXIMUM and MINIMUM are the minimal and maximal numbers the number needs to be guessed can be. Human, Monkey, Computer and Gumbler are classes extending the absract class Player. players is the array of players (of type Player).
End your each case block with a break.
You will need a break statement in each case. See switch in java tutorial
String name;
int count = 0;
System.out.println("How many players will participate?");
players = new Player[reader.nextByte()];
for (Player player: players)
{
count++;
System.out.print("\n1)Name of the player: ");
name = reader.next();
System.out.println("What will be its type? WRITE A NUMBER\n1- Monley\n2-Human\n3-Gumbler\n4-Computer");
switch (reader.nextInt())
{
case 1:
player = new Monkey (name, MAXIMUM, MINIMUM);
System.out.println("A moneky was created");
break;
case 2:
player = new Human (name);
System.out.println("A human was created");
break;
case 3:
player = new Gumbler (name, MAXIMUM, MINIMUM);
System.out.println("A gummbler was created");
break;
case 4:
player = new Computer (name, MAXIMUM, MINIMUM);
System.out.println("A computer was created");
}
}