Question

In: Computer Science

Java Proect Project Outcomes Develop a Java program that: • creates a user designed class •...

Java Proect

Project Outcomes

Develop a Java program that:

creates a user designed class

uses proper design techniques including reading UML Class

Diagrams

reads input from the keyboard using a Scanner Object and its

methods

uses selection (if and if else) statements

uses iteration (while, do while or for) statements

uses String comparison methods.

follows standard acceptable programming practices.

handles integer overflow errors

Prep Readings:

Zybooks chapter 1 - 6

1. General

The game of Stix is a simplified version of an ancient game and somehow

looks like this:

It is played by two players

A number of sticks (like matches) are placed on a table.

The first player takes 1, 2 or 3 sticks away, provided that there that

many on the table.

Then the second player takes 1, 2 or 3 sticks away (if possible), and so

on.

Whoever takes the last stick, loses (!)

Your program does the following:

1.

It asks the initial number or stix (between 5 and 30)

2.

It asks if the computer (program) takes first

3.

It then lets the user and the computer take turns until the number of

stix drops to 0.

4.

Depending on who took the last stix, it declares the winner.

A simple run looks like this (Computer goes first and wins)

How many stix to begin with? [5 - 30] > 12

Computer goes first? [y/n] > y

Stix on the table: ||||||||||||

Computer takes 3

Stix on the table: |||||||||

How many stix to take? [1 - 3] > 2

Stix on the table: |||||||

Computer takes 2

Stix on the table: |||||

How many stix to take? [1 - 3] > 1

Stix on the table: ||||

Computer takes 3

Stix on the table: |

How many stix to take? [1 - 1] > 1

Stix on the table:

I win!

Here is another short run (Player goes first and wins)

How many stix to begin with? [5 - 30] > 14

Computer goes first? [y/n] > n

Stix on the table: ||||||||||||||

How many stix to take? [1 - 3] > 1

Stix on the table: |||||||||||||

Computer takes 1

Stix on the table: ||||||||||||

How many stix to take? [1 - 3] > 3

Stix on the table: |||||||||

Computer takes 1

Stix on the table: ||||||||

How many stix to take? [1 - 3] > 3

Stix on the table: |||||

Computer takes 1

Stix on the table: ||||

How many stix to take? [1 - 3] > 3

Stix on the table: |

Computer takes 1

Stix on the table:

You win!

There are of course two more possible situations: a) Computer goes first,

Player wins and b) Player goes first, Computer wins. Actually, all four can

happen. So be prepared for that!

Solutions

Expert Solution

Thanks for the question.


Below is the code you will be needing  Let me know if you have any doubts or if you need anything to change.


Thank You !!


===========================================================================

import java.util.Random;
import java.util.Scanner;

public class Stix {

    private static Scanner scanner = new Scanner(System.in);
    private static Random random = new Random();

    public static void main(String[] args) {

        int count = getStickCount();
        boolean computerFirst = computerGoesFirst();
        displaySticks(count);
        if (computerFirst) {
            int computer = getComputerCount(count);
            System.out.println("Computer takes " + computer);
            count -= computer;
            displaySticks(count);
        }
        while (true) {

            int player = getUserCount(count);
            displaySticks(count-player);
            if (count == 1 && player==1) {
                System.out.println("I win!");
                break;
            }
            count -= player;
            int computer = getComputerCount(count);
            System.out.println("Computer takes " + computer);

            displaySticks(count-computer);
            if(count == 1 && computer==1){
                System.out.println("You win!");
                break;
            }
            count -= computer;
        }


    }

    private static int getUserCount(int currentStix) {

        int count;
        while (true) {
            System.out.printf("How many stick to take? [1 - 3] > ");
            count = scanner.nextInt();
            if (1 <= count && count <= 3 && count <= currentStix) {
                return count;
            }
        }
    }

    private static int getStickCount() {

        int count = 0;
        while (true) {

            System.out.print("How many stix to begin with?[5-30] >");
            count = scanner.nextInt();
            scanner.nextLine();
            if (5 <= count && count <= 30) return count;
        }
    }

    private static void displaySticks(int count) {

        System.out.print("Stix on the table ");
        for (int i = 1; i <= count; i++)
            System.out.printf("|");
        System.out.println();
    }

    private static boolean computerGoesFirst() {

        char letter;
        while (true) {
            System.out.print("Computer goes first? [y/n] > ");
            letter = scanner.nextLine().charAt(0);
            if (letter == 'y' || letter == 'Y') return true;
            else if (letter == 'n' || letter == 'N') return false;
            else continue;
        }
    }

    private static int getComputerCount(int count) {

        if (count > 3) return 1 + random.nextInt(3);
        else if (count == 3) return 2;
        else if (count == 2) return 1;
        else return 1;
    }
}


Related Solutions

in. java Write a program that reads a string from the user, and creates another string...
in. java Write a program that reads a string from the user, and creates another string with the letters from in reversed order. You should do this using string concatenation and loops. Do not use any method from Java that does the work for you. The reverse string must be built. Do not just print the letters in reversed order. Instead, concatenate them in a string. --- Sample run: This program will create a string with letters in reversed order....
Develop a Java program for this problem where the user inputs an Earth age and the...
Develop a Java program for this problem where the user inputs an Earth age and the program will then display the age on Mercury, Venus, Jupiter, and Saturn. The values for d are listed in the table. Planet d = Approximate Number of Earth Days for This Planet to Travel Around the Sun Mercury 88 Venus 225 Jupiter 4380 Saturn 10767
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple The class Apple DOES NOT HAVE a main method Some of the attributes of Apple are Type: A string that describes the apple.  It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith Weight: A decimal value representing...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter five floating point values from the keyboard (warning: you are not allowed to use arrays or sorting methods in this assignment - will result in zero credit if done!). Have the program display the five floating point values along with the minimum and maximum values, and average of the five values that were entered. Your program should be similar to (have outputted values be...
Exercise Overview Implement a Java program that creates math flashcards for elementary grade students. User will...
Exercise Overview Implement a Java program that creates math flashcards for elementary grade students. User will enter his/her name, the type (+, -, *, /), the range of the factors to be used in the problems, and the number of problems to work. The system will provide problems, evaluate user responses to the problems, score the problems, and provide statistics about the session at the end. Functional Requirements User enters name at the beginning of a session. System covers four...
JAVA PROGRAM: Creates a Bank Account class with one checking account and methods to withdraw and...
JAVA PROGRAM: Creates a Bank Account class with one checking account and methods to withdraw and deposit. Test the methods in the main function.
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The program asks the user questions and then creates a paragraph using the user’s answers. 2.The program must perform the following: a.Uses a Scanner object to ask the user: (The program asks for no other information) i.Full Name (First and Last name only) - stores this Full Name in one String object variable. ii.Age – must be read in as an int. iii.Profession or expected...
Project Outcomes: Develop a python program that uses:  decision constructs  looping constructs  basic...
Project Outcomes: Develop a python program that uses:  decision constructs  looping constructs  basic operations on an list of objects (find, change, access all elements)  more than one class and has multiple objects Project Requirements: 1. Develop a simple Hotel program. We will have two classes, a Hotel class representing an individual hotel and a Room class. The Hotel class will contain several Room objects and will have several operations. We will also have a driver program...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static void main(String[] args) { String bottle1_label="Milk"; float bottle1_volume=250; float bottle1_capacity=500; bottle1_volume=addVolume(bottle1_label, bottle1_volume,bottle1_capacity,200); System.out.println("bottle label: " + bottle1_label + ", volume: " + bottle1_volume + ", capacity: " +bottle1_capacity); String bottle2_label="Water"; float bottle2_volume=100; float bottle2_capacity=250; bottle2_volume=addVolume(bottle2_label, bottle2_volume,bottle2_capacity,500); System.out.println("bottle label: " + bottle2_label + ", volume: " + bottle2_volume + ", capacity: " +bottle2_capacity); } public static float addVolume(String label, float bottleVolume, float capacity, float addVolume)...
Develop a Java program that prompts the user to enter her birth year (e.g. 1980). The...
Develop a Java program that prompts the user to enter her birth year (e.g. 1980). The program then uses the current year (i.e. 2020) and displays age of the user (i.e. 40). The program should handle any exception that can occur due to the value entered by the user.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT