Fiveable
Fiveable

or

Log in

Find what you need to study


Light

Find what you need to study

5.6 Writing Methods

5 min readdecember 29, 2022

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Now, it’s time to write most of the rest of our methods. But first, a quick reminder about 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.

Writing Methods

Now, we will write some of the methods for our two classes (others will require information from future topics). Take note of the and also new 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.

Java Example

/** Represents an 

Format

The typical format of a method header is <> <> <method name> (<parameters>).

  • Access modifiers for Java methods can be: , , or . One of these three can be followed by , which we will discuss in the next topic. We will talk more about access modifiers in Topic 5.8.

  • can be: void, , , , 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:

void main ( args[])

sayHello ()

addNums ( a, b)

void printSum (double a, double b, c, flag, text)

Key Terms to Review (22)

@Override

: 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.

access modifier

: 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.

Assignment

: An assignment is the act of giving a value to a variable in programming. It involves storing information into memory locations so it can be accessed and manipulated later.

boolean

: A boolean is a data type that can only have two possible values: true or false. It is often used in programming to make decisions and control the flow of a program.

getGradeDecimal()

: The term "getGradeDecimal()" is a method or function used in programming to retrieve the numerical value of a student's grade. It returns the grade as a decimal number.

Instance Variables

: 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.

int

: The int is a primitive data type in Java used to store whole numbers without decimal points. It has a fixed size and represents integer values within a specific range.

Javadoc comments

: 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.

Pass-by-value

: 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.

Private

: In the context of programming, private refers to a visibility modifier that restricts access to certain variables or methods within a class. It means that only other members of the same class can access those private elements.

protected

: In programming, "protected" is an access modifier that grants accessibility to members within subclasses or classes within the same package.

public

: Public is an access modifier in Java that indicates unrestricted visibility for classes, methods, and variables. Public members can be accessed from any other class or package.

public class

: A public class is one that can be accessed and used by any other class or program. It serves as the blueprint for creating objects and contains variables and methods.

Return Type

: 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.

returnCurrentAssignment()

: This term refers to a method in programming that retrieves the current assignment. It is used to access and display the details of the assignment that is currently being worked on.

setGradeLevel()

: In programming, setGradeLevel() is a method used to assign or update the grade level of an individual. It allows you to specify which grade level someone belongs to within your program.

setName()

: setName() is a function or method used in programming to assign or change the name associated with an object or variable. It allows you to update or set a new name for an entity within your program.

Static

: 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.

String

: A String is a sequence of characters in Java, enclosed in double quotes. It represents text and can be manipulated using various methods.

Student

: A student is an individual who is enrolled in a school or educational institution and is actively pursuing knowledge and education.

submitAssignment()

: The term "submitAssignment()" refers to a method or function in programming that allows a user to send their completed work or task for evaluation or processing.

toString()

: The toString() method is used to convert an object into a string representation. It returns a string that represents the state of the object.

5.6 Writing Methods

5 min readdecember 29, 2022

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Now, it’s time to write most of the rest of our methods. But first, a quick reminder about 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.

Writing Methods

Now, we will write some of the methods for our two classes (others will require information from future topics). Take note of the and also new 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.

Java Example

/** Represents an 

Format

The typical format of a method header is <> <> <method name> (<parameters>).

  • Access modifiers for Java methods can be: , , or . One of these three can be followed by , which we will discuss in the next topic. We will talk more about access modifiers in Topic 5.8.

  • can be: void, , , , 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:

void main ( args[])

sayHello ()

addNums ( a, b)

void printSum (double a, double b, c, flag, text)

Key Terms to Review (22)

@Override

: 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.

access modifier

: 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.

Assignment

: An assignment is the act of giving a value to a variable in programming. It involves storing information into memory locations so it can be accessed and manipulated later.

boolean

: A boolean is a data type that can only have two possible values: true or false. It is often used in programming to make decisions and control the flow of a program.

getGradeDecimal()

: The term "getGradeDecimal()" is a method or function used in programming to retrieve the numerical value of a student's grade. It returns the grade as a decimal number.

Instance Variables

: 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.

int

: The int is a primitive data type in Java used to store whole numbers without decimal points. It has a fixed size and represents integer values within a specific range.

Javadoc comments

: 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.

Pass-by-value

: 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.

Private

: In the context of programming, private refers to a visibility modifier that restricts access to certain variables or methods within a class. It means that only other members of the same class can access those private elements.

protected

: In programming, "protected" is an access modifier that grants accessibility to members within subclasses or classes within the same package.

public

: Public is an access modifier in Java that indicates unrestricted visibility for classes, methods, and variables. Public members can be accessed from any other class or package.

public class

: A public class is one that can be accessed and used by any other class or program. It serves as the blueprint for creating objects and contains variables and methods.

Return Type

: 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.

returnCurrentAssignment()

: This term refers to a method in programming that retrieves the current assignment. It is used to access and display the details of the assignment that is currently being worked on.

setGradeLevel()

: In programming, setGradeLevel() is a method used to assign or update the grade level of an individual. It allows you to specify which grade level someone belongs to within your program.

setName()

: setName() is a function or method used in programming to assign or change the name associated with an object or variable. It allows you to update or set a new name for an entity within your program.

Static

: 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.

String

: A String is a sequence of characters in Java, enclosed in double quotes. It represents text and can be manipulated using various methods.

Student

: A student is an individual who is enrolled in a school or educational institution and is actively pursuing knowledge and education.

submitAssignment()

: The term "submitAssignment()" refers to a method or function in programming that allows a user to send their completed work or task for evaluation or processing.

toString()

: The toString() method is used to convert an object into a string representation. It returns a string that represents the state of the object.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.