0

Before you read on this is for my homework and will be oddly specific.

I am very new to object-oriented programming and currently, it is very hard for me to grasp the new concepts. My assignment is to get information from object classes with two files. One file is for encapsulating the functions while the other file is to implement the functions and execute the code (the public static void file). So for the part that I am stuck on, I am unsure on how to do this. My objective of the output is to have this get the student's quiz marks. What I have tried is to use an array to collect the quiz scores of the students and find the total average of all of them. The problem that I am having with the implementation of gathering the quiz marks is that the array always gives me error and red lines and saying that there is not existing class even though I have functions in the main file.

This is the code for the encapsulated functions:

public class Student {
    // instance fields
    private String firsName;
    private String surName;
    private long studentNumber;

}

//constuctors

    public Student(String firstName, String surName, long studentNumber){

    }

    //accessors and mutator methods

//set student's name and surname. Changing student's name does not affect the students's loginId
    public void setName(String firstName, String surName){

}

//returns name and surname separated by comma (name, surname)
public String getName(){
    return firsName + ", " + surName;
}

public long getStudentNumber(){
    return studentNumber;
}

public String getLoginId(){
    return firsName.charAt(0) + surName.substring(0,4) + studentNumber % 100;
}

public String getInfo(){
    return firsName + ", " + surName + "( " + getLoginId() + ", " + getStudentNumber() + ")";
}

public void setAddress(String number, String street, String city, String province, String postalCode){

}

public String getAddress(){
    String address = getAddress();
    return address;
}

//add a quiz score to the student
public void addQuiz(double quiz){
    double [] quizMarks;
    quizMarks = new double[]{ };
}

public double getQuizAverage(){

}


public String toString(){

}

This is the code for the test cases AKA the main file:

class StudentTester{
    public static void main(String[] args){
        // test Student constructor
        Student student = new Student("Marry","jones",10000001);
        System.out.println(student); 

        // test Student getName, getStudentNumber(), getLoginId()
        System.out.println(student.getName());
        System.out.println(student.getStudentNumber());
        System.out.println(student.getLoginId());

        // test Student getInfo()
        System.out.println(student.getInfo());

        // test Student addQuiz and getAverage
        student.addQuiz(6.0);
        student.addQuiz(8.5);
        student.addQuiz(9.8);
        System.out.println(student.getQuizAverage());

        // add your test cases:
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41

2 Answers2

1

Since this is a homework exercise for a beginner, I will keep the code as simple as possible. Note that there are many other ways to write code that performs the requirements of the exercise.

You need to make variable quizMarks a member of class Student so that the variable exists for the lifetime of the Student object that you create (by calling the constructor of class Student in method main of class StudentTester). When you declare a variable inside a method, the variable only exists while that method executes. As soon as the method terminates, the variable no longer exists. This is referred to as the scope of the variable.

The problem with an array is that it has a fixed number of elements. So if you want to add a quiz mark to array quizMarks, you may need to increase the number of elements in the array. Alternatively, you could use an ArrayList, but I'm guessing that maybe you haven't learned about that class yet and so you are probably not allowed to use it in your code.

Here is my version of class Student. Note that I added method main to class Student for performing your test, rather than a separate StudentTester class, just to minimize the amount of code in this answer.

public class Student {
    private String firsName;
    private String surName;
    private long studentNumber;
    private double[] quizMarks;

    public Student(String firsName, String surName, long studentNumber) {
        this.firsName = firsName;
        this.surName = surName;
        this.studentNumber = studentNumber;
        quizMarks = new double[0];
    }

    public String getFirsName() {
        return firsName;
    }

    public void setFirsName(String firsName) {
        this.firsName = firsName;
    }

    public String getSurName() {
        return surName;
    }

    public void setSurName(String surName) {
        this.surName = surName;
    }

    public long getStudentNumber() {
        return studentNumber;
    }

    public void setStudentNumber(long studentNumber) {
        this.studentNumber = studentNumber;
    }

    public String toString() {
        return String.format("%d %s %s", studentNumber, firsName, surName);
    }

    public void addQuiz(double quiz) {
        double[] temp = new double[quizMarks.length + 1];
        for (int i = 0; i < quizMarks.length; i++) {
            temp[i] = quizMarks[i];
        }
        temp[quizMarks.length] = quiz;
        quizMarks = temp;
    }

    public double getQuizAverage() {
        double average = 0;
        if (quizMarks.length > 0) {
            double total = 0;
            for (int i = 0; i < quizMarks.length; i++) {
                total += quizMarks[i];
            }
            average = total / quizMarks.length;
        }
        return average;
    }

    public static void main(String[] args) {
        Student student = new Student("Marry","jones",10000001);
        System.out.println(student);
        student.addQuiz(6.0);
        student.addQuiz(8.5);
        student.addQuiz(9.8);
        System.out.println(student.getQuizAverage());
    }
}
  • In the constructor of class Student I initially set the number of elements in quizMarks to 0 (zero).
  • In method addQuiz, I create a new array whose size is one larger than that of quizMarks. Then I copy all the values from quizMarks to the new array and finally I add the new quiz mark to the array, after which I set quizMarks equal to the new array. Now quizMarks has one more element than it had before the method addQuiz was called.
  • Calculating the average quiz mark is simply performing some arithmetic, namely totalling all the marks in quizMarks and then dividing by the number of elements in quizMarks.
  • In method getQuizAverage I check that quizMarks contains at least one element because dividing a double by zero returns NaN (Not a Number).

This is the output I get when I run the above code.

10000001 Marry jones
8.1
Abra
  • 19,142
  • 7
  • 29
  • 41
0

I hope that this is what you are looking for.

Array studentArray = new Array<Student>();

Student student = new Student("Marry","jones",10000001);

studentArray.push(student);
  • Pardon my ignorance but I can't find method `push` in the [API documentation](https://docs.oracle.com/en/java/javase/16/docs/api/index-files/index-16.html) of the JDK. Is class `Array` not part of the JDK? – Abra May 23 '21 at 09:41
  • This is not the correct way to create a new Array in Java. You would use `Student[] studentArray = new Student[] { student }` – twobiers May 23 '21 at 11:27
  • Yes you are right I mixed java and js. Thanks for the correction. – Zain Ul Abedin May 25 '21 at 05:45