Instructions
Objective
Write a Java assignment program to implement multiple classes.
Requirements and Specifications
Name is Marcus
I use eclipse.
This is a program for java course. I will provide PowerPoint for chapter 7.
This assignment involves writing a java program which uses multiple classes. It is based on the material you will learn in chapter 7.
Chapter 7 begins with the four basic software development activities which are requirements, design, implementation, and testing. So for this assignment I want to give you some exposure to how to go from requirements to design. Read the example 7.below , you will see a written narrative of the problem description. Notice the blue highlighting of them nouns. This helps you identify which classes you will need to create. Next you identify the verbs which help you determine the actions or behavior for each of the classes. Let's try it with a card game.
The game uses a deck of cards. Each card has a color and a number. Each player in the game is dealt five cards, which is typically called their hand. The object of any game is to win. Winning is determined by a score. There are multiple levels of this game which differ in how the score is computed.
The simplest version (aka C version) score is simply the addition of all the numbers in a player's hand.
The next 2 levels (A and B version) take into account the number of cards with the same color.
The goal is to collect the highest value cards with the fewest number of colors. Play consists of each player, in order, selecting to return some number of their cards to the bottom of the deck and then replacing those cards from the top of the deck. After each round, where a round is all players replacing as many cards as they choose, the deck of cards is shuffled. After 10 rounds the player with the highest score is the winner.
Your job is to design and implement a Java program using multiple classes to simulate playing the game. Let's think about the classes we will need by identifying all of the nouns in the above problem description. Here's a potential list: game, deck, card, player, hand, score, round.
Now let's consider the attributes that are involved. These ultimately become class instance variables. Here's a potential list: color of card, number of card, number of players, number of cards in a hand, number of cards in a deck, range of colors, range of numbers, number of rounds, player's turn.
Finally let's consider the actions or behaviors. These usually become methods in the various classes. Here's a possible list: shuffling the cards, dealing the cards, exchanging cards, computing score, playing a round, playing the game.
Now we take all of this information, perhaps draw a diagram showing relationships and containment to decide which classes are related to each other and what should be contained within a class. We also need to decide which methods and variables should go with each class. Finally we need to think about the constructors, setter and getter methods. There really is not completely right or wrong solution. This is probably the most creative part of programming. It also has a huge impact on who easy or hard it will be to program the end result.
The list of classes I would implement. Place all in the same folder. That way all you have to do to compile is this from the commend line: javac *.java
Color
- this will contain the enumerated type for card colors
- provided file in the bottom
- public enum Color {RED, GREEN, BLUE, YELLOW}
Card
- 2 properties - a color (RED, GREEN, BLUE, YELLOW) and a number (1 - 9)
- constructor that initiates color and number
- setter and getter methods
- toString method that returns a string with the color and number like this: R-5 or G-2, etc
- provided on the bottom
// card class
import java.util.*;
public class Card {
// Card constructor; set the color and number of the card
public Card (/* parameters go here */ ) {
}
public void setFaceValue(/* parameter */) { }
public void setFaceColor(/* parameter */ ) { }
public int getFaceValue() { }
// return String value of the card: R-7, G-3, etc
// first name of color
// google how to convert java enum value to string
// then google how to get 1st character of a string
// then google how to convert an int to a String
public String toString() {
}
}
Deck
- an ArrayList of Cards
- one card for each color and number, so for example R-1, R-2 .... R-9, G-1, G-2 .... G-9, B-1, B-2 ... B-9, .....
- constructor will create the cards and add to the ArrayList; then it will call its own Shuffle method
- method to shuffle the deck of cards - checkout the shuffle method in ArrayList
- method to draw the next card from top of the deck and them remove it; call this drawCard
- method to return a card to the bottom of the deck; call this returnCard
- provide on bottom
// deck class
import java.util.*;
public class Deck {
// constructor for Deck
// creates an ArrayList of Card objects
// fills the deck with cards of all colors and all numbers
// shuffles the deck
public Deck (int num) {
}
// Shuffle method
// check out the shuffle method for collections - see link in assignment
// you need to shuffle so that you control the random number sequence
// google to find out how to control this; it is known as the random number generator seed
public void Shuffle() {
}
// drawCard method
// return the top card from the deck and then remove it
public Card drawCard() {
}
// returnCard method
// place card at bottom of deck
public void returnCard(Card card) {
}
}
Player
- each player has a name, just a string
- each player has a hand which is an ArrayList of 5 cards
- each player has a score, just an int
- each player must have access to the deck of cards; this deck will be created by the Game class, but needs to be passed in to the Player constructor and stored in the Player class so the player can draw cards from the deck
- each player needs a toString method that returns something like this
- Player: Danny Score: 55 Hand: B-1/G-8/B-3/B-9/R-9/
- in this example Danny was the player's name, his score was 55 and the cards in his hands were are shown
- each player needs a discard method that will remove a card from their hand, return it to the bottom of the deck and update their score
- each player needs a method to update their score as follows
- (C) version score is determined by adding up the facevalue of each card in a player's hand
- R-5/G-3/B-1/R-3/G-1 = 5+3+1+3+1 = 13
- version score is determined the same as C version BUT THEN you divide by the number of distinct colors in the player's hand and truncating
- R-5/G-3/B-1/R-3/G-1 = 5+3+1+3+1 = 13 / 3 = 4.3333 = 4 when you truncate; you divide from 3 because player has 3 distinct colors - RGB
- (A) version score - sum up facevalues for each color, then multiply by number of times that color occurs in players hand, then sum all of these up
- Provide on bottom
// player class
public class Player {
private String name;
private ArrayList hand = new ArrayList();
private int score;
private Deck theGameDeck;
// Player constructor; pass in the player's name and reference to deck
// note; deck should be created by the Game class
public Player (String n, Deck deck) {
name = n;
theGameDeck = deck;
score = 0;
for (int i=1; i<=5; i++) {
// draw a card from the deck and add to hand
}
upDateScore();
}
public void setScore(int sc) { score = sc; }
public int getScore() { return score; }
// return a string version of the player that looks like this
// Player: player's name Score: player's score Hand: player's hand like this - R-3/B-2/G-9/..... (list all the cards in hand)
// google how to convert an int (score) to string
// google how to get object from ArrayList (hand)
public String toString() {
}
// remove top card(s) from player's hand
// return it to bottom of deck
// draw another card from top of deck
// parameter num specifies how many cards to discard & draw
// be sure to update player's score when done
public void discard(int num) {
}
// method to update the player's score
// notice this is a private method; only members of this class can call it
// call any time the player's hand changes
private void upDateScore() {
}
}
Game
- each game has 2 player objects
- each game has 1 deck object
- you will need a constructor that receives the names of each player; it will create the deck and the 2 players
- you will need a getPlayer1 and getPlayer2 method that returns the corresponding player object
// game class
public class Game {
// Game constructor
// create a deck of cards by calling the Deck constructor
// create the number of players you want (default is 2)
public Game( /* pass in the player's names */ ) {
}
public Player getPlayer1() { return p1; }
public Player getPlayer2() { return p2; }
}
PlayGame
- this will be the main program
- I will provide a simple one but will test with more complex ones, so you should test with ones you create also
- DO NOT SUBMIT THIS CLASS FILE,
- Simple code bellow
// play game - main program
// demonstrate a simple test program
// create a game for 2 people
// display each player's status
// have each player discard a few cards
// display each player's status
class PlayGame {
public static void main(String[] args) {
Game g = new Game("Danny", "Karen");
System.out.println(g.getPlayer1().toString());
System.out.println(g.getPlayer2().toString());
g.getPlayer1().discard(3);
g.getPlayer2().discard(5);
System.out.println(g.getPlayer1().toString());
System.out.println(g.getPlayer2().toString());
}
}
Source Code
CARD
public class Card {
private Color color;
private int number;
// Create a card
public Card(Color color, int number) {
this.color = color;
this.number = number;
}
// Return a string representation
@Override
public String toString() {
if (color == Color.RED) {
return "R-" + number;
}
if (color == Color.GREEN) {
return "G-" + number;
}
if (color == Color.BLUE) {
return "B-" + number;
}
return "Y-" + number;
}
// Setter and getter methods
public Color getFaceColor() {
return color;
}
public void setFaceColor(Color color) {
this.color = color;
}
public int getFaceValue() {
return number;
}
public void setFaceValue(int number) {
this.number = number;
}
}
GAME
public class Game {
private Player p1;
private Player p2;
private Deck deck;
// Create a game with 2 players
public Game(String p1Name, String p2Name) {
deck = new Deck();
p1 = new Player(p1Name, deck);
p2 = new Player(p2Name, deck);
}
// Getter methods
public Player getPlayer1() {
return p1;
}
public Player getPlayer2() {
return p2;
}
}
Similar Sample
Explore our Java assignment sample to understand the high-quality work we deliver. This example showcases clear, well-commented code and effective problem-solving strategies, reflecting our commitment to helping students excel. Whether you're tackling basic concepts or advanced projects, our sample demonstrates the level of detail and expertise you can expect from our programming homework help.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java