Write a Java Console application in which you initialize an arraylist with 10 string values. For example, 10 colour names, or fruit names, or car names. Display all the values in the list in a neat tabular format. Randomly select a value from the array. Now allow the user 3 chances to guess the value. After the first incorrect guess, provide the user with a clue i.e., the first letter of the randomly selected word. After the second incorrect guess, provide the user with another clue such as the number of letters in the word. When the user correctly guesses the word, remove that word from the list. Display the number of items remaining in the list. The user must have the option to play again.

Answers

Answer 1

Answer:

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

public class GuessTheCar {

   public static void main(String[] args) {

       ArrayList<String> cars = new ArrayList<>();

       cars.add("Toyota");

       cars.add("Honda");

       cars.add("Chevrolet");

       cars.add("BMW");

       cars.add("Mercedes");

       cars.add("Tesla");

       cars.add("Ford");

       cars.add("Audi");

       cars.add("Porsche");

       cars.add("Ferrari");

       boolean play = true;

       Scanner scanner = new Scanner(System.in);

       while (play && cars.size() > 0) {

           int randomIndex = new Random().nextInt(cars.size());

           String randomCar = cars.get(randomIndex);

           System.out.println("Car list:");

           for (int i = 0; i < cars.size(); i++) {

               System.out.printf("%d. %s\n", i + 1, cars.get(i));

           }

           System.out.println();

           boolean correctGuess = false;

           for (int i = 0; i < 3; i++) {

               System.out.print("Guess the car name: ");

               String guess = scanner.nextLine();

               if (guess.equalsIgnoreCase(randomCar)) {

                   correctGuess = true;

                   break;

               } else if (i == 0) {

                   System.out.printf("Clue: The car name starts with '%s'.\n", randomCar.charAt(0));

               } else if (i == 1) {

                   System.out.printf("Clue: The car name has %d letters.\n", randomCar.length());

               }

           }

           if (correctGuess) {

               System.out.println("Congratulations! You guessed correctly!");

               cars.remove(randomCar);

           } else {

               System.out.println("Sorry, you didn't guess the car correctly. The car was " + randomCar + ".");

           }

           System.out.printf("Cars remaining: %d\n", cars.size());

           System.out.print("Do you want to play again? (Y/N): ");

           String response = scanner.nextLine();

           play = response.equalsIgnoreCase("Y");

       }

       scanner.close();

       System.out.println("Thanks for playing!");

   }

}

Answer 2

Here's an example of a Java console application that implements the described functionality:

java

Copy code

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

import java.util.Scanner;

   private static final int MAX_GUESSES = 3;

   public static void main(String[] args) {

       Scanner scanner = new Scanner(System.in);

       List<String> wordList = initializeWordList(); // Initialize the word list

       do {

           playGame(scanner, wordList); // Play the game

           System.out.print("Do you want to play again? (y/n): ");

       } while (scanner.nextLine().equalsIgnoreCase("y"));

       System.out.println("Thanks for playing!");

   }

  private static List<String> initializeWordList() {

       List<String> wordList = new ArrayList<>();

       // Add 10 words to the list

       wordList.add("apple");

       wordList.add("banana");

       wordList.add("orange");

       wordList.add("strawberry");

       wordList.add("watermelon");

       wordList.add("kiwi");

       wordList.add("mango");

       wordList.add("grape");

       wordList.add("pineapple");

       wordList.add("pear");

       return wordList;

   }

   private static void playGame(Scanner scanner, List<String> wordList) {

       System.out.println("=== Guessing Game ===");

       // Select a random word from the list

       Random random = new Random();

       int randomIndex = random.nextInt(wordList.size());

       String randomWord = wordList.get(randomIndex);

       int remainingGuesses = MAX_GUESSES;

       boolean wordGuessed = false;

       while (remainingGuesses > 0 && !wordGuessed) {

           System.out.println("Guess the word (remaining guesses: " + remainingGuesses + "):");

           String guess = scanner.nextLine();

           if (guess.equalsIgnoreCase(randomWord)) {

               wordGuessed = true;

               System.out.println("Congratulations! You guessed it right.");

           } else {

               remainingGuesses--;

               if (remainingGuesses > 0) {

                   if (remainingGuesses == 2) {

                       System.out.println("Incorrect guess. Here's a clue: First letter of the word is '" +

                               randomWord.charAt(0) + "'.");

                   } else if (remainingGuesses == 1) {

                       System.out.println("Incorrect guess. Here's another clue: The word has " +

                               randomWord.length() + " letters.");

                   }

                   System.out.println("Try again!");

               } else {

                   System.out.println("Sorry, you're out of guesses. The word was '" + randomWord + "'.");

               }

           }

       }

       if (wordGuessed) {

           wordList.remove(randomWord);

       }

       System.out.println("Number of items remaining in the list: " + wordList.size());

   }

}

