Question

please do num 3,4 and 5.

Practice on Selecting the Appropriate Loop: You are playing a she guess lfavorite color variable guess the favorite color, an

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.

Here is the completed code for this problem. Comments are included so that you understand whats going on. Let me know if you have any doubts or if you need anything to change.

Thank You !!

========================================================================================
========================================================================================

import javax.swing.*;
import java.util.Random;

public class Exercise {

    //// problem 3
    public static void arcadeGame() {

        int numberOfTokens = 100;

        // we will use while loop because we dont know ahead how many times the loop will execute
        // we use while loop in scenarios where the loop count is not known before hand
        while (numberOfTokens > 0) {

            int tokenCost = Integer.parseInt(JOptionPane.showInputDialog("How many tokens the next game cost: "));
            if (tokenCost <= numberOfTokens) {
                numberOfTokens -= tokenCost;
                JOptionPane.showMessageDialog(null, "You only have " + numberOfTokens + " tokens left");
            } else {
                JOptionPane.showMessageDialog(null, "You only have " + numberOfTokens + " tokens. Please select a lesser number.");
            }
        }
    }

    //// problem 4
    public static void eatUntilFilled() {
        // we will use while loop because we dont know ahead how many times the loop will execute
        // we use while loop in scenarios where the loop count is not known before hand
        String hungry = "Yes";
        while (hungry.equalsIgnoreCase("yes")) {
            hungry = JOptionPane.showInputDialog("Are you still hungry (yes/no)? ");
            if (hungry.equalsIgnoreCase("yes")) {
                JOptionPane.showMessageDialog(null, "Eat more..");
            }
        }
    }

    //// problem 5
    public static void generateLottoTicket() {

        Random random = new Random();

        // in this scenario we know exactly how many tickets we need to generate and
        // also how many numbers each ticket will have, hence we can use for loop
        // Here we know before hand how many iterations we need to make
        for (int ticket = 1; ticket <= 10; ticket++) {

            System.out.print("\n Ticket " + ticket + ": ");
            for (int luckyNumber = 1; luckyNumber <= 5; luckyNumber++) {
                System.out.print(random.nextInt(60) + 1 + " ");
            }
        }

    }

    public static void main(String[] args) {

        arcadeGame(); // problem 3
        eatUntilFilled(); // problem 4
        generateLottoTicket(); // problem 5
    }
}

========================================================================================
========================================================================================

Add a comment
Know the answer?
Add Answer to:
please do num 3,4 and 5. Practice on Selecting the Appropriate Loop: You are playing a she guess lfavorite color variab...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using java use a while loop use JOptionPane Scenario – You go to an arcade with...

    Using java use a while loop use JOptionPane Scenario – You go to an arcade with 100 tokens, and each game costs a variable number of tokens. Before the loop begins, display how many tokens you have available. Inside the loop, ask the user how many tokens does the next game cost, and subtract that amount from the total tokens if it is less than or equal to the total tokens. Then, state how many tokens are still left.

  • java/javafx assignment: Island Paradise just started running their city lotto. You've been tasked with writing a...

    java/javafx assignment: Island Paradise just started running their city lotto. You've been tasked with writing a program for them. The program is going to allow to user to first select their lotto numbers. The program will then randomly generate the winning lotto numbers for the week and then check the winning numbers against the random ticket the user played in the Lotto to see how many numbers the user guessed correctly. The rules for lotto work as follows: Select 7...

  • The purpose of this assignment is to get experience with an array, do while loop and...

    The purpose of this assignment is to get experience with an array, do while loop and read and write file operations. Your goal is to create a program that reads the exam.txt file with 10 scores. After that, the user can select from a 4 choice menu that handles the user’s choices as described in the details below. The program should display the menu until the user selects the menu option quit. The project requirements: It is an important part...

  • python code for guessing game users enter the number the computer will guess 8:38 PM 100...

    python code for guessing game users enter the number the computer will guess 8:38 PM 100 nstructure.com 1. Number guessing game : (5 points) 1) Requirements: Your program will ask the user to enter a number between 1 and 1000. Then it will try to guess the correct number with the guaranteed minimal average number of guesses. Which search algorithm should you use? 2) Expected Output: Enter a number between 1 and 1000? 784 Computer guesses: 500 Type 'l' if...

  • Please help: For this project, you will create a shape drawing program using Turtle graphics. Your...

    Please help: For this project, you will create a shape drawing program using Turtle graphics. Your program will begin by asking the user how many points they would like to enter (# of vertices for the shape). Next, you will use a loop to input each X and Y coordinate and store these coordinates in lists. After inputting all of the coordinates, create a second loop that will draw and fill the shape using the coordinates that were entered. All...

  • #include<stdio.h> #include<stdlib.h> #include <time.h> int main() { /* first you have to let the computer generate...

    #include<stdio.h> #include<stdlib.h> #include <time.h> int main() { /* first you have to let the computer generate a random number. Then it has to declare how many tries the user has for the game loop. we then need the player to enter their guess. After every guess we have to give an output of how many numbers they have in the right location and how many they have the right number. The player will keep guessing until their 10 tries are...

  • Need help with assignment This assignment involves simulating a lottery drawing. In this type of lottery...

    Need help with assignment This assignment involves simulating a lottery drawing. In this type of lottery game, the player picks a set of numbers. A random drawing of numbers is then made, and the player wins if his/her chosen numbers match the drawn numbers (disregarding the order of the numbers). More specifically, a player picks k distinct numbers between 1 and n (inclusive), as well as one bonus number between 1 and m (inclusive). "Distinct" means that none of the...

  • PLEASE DO IN JAVA You can use any source for the dictionary or just a few...

    PLEASE DO IN JAVA You can use any source for the dictionary or just a few words are fine! description The purpose of this assignment is to practice with ArrayLists (and hopefully, you'll have some fun). As far as the user knows, play is exactly as it would be for a normal game of hangman, but behind the scenes, the computer cheats by delaying settling on a mystery word for as long as possible, which forces the user to use...

  • Please help with this, and leave comments explainging the code you wrote, thank you for your...

    Please help with this, and leave comments explainging the code you wrote, thank you for your help Write a Lottery class that simulates a 6-number lottery (e.g. "Lotto"). The class should have an array of six integers named lotteryNumbers, and another array of six integers named userLotteryPicks. The class' constructor should use the Random class to generate a unique random number in the range of 1 to 60 for each of the 6 elements in the lotteryNumbers array. Thus, there...

  • Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The...

    Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The computer must select a word at random from the list of avail- able words that was provided in words.txt. The functions for loading the word list and selecting a random word have already been provided for you in ps3 hangman.py. 2. The game must be interactive; the flow of the game should go as follows: • At the start of the game, let the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT