Description
Maximum points: 20 pts
Topics • If statements (if, if-else, if-else if-else) • 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.
Important Note: All submitted assignments must begin with the descriptive comment block. To avoid losing trivial points, make sure this comment header is included in every assignment you submit, and that it is updated accordingly from assignment to assignment. Your programming assignments require individual work and effort to be of any benefit. Every student must work independently on his or her assignments. This means that every student must ensure that neither a soft copy nor a hard copy of their work gets into the hands of another student. Sharing your assignments with others in any way is NOT permitted. Violations of the University Academic Integrity policy will not be ignored. The university academic integrity policy is found at https://www.asu.edu/studentlife/judicial/integrity.html Don’t use any paid tutoring service online. We have strong connections to the world-wide proctors on the site, and if you use the commercial services, very serious penalty (expel, cheating academic history, etc) will be given.
© 2018 Yoshihiro Kobayashi <ykobaya@asu.edu
Part 1: Writing Exercise: (5 pts) The following code is to read three integers and display 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 display 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.
a. This program is not completed yet. We need to test six cases to figure out what is wrong. For the case of 1, 2, and 3 inputs, we need to test the 6 cases of (first, second, third) = (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), and (3, 2, 1).
Test all of them 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 to complete the program. (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
© 2018 Yoshihiro Kobayashi <ykobaya@asu.edu
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 statement, and explain the reason. (1 pt).
“Yoshi”.compareTo(“yoshi”) 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) Note: The answers to the 5 questions (a through e) above should be typed in the block of comments in the java file such as; /* 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 */
© 2018 Yoshihiro Kobayashi <ykobaya@asu.edu
Part 2: Programming (15 pts) Write a Java program called Assignment3.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, and Smith”. 2. The first letter of output (displayed) name should be capitalized and the others are lower cases (5 pts). 3. 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) IMPORTANT on Programming • It is allowed to make only one Scanner variable. In your PC, it may work with multiple Scanner variables, but the server site (online submission) does not accept it. If you make more than one Scanner variables, you may lose all 15 points. • Use only the Java statements that have been covered in class (or textbook) to date. This means you CAN use declaration, assignment, input and output statements. DO NOT use any other statements ( loop, array, break, sort, etc.). If in doubt, ask your TA or instructor. If you use them, then you lose the points of task. • Use the next() method instead of nextLine()when you read the String data using Scanner in this assignment. Your PC can work in either way, but not on the server site. 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 *** TASK: Read names and display them in alphabetic order *** Please input the first name: Smith (Smith) is the name list Please input the second name: jOHN (John, Smith) is the name list Please input the third name: mike
Get input Calculate Display results
© 2018 Yoshihiro Kobayashi <ykobaya@asu.edu
(John, Mike, Smith) is the name list. *** END OF Assignment#3 *** Example 2 *** TASK: Read names and display them in alphabetic order *** Please input the first name: 007James Error: The first letter should be an alphabet. () is the name list Please input the second name: chris (Chris) is the name list Please input the third name: GEORGE (Chris, George) is the name list *** END OF Assignment#3 ***
/********************************************************************************** Submit your homework by following the instructions below: **********************************************************************************/ • Go to the course web site (my.asu.edu), and then click on the on-line Submission tab. Log in the site using the account, which was registered at the first Lab session. Make sure you use the correct email address for registration. This will allow you to submit assignments. Please use your ASU email address. • Submit your Assignment3.java file on-line. Make sure to choose HW3 from drop-down box. • The Assignment3.java should have the following, in order: • In comments, the assignment Header including your name, title, lab, date, etc. • In comments, the answers to questions presented in Part#1.
• The working Java code requested in Part #2. • The Assignment3.java file must compile and run as you submit it. You can confirm this by viewing your submission results. Important Note: You may resubmit as many times as you like until the deadline, but we will only mark your last submission. NO LATE ASSIGNMENTS WILL BE ACCEPTED.