This program initializes an ArrayList with 10 words, allows the user to play the guessing game, and provides clues after incorrect guesses. After the game ends, it prompts the user to play again or exit.

Please note that this implementation assumes you have basic knowledge of Java programming and how to run a Java console application.

Learn more about Java programming on:

https://brainly.com/question/30613605

#SPJ1


Related Questions

Which of the following is a positional argument?
a) ABOVE
b) MIN
c) AVERAGE
d) MAX

Answers

D) MAX is a positional argument

Rank the following functions from lowest to highest asymptotic growth rate.

Answers

To rank the functions from lowest to highest asymptotic growth rate, we need to compare their growth rates as the input size tends to infinity.

Here's the ranking from lowest to highest:

O(1): This represents constant time complexity. It means that the algorithm's runtime remains constant, regardless of the input size. It has the lowest growth rate.

O(log n): This represents logarithmic time complexity. It means that the algorithm's runtime grows logarithmically with the input size. Logarithmic growth is slower than linear growth but faster than polynomial growth.

O(n): This represents linear time complexity. It means that the algorithm's runtime grows linearly with the input size. As the input size increases, the runtime increases proportionally. Linear growth is slower than logarithmic growth but faster than polynomial growth.

O(n log n): This represents linearithmic time complexity. It means that the algorithm's runtime grows in proportion to n multiplied by the logarithm of n. Linearithmic growth is slower than linear growth but faster than polynomial growth.

Learn more about logarithmic on:

https://brainly.com/question/30226560

#SPJ1

Problem: Prime Number Generator

Write a Python function that generates prime numbers up to a given limit. The function should take a single parameter limit, which represents the maximum value of the prime numbers to be generated. The function should return a list of all prime numbers up to the limit.

A prime number is a positive integer greater than 1 that has no positive divisors other than 1 and itself. For example, the first few prime numbers are 2, 3, 5, 7, 11, and so on.

Your task is to implement the generate_primes(limit) function that returns a list of all prime numbers up to the given limit.

Example usage:
"""
primes = generate_primes(20)
print(primes)
"""

Output:
"""
[2, 3, 5, 7, 11, 13, 17, 19]
"""

Note:
You can assume that the limit parameter will be a positive integer greater than 1.
Your implementation should use an efficient algorithm to generate prime numbers.

Answers

Here's a possible implementation of the generate_primes(limit) function using the Sieve of Eratosthenes algorithm:

```python
def generate_primes(limit):
# Create a boolean array "is_prime[0..limit]" and initialize
# all entries as true. A value in is_prime[i] will finally be
# false if i is Not a prime, else true.
is_prime = [True] * (limit + 1)
# 0 and 1 are not prime
is_prime[0] = is_prime[1] = False

# Iterate over the numbers from 2 to sqrt(limit)
for i in range(2, int(limit**0.5) + 1):
if is_prime[i]:
# Mark all multiples of i as composite numbers
for j in range(i**2, limit + 1, i):
is_prime[j] = False

# Generate the list of prime numbers
primes = [i for i in range(2, limit + 1) if is_prime[i]]
return primes
```

The function first initializes a boolean array `is_prime` of size `limit + 1` with all entries set to `True`, except for the first two entries which represent 0 and 1, which are not prime. Then, the function iterates over the numbers from 2 to the square root of `limit`, checking ifeach number is prime and marking its multiples as composite numbers in the `is_prime` array. Finally, the function generates a list of prime numbers based on the `is_prime` array and returns it.

To use the function, simply call it with a limit value and assign the returned list of primes to a variable, like this:

```python
primes = generate_primes(20)
print(primes)
```

This will generate a list of all prime numbers up to 20 and print it:

```
[2, 3, 5, 7, 11, 13, 17, 19]
```
Other Questions
What is the principle that explains why messages are perceived as more important when delivered by a live speaker?a. The primacy effectb. The recency effectc. The halo effectd. The immediacy effect an average middle-aged man weighing 90 kg (200 lb) contains 15% body fat stored in adipose tissue. calculate the amount of energy stored as fat in this man in kilojoules, assuming that the energy yield from fat is 37 kj/g. DETAILS SCALCCC4 13.2.007. .. 1-/10 Points) Erauate the line integral, where C is the given curve. Sony dx + (x - y)dy C consists of line segments from (0,0) to (3,0) and from (3,0) to (4,2). of the following air masses, which one exhibits the greatest temp change between winter and summer? maritime tropical, continental polar, maritime polar, equatorial there is an increase in the price of the complementary good of this good. as a result, the equilibrium price will rise and the equilibrium quantity will which of the following is an example of a proper internal controls strategy?(A) Segregating the responsibilities of the Teller and Associate Pastor(B) Segregating the responsibilities of the Treasurer and Financial Secretary(C) Segregating the responsibilities of the Preschool Principal and Preschool Teacher(D) Segregating the responsibilities of the Pastor and Choir Director All the following are best practices to handle cash The recent attacks on foreigners (who are also the owners of most small business operations in high-density suburbs) are going to counter the governments plans and negatively impact employment projections. In your own opinion explain the root cause of the xenophobia attacks around your area and explain how it has affected your campus or your area of location Numerous societal, technical, and demographic drivers will determine the development of BIM in the future. Select one: a. True b. False Different types of customers will pay different amounts for the same products or services, depending on how early or late they are buying compared to other customers.TrueFalse 5 . . A= = 2, B = 3, and the angle formed by A and B is 60. Calculate the value of +2B \ 60 B Which situation describes data transmissions over a WAN connection?a)An employee prints a file through a networked printer that is located in another building.b)A network administrator in the office remotely accesses a web server that is located in the data center at the edge of the campus.c)An employee shares a database file with a co-worker who is located in a branch office on the other side of the city.d)A manager sends an email to all employees in the department with offices that are located in several buildings. 7. (13pts) Evaluate the iterated integral 1 2y x+y 0 y [xy dz dx dy 0 FILL THE BLANK. traditional criminological theories focused exclusively on ______. In triangle UVW. m/U 129. m/V 18, and u = 57.1) What is the measure of angle W?2) What is the length of side v?3) What is the length of side w?4) What is the area of the triangle? (A = bh)-- Morgan and Donna are cabinet makers. When working alone, it takes Morgan 8 more hours than Donna to make one cabinet. Together, they make one cabinet in 3 hours. Find how long it takes Morgan to make one cabinet by herself. which physical characteristic of land addresses the concept that the geographic location of a piece of land is fixed and can never be changed? sometimes a project is terminated before its normal completion. T/F _________ are dedicated computers that can hold actual database. Two wines are available for blending: one tank of 2000 L has a TA of 8.6 g/L another tank of 4000 L has a TA of 6.2 g/L.How much volume of the low acid wine do you need to mix with all of the 8.6 g/L TA wine to have the resulting blend equivalent to 7.2 g/L? Show your calculations At a blood drive, 6 donors with type O+blood , 3 donors with typeA+blood , and 3 donors with type B + blood are in line. In how many distinguishable ways can donors be in line? Steam Workshop Downloader