Question 1 [20 marks]
Write a Java Console application in which you initialize an arraylist with 10 string values. For example, 10 colour names, or fruit names, or vegetable 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.
Question 2 [30 marks]
Below is a Unified Modelling Language (UML) diagram of an election class.
Election |
– candidate : String
– numVotes : int |
<<constructor>>+ Election ( )
<<constructor>>+ Election (nm : String, nVotes : int) + setCandidate( nm : String) + setNumVotes( ) : int + toString( ) : String |
Using your knowledge of classes, arrays, and array list, write the Java code for the UML above in NetBeans. [7 marks]
Write the Java code for the main method in a class called TestElection to do the following:
a) Declare an array to store objects of the class defined by the UML above. Use a method from the JOptionPane class to request the length of the array from the user.
[3 marks]
b) Use a method from the JOptionPane class to request values from the user to initialize the instance variables of Election objects and assign these objects to the array. The array must be filled.
[5 marks]
c) Determine the total number of votes and the percentage of the total votes received by each candidate and the winner of the election. The sample output of your program is shown below. Use methods from the System.out stream for your output.
Sample output | ||
Candidate | Votes | % of Total Votes |
Johnson | 5000 | 25.91 |
Miller | 4000 | 20.73 |
Duffy | 6000 | 31.09 |
Robinson | 2500 | 12.95 |
Adams | 1800 | 9.33 |
Total Votes:19300
Winner of election is :Duffy |
d) Declare an array list and assign objects from the array in (a) that have more than or equal to 4000 votes per candidate to it. [3 marks]
e) Using a single JOptionPane dialog box, display only the names of the candidates stored in the array list. [3 marks]
Answers to Above Questions:
Answer 1:
Here is the code:
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
ArrayList<String> wordList = new ArrayList<>();
// Initialize the word list with 10 string values
wordList.add(“Apple”);
wordList.add(“Banana”);
wordList.add(“Orange”);
wordList.add(“Strawberry”);
wordList.add(“Grape”);
wordList.add(“Watermelon”);
wordList.add(“Pineapple”);
wordList.add(“Mango”);
wordList.add(“Cherry”);
wordList.add(“Pear”);
boolean playAgain = true;
while (playAgain) {
System.out.println(“Welcome to the Guessing Game!”);
// Display all values in the list in a neat tabular format
System.out.println(“Word List:”);
displayWordList(wordList);
// Randomly select a value from the array
int randomIndex = random.nextInt(wordList.size());
String selectedWord = wordList.get(randomIndex);
int remainingGuesses = 3;
boolean guessedCorrectly = false;
System.out.println(“\nGuess the word!”);
while (remainingGuesses > 0) {
System.out.print(“Enter your guess: “);
String guess = scanner.nextLine();
if (guess.equalsIgnoreCase(selectedWord)) {
// Correct guess
System.out.println(“Congratulations! You guessed it right.”);
wordList.remove(randomIndex);
guessedCorrectly = true;
break;
} else {
// Incorrect guess
remainingGuesses–;
if (remainingGuesses == 2) {
// Provide clue – first letter of the word
System.out.println(“Incorrect guess. Here’s a clue: The word starts with ‘” + selectedWord.charAt(0) + “‘.”);
} else if (remainingGuesses == 1) {
// Provide clue – number of letters in the word
System.out.println(“Incorrect guess. Here’s another clue: The word has ” + selectedWord.length() + ” letters.”);
}
System.out.println(“Remaining guesses: ” + remainingGuesses);
}
}
if (!guessedCorrectly) {
// No more guesses left
System.out.println(“Sorry, you ran out of guesses. The word was: ” + selectedWord);
}
// Display the number of items remaining in the list
System.out.println(“Items remaining in the list: ” + wordList.size());
// Ask the user if they want to play again
System.out.print(“Play again? (yes/no): “);
String playAgainInput = scanner.nextLine();
if (!playAgainInput.equalsIgnoreCase(“yes”)) {
playAgain = false;
}
System.out.println();
}
System.out.println(“Thank you for playing the Guessing Game!”);
}
// Method to display the word list in a tabular format
private static void displayWordList(ArrayList<String> wordList) {
for (int i = 0; i < wordList.size(); i++) {
System.out.printf(“%-15s”, wordList.get(i));
if ((i + 1) % 3 == 0) {
System.out.println();
}
}
System.out.println();
}
}
Get completed answers on the above questions on Java from the programming experts of Student Life Saviour South Africa.
Content Removal Request
If you believe that the content above belongs to you, and you don’t want it to be published anymore, then request for its removal by filling the details below. It will only be removed if you can provide sufficient evidence of its ownership.