Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

how do i do this folliwing the templte below in java? USING THE TEMPLATE BELOW import java.io.*; import java.util.random; import java.util.scanner; public class MagicBallHelper{ public

how do i do this folliwing the templte below in java?

image text in transcribed

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();

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() I/ use a while loop to repeatedly ask user to enter a question // use a random number generator to generate a random integer between 0 I/ 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

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

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

Recommended Textbook for

Conceptual Database Design An Entity Relationship Approach

Authors: Carol Batini, Stefano Ceri, Shamkant B. Navathe

1st Edition

0805302441, 978-0805302448

More Books

Students also viewed these Databases questions