简体   繁体   中英

Creating Methods and Classes (Java)

I am trying to write a class called Student that is supposed to work with a StudentDriver. However, I am having a very hard time wrapping my head around the concept of methods and classes. I do not know how to receive and return data. Moreover, I don't even know if I am declaring my data correctly. Please help me. I would greatly appreciate it.

Also when I compiled the Student it says that it cannot find the symbol this.setGPA. How so? When in the driver it has .setGPA.

Thank you.

// This will be the "driver" class for the Student class created in
// MinilabWritingClasses (It looks very complicated because of all
// the comments, but it really just creates instances of Student and
// tells them to do things...)

public class StudentDriver
{
public static void main(String[ ] args)
{
    //create an instance of Student
    System.out.println("***** Creating a Student, calling the default constructor");
    Student stud1 = new Student();

    //print it so we can see what the default values were for the class data
    //note that its toString() will be called automatically
    System.out.println("\n***** printing it - notice the default values (set by Java)");
    System.out.println(stud1);

    //create another instance of a Student, passing in initial values to its constructor
    System.out.println("\n***** Creating another Student, passing initial values to its constructor");
    Student msBoss = new Student("Bill Gates", 56, 'm', 3.2, true);

    //tell it to return its age
    System.out.println("\n***** telling it to return its age.");
    int theAge = msBoss.getAge();
    System.out.println("Its age is: " + theAge);

    //print it - note that its toString() will be called automatically;
    System.out.println("\n***** printing it - see if values are correct");
    System.out.println(msBoss);

    //ask it if it is on probation
    System.out.println("\n***** asking it if it is on probation (check answer)");
    System.out.println("onProbation() returned: " + msBoss.onProbation());

    //tell it to change its gpa to 1.3
    System.out.println("\n***** telling it to change its gpa to 1.3");
    msBoss.setGPA(1.3);

    //print it now
    System.out.println("\n***** printing it - see if the values are correct");
    System.out.println(msBoss);

    //ask it if it is on probation now
    System.out.println("\n***** asking it if it is on probation (check answer)");
    boolean boolAnswer = msBoss.onProbation();
    System.out.println("onProbation() returned: " + boolAnswer);

    //tell it to complain
    System.out.println("\n***** telling it to complain");
    System.out.println("complain() returned: " + msBoss.complain());

    //tell it to change its onScholarship field to false
    System.out.println("\n***** telling it to change its onScholarship field to false");
    msBoss.setOnScholarship(false);

    //print it now
    System.out.println("\n***** printing it - see if the values are correct");
    System.out.println(msBoss);

    //ask it if it is on probation now
    System.out.println("\n***** asking it if it is on probation (check answer)");
    boolAnswer = msBoss.onProbation();
    System.out.println("onProbation() returned: " + boolAnswer);

    //create a different student, tell it to have some different values, and tell it to print itself
    System.out.println("\n***** creating a different Student, passing initial values to its constructor");
    Student stud2;
    stud2 = new Student("Hillary Clinton", 64, 'f', 2.0, true);         //notice-can define variable and create it in 2 steps

    //print it
    System.out.println("\n***** printing it - see if the values are correct");
    System.out.println(stud2);

    //ask it if it is on probation now
    System.out.println("\n***** asking it if it is on probation (check answer)");
    boolAnswer = stud2.onProbation();
    System.out.println("onProbation() returned: " + boolAnswer);
 }
}

Here is the class that I am writing.

public class Student
{
private String name;
private int age;
private char gender;
private double gpa;
private boolean onScholarship;

public Student()
{
}

public Student(String newName, int newAge, char newGender, double newGPA, boolean newScholarship)
{
    this.name = newName;
    this.age = newAge;
    this.gender = newGender;
    this.gpa = newGPA;
    this.onScholarship = newScholarship;
}

public int getAge(int newAge)
{
    return age;
}

public double setGPA (double newGPA)
{
    this.setGPA = newGPA;
}

public boolean setOnScholarship (boolean newScholarship)
{
    this.setOnScholarship = newScholarship;
}

public String toString()
{
    return this.name + "\t" + this.age + "\t" + this.gender + "\t" + this.setGPA + "\t" + this.setOnScholarship;
}

public boolean onProbation()
{
    if (onScholarship==true && gpa < 2.0)
        return true;
    else
        return false;
  }

}

Try to change this line:

this.setGPA = newGPA;

to this:

this.gpa = newGPA;

setGPA symbol not found is because there is no setGPA field (it is a method). You are trying to change the gpa field.

You also don't need the empty public Student() {} constructor -- this is automatically created by Java.

Also, as @Sam pointed out, since setOnScholarship() doesn't return anything, you can change the return type boolean to void . This is because there is no return statement, and this return ing of nothing is a void type.

Overall, though, you have a good understanding on creating instances of another class (ie, creating Student s).


On request (although it doesn't have much to do with your code), here is a brief summary on static .

The static keyword is used with methods and fields that are not used with an instance of that class, but the class itself.

For example, in your case, pretty much all of the Student fields and methods are non-static, because they are properties of Student objects:

this.gpa;
this.setGpa();

On the other hand, if it were not changing a variable related to a single object, for example the total number of students, you could create a static field in Student :

public class Student {

    // non-static fields for each instance
    public double gpa;

    // static field for the class
    public static numStudents;

    public Student() {
        // create student by setting object (non-static) fields, for example...
        this.gpa = 3.2;

        // edit static variable
        numStudents++; // notice how there is no `this` keyword - this changes a static variable
    }

}

... and from StudentDriver , numStudents can be retreived with:

Student.numStudents;  // notice how this is like `this.[property]`, but with the class name (Student) - it is an instance of the class

I hope this helps! OOP programming is a difficult topic that can't be explained so simply.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM