CSE 110 – Assignment #2




5/5 - (4 votes)

Part 1: Formatted Output: (5 pts) Chapter 2.3.2
So far we used System.out.println and System.out.print methods in class. Here
introduce a new print method, System.out.printf. This method can print out the value
with flexible format. Write the following code. It is supposed to print out 2.7182818284.
double e = 2.7182818284;
System.out.printf(“%f”, e);
Follow the questions below and answer the output and explain the reason in your words.
Q1. (1 pt) Use “%.2f” instead of “%f”
Q2. (1 pt) Use “%10.2f” instead of “%f”
Q3. (1 pt) Use “%s” instead of “%f”
Q4. (1 pt) Use “%d” instead of “%f”
Q5. (1 pt) Use “%.0f” instead of “%f”
Note: The answers to the questions above should be typed in the block of comments in the
Assignemnt2.java file such as;
Q1):XXXXXX because …
Q2):XXXXXX because …
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.
• Task 1 (5 pts): Calculate the average value from input three integer values. Use the
format to show two digits after the decimal point for the average value. It is OK to
use System.out.printf() method (Look at the textbook), though it is
possible to complete this task without it.
• Task 2 (5 pts): Read two string inputs (first name and last name). From the input,
display the full name, total number of characters, initial letters and the last letter in
the same format of sample output below. The toUpperCase() is the method to
Get input Calculate Display results
© 2018 Yoshihiro Kobayashi
convert all letters to upper case in a String (cannot be used for char type) , and the
toLowerCase() is the method to convert all letters to lower case.
• Task 3 (5 pts): Calculate the cosine and sine values from an input degree value. Use
the Math class method, cos(a) , sin(a), and Math.PI. Both methods accept a radian
value instead of degree. Use the Math function to convert from degree to radians, or
the formula below.
𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟 = 𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑 ∗ 𝜋𝜋/180
Sample Output:
The following is an example input and output. The user input is shown in red. Test your
program by making your own inputs rather than this example. Look at the comments/hints in
the explosions.
*** TASK 1: Calculate the average of three inputs ***
Please input the first integer: 3
Please input the second integer: 5
Please input the third integer: 8
The average of three inputs is: 5.33
*** TASK 2: Display the data of name ***
What is your first name?: LEONHARD
What is your last name?: euler
Your name is : “Leonhard Euler”
The total number of characters is: 13
The initial letter is: LE
The last letter is : R
*** TASK 3: Calculate the cosine and sine values ***
Please input the degree: 25.0
Radian of 25.00 degree is: 0.436332
cos(25.00) =: 0.91
sin(25.00) =: 0.42
*** END OF Assignment#2 **
© 2018 Yoshihiro Kobayashi
1) 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.
2) Use only the Java statements that have been covered in class to date. This means you
CAN use declaration, assignment, input and output statements. DO NOT use any other
statements (if-else, loop, etc.). If in doubt, ask your TA or instructor. If you use them, then
you lose the points of task.
3) Complete each task one by one. If your program file does not run correctly, then you may
lose all 15 points.
Submit your homework by following the instructions below:
• Go to the course web site (my.asu.edu), and then click on the Online 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 Assignment2.java file on-line. Make sure to choose HW2 from drop-down box.
• Assignment2.java should have the following, in order:
• In comments, the assignment Header described in “Important Note”.
• In comments, the answers to questions presented in Part#1.
• The working Java code requested in Part #2.
• The Assignment2.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.