Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

COS 125 Assignment #4 Due: Friday 9/29/17 Late: Friday 10/6/17 Name Lll Cacte As

ID: 3872093 • Letter: C

Question

COS 125 Assignment #4 Due: Friday 9/29/17 Late: Friday 10/6/17 Name Lll Cacte Assignments should be submitted to Blackboard by the due date. Submissions after Friday 9/29 will be marked late with a 15 point penalty. Submissions after Friday 10% are not accepted. ) with answers to PartI questions and the sereen shot of program output. Part I. (20 pts.) The user enters an upper case letter and the code displays that letter and all others through letter Z. public statie void main) { Scanner scan = new Scanner(System.in); char letter; do System.out.print ("Enter a letter A-Z:"" letter scan.nextLine).charAt(0); while Di for G letterZ'; letter++) System.out.printin (letter); scan.closeO; 1. Write the do loop test so the code repeats on bad data. The user is requested to enter a capital letter 2. The user enters three letters: m zS with one letter for each pass of the loop. The do loop makes passes. b. 1 C. d. 3 e. infinite 3. The output section uses a for loop. Which statement is not true about this decision? a. If the user enters letter A, the for loop will iterate 26 times. b. If the user enters letter Z, the for loop will iterate zero times. c. Since the loop iterates for a specific number of times, the for loop is most appropriate. d. By using the printin method in the loop body, each letter displays on a separate line. 4. Why does the for loop use null statement; as its first piece? a. The initialization is the statement: letterz b. This for loop has no loop test so this piece is left blank. c. A for loop only requires two pieces, the loop test and the increment. d. There is no explicit initialization value since letter received its value earlier in the code.

Explanation / Answer


ANS1)
public class Part1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
char letter='s';
do{
System.out.println("enter letters A-Z");
letter=sc.next().charAt(0);
}while(letter<65 || letter>90);
for(;letter<='Z';letter++){
System.out.println(letter);
}
sc.close();
}
}

OUTPUT:
enter letters A-Z
a
enter letters A-Z
w
enter letters A-Z
2
enter letters A-Z
3
enter letters A-Z
4
enter letters A-Z
1
enter letters A-Z
U
U
V
W
X
Y
Z

//WE KEEP A CONDITION INSIDE WHERE LOOP TO KEEP ON ASKING INPUT TILL USER ENTERS CAPITAL LETTERS..ASCII VALUE OF A IS 65 AND Z=90


2 ANS)
enter letters A-Z
m
enter letters A-Z
z
enter letters A-Z
S
S
T
U
V
W
X
Y
Z
IT TAKES 3 PASES .BECAUE m,z are small letters.
3ANS)
WRONG OPTION IS:OPTION 2..BECAUSE WHEN USER ENTERS Z IT LOOPS FOR ONE TIME NOT ZERO TIMES

4 ANS)
OPTION D
THERE IS NO NEED FOR EXPLICIT DECLARATION AS LETTER IS GOING TO INTIALIZE

5 ANS)only first one prints the required output second one prints 8 6 4 2 0 -2

6 ANS)for the above code option is c
enter value
6 3 8 5 4 0
6 8 4

7 ANS)OPTION C
hello
hello
hello
hello
goodbye
hello
hello
hello
hello
goodbye
hello
hello
hello
hello
goodbye
HELLO PRINTS 12 TIMES AND GOODBYE PRINTS 3 TIMES
BECAUSE INSIDE FOR LOOP EXECUTES ONLY ONE STATEMENTS BECASE THERE IS NO FLOR BRACKTES TO EXECUTE MULTIPLE STATEMENTS
8 ans) option c
option option c makes m to 0
becase it is the only if block satisfies the criteria

9 ans)option c
for loop inside of while loop
because we use while loop when we dont know the start and end limit
we only check the condition..and inside for loop to iterate


10 ans)option true
checked exceptions are caught at time of compile time ..it must me handled or throws