Starting from:

$25

CSE110 - Assignment #3  - Solved

Topics 

        •     Loops (Chapter 4) 

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) 

The following are the exercises about the loops in Java.  It is the program to repeatedly read user inputs and display the information how many 0s and 1s are typed. Write the answers in a comment block before the code of Part2. First, make a new Java file, CommandTemplate.java, and copy the following code.  

  

a.       Test the program and write the output(s) when you type 'a', 'b', 'r' and 'q' keys. 

b.      Write if-block (including the if -expression and statements) to add a command to display "Display the result" when the user inputs  'D' or 'd'.   

c.       Add the following code inside of command A block: if (command == 'A' ) 

  

Test the program. Type the 'A' key and add the following data.  

0 0 1 1 1 0 1 

Explain what the values of num0 and num1 will be after inputting the data above. 

d.      Complete the for-loops below to display '*' symbols of num0 and num1. Then copy the code inside of command-D block (question-b) above).  

  

Test the program and type 'A' to add the same data in the question c. Then type 'D' to display the result. Copy the for-loop statements as an answer of this question. 

e.       Write the statement(s) to reset both num0 and num1 as 0 inside the command-R block.  

Note: The answers to the questions above should be typed in the block of comments in the java file such as; 

/* 

a)  a Key: XXX    b Key: XXX 

    ... 

b)  if (xxx) xxx;  

c)  The num0 is ??? the num1 is ???  

d)  for (..... ) ...    for (..... ) ... 

e)  ... */ 

Part 2: Programming (15 pts)  

Write a Java program called Assignment3.java. The program is repeatedly to read a list of A, B, C letters, and display the histogram and GPA in a specific format. This program will follow a very simple loop command-program. 

 

 

 

Format: 

 

Input:  B B B A A A A B C C A 

 

Output:    

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

A           |***** 

B           |****   

C           |** 

          ------------------------           GPA =  3.27 

 

This assignment has only one task, but the development process is decomposed into two 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.  

 

Step1 :  

The first step is to develop a loop command program to execute several different functions corresponding to the input letter/command. When the program starts, it displays the help-message below,  

 

"Choose (A: add grades), (N: new grades), or (Q: quit)" 

 

, and waits for the user input. When a command is input, execute a function corresponding to the command (look at the table below).  Once it is completed, it displays the help-message again and waits for the other user input. The loop continues until the user inputs 'Q' or 'q', which is to terminate the program. Study the CommandTemplate.java in Part#1 if you have no idea. 

 

Command 
Function 
'A' or 'a' 
Display 

"Type the additional input in single line" 

(1 Pt) 
'N' or 'n' 
Display  

"Type a new list of input in single line" 

(1 Pt) 
'Q' or 'q' 
Display  

"*** End of program ***" 

and, terminate the program (1 Pt) 
Other 

 letters 
Display  

"Invalid command was input!" 

(1 Pt) 
 

Step2:  

This is the main step in this assignment. For the cases of 'A' (or  'a') and 'N' (or 'n'), create the statements to read a list of letters and display the histogram and GPA.  For both commands, read letters in a single line after displaying the message in the Step1. The following is one example input.  

 

                            A  B  B  C  B  X A  A  
 

In the part #1, the reading stops when the scanner hasNextInt() fails. On the other hand, in this case, stops while-loop when the scanner hasNext() fails. 

 

•      In the cases of 'A' and 'N' commands, count the numbers of letters A , B. and C, and display the histogram (5 Pts).  The other letters are not counted.  

•      Also, calculate and display the GPA using (A = 4.0, B = 3.0, and C = 2.0) (2 Pts)  

•      In the case of 'A' command, add the data to the existing one and display the data (2 Pts).  

•      In the case of 'N' commmand, renew the data and add the new data in the case of command 'N' (2 Pts). The counters for A, B, and C should be set as 0s. 

•      Look at the format and example outputs. The average, maximum, and minimum are aligned right in each line. (Format Problem: -1 Pts) 

 

  

 

. Program Input (1) 

---------------- INPUT 1 

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



C C A A A B AA                                                                       B 

A A A A B                                                                           B 

A A BB                                                                              C 

 





 

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

YOUR OUTPUT 1 

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

Choose (A: add grades), (N: new grades), or (Q: quit) N 

Type a new list of grade in a single line 

C C A A A B A B
 

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

   A|**** 

   B|** 

   C|** 

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

GPA =  3.25 

 

Choose (A: add grades), (N: new grades), or (Q: quit) A 

Type the additional grade in a single line 

A A A A B B 
------------------------ 

   A|******** 

   B|**** 

   C|** 

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

GPA =  3.43 

 

Choose (A: add grades), (N: new grades), or (Q: quit) N 

Type a new list of grade in a single line 

A A B C 
------------------------ 

   A|** 

   B|* 

   C|* 

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

GPA =  3.25 

 

Choose (A: add grades), (N: new grades), or (Q: quit) B Invalid command was input! 

 

Choose (A: add grades), (N: new grades), or (Q: quit) Q 

*** End of program *** 

 

Program Input (2) 
---------------- INPUT 2 ---------------- n 

X A B B A A A B B B C B A X A A B 

a C C C B B Q A n B B B B 



 

---------------- YOUR OUTPUT 2 

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

Choose (A: add grades), (N: new grades), or (Q: quit)n 

Type a new list of grade in a single line 

X A B B A A A B B B C B A X A A B 
------------------------ 

   A|******* 

   B|******* 

   C|* 

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

GPA =  3.40 

 

Choose (A: add grades), (N: new grades), or (Q: quit)a 

Type the additional grade in a single line 

X A B B A A A B B B C B A X A A B
 

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

   A|******** 

   B|********* 

   C|**** 

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

GPA =  3.19 

 

Choose (A: add grades), (N: new grades), or (Q: quit) n 

Type a new list of grade in a single line 

B B B B
------------------------ 

   A| 

   B|**** 

   C| 

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

GPA =  3.00 

 

Choose (A: add grades), (N: new grades), or (Q: quit)q 

*** End of program *** 

 


More products