Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

How do I do this using the template below? Write a modular program in a class called MagicBall that asks the user to enter a

How do I do this using the template below?

Write a modular program in a class called MagicBall that asks the user to enter a question and gives a Crystal Ball like answer. e.g If the user enters Will I get an A in the class, the answer may be Hard work pays off!. The specifications are: User repeatedly asks questions and terminates program by hitting enter User can enter any question, valid or not (there is no input validation, strings are assumed) The program reads answers from a file called answers.txt that you need to create with interesting answers. Each answer is on a separate line, You may use the sample file given. The answers are stored in a one-dimensional string array called answers of SIZE 20 (constant) The main method calls two static methods called readAnswers() // read answers from file and store answers in array, keep count of actual number of elements. This number should be less than 20. playGame() // use a while loop to repeatedly ask user to enter a question // use a random number generator to generate a random integer between 0 // and count-1 to randomly select an answer from the array answer // Random rand = new Random() creates a random object You will need to import java.util.Scanner and java.io.

USING THE TEMPLATE BELOW

import java.io.*; import java.util.random; import java.util.scanner; public class MagicBallHelper{ public static void main(String[] args) { int numberOfAnswers String[] answers = new String[20]; numberOfAnswers = readAnswers(answers); playGame(numberOfAnswers, answers); } public static int readAnswers(String [] answers) { int numberOfAnswers = 0 String fileName = "answers.txt"; try { File file = new File(fileName); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String line; while((line = br.readLine()) != null) { answers[numberOfAnswers] = line; numberOfAnswers++; } fr.close(); br.close(); } } catch(FileNotFoundException e){ System.out.println("Error: Data file Not Found"); } return numberOfAnswers; } public static void playGame(String []answers, int numberOfAnswers) { String question; boolean repeat = true; Scanner imput = new Scanner(System.in); System.out.print("enter ur question"); question = imput.nextLine();

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions