ERROR actual and formal argument lists differ in length - java

constructor Student in class javaapplication 14.Student cannot be
applied to given types; required:
java.lang.String,int,java.lang.String,java.lang.String,double found:
java.lang.String,double reason: actual and formal argument lists
differ in length
This is the line that gives me error
super(idNum, gpa);
here is my code:
package javaapplication14;
public class JavaApplication14
{
public static void main(String[] args)
{
Person bob = new Person("Coach Bob", 27, "M");
System.out.println(bob);
Student lynne = new Student("Lynne Brooke", 16, "F", "HS95129", 3.5);
System.out.println(lynne);
Teacher mrJava = new Teacher("Duke Java",34,"M","Computer Science",50000);
System.out.println(mrJava);
}
}
class Person
{
protected String myName ;
protected int myAge;
protected String myGender;
public Person(String name, int age, String gender)
{
myName = name;
myAge = age ;
myGender = gender;
}
#Override
public String toString()
{
return myName + ", age: " + myAge + ", gender: " +myGender;
}
//These are the set methods
void setName(String name)
{
myName = name;
}
void setAge(int age)
{
myAge = age ;
}
void setGender(String gender)
{
myGender = gender;
}
//These are the get methods
String myName()
{
return myName;
}
int myAge()
{
return myAge;
}
String myGender()
{
return myGender;
}
}
class Student extends Person
{
protected String myIdNum;
protected double myGPA;
public Student(String name, int age, String gender,String idNum, double gpa)
{
super(name, age, gender);
myIdNum = idNum;
myGPA = gpa;
}
//These are the set methods
void setmyIdNum(String idNum)
{
myIdNum = idNum;
}
void setmyGPA(double gpa)
{
myGPA = gpa;
}
#Override
public String toString()
{
return super.toString() + ", student id: " + myIdNum + ", gpa: " + myGPA;
}
public String myIdNum()
{
return myIdNum;
}
double myGPA()
{
return myGPA;
}
}
class Teacher extends Person
{
public double mySalary;
public String mysubjectName;
public Teacher( String name, int age, String gender, String
subjectName,double salary )
{
super(name, age, gender);
mysubjectName = subjectName;
mySalary = salary;
}
//These are the setter methods
void setsubjectname(String subjectName)
{
mysubjectName = subjectName;
}
void setsalary(double Salary)
{
mySalary = Salary;
}
#Override
public String toString()
{
return super.toString() + ", subject name: " + mysubjectName + ", Salary: "
+ mySalary;
}
//These are the getter methods
public String getsubjectname(String subjectName)
{
return subjectName;
}
double getsalary(double Salary)
{
return Salary ;
}
class CollegeStudent extends Student
{
public String myMajor;
public int myYear;
public CollegeStudent(String name, int age,String gender,
String idNum, double gpa, int year, String major )
{
super(idNum, gpa);
myMajor = major;
myYear = year;
}
void setmajor(String Major)
{
myMajor = Major;
}
void setyear(int Year)
{
myYear = Year;
}
#Override
public String toString()
{
return super.toString() + ", major: " + myMajor + ", Year: " + myYear;
}
//These are the getter methods
public String getmajor(String myMajor)
{
return myMajor;
}
public int getyear(int myYear)
{
return myYear ;
}
}
}

There is no such constructor with two parameters in Student class

The class Student and also the class Person has no constuctor with the signature (String, double).
So you have to create this constructor in one of the superclasses or you have to call an other constructor.

Your super class Student has following constructor with 5 parameter
public Student(String name, int age, String gender,String idNum, double gpa)
But, at CollegeStudent constructor, super(idNum, gpa); expect a constructor with two parameter Student(String idNum, double gpa), which is missing in superclass. So, define a constructor at Student class like:
class Student extends Person {
public Student(String idNum, double gpa){
}
}

Related

File with multiple data types, into arrays, then sort one array while keeping the records together

