Part 1: Exercises (6 pts) Note: The answers to the following questions should be
ID: 3883903 • Letter: P
Question
Part 1: Exercises (6 pts) Note: The answers to the following questions should be typed in the block of comments in the Assignemnt2.java file. Please make sure they're commented out (green). Type them neatly and make them easy to read for the graders. Given the String object called myString with the value "Coding is fun!” answer questions 1 through 3. (page 59 to 65) Question #1 (1pt): Write a statement that will output (System.out) the number of characters in the string. Question #2 (1pt): Write a statement that output the index of the character ‘d’. Question #3 (1 pt): Write a statement that uses the original string to extract the new string "fun" and prints it to the screen Question # 4 (2 pts): What do the following expressions evaluate to in Java given int x = 3, y = 6; (page 102-105) a) x == y / 2 b) x % 2 == 0 || y % 2 != 0 c) x – y < 0 && !(x >= y) d) x + 6 != y || x / y <= 0 Question # 5 (1 pt): What does the following statement sequence print? String str = “Harry”; int n = str.length(); String mystery = str.substring(0,1) + str.substring(n-1, n); System.out.println(mystery); Note : The API (Application Programming Interface) documentation lists the classes and methods of the Java library. Go to http://docs.oracle.com/javase/6/docs/api/ and find out the methods you may need to answer the questions. Part 2: Programming (14 points) Write a Java program called Assignment2.java. Your program is to ask for names and display them in alphabetical order... • Your program reads three strings in lower or uppercase letters, and displays them in alphabetic/lexicographic order. For example, when the user input are "Smith", "john", and "mike", it displays "John, Mike, and Smith". Your program will display the first letter of the output name in capital letter and the rest in lower case letters. If the name starts with non-alphabetic letter, then display an error message. See the sample output: Note: you need to use the compareTo(String) method. (page 88) Use only the Java statements that have been covered in class to date. DO NOT use any other statements (loop, array, break, sort, etc.). If you use topics not covered in class, we will take off points. Sample run: The user input are in red. Example 1 Please input the first name: Smith Smith is the first name. Please input the second name: jOHN John is the second name. Please input the third name: mike Mike is the third name The names in alphabetical order are "John" "Mike" "Smith" Sample run 2: Please input the first name: 48AKB Error: The first name was not accepted. Please input the second name: chris Chris is the second name. Please input the third name: GEORGE George is the third name The names in alphabetical order are: "Chris" "George" Submit your homework by following the instructions below:
Explanation / Answer
Part 1:
Question #1
System.out.println("Number of characters in myString = "+myString.length());
Output:
Number of characters in myString = 14
Question #2 :
System.out.println("Index of character 'd' = "+myString.indexOf('d'));
Output:
Index of character 'd' = 2
Question #3:
System.out.println(myString.substring(10,13));
Output:
fun
Question #4:
a)true
b)false
c)true
d)true
Question #5:
Output:
Hy
Part 2:
import java.util.*;
class Assignment2
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
String name1,name2,name3;
System.out.println("Please input the first name:");
name1 = scan.nextLine();
char c1 = name1.charAt(0);
if(c1 >= '0' && c1 <= '9')
System.out.println("Error: The first name was not accepted.");
System.out.println("Please input the second name:");
name2 = scan.nextLine();
char c2 = name2.charAt(0);
if(c2 >= '0' && c2 <= '9')
System.out.println("Error: The second name was not accepted.");
System.out.println("Please input the third name:");
name3 = scan.nextLine();
char c3 = name3.charAt(0);
if(c3 >= '0' && c3 <= '9')
System.out.println("Error: The third name was not accepted.");
String topString, middleString, bottomString;
topString = null;
middleString = null;
bottomString = null;
if (name1.compareTo(name2) < 0 && (name1.compareTo(name3) < 0))
{ topString = name1; }
else if (name1.compareTo(name2) > 0 && (name1.compareTo(name3) < 0)) {
middleString = name1; }
else { bottomString = name1; }
if (name2.compareTo(name1) < 0 && (name2.compareTo(name3) < 0)) {
topString = name2; }
else if (name2.compareTo(name1) > 0 && (name2.compareTo(name3) < 0)) {
middleString = name2; }
else { bottomString = name2; }
if (name3.compareTo(name1) < 0 && (name3.compareTo(name2) < 0)) {
topString = name3; }
else if (name3.compareTo(name1) > 0 && (name3.compareTo(name2) < 0)) {
middleString = name3; }
else { bottomString = name3; }
System.out.print("The Strings in alphabetical order: " + topString.substring(0, 1).toUpperCase() + topString.substring(1) +",");
System.out.print(middleString.substring(0, 1).toUpperCase() + middleString.substring(1)+" and ");
System.out.print(bottomString.substring(0, 1).toUpperCase() + bottomString.substring(1));
}
}
Output:
Please input the first name:Smith
Please input the second name:john
Please input the third name:mike
The Strings in alphabetical order: Smith,John and Mike