Starting from:

$25

CSE110 - Assignment #2  - Solved

Topics 

•         If statements (if, if/else, if/else if) 

•         Expressions (<, >, ==, !=)  

•         String methods: indexOf (String), substring(int, int), compareTo(String), toUpperCase(), toLowerCase() 

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)
 

This assignment has two parts. The first part is to write the answers briefly in your words. and it is not a coding question but English writing question. Part 1 is a hint for Part 2. Part 1 shows a sample code to try to sort three integers, and Part 2 is  asked to make a program to sort three strings. Try to answer the Part 1 questions first, then apply to the string case in Part 2. 

 

The following code is supposed to read three integers and sort them from the smallest to the largest. It creates six integer variables. The a, b, and c are used for the first, second, and third user inputs. The small, middle, and large are used to assign the values from a, b, c corresponding to the order of values. Write the code and test it. If it works, then answer each question below. (Don't submit this ThreeInteger.java as an assignment #2 ) 

  

a.       This program is not completed but keep it as it is. Test six cases to figure out what is wrong. For the case of 1, 4, and 6 inputs, we need to test the 6 cases of one by one. 

(first, second, third) = (1, 4, 6), (1, 6, 4), (4, 1, 6), (4, 6, 1), (6, 1, 4), and (6, 4, 1).  

Test all of them above and write the input and output of wrong case(s), and explain the reason in your word. (1pt) 

b.      In the line 23 (the line numbers are shown in the left of code), write a single statement in the else-block to make the program sort in all six cases correctly.  (1pt). 

c.       The code is to compare three numbers.  

Suppose to develop another program to compare three string data. When you check the 

alphabet order of string data instead of numbers, it is required to use compareTo(String) method. For example, if you want to compare  str1="Yoshi" and str2="Mario", use  

 

if(str1.compareTo(str2) > 0) 

 

Read the textbook (Special Topic 3.2) and answer whether the if-statement above is evaluated as true or false, and explain the reason in your words.  (1 pt). 

d.      Answer the result of the snipped code below, and explain the reason. (1 pt). 

 

String a = "Yoshi"; 

String b = "yoshi"; 

System.out.println( a.compareTo(b) ); 

 

e.       All characters are ordered in Java. The alphabet letters are ordered as "A", "B", "C"... 

"Z", "a", "b".... "y", "z". In other words, if a capitalized letter is not between "A" and "Z", the letter must not be an alphabet such as a special symbol or number. Make the ifstatement to check if a string variable (supposed to be a single letter), str, is an alphabet letter or not.  (1 pt). 

 

if ( 

   str.toUpperCase().compareTo(???) >=0 &&       str.toUpperCase().compareTo(???) <= 0  ) 


a)  For Input (X, X, X), it returns a wrong output as (X, X, X), because …  

b)  xxx = xxxx; 

c)  XXX because … 

d)  XXX because … 

e)  XXX and XXX  

*/ 

                 

Part 2: Programming (15 pts)  
Write a Java program called Assignment2.java. The program is to display questions and read user inputs, then calculate and print out the requested value with a proper format. This program will follow a very simple process.  *) Part1 is a big hint for this part.  

 

 

 

 

1.      Read three names one by one, and display the list in alphabetic/lexicographic order (5 pts). For example, when the inputs are "Smith", "john", and "mike", it displays: 

 

JOHN, MIKE, SMITH 

 

2.      All letters of output (displayed) name should be capitalized (5 pts). Line breaks may be different from the submission output and your local output, but no problem.  

3.      However, if the name starts with non-alphabetic letter, then display an error message (5 pts) after it is input. In the case, use a blank name. Look at the example execution for more details and the display format. 

 

(*) Use the compareTo(String) method. (Special Topic 3.2 in Textbook) 

  


 

Example Execution: 

The following is an example input and output. The input is shown in red (Not displayed But typed). Make your own questions rather than this example.  

 

Example 1
---------------- 

YOUR OUTPUT 1 

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

*** TASK: Read name and display them in alphabetic order *** 

Please input he first name: Smith 

    SMITH  

Please input the second name: jOHN 

    JOHN, SMITH  

Please input the third name: mike 

    JOHN, MIKE, SMITH 

 

*** END OF Assignment#2 *** 

 

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

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

*** TASK: Read name and display them in alphabetic order *** 

Please input he first name: 007James 

    Error: The first letter should be an alphabet 

Please input the second name: chris 

    CHRIS  

Please input the third name: GEORGE 

    CHRIS, GEORGE 

 

*** END OF Assignment#2 *** 

 

 

More products