Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PROGRAM 02 1General Description In this assignment you will create a class calle

ID: 3891177 • Letter: P

Question

PROGRAM 02 1General Description In this assignment you will create a class called "StackGame". StackGame is a simple game that a) Allows multiple players to play. Their names are stored in a String . b) Each round the game asks each player to pick a lucky number which is used to generate a random number between [-100, 100]. c) The game keeps record of all generated numbers in a stack for each player separately. d) When the user asks the game to calculate scores, it does that by adding all numbers in each stack separately and saves the weighted summations in an array of ints. (see the formula for weighted e) When the user asks for the winner, it finds which player has the max weighted summation and returns a string of this format: "Winner- PlayerName, score- 999" The game should contain a method that prints all data members: names, scores, stacks of generated numbers, etc. f) 2) Data and Methods of StackGame: 1Data: number of players, player names, array of stacks (import java.util.Stack) one stack per player players' scores (summations of stacks content). 2) Methods: Overloaded constructor: sets all data members to passed user values. (recommended parameters: only player names). void play (playerIdx, luckyNumber): allows one player at a time to generate next int [-100, 100] Stores the result in the stack of the player void calcScore0: weighted summations of stacks' contents and saves them in an array. The weighted sum formula is as follows: score( player)-Ywtnd. Stak Top, nd String winner0: finds max score in scores array and returns the names and score of the winner player int sum(idx): compute the summation of one stack. void clear: clears stacks, scores but keeps names in case players want to play again. void print 0: if game is cleared prints a message stating just that otherwise, it prints name of each player and his/her stack and the current score in one line . 3) 4) 5) Add a Driver/Demo/Test class to test ALL methods of StackGame Test at least one game with a group of 4 players and a second game with a group of 8 players. Test your project and screen-capture your sample testing to be sent along with the project.

Explanation / Answer

/*

* StackGame.java

*/

import java.io.*;

import java.util.*;

class StackGame{

String[] players;

int playerno;

Stack<Integer>[] numbers;

int[] scores;

StackGame(String[] plnames)

{

this.players=new String[plnames.length];

this.numbers=new Stack[plnames.length];

this.scores=new int[plnames.length];

for(int i=0;i<plnames.length;i++)

{

this.players[i]=plnames[i];

}

this.playerno=this.players.length;

}

void player(int playerIdx,int luckyNumber)

{

int min=-100,max=100;

Random rand=new Random();rand.setSeed(luckyNumber);

int randnum=rand.nextInt((max - min) + 1)+min;

this.numbers[playerIdx].push(randnum);

}

void calcScore(int idx)

{ this.scores[idx]=0;

this.scores[idx]+=sum(idx);

}

void print(int ct,int ch)

{

if(ch==0)

System.out.println("turn for "+this.players[ct]+" score: "+this.scores[ct]);

else

{

System.out.println(winner());

}

}

int sum(int idx)

{ int sm=0;

for(int i:numbers[idx])

sm+=i;

return sm;

}

void clear()

{

this.scores=new int[playerno];

this.numbers=new Stack[playerno];

}

String winner()

{ int max=0;

for(int i=0;i<scores.length;i++)

{ int id;

if(scores[i]>max)

{max+=scores[i];id=i;}

}

return "winner is "+players[id]+" Score: "+max;

}

public static void main(String[] args) throws Exception{

String[] plnames={"raju","raman"};

StackGame st=new StackGame(plnames);

boolean flag=true;

int ct=-1;

Scanner sc=new Scanner(System.in);

while(flag)

{

if(ct<st.playerno)

ct+=1;

else

ct=0;

st.print(ct,0);

System.out.println("1.enter lucky number 2.end game and display winner");

int choice=sc.nextInt();

if(choice==2)

{

st.print(ct,1);

System.out.println("1.play again 2.exit");

int cho=sc.nextInt();

if(cho==2)

flag=false;

else if(cho==1)

{

st.clear();

}

}

else if(choice==1)

{

System.out.println("enter lucky number:");

int lno=sc.nextInt();

st.player(ct,lno);

}

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote