Could you Please answer the question STEP BY STEP and adding comments according
ID: 3918889 • Letter: C
Question
Could you Please answer the question STEP BY STEP and adding comments according to the question. Thanks in advance
Create a JAVA program that gets 5 students information from the user.
Each student’s information is used to create an instance of Student class.
• Student Class has the following attributes:
o ID, first name, last name and email
Student class has the following methods
• Student constructor - accepts 4 parameters (ID, first name, last name and email)
• getInfro method accepts no parameter, returns strings that shows: ID, first name, last name and email separated by comma.
The student objects are then stored in a Studetn array
Loop through the array and print each student's info to the console screen.
Explanation / Answer
Below is the code with comments to explain the working.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Student {
int id;
String first_name;
String last_name;
String email;
/*
Constructor which accepts accepts 4 parameters (ID, first name, last name and email)
*/
Student(int id1, String f_name, String l_name, String emailid){
this.id = id1;
this.first_name = f_name;
this.last_name = l_name;
this.email = emailid;
}
/*
Function to Return student details as a comma seperated string of the student object which is calling this function.
*/
String getInfo(){
String studentDetails = ""; // String to Store student details
studentDetails = this.id+","+this.first_name+","+this.last_name+","+this.email; // concatinate student details
return studentDetails; // return the studentDetails string
}
public static void main(String args[]) throws IOException{
int id;
String first_name, last_name, email;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // For taking console input
Student stud[] = new Student[5]; // Create a Array a of objects to store student objects
// Loop to take student details from user and store them in the array.
for (int i = 0; i < 5; i++) {
System.out.println("Enter Student ID:=>");
id = Integer.parseInt(br.readLine()); // Parse the input as integer.Only takes integer as input.
System.out.println("Enter First Name:=> ");
first_name = br.readLine();
System.out.println("Enter Last Name:=>");
last_name = br.readLine();
System.out.println("Enter Email:=>");
email = br.readLine();
// Call student class constructor to create an instance of it with input entered by user and store the object in stud array
stud[i] = new Student(id, first_name, last_name, email);
}
System.out.println("Student Details:");
// Loop through the array and print each student's info to the console screen.
for (int i = 0; i < 5; i++) {
String info = stud[i].getInfo(); // Call getInfo() on the ith object in array.
System.out.println(info); // Print the details.
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.