My problem is that I will be giving a file with student information: name, age, and GPA. I then have to turn each data element into a separate array. I then have to sort GPA into descending order, while keeping name and age with the corresponding GPA, and then print it out with "Name Age GPA" heading.
Check below code to sort data based on GPA. However you will have to include relevant code for file reading.
Student.java (POJO)
public class Student {
String name;
int age;
double GPA;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getGPA() {
return GPA;
}
public void setGPA(double gPA) {
GPA = gPA;
}
public Student(String name, int age, double gPA) {
super();
this.name = name;
this.age = age;
GPA = gPA;
}
#Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", GPA=" + GPA + "]";
}
}
StudentSorter.java
import java.util.Comparator;
public class StudentSorter implements Comparator<Student> {
#Override
public int compare(Student o1, Student o2) {
if(o1.getGPA() < o2.getGPA()) return 1;
if(o1.getGPA() > o2.getGPA()) return -1;
else return 0;
}
}
Tester.java
public class Tester {
public static void main(String[] args) {
Student s1 = new Student("A",14,7.9);
Student s2 = new Student("B",17,8.2);
Student s3 = new Student("C",20,7.0);
Student s4 = new Student("D",15,6.9);
Student s5 = new Student("E",14,9.1);
List<Student> list = new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
StudentSorter ss = new StudentSorter();
Collections.sort(list, ss);
System.out.println(list.toString());
}
}

How to add multiple students to student class?

