Program for using arraylists in two ways, JAVA Programming

Assignment Help:

In this lab, you will practice using ArrayLists in two ways.  The first is in the problem domain class Question.java (see the UML diagram shown below) which contains data for trivia questions.  The instance variable question is a String that holds the trivia question.  An ArrayList of Strings is used to hold the correct answer along with one or more incorrect answers.  The index value of the ArrayList element holding the correct answer is held in the variable correctAnswer. 

Question

- question: String

- answers: ArrayList

- correctAnswer: int

+ Question()            

+ Question( newQuestion: String, newAnswers: ArrayList, newCorrectAnswer: int )

+ getQuestion(): String

+ getAnswers(): ArrayList

+ getCorrectAnswer(): int

+ setQuestion( newQuestion: String ): void

+ setAnswers( newAnswers: ArrayList ): void

+setCorrectAnswer( newCorrectAnswer: int ): void

+ toString(): String

+ checkAnswer( userAnswer: int ): boolean

+ displayAllAnswers(): String

 

The second way you will practice with ArrayLists is in the tester class TestQuestions.java.  In this class, you will create an ArrayList which will contain several Question references.  You will write code which will present the questions and answers to a user and allow the user to select the correct answer.

1.       Download and save Question.java and TestQuestions.java.  Add your name and Eagle ID in a comment at the top of each class.  Compile both classes and look over the existing code carefully before you proceed.  Please do not delete or modify any of the existing code in these classes.

2.       Write the code for two domain specific methods in Question.java.  Both domain specific methods are shown in the UML class diagram and the logic of the methods is explained below.  Please be sure that the method parameters and return types that you use are consistent with both the UML class diagram and this explanation.

a.       The checkAnswer() method takes an int argument which represents the user's selected answer to a question.  This method should compare the user's answer to the value stored in the instance variable correctAnswer.  If the two values match, the method should return true.  If they do not match, the method should return false.

b.      The displayAllAnswers() method should return a String which contains all of the answers for a particular trivia question.  Use line breaks in this String so that each answer is on a separate line.  Include the index value for each answer's position in the ArrayList that holds all of the answers.  So for example, if a particular question had three possible answers which were stored as "Yes", "No", and "Maybe" in the answersArrayList, then this method would return a String which would appear as shown below:

                         0. Yes
                         1. No
                         2. Maybe

3.       In the tester class, you will write code to create some new question objects and to allow a user to answer your questions.

a.       In TestQuestions.java, follow the example code to create at least two new trivia questions which have one right answer and multiple wrong answers to choose from.  Use the ArrayList called questionList(already created in this class) to hold the references to all of your Question objects.

b.      Add one or more lines of code which displays toString() output for all of the trivia questions to the console.  Look over the display and be sure that all of the data that you entered is shown correctly.  You can think of this as diagnostic output that you (the developer) can use to verify your data.  If we were really writing a trivia game for a user, then you would hide or eliminate this display.  But for now, you should leave it in.

c.       Create a loop which will display all of the questions to a user and allow the user to answer them.  You may use either Scanner input and console output or JOptionPaneinput and output.  The example shown below usesJOptionPane.

181_Program for Using Arraylists in Two Ways 1.png


This dialog box shows the text of the first question and the String which is produced by the displayAllAnswers() method in Question.java.  It also includes instructions to the user to enter the number of the correct answer.

After the user enters their selection, call the checkAnswer() method to determine whether the answer is correct.  Display a message to the user indicating whether the entered answer was right or wrong.

For this question, if the user typed in a 1 (the correct answer), the following message is displayed:

957_Program for Using Arraylists in Two Ways 2.png


Typing in any number other than 1 will cause the following message to be displayed:

2356_Program for Using Arraylists in Two Ways 3.png


When the user clicks OK to close the dialog box, the next question will be displayed.  Your loop should carry out these steps for all of the trivia questions that you have created.

4.       Save your java files with the modifications.  Zip your two java files together into a compressed folder and attach them to the Folio assignment to submit your work.


Related Discussions:- Program for using arraylists in two ways

Explain the java.net package, Explain The java.net package Each package...

Explain The java.net package Each package describes a number of classes, interfaces, exceptions, and errors. For instance, the java.net package contains these, classes, interfa

Change encrypted hsql database into csv format, Change encrypted HSQL datab...

Change encrypted HSQL database into CSV format Project Description: We have an encrypted HSQL Database which we have to convert into csv format. HSQL DB . We have java app

Exportobject of unicastremoteobject do, What does the exportObject of Unica...

What does the exportObject of UnicastRemoteObject do? Ans) Exports the remote object to make it available to receive incoming calls, using the certain supplied port. If port not

Describe importing classes in java, Describe Importing Classes in java? ...

Describe Importing Classes in java? Fully qualified names such as java.net.URL are not the most convenient thing to have to type. You can use the shorter class names like URL w

Develop a desktop application with lync 2013, Develop a desktop application...

Develop a desktop application with Lync 2013 Project Description: We want to make desktop application which interfaces to Lync 2013 ? Skills required are .NET, ASP, Java,

What are the sub categories of artificial intelligence, What are the Sub ca...

What are the Sub categories of Artificial Intelligence? Briefly explain any two? 1. Expert systems: Expert system is in which system that is in some limited sense, could repl

Luminous Jewels - The Polishing Game, Byteland county is very famous for lu...

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Explain all java primitive data types, Explain all Java Primitive Data Type...

Explain all Java Primitive Data Types? boolean 1-bit. May take on the values true and false only. true and false are describe constants of the language and are not the s

Bidirectional currency conversion, Write a AXIS2 SOAP Java Web Service that...

Write a AXIS2 SOAP Java Web Service that performs a bidirectional currency conversion for the following currencies- GBP, EUR and USD (American Dollar). You are allowed to hardco

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd