Design a java program that asks for the number of students registered in a cours
ID: 3552145 • Letter: D
Question
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.
Write a java program based on the design you created above. 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 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 vairiable's value changes.
Execute your program. Then copy the output and save it in a block comment at the end of your program
Explanation / Answer
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Roster1 {
public static void main(String[] args) throws IOException{
Scanner in = new Scanner(System.in);
System.out.print("Enter the no of students enrolled: ");
int num = in.nextInt();
PrintWriter writer = new PrintWriter(new File("student1.dat"));
if(num>0){
int count=1;
while(count<=num){
System.out.print("Enter the name of student "+count+": ");
String name = in.next();
writer.println(name);
writer.flush();
count++;
}
System.out.println("You entered details of all the students.");
System.out.println("Your ouput file is created 'student1.dat'");
}
//closing the streams
writer.close();
in.close();
}
}
/**********************Output Screen*******************************
Enter the no of students enrolled: 6
Enter the name of student 1: John
Enter the name of student 2: Rick
Enter the name of student 3: Matt
Enter the name of student 4: Paul
Enter the name of student 5: Larry
Enter the name of student 6: Sam
You entered details of all the students.
Your ouput file is created 'student1.dat'
**********************************************************************/
/*******************Output File****************
John
Rick
Matt
Paul
Larry
Sam
**************************************/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.