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

I have created a static ArrayList called classList. I am using this keyword  to represent the current state of object in the constructor of Student Class. Please find attached code ,screenshot and output.

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

import java.util.ArrayList;

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());
}
}

class Student // This should not be public as java allows only one public class in a java file
{
private String name;
private int grade;
//Implement classList here:
static ArrayList<Student> classList = new ArrayList<Student>(); // This is the arrayList declaration
  
public Student(String name, int grade)
{
this.name = name;
this.grade = grade;
classList.add(this); // Adding current object to the classList
}
  
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;
}
}

1 /* package whatever; // dont place package name! */ import java.util.*; import java.lang.*; import java.io.*; au 7 import13 //You dont need to change anything here, but feel free to add more Students! 14 Student alan = new Student (Alan, 11);

OUTPUT:

37 return this.name; 381 Input Goes Here.. Copy > Run > Run+URL (Generates URL as well) Time(sec): 0.08 Memory(MB): 44.5938 O

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
  • 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;    }   ...

  • Create an abstract Student class for Parker University. The class contains fields for student ID number,...

    Create an abstract Student class for Parker University. The class contains fields for student ID number, last name, and annual tuition. Include a constructor that requires parameters for the ID number and name. Include get and set methods for each field; the setTuition() method is abstract. Create three Student subclasses named UndergraduateStudent, GraduateStudent, and StudentAtLarge, each with a unique setTuition() method. Tuition for an UndergraduateStudent is $4,000 per semester, tuition for a GraduateStudent is $6,000 per semester, and tuition for...

  • Given a class called Student and a class called Course that contains an ArrayList of Student....

    Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called getDeansList() as described below. Refer to Student.java below to learn what methods are available. I will paste both of the simple .JAVA codes below COURSE.JAVA import java.util.*; import java.io.*; /****************************************************** * A list of students in a course *****************************************************/ public class Course{     /** collection of Students */     private ArrayList<Student> roster; /***************************************************** Constructor for objects of class Course *****************************************************/...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • Below are the Car class and Dealership class that I created In the previous lab import...

    Below are the Car class and Dealership class that I created In the previous lab import java.util.ArrayList; class Car { private String make; private String model; private int year; private double transmission; private int seats; private int maxSpeed; private int wheels; private String type; public Car() { } public Car(String make, String model, int year, double transmission, int seats, int maxSpeed, int wheels, String type) { this.make = make; this.model = model; this.year = year; this.transmission = transmission; this.seats =...

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
Active Questions
ADVERTISEMENT