3 min read•Last Updated on June 18, 2024
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](https://www.fiveableKeyTerm:Assignment) that a [student](https://www.fiveableKeyTerm:Student) will complete
*/
[public class](https://www.fiveableKeyTerm:public_class) Assignment {
[private](https://www.fiveableKeyTerm:Private) [boolean](https://www.fiveableKeyTerm: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](https://www.fiveableKeyTerm:public) Assignment(boolean answer) {
correctAnswer = answer;
}
/** Prints details about the assignment
*/
[@Override](https://www.fiveableKeyTerm:@Override)
public [String](https://www.fiveableKeyTerm:String) [toString()](https://www.fiveableKeyTerm:toString()) {
return "This is an assignment with correct answer " + answer;
}
**/** Grades an assignment, returns true if correct, false if incorrect
*/
public boolean gradeAssignment(boolean studentAnswer) {
return studentAnswer == correctAnswer;
}**
}
/** Represents a high school student
*/
public class Student {
private [int](https://www.fiveableKeyTerm:int) gradeLevel; // a grade between 9-12
private String name; // the students name in the form "FirstName LastName"
private int age; // the student's age, must be positive
private Assignment assignment; // the current assignment the student is working on
**private int assignmentsComplete; // number of assignments completed
private int correctAssignments; // number of correct assignments**
/** Makes a new student with grade gradeLev, name fullName, and age ageNum
*/
public Student(int gradeLev, String fullName, int ageNum) {
gradeLevel = gradeLev;
name = fullName;
age = ageNum;
assignment = null; // There is no active assignment at the moment
**assignmentsComplete = 0; // no assignments complete yet
correctAssignments = 0;**
}
/** Returns the student's grade level
*/
public int getGradeLevel() {
return gradeLevel;
}
/** Returns the student's name
*/
public String getName() {
return name;
}
/** Returns the current assignment the student is working on
*/
public Assignment [returnCurrentAssignment()](https://www.fiveableKeyTerm:returnCurrentAssignment()) {
return assignment;
}
/** Prints details about the student
*/
@Override
public String toString() {
return name + ", a " + gradeLevel + "th grade high school student has an average grade of " + averageGrade + ".";
}
/** Changes the student's name
*/
public void setName(String fullName) {
name = fullName;
}
/** Changes the student's grade level
*/
public void setGradeLevel(int gradeLev) {
gradeLevel = gradeLev;
}
**/** Submits an assignment
*/
public void [submitAssignment()](https://www.fiveableKeyTerm:submitAssignment()) {
boolean grade = assignment.gradeAssignment();
assignmentsComplete++;
if grade {
correctAssignments++
}
}
/** Calculates the student's grade as a decimal**
***/
public double [getGradeDecimal()](https://www.fiveableKeyTerm:getGradeDecimal()) {
return (double) correctAssignments / assignmentsComplete;
}**
}
The typical format of a method header is <access modifier> <return type>
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)
The @Override annotation is used in Java to indicate that a method in a subclass is intended to override a method with the same name in its superclass. It helps ensure that the method signature and return type are correct.
Term 1 of 22
The @Override annotation is used in Java to indicate that a method in a subclass is intended to override a method with the same name in its superclass. It helps ensure that the method signature and return type are correct.
Term 1 of 22
The @Override annotation is used in Java to indicate that a method in a subclass is intended to override a method with the same name in its superclass. It helps ensure that the method signature and return type are correct.
Term 1 of 22
Pass-by-value is the method of passing arguments to functions or methods by creating copies of their values instead of directly passing references to them.
Pass-by-reference: A method of passing arguments where the memory address (reference) is passed instead of creating copies.
Call stack: A stack-like data structure that keeps track of function calls during program execution.
Return value: The value that is returned by a function after it completes its execution.
Javadoc comments are special comments in Java that begin with /** and end with */. They are used to generate documentation for classes, methods, and fields, making it easier for other developers (including yourself) to understand how to use them.
API Documentation: API documentation provides information about how to use specific software libraries or frameworks. It includes details about classes, methods, parameters, and return values.
Code Documentation: Code documentation refers to any form of written explanation or commentary within source code files that helps developers understand the purpose and functionality of the code.
Commenting Style Guide: A commenting style guide is a set of guidelines or rules that dictate how comments should be written in a particular programming language or project. It ensures consistency and readability across the codebase.
Instance variables are variables declared within a class but outside any method. They hold unique values for each instance (object) of the class and define the state or characteristics of an object.
Methods: Methods are actions or behaviors associated with objects in a class. They define what an object can do or how it can interact with other objects.
Constructor: A constructor is a special method within a class that is automatically called when an object is created from that class. It initializes the object's state by assigning initial values to its instance variables.
Access Modifiers: Access modifiers determine the accessibility or visibility of classes, methods, and variables in object-oriented programming. They control which parts of a program can access certain elements and help enforce encapsulation and data hiding principles.
An access modifier is used in programming languages to control the visibility and accessibility of variables, methods, and classes within different parts of code. It determines whether other parts of code can access and modify certain elements.
public: This access modifier allows unrestricted access to variables, methods, or classes from any part of the code.
private: This access modifier restricts access to variables, methods, or classes only within the same class where they are declared.
protected: This access modifier allows access within the same class and its subclasses but not outside those boundaries.
The return type in programming refers to the data type of the value that a method or function will return when it is called. It specifies what kind of data will be returned by the method.
Method Name: The name given to a method or function which helps identify and call it within a program.
Parameters: Inputs or arguments passed into a method or function for it to perform its task.
Data Type: Specifies the type of data that can be stored in a variable, such as integer, string, boolean, etc.
In programming, "protected" is an access modifier that grants accessibility to members within subclasses or classes within the same package.
public: In the context of programming, "public" refers to an access modifier that allows a class, method, or variable to be accessed from anywhere in the program.
private: In programming, "private" is an access modifier that restricts the visibility of a class member (method or variable) to only within its own class.
inheritance: The mechanism of creating new classes based on existing ones, allowing for code reuse and adding additional functionality.
In programming, "static" refers to a variable or method that belongs to the class itself, rather than an instance of the class. It can be accessed without creating an object of the class.
Instance Variable: A variable that belongs to each individual object created from a class.
Class Method: A method that belongs to the class itself and can be called without creating an object.
Static Block: A block of code in a class that is executed only once when the class is loaded into memory.