Browse By Unit
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
Now, it’s time to write most of the rest of our methods. But first, a quick reminder about pass-by-value from Unit 2. When we put a primitive variable in as a parameter, we are giving the method a copy of that value so if you change the value of that variable, it does not get carried over to outside that method.
Meanwhile, if you put a reference variable or object as a parameter, it passes a copy of the reference to the method. Changing the value of the variable has the same effect as a primitive variable, but changing information about the variable (such as using a getter method) will carry over outside the method as well. Normally we do not want to modify mutable objects that are passed in as parameters.
Now, we will write some of the methods for our two classes (others will require information from future topics). Take note of the Javadoc comments and also new instance variables that we have added in order for these methods to work. Any changes we have made to the class will be bolded as in previous topics.
/** Represents an assignment that a student will complete
*/
public class Assignment {
private boolean correctAnswer; // represents the answer to an assignment, either T/F
/** Makes a new assignment with one True/False question and sets the correct answer
*/
public Assignment(boolean answer) {
correctAnswer = answer;
}
/** Prints details about the assignment
*/
@Override
public String toString() {
return "This is an assignment with correct answer " + answer;
}
The typical format of a method header is <access modifier> <return type> <method name> (<parameters>)
.
Access modifiers for Java methods can be: public, private, or protected. One of these three can be followed by static, which we will discuss in the next topic. We will talk more about access modifiers in Topic 5.8.
Return type can be: void, String, boolean, int, double, float, char, etc.
Method name can be anything, but usually something descriptive that allows you to infer what the method does.
You can have any number of parameters or no parameters.
Here are some examples of method headers:
public static void main (String args[])
private String sayHello ()
protected static int addNums (int a, int b)
public void printSum (double a, double b, int c, boolean flag, String text)
<< Hide Menu
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
Now, it’s time to write most of the rest of our methods. But first, a quick reminder about pass-by-value from Unit 2. When we put a primitive variable in as a parameter, we are giving the method a copy of that value so if you change the value of that variable, it does not get carried over to outside that method.
Meanwhile, if you put a reference variable or object as a parameter, it passes a copy of the reference to the method. Changing the value of the variable has the same effect as a primitive variable, but changing information about the variable (such as using a getter method) will carry over outside the method as well. Normally we do not want to modify mutable objects that are passed in as parameters.
Now, we will write some of the methods for our two classes (others will require information from future topics). Take note of the Javadoc comments and also new instance variables that we have added in order for these methods to work. Any changes we have made to the class will be bolded as in previous topics.
/** Represents an assignment that a student will complete
*/
public class Assignment {
private boolean correctAnswer; // represents the answer to an assignment, either T/F
/** Makes a new assignment with one True/False question and sets the correct answer
*/
public Assignment(boolean answer) {
correctAnswer = answer;
}
/** Prints details about the assignment
*/
@Override
public String toString() {
return "This is an assignment with correct answer " + answer;
}
The typical format of a method header is <access modifier> <return type> <method name> (<parameters>)
.
Access modifiers for Java methods can be: public, private, or protected. One of these three can be followed by static, which we will discuss in the next topic. We will talk more about access modifiers in Topic 5.8.
Return type can be: void, String, boolean, int, double, float, char, etc.
Method name can be anything, but usually something descriptive that allows you to infer what the method does.
You can have any number of parameters or no parameters.
Here are some examples of method headers:
public static void main (String args[])
private String sayHello ()
protected static int addNums (int a, int b)
public void printSum (double a, double b, int c, boolean flag, String text)
© 2024 Fiveable Inc. All rights reserved.