Im trying to create a student class, a course class and the main class. I am trying to add students to the course, and when students are added to the class the number of students in the course should increase, when the the code is run it should print the course details followed by the students in the course.
I have got the following code:
Main class:
public class JavaLecture3 {
public static final int DEBUG = 0;
public static void main(String [] args){
//Student student = new Student(); // Calling default constructor here.
Course course = new Course();
student = new Student(21, "Joe", "CSE", "07447832342");
course = new Course("CSE", "Tom", 5);
System.out.println("Course Information: ");
System.out.println("-------------------");
System.out.println(course);
System.out.println();
System.out.println("Student contains: "); // calls student.toString());
System.out.println("-------------------");
System.out.println(student);
}
}
Course class:
public class Course {
ArrayList<Student> studentList;
private String courseName;
private String teacher;
private int noOfStudents;
//Getters
public String getCourseName(){
return this.courseName;
}
public int getNoOfStudents(){
return this.noOfStudents;
}
public String getTeacher(){
return this.teacher;
}
//Setters
public void setCourseName(String courseName){
this.courseName = courseName;
}
public void setNoOfStudents(int noOfStudents){
this.noOfStudents = noOfStudents;
}
public void setTeacher(String teacher){
this.teacher = teacher;
}
/**
* Default constructor. Populates course name, number of students with defaults
*
*/
public Course(){
this.noOfStudents = 0;
this.courseName = "Not Set";
this.teacher = "Not Set";
studentList = new ArrayList<Student>();
}
/**
* Constructor with parameters
* #param noOfStudents integer
* #param courseName String with the Course name
* #param teacher String with the teacher
*/
public Course(String courseName, String teacher, int noOfStudents){
this.courseName = courseName;
this.teacher = teacher;
noOfStudents = noOfStudents;
studentList = new ArrayList<Student>();
}
public static void addStudent(Student newStudent){
if(studentList.size()==noOfStudents){
System.out.println("The class is full, you cannot enrol.");
}
else {
studentList.add(newStudent);
}
}
public String toString() {
return "Course Name: " + this.courseName + " Teacher: " + this.teacher
+ " Number of Students: " + this.noOfStudents;
}
}
Student class:
public class Student {
private String name;
private int age;
public String gender = "na";
private String course;
private String phoneNo;
public static int instances = 0;
// Getters
public int getAge(){
return this.age;
}
public String getName(){
return this.name;
}
public String getCourse(){
return this.course;
}
public String getPhoneNo(){
return this.phoneNo;
}
// Setters
public void setAge(int age){
this.age = age;
}
public void setName(String name){
if (JavaLecture3.DEBUG > 3) System.out.println("In Student.setName. Name = "+ name);
this.name = name;
}
public void setCourse(String course){
this.course = course;
}
public void setPhoneNo(String phoneNo){
this.phoneNo = phoneNo;
}
/**
* Default constructor. Populates name,age,gender,course and phone Number
* with defaults
*/
public Student(){
instances++;
this.age = 18;
this.name = "Not Set";
this.gender = "Not Set";
this.course = "Not Set";
this.phoneNo = "Not Set";
}
/**
* Constructor with parameters
* #param age integer
* #param name String with the name
* #param course String with course name
* #param phoneNo String with phone number
*/
public Student(int age, String name, String course, String phoneNo){
this.age = age;
this.name = name;
this.course = course;
this.phoneNo = phoneNo;
}
/**
* Gender constructor
* #param gender
*/
public Student(String gender){
this(); // Must be the first line!
this.gender = gender;
}
protected void finalize() throws Throwable{
//do finalization here
instances--;
super.finalize(); //not necessary if extending Object.
}
public String toString (){
return "Name: " + this.name + " Age: " + this.age + " Gender: "
+ this.gender + " Course: " + this.course + " Phone number: "
+ this.phoneNo;
}
}
Reference:
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html?is-external=true
Course.getNoOfStudents should just return studentList.size(). No need to maintain a separate variable.
To print a list of students (from Course):
for(int i=0;i<studentList.size();i++) System.out.println(studentList.get(i));
Since you initialized the array list in the Course class, you should add the students to it. You should create a method in Course that adds an object to the array list such as:
public void addStudent(Student s){
studentList.add(s);
noOfStudents++;
}
To add multiple students:
public void addStudents(Student[] students){
for(int i = 0; i < students.length; i++){
studentList.add(students[i]);
}
}
You're almost there, just use the .add method made for you in the array list.
public class JavaLecture3 {
public static final int DEBUG = 0;
public static void main(String [] args){
Student student = new Student(); // Calling default constructor here.
Course course = new Course();
student = new Student(21, "Joe", "CSE", "07447832342");
course = new Course("CSE", "Tom", 5);
course.addStudent(student);
System.out.println("Course Information: ");
System.out.println("-------------------");
System.out.println(course);
System.out.println();
System.out.println("Student contains: "); // calls student.toString());
System.out.println("-------------------");
System.out.println(student);
}
}
public class Course {
List<Student> studentList;
private String courseName;
private String teacher;
private int noOfStudents;
// Getters
public String getCourseName() {
return this.courseName;
}
public int getNoOfStudents() {
return this.noOfStudents;
}
public String getTeacher() {
return this.teacher;
}
// Setters
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public void setNoOfStudents(int noOfStudents) {
this.noOfStudents = noOfStudents;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
/**
* Default constructor. Populates course name, number of students with
* defaults
*
*/
public Course() {
this.noOfStudents = 0;
this.courseName = "Not Set";
this.teacher = "Not Set";
studentList = new ArrayList<Student>();
}
/**
* Constructor with parameters
*
* #param noOfStudents
* integer
* #param courseName
* String with the Course name
* #param teacher
* String with the teacher
*/
public Course(String courseName, String teacher, int noOfStudents) {
this.courseName = courseName;
this.teacher = teacher;
this.noOfStudents = noOfStudents;
studentList = new ArrayList<Student>();
}
public void addStudent(Student newStudent) {
if (studentList.size() == noOfStudents) {
System.out.println("The class is full, you cannot enrol.");
} else {
studentList.add(newStudent);
}
}
#Override
public String toString() {
return "Course Name: " + this.courseName + " Teacher: " + this.teacher
+ " Number of Students: " + studentList.size();
}
}
public class Student {
private String name;
private int age;
public String gender = "na";
private String course;
private String phoneNo;
public static int instances = 0;
// Getters
public int getAge() {
return this.age;
}
public String getName() {
return this.name;
}
public String getCourse() {
return this.course;
}
public String getPhoneNo() {
return this.phoneNo;
}
// Setters
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
if (JavaLecture3.DEBUG > 3)
System.out.println("In Student.setName. Name = " + name);
this.name = name;
}
public void setCourse(String course) {
this.course = course;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
/**
* Default constructor. Populates name,age,gender,course and phone Number
* with defaults
*/
public Student() {
instances++;
this.age = 18;
this.name = "Not Set";
this.gender = "Not Set";
this.course = "Not Set";
this.phoneNo = "Not Set";
}
/**
* Constructor with parameters
*
* #param age
* integer
* #param name
* String with the name
* #param course
* String with course name
* #param phoneNo
* String with phone number
*/
public Student(int age, String name, String course, String phoneNo) {
this.age = age;
this.name = name;
this.course = course;
this.phoneNo = phoneNo;
}
/**
* Gender constructor
*
* #param gender
*/
public Student(String gender) {
this(); // Must be the first line!
this.gender = gender;
}
protected void finalize() throws Throwable {
// do finalization here
instances--;
super.finalize(); // not necessary if extending Object.
}
public String toString() {
return "Name: " + this.name + " Age: " + this.age + " Gender: "
+ this.gender + " Course: " + this.course + " Phone number: "
+ this.phoneNo;
}
}
public class JavaLecture3 {
public static final int DEBUG = 0;
public static void main(String [] args){
//Create course object
Course course = new Course("CSE", "Tom", 5);
Scanner scanner = new Scanner(System.in);
String cmd = "Yes";
while(cmd.equals("Yes")){
Student student = new Student();
System.out.print("Enter a new student? ");
cmd = scanner.next();
if (cmd.equals("Yes")){
//Read student name
System.out.print("Enter a student name: ");
String name = scanner.next();
student.setName(name);
//Read student Age
System.out.print("Enter a student age: ");
int age = scanner.nextInt();
student.setAge(age);
//Read student Course
System.out.print("Enter a student course: ");
String stdent_course = scanner.next();
student.setCourse(stdent_course);
//register the student to the class
course.addStudent(student);
}
}
scanner.close();
System.out.println("Course Information: ");
System.out.println("-------------------");
System.out.println(course.toString());
System.out.println();
}
}
I reworked a bit your toString() method from Course class. This should print the list of students attending a class.
public String toString (){
String ret_value = "Name: " + this.name + " Age: " + this.age + " Gender: "
+ this.gender + " Course: " + this.course + " Phone number: "
+ this.phoneNo + " Students attending this course:";
for (Student student: studentList) {
ret_value = ret_value + " " + Student.getName();
}
return ret_value;
}

polymorphism (dynamic dispatching bidding)

/* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication3;
class Employee {
private String name;
private String address;
private int number;
public Employee(Employee emp) {
System.out.println("Constructing an Employee");
this.name = emp.name;
this.address = emp.address;
this.number = emp.number;
}
/*public Employee(String name, String address, int number) {
System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}*/
public void mailCheck() {
System.out.println("Mailing a check to " + this.name
+ " " + this.address);
}
public String toString() {
return name + " " + address + " " + number;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public void setAddress(String newAddress) {
address = newAddress;
}
public int getNumber() {
return number;
}
}
class Salary extends Employee {
private double salary; //Annual salary
public Salary(Salary obj) {
super(obj);
setSalary(obj.salary);
}
/*public Salary(String name, String address, int number, double Salary) {
super(name, address, number);
setSalary(Salary);
}*/
public void mailCheck() {
System.out.println("Within mailCheck of Salary class ");
System.out.println("Mailing check to " + getName()
+ " with salary " + salary);
}
public double getSalary() {
return salary;
}
public void setSalary(double newSalary) {
if (newSalary >= 0.0) {
salary = newSalary;
}
}
public double computePay() {
System.out.println("Computing salary pay for " + getName());
return salary / 52;
}
}
public class JavaApplication3 {
public static void main(String[] args) {
Salary s = new Salary("Mohd Mohtashim", "Ambehta,UP", 3, 2000.00); // error why
System.out.println("Call mailCheck using Salary reference --");
s.mailCheck();
}
}
If we removed the comments from the constructor, then it will be okay. Why?
Salary s = new Salary("Mohd Mohtashim", "Ambehta,UP", 3, 2000.00); you're calling a constructor that takes multiple parameters but it doesn't exist since you've commented it out .

Inheritance Lab

I'm working on a lab with couple super classes. When I used my compiled my TestingClass it highlighted the following as a syntax error:
CollegeStudent ima = new CollegeStudent("Ima Frosh", 18, "F", "UCB123",
The following is all of my code for this lab:
TESTING CLASS
public class TestingClass
{
public static void main(String[] args)
{
Person bob = new Person("Coach Bob", 27, "M");
System.out.println(bob);
Student lynne = new Student("Lynne Brooke", 16, "F", "HS95129", 3.5);
System.out.println(lynne);
Teacher mrJava = new Teacher("Duke Java", 34, "M", "Computer Science", 50000);
System.out.println(mrJava);
CollegeStudent ima = new CollegeStudent("Ima Frosh", 18, "F", "UCB123",
4.0, 1, "English");
System.out.println(ima);
}
}
PERSON CLASS
public class Person
{
private String myName ; // name of the person
private int myAge; // person's age
private String myGender; // "M" for male, "F" for female
// constructor
public Person(String name, int age, String gender)
{
myName = name;
myAge = age;
myGender = gender;
}
public String getName()
{
return myName;
}
public int getAge()
{
return myAge;
}
public String getGender()
{
return myGender;
}
public void setName(String name)
{
myName = name;
}
public void setAge(int age)
{
myAge = age;
}
public void setGender(String gender)
{
myGender = gender;
}
public String toString()
{
return myName + ", age: " + myAge + ", gender: " +
myGender;
}
}
TEACHER CLASS
public class Teacher extends Person
{
// instance variables - replace the example below with your own
private String mySubject;
private double myAnnualSalary;
/**
* Constructor for objects of class Teacher
*/
public Teacher(String name, int age, String gender, String Subject, double AnnualSalary)
{
// initialise instance variables
super(name, age, gender);
mySubject = Subject;
myAnnualSalary = AnnualSalary;
}
/**
* An example of a method - replace this comment with your own
*
* #param y a sample parameter for a method
* #return the sum of x and y
*/
public String getSubject()
{
return mySubject;
}
public double getAnnualSalary()
{
return myAnnualSalary;
}
public void setSubject(String Subject)
{
mySubject = Subject;
}
public void setAnnualSalary(double AnnualSalary)
{
myAnnualSalary = AnnualSalary;
}
public String toString()
{
return super.toString() + "Subject:" + mySubject + "Annual Salary:" + myAnnualSalary;
}
}
STUDENT CLASS
public class Student extends Person
{
private String myIdNum; // Student Id Number
private double myGPA; // grade point average
// constructor
public Student(String name, int age, String gender,
String idNum, double gpa)
{
// use the super class' constructor
super(name, age, gender);
// initialize what's new to Student
myIdNum = idNum;
myGPA = gpa;
}
public String getIdNum()
{
return myIdNum;
}
public double getGPA()
{
return myGPA;
}
public void setIdNum(String idNum)
{
myIdNum = idNum;
}
public void setGPA(double gpa)
{
myGPA = gpa;
}
// overrides the toString method in the parent class
public String toString()
{
return super.toString() + ", student id: " + myIdNum + ", gpa: " + myGPA;
}
}
COLLEGE STUDENT CLASS
public class CollegeStudent extends Student
{
// instance variables - replace the example below with your own
private int myYear;
private String myMajor;
/**
* Constructor for objects of class dasf
*/
public CollegeStudent(String idNum, double gpa, String name, int age, String gender, int Year, String Major)
{
// initialise instance variables
super(name, age, gender, idNum, gpa);
myMajor = Major;
myYear = Year;
}
/**
* An example of a method - replace this comment with your own
*
* #param y a sample parameter for a method
* #return the sum of x and y
*/
public String getMajor()
{
return myMajor;
}
public int getYear()
{
return myYear;
}
public void setMajor(String Major)
{
myMajor = Major;
}
public void setYear(int Year)
{
myYear = Year;
}
public String toString()
{
return super.toString() + "Year:" + myYear + "Major:" + myMajor;
}
}
// Your call:
CollegeStudent ima = new CollegeStudent("Ima Frosh", 18, "F", "UCB123",
4.0, 1, "English");
// The constructor
public CollegeStudent(String idNum, double gpa, String name,
int age, String gender, int Year, String Major)
As you can see the constructor takes a lot of arguments, and the types matter when doing this.
The types it wants are: String, double, String, int, String, int, String.
However, you are passing: String, int (ok), String, String, double, int, String.
I think you just have the arguments all jumbled, try this:
CollegeStudent ima = new CollegeStudent("UCB123", 4.0, "Ima Frosh", 18,
"F", 1, "English");
..aaand at the end put it like this:
return super.toString() + ", Year: " + myYear + ", Major: " + myMajor;

My toString() is returning only one object times the number of the total object

Can someone help solving this. I want to print all the object once please
public class Student {
private static String firstName;
private static String lastName;
private static int studentId;
private static String major;
private static double balance;
public Student (String fName, String lName,int id,String mjr,double blce) {
firstName = new String(fName);
lastName = new String(lName);
studentId = id;
major = new String(mjr);
balance = blce;
}
public String toString () {
return firstName + "\t" + lastName + "\t" + studentId + "\t" + major + "\t$" + balance;
}
public boolean equals (Object obj) {
if (obj instanceof Student) {
Student collegeStud = (Student) obj;
return (this.firstName.equals(collegeStud.firstName));
} else
return false;
}
public static String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public static String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public static int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public static String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public static double getBalance() {
return balance;
}
/*
* .commmm
*/
public void setBalance(double balance) {
this.balance = balance;
}
public static void main (String[] args) {
Student Mike = new Student ("Mike","Versace", 99, "CS",0.00);
Student John = new Student ("John","Sling" ,97, "Maths", 20.00);
Student Bob = new Student ("Bob","Tomson" ,57, "Physic",5.00);
System.out.println (Mike.toString() + "\n" + John.toString());
if (Mike.equals(John))
System.out.println ("Mike is John");
else
System.out.println ("Mike is NOT John");
}
}
import java.io.ObjectInputStream.GetField;
public class StudentList {
private int numberOfStudents=0;
private Student[] studentListArray;
//private int studentCount = 0;
StudentList () {
numberOfStudents=0;
studentListArray = new Student[100];
}
public void createStudent(String firstName, String lastName,int studentId, String major, double balance){
Student collegeStud = new Student(firstName, lastName, studentId, major, balance);
addStudent(collegeStud);
numberOfStudents++;
}
public void addStudent (Student collegeStud) {
studentListArray[numberOfStudents++]=new Student(collegeStud.getFirstName(), collegeStud.getLastName(),
collegeStud.getStudentId(), collegeStud.getMajor(),collegeStud.getBalance());
}
public String toString() {
String result = "";
for (int i=0; i<numberOfStudents; i++) {
result += studentListArray[i].toString() + "\n";
}
return result;
}
public Student[] getList() {
return studentListArray;
}
public int listSize() {
return numberOfStudents;
}
public Student searchForStudent (String firstName){
int index = 0;
while (index < numberOfStudents) {
if (studentListArray[index].equals(new Student(Student.getFirstName(),Student.getLastName(),Student.getStudentId(),Student.getMajor(),Student.getBalance()))) {
return studentListArray[index];
}
index++;
}
return null;
}
public static void main(String args[]) {
StudentList theList = new StudentList();
theList.addStudent (new Student ("John","Sling" ,97, "Maths", 20.00));
theList.addStudent (new Student ("Mike","Versace", 99, "CS",0.00));
theList.addStudent (new Student ("Bob","Tomson" ,57, "Physic",5.00));
//theList.createStudent(new Student(Student.getFirstName(),Student.getLastName(),Student.getStudentId(),Student.getMajor(),Student.getBalance()));
//theList.searchForStudent(new String());
System.out.println (theList.toString());
}
}
The problem is that you marked your fields as static. Remove it and the method will work as expected.
public class Student {
//non-static fields
private String firstName;
private String lastName;
private int studentId;
private String major;
private double balance;
//similar for getters, setters and toString method
}
Static members are shared amongst all objects in a class rather than being one per object. Hence each new object you create is overwriting the data of the previous one.
More info:
What does the 'static' keyword do in a class?

Categories