Question
JAVA Code: The lab this lesson introduces students to while and for loops. By the end of this lab, students should be able to Write
JAVA Code:
The lab this lesson introduces students to while and for loops. By the end of this lab, students should be able to
Write loops that are counter controlled
Write loops that are sentinel controlled
Write nested loops
Write loops that have nested if statements in the loop body
Write loops that analyze a sequence of numbers
Problem 1:
Gimme a W
Write a console program that requests that the user enter a string. Using only String's length and charAt methods, print the locations of each 'w', and 't'. When an input has been fully processed, request the user to enter another string. When the user enters -1, terminate the program. Use a Scanner and nextLine to read the user input.
For example, assume the user enters
wake tech is wonderful
the program prints
'w' or 't' are at locations 0, 5, 13
the user enters
Java wouldn't be the same without it
the program prints
'w' or 't' are at locations 5, 12, 17, 26, 28, 32, 35
the user enters
-1
the program prints
bye
Program 2:
Averaging Grades
Write a console program that asks the user to enter a number of students in a class. For each student, the program asks whether the student is male or female, and the course average for that student. When all student information has been entered, the program prints the number of male and female students and their averages.
For example,
How many students? 5
Is student 1 male or female (M/F)? m
What is student 1's average? 85
Is student 2 male or female (M/F)? m
What is student 2's average? 91
Is student 3 male or female (M/F)? f
What is student 3's average? 95
Is student 4 male or female (M/F)? f
What is student 4's average? 80
Is student 5 male or female (M/F)? m
What is student 5's average? 88
The 3 male students average is 88
The 2 female students average is 87.5
Program 3:
Write a program using a Scanner that asks the user for a number n between 1 and 9 (inclusive). The program prints a triangle with n rows. The first row contains only the square of 1, and it is right-justified. The second row contains the square of 2 followed by the square of 1, and is right justified. Subsequent rows include the squares of 3, 2, and 1, and then 4, 3, 2 and 1, and so forth until n rows are printed.
Assuming the user enters 4, the program prints the following triangle to the console
1 4 1 9 4 1 16 9 4 1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started