Create a user-defined class called Student . Each Player object will have the fo
ID: 3855068 • Letter: C
Question
Create a user-defined class called Student. Each Player object will have the following attributes (instance variables): name, ID, and GPA. Use appropriate data types to define these instance variables and use recommended naming conventions for the variable names. Provide a constructor, implement the accessor and mutator methods for each of the instance variables, and include the toString( ) and equals( ) methods. Also include a static factory method that can be used to generate instances of the Student class. This method should create a student with a random name, ID, and GPA and return that student object.
Finally, create a Client Program BagClient with the main( ) method. Inside the main method do the following:
Create an object of ArrayBag called course1 to store information about 17 different students. Create an object of LinkedBag called course2 to store information about 14 different students.
Run a for loop to call the static factory method of the Student class to generate student objects to store in the bags.
Remove a random student from each course.
Add a new Student with some made up information to each team. You should read this in using the Scanner class.
Display the current count of students in each course.
Remove the Student that you just added earlier with made up information from each course using appropriate method.
Display the current count of students in each course.
Use a for loop (or implement the toString method for each of your Bag classes) to print the information of the Students in each course.
Explanation / Answer
Here you go champ. If you are facing any problm with the code, please provide me the other classes LinkedBag and ArrayBag
I tried my best to fit all the cases within this class.
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Scanner;
/**
*
* @author Sam
*/
public class Student {
String name;
long id;
float gpa;
public Student() {
name = null;
id = 0;
gpa = 0;
}
public Student(String name, long id, float gpa) {
this.name = name;
this.id = id;
this.gpa = gpa;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public float getGpa() {
return gpa;
}
public void setGpa(float gpa) {
this.gpa = gpa;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Student other = (Student) obj;
if (!Objects.equals(this.name, other.name)) {
return false;
}
if (this.id != other.id) {
return false;
}
if (Float.floatToIntBits(this.gpa) != Float.floatToIntBits(other.gpa)) {
return false;
}
return true;
}
@Override
public String toString() {
return "Student{" + "name=" + name + ", id=" + id + ", gpa=" + gpa + '}';
}
public static Student getRandStudent(int i) {
return new Student("AXS", 99999999%i , (i*99)%10);
}
}
class BagClient {
public static void main(String[] args) {
ArrayBag<Student> course1 = new ArrayBag<>();
LinkedBag<Student> course2 = new LinkedBag<>();
for (int i = 0; i < 17; i++) {
course1.add(Student.getRandStudent(i));
}
for (int i = 0; i < 14; i++) {
course2.add(Student.getRandStudent(i));
}
course1.remove(Student.getRandStudent(10));
course2.remove(Student.getRandStudent(5));
Student tmp = new Student();
Scanner sc = new Scanner(System.in);
System.out.println("Enter new Student name for couse 1");
String name1 = sc.nextLine();
System.out.println("Enter new Student id for couse 1");
long id1 = sc.nextLong();
System.out.println("Enter new Student gpa for couse 1");
float gpa1 = sc.nextFloat();
tmp.setName(name1);
tmp.setId(id1);
tmp.setGpa(gpa1);
course1.add(tmp);
tmp = new Student();
sc = new Scanner(System.in);
System.out.println("Enter new Student name for couse 2");
String name2 = sc.nextLine();
System.out.println("Enter new Student id for couse 2");
long id2 = sc.nextLong();
System.out.println("Enter new Student gpa for couse 2");
float gpa2 = sc.nextFloat();
tmp.setName(name2);
tmp.setId(id2);
tmp.setGpa(gpa2);
course2.add(tmp);
System.out.println("Course 1 contains " + course1.size() + " students" );
System.out.println("Course 2 contains " + course2.size() + " students" );
course1.remove(new Student(name1, id1, gpa1));
course2.remove(new Student(name2, id2, gpa2));
System.out.println("Course 1 contains " + course1.size() + " students" );
System.out.println("Course 2 contains " + course2.size() + " students" );
while(course1.size() > 0) {
tmp = course1.grab();
System.out.println(tmp);
course1.remove(tmp);
}
System.out.println("Couse 2 students:");
Iterator<Student> iterator = course2.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.