Create a class named Student that includes String data fields titled firstName a
ID: 3560028 • Letter: C
Question
Create a class named Student that includes String data fields titled firstName and lastName, and an integer field titled studentID. Create a constructor that requires two String parameters and one integer parameter to populate the firstName, lastName, and studentID fields. Create a LinkedList of 20 Student objects each having different first names, last names, and student IDs. Print your LinkedList, first in the order your items were placed onto the LinkedList and then in reverse order. Save your files as Student.java and ClassRoster.java.
Please save in two files. Will chose the first responder as best choice so long as the program is functional and looks ok. Thanks.
Explanation / Answer
Here is what I solved before, please modify the figures as per your question. Please let me know if you have further questions. If this helps then kindly rate 5-stars
Create a class named Student that includes String data fields for firstName and lastName. Create a constructor that requires two String parameters to populate the firstName and lastName fields. Create a LinkedList of 20 Student objects each having different first and last names. Print your LinkedList, first in the order your items were placed onto the LinkedList and then in reverse order. Save your files as Student.java and ClassRoster.java.
solution:
Class : Student.java
==================
import java.util.*;
class Student {
public String firstName; // First name
public String lastName; // Last name
//=================== constructor
public Student(String fn, String ln) {
firstName = fn;
lastName = ln;
}
}
Class : ClassRooster.java
==================
import java.util.*;
import mypackage.Student;
public class ClassRoster {
public static void main(String[] args) {
LinkedList <Student>list = new LinkedList<Student>();
for(int i=1; i<=20; i++) {
Student student = new Student("name"+i,"last"+i);
list.add(student);
}
int size = list.size();
System.out.println( "Linked list data: ");
System.out.println( "=============================== ");
//Create a iterator
Iterator iterator = list.iterator();
int i=1;
while (iterator.hasNext()){
Student std = (Student) iterator.next();
System.out.println("Student " + i );
System.out.println("First Name : " + std.firstName );
System.out.println("Last Name : " + std.lastName );
i++;
}
System.out.print( " Linked list data in reverse Order: ");
System.out.println( "==================================== ");
for(int index=size-1; index>-1; index--) {
Student std = (Student) list.get(index);
System.out.println("Student " + index );
System.out.println("First Name : " + std.firstName );
System.out.println("Last Name : " + std.lastName );
}
}
}
Please rate me. Thanks a lot.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.