Student Lab Workbook: Java Programming Class - Rock, Paper, Scissors Game
Introduction
In this lab workbook, you will create a Java program that allows the user to play the classic game of Rock, Paper, Scissors against the computer. You will learn about Java basics, including variables, conditionals, loops, and methods.
Learning Outcomes:
The Enum data structure
Switch Statements
Defining Classes and Calling Methods
Translating Business Rules into Java Classes
Java basics, including variables, loops, conditionals, and methods
Prerequisites
Basic knowledge of Java programming concepts
Java Development Kit (JDK) installed
A Java Integrated Development Environment (IDE), such as Eclipse or IntelliJ IDEA
Table of Contents
Overview of the Rock, Paper, Scissors Game
Creating the Project
Writing the Main Class
Defining the Game Logic
Running the Game
Conclusion
1. Overview of the Rock, Paper, Scissors Game
In the Rock, Paper, Scissors game, there are two players who choose one of the three options: Rock, Paper, or Scissors. The rules are as follows:
Rock beats Scissors
Scissors beats Paper
Paper beats Rock
If both players choose the same option, the game ends in a draw.
2. Creating the Project
Open your Java IDE and create a new Java project. Name the project "RockPaperScissorsGame".
3. Writing the Main Class
Create a new Java class named "RPSGame" in your project. This class will serve as the main class for your program.
publicclassRPSGame {
publicstaticvoidmain(String[] args) {
// Your code will go here
}
}
4. Defining the Game Logic
Now, let's define the game logic in the RPSGame class.
Import the required libraries:
import java.util.Scanner;
import java.util.Random;
Define the enum Move to represent the three options (Rock, Paper, and Scissors):
publicenumMove {
ROCK, PAPER, SCISSORS;
}
Create a method named playGame to handle the game loop:
publicstaticvoidplayGame() {
// Your code will go here
}
Inside the playGame method, add the game loop and logic:
Scanner scanner = newScanner(System.in);
Random random = newRandom();
System.out.println("Welcome to Rock, Paper, Scissors!");
while (true) {
System.out.print("Enter your move (rock, paper, or scissors): ");
Update the main method to call the playGame method:
publicstaticvoidmain(String[] args) {
playGame();
}
5. Running the Game
Now, it's time to run the game. In your IDE, run the RPSGame class as a Java application. The game will start, and you'll be able to play Rock, Paper, Scissors against the computer.
6. Conclusion
Congratulations! You have successfully created a simple Rock, Paper, Scissors game in Java.
This project introduced you to Java basics, including variables, loops, conditionals, and methods. You can now expand on this project, adding features such as a scoring system, improved user interface, or even multiplayer support. Keep practicing and exploring the vast possibilities of Java programming!
Lecture Note: Java Enum
Introduction
Java Enum, short for enumeration, is a special data type introduced in Java 5.0 that allows you to define a collection of constants with a specific data type. Enums are used when you need to represent a fixed set of values, making your code more readable and maintainable. Some common use cases for enums include representing days of the week, months, seasons, or even directions.
Defining an Enum
To define an enum in Java, you use the enum keyword followed by the name of the enumeration. Inside the enumeration, you list your constants, separated by commas. Here's an example of an enum representing the days of the week:
While classes are more versatile and can be used to create objects with varying properties and behaviors, enums provide a type-safe way to represent a fixed set of constant values.
Enums are essentially specialized classes that implicitly extend java.lang.Enum, and they can also implement interfaces. Enums cannot create new instances, and their constructors must be private.
Classes can have multiple constructors, instance methods, static methods, and abstract methods.
Enums can define instance methods, static methods, and constructors, but they cannot have abstract methods.
Both classes and enums can define instance and static fields with various access modifiers.
Enums, however, have their constants implicitly set to public static final, and their constructors are implicitly private.
Using Enums
To use an enum, you can declare a variable of the enum type and assign one of the constants to it:
Day today = Day.MONDAY;
Enums can be used in switch statements, for loops, and also as method parameters and return types. Here's an example of a switch statement using an enum:
java
Copy code
switch (today) {
case MONDAY:
System.out.println("It's Monday!");
break;
case TUESDAY:
System.out.println("It's Tuesday!");
break;
// ... And so on for the rest of the days
}
Enum Methods
Enums in Java come with some built-in methods:
values(): Returns an array containing all the constants of the enum.
ordinal(): Returns the position of the enum constant in the declaration (starting from 0).
name(): Returns the name of the enum constant, as a string.
valueOf(String name): Returns the enum constant with the specified name.
Here's an example of using the values() method to iterate through all the days of the week:
for (Day day : Day.values()) {
System.out.println(day.name() + " is at position " + day.ordinal());
}
Customizing Enum Behavior
Enums can have fields, constructors, and methods just like regular classes.
This allows you to associate additional data with each constant or even override methods to provide custom behavior for each constant. Here's an example of an enum representing the planets in our solar system, with each constant holding its mass and radius:
enumPlanet {
MERCURY(3.303e+23, 2.4397e6),
VENUS(4.869e+24, 6.0518e6),
EARTH(5.976e+24, 6.37814e6),
MARS(6.421e+23, 3.3972e6),
JUPITER(1.9e+27, 7.1492e7),
SATURN(5.688e+26, 6.0268e7),
URANUS(8.686e+25, 2.5559e7),
NEPTUNE(1.024e+26, 2.4746e7);
privatefinaldouble mass; // in kilograms
privatefinaldouble radius; // in meters
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
publicdoublegetMass() {
return mass;
}
publicdoublegetRadius() {
return radius;
}
}
In this example, we've added a constructor, fields, and getter methods for mass and radius to the Planet enum.
Conclusion
Java Enum is a powerful and flexible feature that helps you represent a fixed set of values in a type-safe and readable manner. Enums can be used in various programming constructs, such as switch statements, loops, and method parameters/return types. Moreover, you can customize enums by adding fields, constructors, and methods, enabling you to associate additional data with the constants or provide custom behavior.
Want to print your doc? This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (