Question

A teacher wants to create a list of students in her class. Using the existing Student...

A teacher wants to create a list of students in her class.

Using the existing Student class in this exercise. Create a static ArrayList called classList that adds a student to the classList whenever a new Student is created.

In the constructor, you will have to add that Student to the ArrayList.

Coding below was given to edit and use

public class ClassListTester
{
public static void main(String[] args)
{
//You don't need to change anything here, but feel free to add more Students!
Student alan = new Student("Alan", 11);
Student kevin = new Student("Kevin", 10);
Student annie = new Student("Annie", 12);
System.out.println(Student.printClassList());
}
}

import java.util.ArrayList;
public class Student
{
private String name;
private int grade;
//Implement classList here:
  
public Student(String name, int grade)
{
this.name = name;
this.grade = grade;
  
}
  
public String getName()
{
return this.name;
}
  
/*Don't change the code in this method!
This method will print out all the Student names in the classList Array
*/
public static String printClassList()
{
String names = "";
for(Student name: classList)
{
names+= name.getName() + "\n";
}
return "Student Class List:\n" + names;
}
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1


ANSWER:-

CODE:-

import java.util.ArrayList;
class Student
{
   private String name;
   private int grade;
   private static ArrayList<Student> classList = new ArrayList<Student>();
  
   public Student(String name, int grade)
   {
       this.name = name;
       this.grade = grade;
       classList.add(this);
   }
  
   public String getName()
   {
       return this.name;
   }
  
   /*Don't change the code in this method!
   This method will print out all the Student names in the classList Array
   */
   public static String printClassList()
   {
       String names = "";
       for(Student name: classList)
       {
           names+= name.getName() + "\n";
       }
       return "Student Class List:\n" + names;
   }
}
public class ClassListTester
{
   public static void main(String[] args)
   {
       //You don't need to change anything here, but feel free to add more Students!
       Student alan = new Student("Alan", 11);
       Student kevin = new Student("Kevin", 10);
       Student annie = new Student("Annie", 12);
       Student smith = new Student("Smith", 11);
       Student kane = new Student("Kane", 10);
       Student virat = new Student("Virat", 12);
       Student abd = new Student("ABD", 12);
       Student root = new Student("Root", 12);

       System.out.println(Student.printClassList());
   }
}

NOTE:- If you need any modifications in the answer, please comment below. Please give positive rating. THUMBS UP.

                 THANK YOU!!!!

OUTPUT:-

Add a comment
Know the answer?
Add Answer to:
A teacher wants to create a list of students in her class. Using the existing Student...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A teacher wants to create a list of students in her class. Using the existing Student...

    A teacher wants to create a list of students in her class. Using the existing Student class in this exercise. Create a static ArrayList called classList that adds a student to the classList whenever a new Student is created. In the constructor, you will have to add that Student to the ArrayList. Coding below was given to edit and use public class ClassListTester { public static void main(String[] args) { //You don't need to change anything here, but feel free...

  • Java help: 1.1) Create a class TA  that extends class Student. 1.2) Add a variable to the...

    Java help: 1.1) Create a class TA  that extends class Student. 1.2) Add a variable to the TA class to represent the course the student is TA'ing. 1.3) Provide a constructor for the class so the code in the PeopleFactory will work as provided. 1.4) Provide a toString() method for the TA  class so that TA's will output as shown in the sample code. Provide a class Staff so that you can un-comment the lines of code in the PeopleFactory class that...

  • For the code below write a public static main() method in class Student that: - creates...

    For the code below write a public static main() method in class Student that: - creates an ArrayList<Student> object called students - adds 4 new Student objects to the students list, with some made up names and dates - sort the students list by name and display the sorted collection to System.out. use function getCompByName() - sort the students list by enrollment date and display the sorted collection to System.out. use function getCompByDate() import java.util.Comparator;    import java.util.Date;    public...

  • I need help in converting this into pseudo-code Student.java public class Student implements Comparable<Student>{ private String...

    I need help in converting this into pseudo-code Student.java public class Student implements Comparable<Student>{ private String name, major, status; private int rank;    public Student() { this.name = this.major = this.status = ""; this.rank = 0; }    public Student(String name, String major, String status) { this.name = name; this.major = major; this.status = status; } public String getName() { return name; } public String getMajor() { return major; } public String getStatus() { return status; }    public int...

  • public class Scheduler {    private List<Course> classes;    private List<Student> students;    public Scheduler() {...

    public class Scheduler {    private List<Course> classes;    private List<Student> students;    public Scheduler() {        classes = new ArrayList<Course>();        students = new ArrayList<Student>();    }       public void addCourse(Course course) {        this.classes.add(course);    }    public List<Course> getCourses() {        List<Course> newCList=new ArrayList<>();        for(int i=0;i<classes.size();i++){        newCList.add(classes.get(i));        }        return newCList;    }    public void addStudent(Student student) {        this.students.add(student);    }   ...

  • Hi, I am writing Java code and I am having a trouble working it out. The...

    Hi, I am writing Java code and I am having a trouble working it out. The instructions can be found below, and the code. Thanks. Instructions Here are the methods needed for CIS425_Student: Constructor: public CIS425_Student( String id, String name, int num_exams ) Create an int array exams[num_exams] which will hold all exam grades for a student Save num_exams for later error checking public boolean addGrade( int exam, int grade ) Save a grade in the exams[ ] array at...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Java Do 68a, 68b, 68c, 68d. Show code & output. public class Employee { private int...

    Java Do 68a, 68b, 68c, 68d. Show code & output. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

    java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...

  • This is the question: These are in Java format. Comments are required on these two Classes...

    This is the question: These are in Java format. Comments are required on these two Classes (Student.java and StudentDemo.java) all over the coding: Provide proper comments all over the codings. --------------------------------------------------------------------------------------------------------------- import java.io.*; import java.util.*; class Student {    private String name;    private double gradePointAverage;    public Student(String n , double a){    name = n;    gradePointAverage = a;    }    public String getName(){ return name;    }    public double getGradePointAverage(){ return gradePointAverage;    }   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT