Add to lab 6: Lame Game Computer Game Room Your menu will stay the same. You wil
ID: 3857674 • Letter: A
Question
Add to lab 6: Lame Game Computer Game Room
Your menu will stay the same. You will still have the following options:
1. Make Change
2. High Card
3. Deal Hand
4. Save Dream Hand
5. Display Dream Hand
6. Quit
The High Card function should remain the same.
You will modify the Deal Hand function:
1. Generate the 5 cards and store them in a list
2. Pass the entire 5-card list to a function “display_hand” that will take the list as input and go through the list one at a time and call your display_face_value function
3. Pass the entire 5-card list to a different function “hand_stats” that will take the list as input and display the total value of the cards added together and the average value of the cards in the hand. Be sure to label the output.
You will modify the Save Dream Hand function:
1. Read the cards from the user and save in a list, not individual variables
2. After all 5 card values have been read and stored in a list, save the list of cards to the file
You will modify the Display Dream Hand function:
1. Read the cards from the file and save in a list, not individual variables
2. Pass this list to your new “display_hand” function (the one you wrote for the Deal Hand function to use) in order to display the 5-card hand
Explanation / Answer
It appears to be a modification of an existing program. Since that was not provided. Feel free to make any adjustments necessary in accordance to your existing program. Good Luck..
Solution======================================
//Serializable enables us to write the objects of this class
//into a file
class Card implements Serializable{
private static final long serialVersionUID = 1L;
int faceVal;
Random rand = new Random();
public Card(){
faceVal=rand.nextInt(13)+1;
}
public Card(int nextInt) {
faceVal=nextInt;
}
}
public class LameGame {
public void showMenu(){
System.out.println(" 1. Make Change "
+"2. High Card "
+"3. Deal Hand "
+"4. Save Dream Hand "
+"5. Display Dream Hand "
+"6. Quit ");
}
public void dealHand(){
ArrayList<Card> dealHandCards=new ArrayList<>();
//Generating 5 cards and storing them in a list
for(int i=0;i<5;i++){
Card card = new Card();
dealHandCards.add(card);
}
dipslay_Hand(dealHandCards);
hand_stats(dealHandCards);
}
private void hand_stats(ArrayList<Card> dealHandCards2) {
int total=0;
for(Card card:dealHandCards2){
total+=card.faceVal;
}
System.out.println("Total value of Hand Cards:"+total);
System.out.println("Average value of Hand Cards: "+(float)total/5);
}
private void dipslay_Hand(ArrayList<Card> dealHandCards2) {
for(Card card:dealHandCards2){
display_face_value(card);
}
}
private void display_face_value(Card card) {
System.out.println("Card:"+card.faceVal);
}
public void saveDreamHand(Scanner in) throws IOException{
ArrayList<Card> cards = new ArrayList<>();
//Taking inputs and adding it to the list
for(int i=0;i<5;i++){
Card card = new Card(in.nextInt());
cards.add(card);
}
//Saving list of Cards to a File
FileOutputStream fout = new FileOutputStream("data.txt",false);
ObjectOutputStream oos = new ObjectOutputStream(fout);
for(Card card:cards){
oos.writeObject(card);
}
oos.close();
fout.close();
}
public void displayDreamHand() throws IOException, ClassNotFoundException{
ArrayList<Card> cards = new ArrayList<>();
FileInputStream fi = new FileInputStream(new File("data.txt"));
ObjectInputStream oi = new ObjectInputStream(fi);
for(int i=0;i<5;i++){
cards.add((Card)oi.readObject());
}
oi.close();
fi.close();
dipslay_Hand(cards);
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
LameGame lgame = new LameGame();
boolean exit=false;
Scanner ins = new Scanner(System.in);
while(!exit){
lgame.showMenu();
int option= ins.nextInt();
switch(option){
case 1:
case 2:
case 3: lgame.dealHand();
break;
case 4:lgame.saveDreamHand(ins);
break;
case 5:lgame.displayDreamHand();
break;
case 6:exit=true;
break;
}
}
ins.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.