Java Write statements from the client perspective to accomplish the following ta
ID: 3883051 • Letter: J
Question
Java
Write statements from the client perspective to accomplish the following tasks.
Display the last names of all students in a ListInterface<Student>object called studentList.
Interchange the first and last students in the list.
Do not interchange the first and last names of any student- interchange the first and last students on the list.
Notes:Refer to the Student and Name classes provided in the textbook to complete the task.
You can download both from the Textbook Source Code- they are in the Appendix C (folder C.16) and D folders (folder D.2).
Do not make any assumptions about the length of the list! This means your code should work with empty lists, one-element lists, or larger.
For full credit, do not use the toArray() method.
Explanation / Answer
public static void listModifier(List<Student> studentList) {
//print last name of all the student
for (int i = 0; i < studentList.size(); i++)
System.out.println(studentList.get(i).getName().getLastName());
//Champ you have not provided the Student class, so I guessed the structure.
//if you find this wrong, please send me the Student and Name classes
//swap last and first student
studentList.add(1, studentList.remove(studentList.size() - 1));//thus statement is little complex
//studentList.remove(studentList.size() - 1) <- removes the last item from list
//and the add statement add the item at position 1, that is the 2nd position
//this will become the first element once the first element is removed
studentList.add(studentList.remove(0)); //this is much simpler.
//it removes the item at position 0, i.e. the first item
//then it appends the item i.e. adds the item to the end of the list
}
Champ please go through the comments throughly. If you find any difficulty, please comment below. I shall help you solve the ploblem if any.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.