I'm currently on a self learning course and need to write a Test program for the following code and repeat steps by assigning different values to the member field.
public class DayTwo {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public void displayDay() {
int dayNumber = 1;
if (dayNumber == 1) {
System.out.println("Monday");
} else if (dayNumber == 2) {
System.out.println("Tuesday");
} else if (dayNumber == 3) {
System.out.println("Wednesday");
} else if (dayNumber == 4) {
System.out.println("Thursday");
} else if (dayNumber == 5) {
System.out.println("Friday");
} else if (dayNumber == 6) {
System.out.println("Saturday");
} else if (dayNumber == 7) {
System.out.println("Sunday");
} else {
System.out.println(dayNumber + " entered. This is not a valid date.");
}
}
}
Can anybody help?
If you want to purely test the code you have, remove public static void main(String[] args){} from DayTwo as it's not needed, and use the following Test class:
public class TestDay2 {
public static void main(String[] args) {
DayTwo example = new DayTwo();
example.displayDay();
}
}
However this will always print 'Monday' due to dayNumber being hard coded. You'll have to change this to test each if statement as I assume you haven't touched on loops yet.
int dayNumber = 1; it is hard coded value and will always satisfy the first condition skipping the rest of the statements, instead take input using Scanner or BufferedInputStream
Scanner sc = new Scanner(System.in);
int dayNumber = sc.nextInt();
First o all, Classes that dont run by themselves dont come with a main method. Another mistake of your code is that it will always print "Monday".
For making a test class
public class Test{
public static void main(String[] args){
DayTwo dayTwo = new DayTwo();
dayTwo. displayDay();
}
}
Something like this....
import java.util.*;
public class DayTwo
{
public static void main(String[] args)
{
int number,choice=1;
Scanner sc = new Scanner(System.in);
while(choice!=0)
{
System.out.println("Enter the day number: ");
number=sc.nextInt();
displayDay(number);
System.out.println("Press 0 to exit, 1 to continue:");
choice=sc.nextInt();
}
}
public static void displayDay(int dayNumber)
{
if (dayNumber == 1) {
System.out.println("Monday");
} else if (dayNumber == 2) {
System.out.println("Tuesday");
} else if (dayNumber == 3) {
System.out.println("Wednesday");
} else if (dayNumber == 4) {
System.out.println("Thursday");
} else if (dayNumber == 5) {
System.out.println("Friday");
} else if (dayNumber == 6) {
System.out.println("Saturday");
} else if (dayNumber == 7) {
System.out.println("Sunday");
} else {
System.out.println(dayNumber + " entered. This is not a valid date.");
}
}
}
To follow the instructions just cut and paste the dayNumber declaration so it is a member field (between class declaration and main declaration). Then create a new DayTwo in main and you can assign dayNumber to different things and print it.
public class DayTwo {
// now a dayNumber belongs to every DayTwo object
int dayNumber = 1;
// main is static and belongs to the DayTwo class
public static void main(String[] args) {
// make a new DayTwo object
DayTwo days = new DayTwo();
// displays Monday
days.displayDay();
days.dayNumber = 4;
// displays Thursday
days.displayDay();
for (int i = 1; i <= 7; i++) {
days.dayNumber = i;
// displays all days
days.displayDay();
}
}
public void displayDay() {
if (dayNumber == 1) {
System.out.println("Monday");
} else if (dayNumber == 2) {
System.out.println("Tuesday");
} else if (dayNumber == 3) {
System.out.println("Wednesday");
} else if (dayNumber == 4) {
System.out.println("Thursday");
} else if (dayNumber == 5) {
System.out.println("Friday");
} else if (dayNumber == 6) {
System.out.println("Saturday");
} else if (dayNumber == 7) {
System.out.println("Sunday");
} else {
System.out.println(dayNumber + " entered. This is not a valid date.");
}
}
}
You don't need anything more complicated than that.
Related
The code compiles without any issue, then I enter 0 it plays, then when entering X, it crashes with the error in set(playerVal, scan.nextInt());
public static void main(String[] args) {
createBoard(3,3);
int turn = 0;
int playerVal;
int outcome;
java.util.Scanner scan = new
java.util.Scanner(System.in);
do {
displayBoard();
playerVal = (turn % 2 == 0)? NOUGHT : CROSS;
if (playerVal == NOUGHT) {
System.out.println ("\n-0's turn-");
} else {
System.out.println("\n-X's turn-");
}
System.out.print("Enter row and Column:");
try {
set(playerVal, scan.nextInt());
} catch (IllegalArgumentException ex)
{System.err.println(ex);}
turn ++;
outcome = winOrTie();
} while ( outcome == -2 );
displayBoard();
switch (outcome) {
case NOUGHT:
System.out.println("0 wins!");
break;
case CROSS:
System.out.println("X wins!");
break;
case 0:
System.out.println("Tie.");
break;
}
}
}
Be able to play the game when entering the 0 and X.
I have a game that's running perfectly. I want to put a line of code that asks the player if they want to play again at the end of the game. I would also like to keep a score system for every player and computer win.
I'm having trouble with the input = Integer.parseInt(sc.nextInt()); line
import java.util.Scanner;
public class Sticks {
public static boolean whoStart(String choice) {
int ran = (int) (Math.random() * 2 + 1);
String ht = "";
switch (ran) {
case 1:
ht = "head";
break;
case 2:
ht = "tails";
}
if (ht.equals(choice.toLowerCase())) {
System.out.println("you start first");
return true;
} else {
System.out.println("computer starts first");
return false;
}
}
public static int playerTurn(int numberOfSticks) {
System.out.println(" \nthere are " + numberOfSticks + " sticks ");
System.out.println("\nhow many sticks do you wanna take? 1 or 2?");
Scanner in = new Scanner(System.in);
int sticksToTake = in.nextInt();
while ((sticksToTake != 1) && (sticksToTake != 2)) {
System.out.println("\nyou can only take 1 or 2 sticks");
System.out.println("\nhow many sticks do you wanna take?");
sticksToTake = in.nextInt();
}
numberOfSticks -= sticksToTake;
return numberOfSticks;
}
public static int computerTurn(int numberOfSticks) {
int sticksToTake;
System.out.println("\nthere are " + numberOfSticks + " sticks ");
if ((numberOfSticks - 2) % 3 == 0 || (numberOfSticks - 2 == 0)) {
sticksToTake = 1;
numberOfSticks -= sticksToTake;
} else {
sticksToTake = 2;
numberOfSticks -= sticksToTake;
}
System.out.println("\ncomputer took " + sticksToTake + " stick ");
return numberOfSticks;
}
public static boolean checkWinner(int turn, int numberOfSticks) {
int score = 0;
int input;
int B = 1;
int Y=5, N=10;
if ((turn == 1) && (numberOfSticks <= 0)) {
System.out.println("player lost");
return true;
}
if ((turn == 2) && (numberOfSticks <= 0)) {
System.out.println("player won");
score++;
return true;
}
System.out.println("Your score is "+ score);
System.out.println("Do you want to play again? Press (5) for Yes / (10) for No");
// ----- This line -----
input = Integer.parseInt(sc.nextInt());
if (input == Y) {
B = 1;
System.out.println("Rock, Paper, Scissors");
} else if (input == N) {
System.exit(0);
System.out.println("Have A Good Day!");
}
}
public static void main(String args[]) {
int turn;
int numberOfSticks = 21;
Scanner in = new Scanner(System.in);
System.out.println("choose head or tails to see who starts first");
String choice = in.next();
if (whoStart(choice) == true) {
do {
turn = 1;
numberOfSticks = playerTurn(numberOfSticks);
if (checkWinner(turn, numberOfSticks) == true) {
break;
};
turn = 2;
numberOfSticks = computerTurn(numberOfSticks);
checkWinner(turn, numberOfSticks);
} while (numberOfSticks > 0);
} else {
do {
turn = 2;
numberOfSticks = computerTurn(numberOfSticks);
if (checkWinner(turn, numberOfSticks) == true) {
break;
};
turn = 1;
numberOfSticks = playerTurn(numberOfSticks);
checkWinner(turn, numberOfSticks);
} while (numberOfSticks > 0);
}
}
}
The title of your question almost answered you what you need to add: a loop!
I suggest you to refactor your function main and extract all your game logic from it to be stored within a dedicated function for the sake of the readability. Let's call it startGame().
Your main is going to become shorter and can represent a good location to introduce this loop, such as:
public static void main(String[] a) {
boolean isPlaying = true;
Scanner in = new Scanner(System.in);
while(isPlaying) {
startGame();
// Your message to continue or stop the game
if(in.next().equalsIgnoreCase("No")) {
isPlaying = false;
}
}
}
I recommend you to use a boolean that is checked in your while loop rather than using a break statement, as it brings a better control flow in your application.
Just put everything in a while(true) loop and use a break; if they choose no. Something like:
static int playerPoints = 0;
public static void main(String args[]) {
int turn;
int numberOfSticks = 21;
Scanner in = new Scanner(System.in);
while(true){
...
System.out.println("You have " + playerPoints + " points!")
System.out.println("Do you want to play again?");
if (!in.nextLine().toLowerCase().equals("yes")){
break;
}
}
}
Edit: ZenLulz's answer is better than this one, mainly because it encourages better programming practice. Mine works but isn't the best way to solve the issue.
I am trying to make a program that gets the day from the user input and then tells them the day before and the day after.The user should also be able to input how many days to add and the program should output that day.
example user enters 1 = Monday, tomorrow is = 2 Tuesday yesterday was = 3 Sunday
if user says its Monday(1) and adds 12 days the output should be Saturday(6)
The problem is whenever "theWeekDay" is greater than 7 it outputs nothing because TheDay(); doesn't have a condition for something greater than 7. Please help me!
Thanks you so much!
import java.util.Scanner;
import java.util.Scanner;
public class Problem_3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int theWeekDay;
System.out.println("What Day Is It?");
theWeekDay = input.nextInt();
Days one = new Days(theWeekDay);
System.out.println("Today It Is: ");
one.TheDay(theWeekDay);
System.out.println("Yesterday It Was: ");
one.PreviousDay(theWeekDay);
System.out.println("Tomorrow It Is: ");
one.NextDay(theWeekDay);
System.out.println("How Many Days To Add?");
int x = input.nextInt();
System.out.println("Now It Is: ");
one.AddedDays(x);
}
}
class Days {
private int theWeekDay;
public Days(int theWeekDay) {
this.theWeekDay = theWeekDay;
}
public int getTheWeekDay() {
return theWeekDay;
}
public void setTheWeekDay(int theWeekDay) {
this.theWeekDay = theWeekDay;
}
public int TheDay(int theWeekDay) {
// an arra days of week + then add days in it
if (theWeekDay == 0) {
theWeekDay = theWeekDay + 7;
}
if (theWeekDay == 1) {
System.out.println("Monday");
} else if (theWeekDay == 2) {
System.out.println("Tuesday");
} else if (theWeekDay == 3) {
System.out.println("Wednsday");
} else if (theWeekDay == 4) {
System.out.println("Thursday");
} else if (theWeekDay == 5) {
System.out.println("Friday");
} else if (theWeekDay == 6) {
System.out.println("Saturday");
} else if (theWeekDay == 7) {
System.out.println("Sunday");
}
return theWeekDay;
}
public int PreviousDay(int theWeekDay) {
theWeekDay = theWeekDay - 1;
return TheDay(theWeekDay);
}
public int NextDay(int theWeekDay) {
theWeekDay = theWeekDay + 1;
if (theWeekDay > 7) {
theWeekDay = 1;
}
return TheDay(theWeekDay);
}
public int AddedDays(int AddedDays) {
getTheWeekDay();
theWeekDay = theWeekDay + AddedDays;
return TheDay(theWeekDay);
}
}
If you want to assume value greater that 7 as valid, you should definitely use modulo operation. Something like this:
if(theWeekDay > 7) {
theWeekDay = theWeekDay % 7;
}
Otherwise you should throw and exception.
your if else must cover all cases ....
add an else after
else if(theWeekDay == 7){
System.out.println("Sunday");
}
something like:
if(theWeekDay == 1){
System.out.println("Monday");
}else if(theWeekDay == 2){
System.out.println("Tuesday");
}else if(theWeekDay == 3){
System.out.println("Wednsday");
}else if(theWeekDay == 4){
System.out.println("Thursday");
}else if(theWeekDay == 5){
System.out.println("Friday");
}else if(theWeekDay == 6){
System.out.println("Saturday");
}else if(theWeekDay == 7){
System.out.println("Sunday");
}else{
System.out.println("Invalid input");
}
return theWeekDay;
As user629735 said, use modulo in your formula.
public int AddedDays(int AddedDays) {
getTheWeekDay();
theWeekDay = (theWeekDay + AddedDays) % 7;
return TheDay(theWeekDay);
}
I made some command line games and at the end of the game I want to ask if the player wants to play again. You can see in the code how I made but it's not working and I don't know why.
Can anybody help me?
import java.util.Scanner;
//WIP
public class GuessingGame2 {
static Scanner userInput = new Scanner(System.in);
static int randNumber;
static int guessNumber;
static boolean gameStatus;
static void checkNum(int x) {
guessNumber++;
if(x == randNumber) {
gameStatus = true;
} else if(x < randNumber) {
System.out.println("Too small!");
} else {
System.out.println("Too big!");
}
}
static void checkAnsw() {
if(userInput.hasNextLine()) {
if(userInput.nextLine() == "Y" || userInput.nextLine() == "y") {
guessGame();
} else if(userInput.nextLine() == "N" || userInput.nextLine() == "n") {
} else {
System.out.print("Y or N ");
checkAnsw();
}
} else {
System.out.print("Y or N ");
userInput.next();
checkAnsw();
}
}
static void guessGame() {
randNumber = (int) (Math.random() * 1000);
guessNumber = 0;
gameStatus = false;
System.out.println("Try to guess a number from 1 to 1000!");
while(gameStatus == false) {
System.out.print("Your guess: ");
if(userInput.hasNextInt()) {
checkNum(userInput.nextInt());
} else {
System.out.println("You need to choose a number!");
userInput.next();
}
}
System.out.println("You guessed the number in " + guessNumber + " tries.");
System.out.print("Do you want to play again? Y or N ");
checkAnsw();
}
public static void main(String[] args) {
guessGame();
}
}
Change your checkAnsw method to this:
static void checkAnsw() {
if(userInput.hasNextLine()) {
if(userInput.nextLine().equalsIgnoreCase("y")) {
guessGame();
} else if(userInput.nextLine().equalsIgnoreCase("n")) {
} else {
System.out.print("Y or N ");
checkAnsw();
}
} else {
System.out.print("Y or N ");
userInput.next();
checkAnsw();
}
}
You cannot compare Strings with the = as they are objects. Use the .equals method to compare Strings.
Your code works fine, I have copied and pasted it in Eclipse and this is the output:
Try to guess a number from 1 to 1000!
Your guess: eeeee
You need to choose a number!
Your guess: 1
Too small!
Your guess: 2
Too small!
Your guess: 2
I don't understand which is your problem, try to explain better
First of all I am not asking anyone to do anything just need a little help to fix this bug with boolean. I put false but the program stops. I got two parts to the program.
First part where i did the calculations:
class FibonacciNumbers {
FibonacciNumbers() {} //default constructor
public int fOf(int n) {
if (n == 0) //the base case
{
return 0;
} else if (n == 1) {
return 1;
} else {
return fOf(n - 1) + fOf(n - 2);
}
}
}
Second where the main method is:
import java.util.*;
public class FibonacciNumbersTesters {
public static void main(String[] args) {
FibonacciNumbers fNumbers = new FibonacciNumbers(); //creates new object
Scanner in = new Scanner(System.in);
String again;
String test;
boolean IsRepeat = true;
boolean isQuit;
try {
isQuit = false;
while (!isQuit) {
System.out.print("Enter the number you want to convert to Fibanocci('q' to quit): ");
int n = in.nextInt();
System.out.print("The Fibanocci number for " + n + " is: ");
n = fNumbers.fOf(n);
System.out.println(n);
System.out.print("Do you want to run again? (Y or N): ");
again = in.next();
if (again.equalsIgnoreCase("N")) {
System.out.println("Thank you! Please terminate the program by entering 'Q' or 'q' OR you can cotinue by entering anything else: ");
String toQuit = in.next();
if ((toQuit.charAt(0) == 'q') || (toQuit.charAt(0) == 'Q')) {
System.out.println("Good-bye!");
isQuit = true;
}
} else {
IsRepeat = true;
}
}
} catch (InputMismatchException ex) {
test = in.nextLine();
if ((test.charAt(0) == 'q') || (test.charAt(0) == 'Q')) {
System.out.println("Good-bye!");
isQuit = true;
} else {
System.out.println("Invalid input!");
System.out.println("Try again! ");
isQuit = false;
}
}
}
}
This part where i put isQuit = false; at the end it just stops. I want it to continue.
Try putting your try catch statement inside of your while loop.