Starting from:

$25

CSE110 - Assignment #5  - Solved

Topics 

•         Object Oriented Programming (Chapter 8)  

-   Encapsulation  

-   Implementing classes 

-   Implementing methods 

-   Object construction 

-   Constructors 

•         Methods (Chapter 5) 

-   Definition and Invocation. 

Use the following Guidelines: 

•       Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc).  
    
Keep identifiers to a reasonably short length.  

•       User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects).  

•       Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent.  

•       Use white space to make your program more readable.  

Part 1: Writing Exercise: (5 pts)
Don't change ANY codes or add any lines to the Assignment5.java (If changed, -5 Pts). Write the answers in a comment block in your Quiz.java (Part 2). Download and look at the Assignment5.java, and answer the following questions. This Assignment5.java is a test code for the Quiz class, which you are asked to develop in the Part2.  

a.       Explain what this program is within 500 words. Using the keyword quiz and list up the task of each command (A, B, ...) 

b.       What is the quiz called, and what is the Quiz called in OOP? 

c.       List up all Quiz 's methods/constructors invoked in the Assignment5.java. (Show the line number and name of method for each) 

d.       Explain in your words why the nextLine() methods are used instead of next( )? 

e.       Explain what is happened when the user type "?" command. 

 /* 

a)  This program is to ... 

b)  The quiz is ... , and Quiz is ... 

c)  XXX(XXX)at line-5, XXX(xxx, xxx)at line-100, … 

d)  The nextLine() is required because ... 

e)  It returns 

Part 2: Programming (15 Pts)
This assignment is to write a class definition (not a program, there is no main method) named Quiz (saved in a file Quiz.java). The class has the following instance variables:  

 

private  String     question  private   String    choiceA private   String    choiceB private  String  choiceC private String    choiceD 

                   private char       answer        

 

The class must include the following constructor and methods: (If your class does not contain any of the following methods, points will be deducted). 

 

 

 

 

Method 
Description of the Method 
public  

Quiz(String, String, String, 

String, String, char) 
Initialize all instance variables with the inputs. (2 Pts).  
public String displayQuiz() 
Return the question and 4 choices in the specific format (look at the samples). Don't use any System.out method in the method, but return the all items as one single string data. 

Use "\n" for line-changes. (2 Pts) 
public void setQuestion(String) 
Set the question with the input. (1 Pts) 
public void set4Choices (String, String, String, String) 
Set/override the four choices. (1 Pts) 
public void 

setCorrectAnswer(char) 
Set the correct answer (2 Pt) 
 
private
 String 

 (char) 
This is not a regular getter method. It returns one of the questions corresponding to the input character. e.g. getChoice('C') returns the choiceC. (3 Pts) 
getChoice
public boolean isCorrect (char) 
Check whether the input character is same as the correct answer.  (2 Pts) 
public void shuffle() 
Change the order of questions, and update the correct answer matching to the new order.  

The following is the pseudo code. (2 Pts) 

1)       Make a random list of 4 letters, A,B,C, and D such as "BDCA". 

2)       Reset the correct answer by finding the position of answer letter generated in the step above. Suppose that 'B' is the answer and random list is "BACD". The correct answer is changed to the position 0, which will be the new choiceA. So the correct answer must be updated from B to A. 

 

BACD changes the choices as  choiceB is changed to choiceA choiceA is changed to choiceB choiceC is changed to choiceC choiceD is changed to choiceD 

3)       Update the choices by calling the getChoice(char) four times to get each choice, and reset the choices by using set4Choices(...) method. 
This assignment has only one task, but the development process is decomposed into several steps. Follow the instruction one by one. Don't go to the next step if your program does not return the proper output in each step.  

 

 

Step 1: Check the Assignment5.java
Compile and run the Assignment5.java. You MUST have error messages because another class is required to compile the code. Don't change anything in the Assignment5.java during this homework. If you change any code, you lose 5 Pts. Go to the next step and start to implement the Quiz.java.  

 

Step 2: Develop a class with dummy methods  
Make a new file and save it a Quiz.java in the same location as Assginment5.java. Create all required instance variables and methods with dummy (no error code) statements in the class. Keep empty in the void-type method, and make only one return statement in the return-type method. The Quiz class with dummy statements looks like below. 

 

  

 

Once all dummy methods are finished, compile the Quiz.java and Assignment5.java in your development environment. Run (execute) the Assignment5 after both files are complied. If it works, submit the both java files to the online site. You will see the following output, which run without any error, but nothing is done because all methods are dummy so far.  

 

Dummy Program Output (No compile error but do nothing) 

--------------- 

YOUR OUTPUT 1 

---------------- 

 ****** Tester Program ****** 

 

Command Options -------------------- 

A: Test the quiz 

B: Edit the question 

C: Edit the choices 

S: Shuffle the choices 

Q: Quit the program 

?: Display this menu  

------------------------------------ 

Please input a command: 

(A:Quiz, B:Edit question, C:Edit choices, S:Shuffle, Q:quit)      B [Edit the question]       [Type a question]:  

 

Please input a command: 

(A:Quiz, B:Edit question, C:Edit choices, S:Shuffle, Q:quit) 

     C [Edit the first choice]  

     [Type the first choice A]:  

     [Type the second choice B]:  

     [Type the third choice C]:  

     [Type the forth choice D]:  

     [Type the correct answer]:  

 

Please input a command: 

(A:Quiz, B:Edit question, C:Edit choices, S:Shuffle, Q:quit) 

     A [Test the quiz!]  

 

     [Type A, B, C or D]  No! 

 

 

 

 

... Continued to display the similar comments 

 

 

Please input a command: 

(A:Quiz, B:Edit question, C:Edit choices, S:Shuffle, Q:quit) 

Command Options -------------------- 

A: Test the quiz 

B: Edit the question 

C: Edit the choices 

S: Shuffle the choices 

Q: Quit the program 

?: Display this menu  

------------------------------------ 

 

Please input a command: 

(A:Quiz, B:Edit question, C:Edit choices, S:Shuffle, Q:quit)  

 ****** End of Program ****** 

 

Step 3: Complete Methods  
Now it is time to start the real missions. Complete all constructor and methods required in the table above. If it works in your environment, submit the both java files to the online site, and check if your output is the same as the output example. 

*) The shuffle() is a challenging question but only 2 points. If you cannot finish it, keep it as 
a dummy to get the 90% of full points.
 
 

 

Example Execution:(Not dummy but real program) 

The following are example inputs and outputs. The user inputs are shown in red (which is not seen online submission). Make your own questions rather than the examples. The output may be different from yours in green because the random letter is generated.  

 

---------------- 

INPUT 1 

---------------- 



What is 3 + 5? 





9  

10 

11 























B ? 



 

---------------- YOUR OUTPUT 1 

---------------- 

 ****** Tester Program ****** 

 

Command Options -------------------- 

A: Test the quiz 

B: Edit the question 

C: Edit the choices 

S: Shuffle the choices 

Q: Quit the program 

?: Display this menu  

------------------------------------ 

Please input a command: 

(A:Quiz, B:Edit question, C:Edit choices, S:Shuffle, Q:quit)B 

     B [Edit the question]  

     [Type a question]: What is 3 + 5? 

 

Please input a command: 

(A:Quiz, B:Edit question, C:Edit choices, S:Shuffle, Q:quit)C      C [Edit the first choice]  

     [Type the first choice A]: 8 

     [Type the second choice B]: 9 

     [Type the third choice C]: 10 

     [Type the forth choice D]: 11 

     [Type the correct answer]: A 

 

Please input a command: 

(A:Quiz, B:Edit question, C:Edit choices, S:Shuffle, Q:quit)A      A [Test the quiz!]  

Q: What is 3 + 5? 

A): 8 

B): 9  C): 10 

D): 11 

 

     [Type A, B, C or D] B No! 

 

Please input a command: 

(A:Quiz, B:Edit question, C:Edit choices, S:Shuffle, Q:quit) A      A [Test the quiz!]  

Q: What is 3 + 5? 

A): 8 

B): 9  C): 10 

D): 11 

 

     [Type A, B, C or D] C No! 

 

Please input a command: 

(A:Quiz, B:Edit question, C:Edit choices, S:Shuffle, Q:quit) A      A [Test the quiz!]  

Q: What is 3 + 5? 

A): 8 

B): 9  C): 10 

D): 11 

 

     [Type A, B, C or D] A Good Job! 

 

Please input a command: 

(A:Quiz, B:Edit question, C:Edit choices, S:Shuffle, Q:quit) S 

     S [Shuffle the choices]  

 

Please input a command: 

(A:Quiz, B:Edit question, C:Edit choices, S:Shuffle, Q:quit) A      A [Test the quiz!]  

Q: What is 3 + 5? 

A): 10
B): 8 C): 11 D): 9  

 

     [Type A, B, C or D] A No! 

 

Please input a command: 

(A:Quiz, B:Edit question, C:Edit choices, S:Shuffle, Q:quit) A      A [Test the quiz!]  

Q: What is 3 + 5? 

A): 10 B): 8 C): 11 D): 9  
 

     [Type A, B, C or D] B 

Good Job! 

 

Please input a command: 

(A:Quiz, B:Edit question, C:Edit choices, S:Shuffle, Q:quit) ? 

Command Options -------------------- 

A: Test the quiz 

B: Edit the question 

C: Edit the choices 

S: Shuffle the choices 

Q: Quit the program 

?: Display this menu  

------------------------------------ 

 

Please input a command: 

(A:Quiz, B:Edit question, C:Edit choices, S:Shuffle, Q:quit) Q 

 

 ****** End of Program ****** 

More products