Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using java I am having alot of problems with creating these two programs Program 1 Random Numbers Make a method called PlayGame() that takes four

Using java I am having alot of problems with creating these two programs

Program 1 Random Numbers

Make a method called PlayGame() that takes four variables: player1name, player1maxsteps, player2name, player2maxsteps as parameters. This method should make a total for player 1 and a total for player 2. It should then have a loop that goes until the total for player 1 or the total for player 2 is over 30 to cross a finish line. Inside the loop it should find a random number between 1 and player1maxsteps and add it to that players total, and find a random number between 1 and player2maxsteps and add it to that players total. After the loop is over it should use an IF to check who has the most steps and display that persons name as the winner.

[So this method is having them walk random number of steps until they cross a finish line, then whoever is farther is the winner, even if they both crossed on the same turn]

In main() ask the user for the first players name and how many steps that person might make in a turn (how fast or risky they will to be), and () ask the user for the second players name and how many steps that person might make in a turn (how fast or risky they will to be). Then call the PlayGame() and pass it the four variables and see who wins.

Program 2 Overloaded Methods

Take the program above and modify it to handle another choice by the user.

Copy the PlayGame() method and add a fifth parameter for the number of steps to cross the finish line. Change the loop to count up to this variable instead of the regular 30 the original version is still using.

[Making an overloaded method. You should have 2 versions of PlayGame() now]

Change main() right before it calls the PlayGame() method and ask the user of they want to play a regular 30 step game, or a different number of steps. Use an IF to call the first PlayGame() if they said they want to play a regular game, and for the variable distance choice ask them how many steps it will be and call the new version of PlayGame() and pass the number they give as the fifth variable.

import java.util.Random;

public class Playgame { public static void main(String[] args) { Random rand = new Random(); System.out.print("Enter player 1 name: "); String player1name = System.console().readLine(); System.out.print("Enter player 1 max steps: "); int player1maxsteps = Integer.parseInt(System.console().readLine()); System.out.print("Enter player 2 name: "); String player2name = System.console().readLine(); System.out.print("Enter player 2 max steps: "); int player2maxsteps = Integer.parseInt(System.console().readLine()); PlayGame(player1name, player1maxsteps, player2name, player2maxsteps); }

public static void PlayGame(String player1name, int player1maxsteps, String player2name, int player2maxsteps) { int player1total = 0; int player2total = 0; Random rand = new Random(); while (player1total < 30 && player2total < 30) { player1total += rand.nextInt(player1maxsteps) + 1; player2total += rand.nextInt(player2maxsteps) + 1; } if (player1total > player2total) { System.out.println(player1name + " wins!"); } else if (player2total > player1total) { System.out.println(player2name + " wins!"); } else { System.out.println("It's a tie!"); } } Random rand = new Random(); System.out.print("Enter player 1 name: "); String player1name = System.console().readLine(); System.out.print("Enter player 1 max steps: "); int player1maxsteps = Integer.parseInt(System.console().readLine()); System.out.print("Enter player 2 name: "); String player2name = System.console().readLine(); System.out.print("Enter player 2 max steps: "); int player2maxsteps = Integer.parseInt(System.console().readLine()); System.out.print("Do you want to play a regular 30 step game or a different number of steps? (30/different) "); String choice = System.console().readLine(); if (choice.equals("30")) { PlayGame(player1name, player1maxsteps, player2name, player2maxsteps); } else { System.out.print("Enter the number of steps to cross the finish line: "); int finishLine = Integer.parseInt(System.console().readLine()); PlayGame(player1name, player1maxsteps, player2name, player2maxsteps, finishLine); } }

public static void PlayGame(String player1name, int player1maxsteps, String player2name }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mastering Apache Cassandra 3 X An Expert Guide To Improving Database Scalability And Availability Without Compromising Performance

Authors: Aaron Ploetz ,Tejaswi Malepati ,Nishant Neeraj

3rd Edition

1789131499, 978-1789131499

More Books

Students also viewed these Databases questions

Question

Describe Freuds view of personality.

Answered: 1 week ago

Question

4. Identify cultural variations in communication style.

Answered: 1 week ago