public class CsCohort {
private int year;
private final int enrolled;
private double retainedPercent;
public CsCohort(int year, int inEnroll, double inPercent) {
this.year = year;
this.enrolled = inEnroll;
this.retainedPercent = inPercent;
}
public int getYear() {
return this.year;
}
public void setYear(int year) {
this.year = year;
}
public int getEnrolled() {
return this.enrolled;
}
public double getPercent() {
return this.retainedPercent;
}
public void setRetainedPercent(double newPercent) {
if (newPercent >= 0 && newPercent <= 1) {
this.retainedPercent = newPercent;
} else {
// newPercent is not a valid value; print an error message
System.out.println("Expected a value between 0 and 1");
}
}
public int retained() {
return (int) (this.retainedPercent * this.enrolled);
}
}
This is a setter method for the retainedPercent instance variable.
We use this setter method to perform some validation on the newPercent parameter, before we assign its value to the retainedPercent instance variable.
This is a setter method for the retainedPercent instance variable.
We use this setter method to perform some validation on the newPercent parameter, before we assign its value to the retainedPercent instance variable.
final indicates that this variable's value cannot be changed after it is initialized.
final indicates that this variable's value cannot be changed after it is initialized.
This is the constructor for the CsCohort class.
The constructor's job is to create a new CsCohort object.
Here, the constructor takes in 3 parameters — the values of these parameters are given to the instance variables.
This is the constructor for the CsCohort class.
The constructor's job is to create a new CsCohort object.
Here, the constructor takes in 3 parameters — the values of these parameters are given to the instance variables.
public indicates that this class is visible to and can be used by other classes.
public indicates that this class is visible to and can be used by other classes.
Since this method's job is only to change the value of the year instance variable, it does not return anything. Hence its return type is declared as void.
Since this method's job is only to change the value of the year instance variable, it does not return anything. Hence its return type is declared as void.
These are the instance variables of this class.
Each CsCohort object will have its own year, enrolled, and
retainedPercent.
These are the instance variables of this class.
Each CsCohort object will have its own year, enrolled, and
retainedPercent.
We only provide a getter method for the enrolled instance variable, but not a setter method.
This means that no other class that modify this CsCohort's enrolled value, but they can still read it.
We only provide a getter method for the enrolled instance variable, but not a setter method.
This means that no other class that modify this CsCohort's enrolled value, but they can still read it.
print(String) and then
println().print(String) and then
println().x - The String to be printed.This is a setter method or mutator method.
It sets the value of the year instance variable.
This is a setter method or mutator method.
It sets the value of the year instance variable.
Unlike the getter method above, this setter method takes in a parameter, which is the new value to be given to the year instance variable. The type of that parameter needs to match the type of the instance variable.
Unlike the getter method above, this setter method takes in a parameter, which is the new value to be given to the year instance variable. The type of that parameter needs to match the type of the instance variable.
Console.charset() if the Console exists,
stdout.encoding otherwise.
Console.charset() if the Console exists,
stdout.encoding otherwise.
For simple stand-alone Java applications, a typical way to write a line of output data is:
System.out.println(data)
See the println methods in class PrintStream.
This is the declaration of a public class called CsCohort.
This is the declaration of a public class called CsCohort.
public indicates that the method is visible to other classes. That is, when other classes have a CsCohort object, they can call this method on that object.
public indicates that the method is visible to other classes. That is, when other classes have a CsCohort object, they can call this method on that object.
int is the return type of this method. It indicates that this method returns an integer value. The return type here is dictated by the data type of the year instance variable.
int is the return type of this method. It indicates that this method returns an integer value. The return type here is dictated by the data type of the year instance variable.
These are the behaviours that each CsCohort can perform, using
its instance variables. We refer to these behaviours as instance methods.
These are the behaviours that each CsCohort can perform, using
its instance variables. We refer to these behaviours as instance methods.
This is a getter method or accessor method.
It returns the value of the year instance variable.
This is a getter method or accessor method.
It returns the value of the year instance variable.
this is the keyword used for the object to refer to itself.
So, in this example, this.year refers to this particular CsCohort object's year instance variable.
Notice that some of constructor parameters have the same name as the instance variables.
In this case, this is necessary to help the compiler distinguish between the two.
For variables where the instance variable and local variables are not named the same, you don't need to use this, but I still recommend it because it makes your code more clear.
this is the keyword used for the object to refer to itself.
So, in this example, this.year refers to this particular CsCohort object's year instance variable.
Notice that some of constructor parameters have the same name as the instance variables.
In this case, this is necessary to help the compiler distinguish between the two.
For variables where the instance variable and local variables are not named the same, you don't need to use this, but I still recommend it because it makes your code more clear.