Using the while Looping (Repetition) Structure Design and write code for the Jav
ID: 3561525 • Letter: U
Question
Using the while Looping (Repetition) Structure
Design and write code for the Java programs that use while loops.
1. a. Design a Java program that asks for the number of students registered in a course. The user should be prompted to enter the number of students enrolled in a course. If the number of students is greater than 0, use a counter-controlled while loop to prompt the user to enter the names of the students registered for the class. Create an output file that contains the names of the students in the class. Display a message to the user when the program is complete.
Following is a copy of the screen results that might appear after running your program, depending on the data entered. The input entered by the user is in bold. This program asks the user to enter the number and names of students in a course.
How many students are registered for this class? 10
Enter the student's name: George Smith
Enter the student's name: Elaine Sanders
Enter the student's name: Jack Cunningham
Enter the student's name: Susie Brown
Enter the student's name: Marvella Garcia
Enter the student's name: Tony Peterson
Enter the student's name: John Jones
Enter the student's name: Mary Evans
Enter the student's name: Nancy Drew
Enter the student's name: Lola Zapata
The class is full.
Write your design in the following space. Your design should be a list of Java comments without any code.
b. Write a Java program based on the design you created in Exercise 1a.Enter your program and name it Roster1.java. Name your output file student1.dat.
Step through your code by hand and complete a memory chart showing what occurs in memory when the Java code is executed.
To fill out the memory chart for each variable, specify the name of the variable, its data type, the line number in which the variable is assigned its initial value, the initial value assigned to the variable, and the line numbers in which the variable
Explanation / Answer
Design -
/*
* Declare variable to store number of students noofstudents
* Prompt user for number of students and store entered value in noofstudents
* declare an array of size noofstudents
* run a while loop for noofstudents
* in loop prompt user to enter student name and store in array
* after loop ends check if user has entered same number of namse as noofstudents
* if yes print message class is full
* use a loop to iterate through array and create a output file
*/
Code-
import java.io.PrintWriter;
import java.util.Scanner;
public class Roster1 {
public static void main(String Args[])
{
int noofstudents=0;
try
{
System.out.println("How many students are registered for this class?");
Scanner objscan=new Scanner(System.in);
noofstudents = objscan.nextInt();
int i=0;
String[] arrStudents=new String[noofstudents];
while(i<noofstudents)
{
System.out.println("Enter the student's name: ");
arrStudents[i] = objscan.next();
i++;
}
if(i>=noofstudents)
System.out.println("The class is full");
PrintWriter writer = new PrintWriter("student1.dat", "UTF-8");
i=0;
while(i<noofstudents)
{
writer.println((i+1) + " ." + arrStudents[i]);
i++;
}
writer.close();
}
catch(Exception e)
{
System.out.println("Error in operation: "+e.getMessage());
}
}
}
output-
How many students are registered for this class?
2
Enter the student's name:
rtyh
Enter the student's name:
yusgdf
The class is full
Variable name
Data type
Declaration line number
Initialization line number
Initial
Change
value
line number
noofstudents
int
8
8
0
i
int
15
15
0
21,28,32
arrStudents
String[]
16
16
null
20
Variable name
Data type
Declaration line number
Initialization line number
Initial
Change
value
line number
noofstudents
int
8
8
0
i
int
15
15
0
21,28,32
arrStudents
String[]
16
16
null
20
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.