Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Java Methods BW

This document summarizes different types of methods in Python, including void methods, value-returning methods, string methods, and static methods. It explains that methods break problems into smaller pieces through divide and conquer and allow for code reuse. Arguments are passed by value in Python so changes to parameter variables inside methods do not affect the original arguments. Value-returning methods perform tasks and return data to the calling statement. String methods allow manipulation of string objects.

Uploaded by

weetan studio
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Java Methods BW

This document summarizes different types of methods in Python, including void methods, value-returning methods, string methods, and static methods. It explains that methods break problems into smaller pieces through divide and conquer and allow for code reuse. Arguments are passed by value in Python so changes to parameter variables inside methods do not affect the original arguments. Value-returning methods perform tasks and return data to the calling statement. String methods allow manipulation of string objects.

Uploaded by

weetan studio
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Methods

methods
invoking

https://coolpythoncodes.com/python-function/
Contents
• void methods
• value-returning methods
• String methods
• static methods
• Methods are commonly used to break a problem
down into small manageable pieces. This is
called divide and conquer.
• Methods simplify programs. If a specific task is
performed in several places in the program, a
method can be written once to perform that
task, and then be executed anytime it is needed.
This is known as code reuse.
void Methods
Header public static void displayMesssage()
{
Body System.out.println("Hello");
}

• A void method is one that simply performs a


task and then terminates.
Method Return Method
Modifiers Type Name Parentheses

public static void displayMessage ()


{
System.out.println("Hello");
}
Parts of a Method Header
• Method modifiers
– public—method is publicly available to code outside the class
– static—method belongs to a class, not a specific object.
• Return type—void or the data type from a value-
returning method
• Method name—name that is descriptive of what the
method does
• Parentheses—contain nothing or a list of one or more
variable declarations if the method is capable of
receiving arguments
Arguments are Passed by Value
• In Java, all arguments of the primitive data types are
passed by value, which means that only a copy of an
argument’s value is passed into a parameter variable.
• A method’s parameter variables are separate and
distinct from the arguments that are listed inside the
parentheses of a method call.
• If a parameter variable is changed inside a method, it
has no affect on the original argument.
displayValue(5); The argument 5 is copied into the
parameter variable num.

public static void displayValue(int num)


{
System.out.println("The value is " + num);
}
The argument 5 is copied into the num1 parameter.
The argument 10 is copied into the num2 parameter.

showSum(5, 10); NOTE: Order matters!

public static void showSum(double num1, double num2)


{
double sum; //to hold the sum
sum = num1 + num2;
System.out.println("The sum is " + sum);
}
Value-Returning Methods
• A value-returning method not only performs a task,
but also sends a value back to the code that called
it.
• Data can be passed into a method by way of the
parameter variables. Data may also be returned
from a method, back to the statement that called it.
double num = Math.pow(4,3);
• Two integers 4 and 3 are passed into the pow
method.
• The double value 64.0 is returned from the
method and assigned to the num variable.
Defining a Value-Returning Method
public static int sum(int num1, int num2)
{
int result; Return type
result = num1 + num2; The return statement
return result; causes the method to end
} execution and it returns a
value back to the
This expression must be of the statement that called the
same data type as the return type method.
Calling a Value-Returning Method
total = sum(value1, value2);
20 40
public static int sum(int num1, int num2)
{
60
int result;
result = num1 + num2;
return result;
}
Returning a Boolean value
• Sometimes we need to write methods to test
arguments for validity and return true or false
int value = 20;
if(isValid(value))
System.out.println("The value is within range");
else
System.out.println("The value is out of range");

public static boolean isValid(int number)


{
boolean status;
if(number >= 1 && number <= 100)
status = true;
else
status = false;
return status;
}
Returning a Reference to a String Object
customerName = fullName("John", "Martin");

public static String fullName(String first, String last)


{
String name;
address
name = first + " " + last; Local variable name holds
return name; the reference to the object.
} The return statement sends
a copy of the reference
“John Martin” back to the call statement
and it is stored in
customerName.
String Methods
• Since String is a class, objects that are
instances of it have methods.
• One of those methods is the length
method.
stringSize = message.length();
• This statement runs the length method on
the object pointed to by the message
variable.
String Methods
• charAt(index): returns the character at
the index position
• toLowerCase(): returns a new string
that is the lowercase equivalent of the string
• toUpperCase(): returns a new string
that is the uppercase equivalent of the string
String Methods
static methods
Review Questions
• Area of rectangle (or other shape)
– Get values for the sides
– Calculate the area
– Display the area
• Pizza Ordering
– Get pizza base
– Get topping / get extras
– Calculate total price
Resources
• BlueJ http://bluej.org/
• Think Java (Allen Downey & Chris Mayfield)
– http://greenteapress.com/thinkjava6/thinkjava.pdf
• Online Java Compiler
– https://www.tutorialspoint.com/compile_java_online.php
– https://www.jdoodle.com/online-java-compiler
– https://www.compilejava.net
• Online Java Tutorial
– https://docs.oracle.com/javase/tutorial/java/
– https://www.w3schools.in/java-tutorial/
– https://www.tutorialspoint.com/java/

You might also like