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

3\" (20 marks) Write a program to display the student information (First name, S

ID: 3737624 • Letter: 3

Question

3" (20 marks) Write a program to display the student information (First name, Study Major, and ID #) that you have just entered. For this problem, you need to create an ArrayListxyz with xyz as the StudentEntry type parameter. You will need to create a method (getEntry () to read in the three student information and return the StudentEntry type for the ArrayList. Within the package, create a separate file for the Student entry class. For exception handling, you need to provide a try catch block and allow user to re-enter if the input format is not correct. For displaying the results, you will use both the for-each and lIterator method as shown in the following example Please enter 3 Student name, study major, and registration number Enter an student's name: Mary Enter an student's Major subject: Computer Science Enter that student's registration number 1234 Enter an student's name: Tom Enter an student's Major subject: Physics Enter that student's registration number: 5344 Enter an student' s name: Matt Enter an student's Major subject: Engineering Enter that student's registration number: 9393 Here's the data you entered: display using For Each loop Name and major subject: (Mary, Computer Science) ID numberr: 1234 Name and major subject: (Tom, Physics) ID numberr: 5344 Name and major subject: (Matt , Engineering) ID numberr: 9393 siect:phys display using Iterator type loop++ Name and major subject: (Mary , Computer Science) ID numberr: 1234 Name and major subject: (Tom, Physics) ID numberr: 5344 Name and major subject: (Matt, Engineering) ID numberr: 9393

Explanation / Answer

Please find my ANswer.

Please rate my answer

####################      StudentInformation.java     ###############


import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class StudentInformation {
//Method to get arrayList of StudentEntry
public ArrayList<StudentEntry> getEntry(StudentEntry studentEntry){
ArrayList<StudentEntry> studentEntries=new ArrayList<StudentEntry>();
studentEntries.add(studentEntry);
return studentEntries;
}
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
System.out.println("Enter Student First Name :");
String fName=sc.next();
System.out.println("Enter student major :");
String studentMajor=sc.next();
System.out.println("Enter student id : ");
String id=sc.next();
Integer properID=null;
//Checking if input iss not integer format, prompting user to re-enter it.
while(properID==null){
try{
properID=Integer.parseInt(id);
}
catch(Exception e){
System.out.println("Incorrect id format. Integers only!");
System.out.println("Enter student id : ");
id=sc.next();
}
}
StudentEntry entry=new StudentEntry(fName, studentMajor, properID);
StudentInformation information=new StudentInformation();
ArrayList<StudentEntry> entries=information.getEntry(entry);
System.out.println();
if(entries!=null){
//Using foreach loop
System.out.println("Using foreach loop");
System.out.println("--------------------");
for(StudentEntry stEntry : entries){
System.out.println("Student First Name : "+stEntry.getFirstName());
System.out.println("Student Major : "+stEntry.getStudentMajor());
System.out.println("Student ID : "+stEntry.getId());
}
System.out.println();
//Using Iterators
System.out.println("Using Iterator");
System.out.println("--------------------");
@SuppressWarnings("rawtypes")
Iterator itr = entries.iterator();
while(itr.hasNext()) {
StudentEntry element = (StudentEntry) itr.next();
System.out.println("Student First Name : "+element.getFirstName());
System.out.println("Student Major : "+element.getStudentMajor());
System.out.println("Student ID : "+element.getId());
}
}
}
}

####################     StudentEntry.java     ####################


public class StudentEntry {
private String firstName;
private String studentMajor;
private Integer id;
//StudentEntry parameterized constructor
public StudentEntry(String firstName, String studentMajor, Integer id) {
super();
this.firstName = firstName;
this.studentMajor = studentMajor;
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getStudentMajor() {
return studentMajor;
}
public void setStudentMajor(String studentMajor) {
this.studentMajor = studentMajor;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}

Sample run:


Enter Student First Name :
David
Enter student major :
CIS
Enter student id :
12sk5
Incorrect id format. Integers only!
Enter student id :
11205
Using foreach loop
--------------------
Student First Name : David
Student Major : CIS
Student ID : 11205
Using Iterator
--------------------
Student First Name : David
Student Major : CIS
Student ID : 11205

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote