Except where otherwise noted, the contents of this document are Copyright 2012 Stuart Reges and Marty Stepp.
lab document created by Marty Stepp and Stuart Reges
Goals for today:
return
values to send data between methodsA parameter allows you to pass in a value to a method as you call it.
public static void name(type name) { // declare
methodName(expression); // call
Example:
public static void squared(int num) { System.out.println(num + " times " + num + " is " + (num * num)); } ... squared(3); // 3 times 3 is 9 squared(8); // 8 times 8 is 64
public class MysteryNums { public static void main(String[] args) { int x = 15; sentence(x, 42); // 15 42 int y = x - 5; sentence(y, x + y); // 10 25 } public static void sentence(int num1, int num2) { System.out.println(num1 + " " + num2); } }
public class MysterySoda { public static void main(String[] args) { String soda = "coke"; String pop = "pepsi"; String pepsi = "soda"; // #1 = "coke", #2 = "pepsi", #3 = "soda" carbonated(soda, pop, pepsi); } // #1 #2 #3 public static void carbonated(String coke, String soda, String pop) { // #2 #3 #1 System.out.println("say " + soda + " not " + pop + " or " + coke); } }
// say#2pepsi not#3soda or#1coke
public class MysterySoda { public static void main(String[] args) { String soda = "coke"; String pop = "pepsi"; String coke = "pop"; String pepsi = "soda"; String say = pop; carbonated(soda, pop, pepsi); // say pepsi not soda or coke carbonated(coke, soda, pop); // say coke not pepsi or pop carbonated(pop, pepsi, pepsi); // say soda not soda or pepsi carbonated("pop", pop, "koolaid"); // say pepsi not koolaid or pop carbonated(say, "say", pop); // say say not pepsi or pepsi } public static void carbonated(String coke, String soda, String pop) { System.out.println("say " + soda + " not " + pop + " or " + coke); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class Parameters { public static void main() { double bubble = 867.5309; double x = 10.01; printer(double x, double y); printer(x); printer("barack", "obama"); System.out.println("z = " + z); } public static void printer(x, y double) { int z = 5; System.out.println("x = " + double x + " and y = " + y); System.out.println("The value from main is: " + bubble); } } |
y
without declaring and
initializing ity
in the method
callprinter
without the correct number of
parameters (2, in this case)printer
by passing the correct type of
parameters (double, in this case)z
: it is in scope
inside printer
, not main
x
main
that were not
passed into printer
as a parameterpublic class Parameters { public static void main() { double bubble = 867.5309; double x = 10.01; double y = 5.3; printer(doublex,doubley); printer(x, 0.0);printer("barack", "obama");int z = 5; System.out.println("z = " + z); } public static void printer(double x, double y) { System.out.println("x = " + x + " and y = " + y);System.out.println("The value from main is: " + bubble);int z = 5; } }
printSquare
printSquare
that accepts min and max integers and prints a square of lines of increasing numbers. The first line should start with the minimum; each line that follows should start with the next-higher number. The numbers on a line wrap back to the minimum after it hits the maximum. For example, the call:
printSquare(3, 6);should produce the following output:
3456 4563 5634 6345
printGrid
printGrid
that accepts two integers representing a number of rows and columns and prints a grid of integers from 1 to (rows*columns) in column-major order.
printGrid(4, 6);should produce the following output:
1 5 9 13 17 21 2 6 10 14 18 22 3 7 11 15 19 23 4 8 12 16 20 24
public class MysteryNumbers { public static void main(String[] args) { String one = "two"; String two = "three"; String three = "1"; int number = 20; sentence(one, two, 3); // three times two = 6 sentence(two, three, 14); // 1 times three = 28 sentence(three, three, number + 1); // 1 times 1 = 42 sentence(three, two, 1); // three times 1 = 2 sentence("eight", three, number / 2); // 1 times eight = 20 } public static void sentence(String three, String one, int number) { System.out.println(one + " times " + three + " = " + (number * 2)); } }
public class Mystery { public static void main(String[] args) { String hear = "bad"; String song = "good"; String good = "hear"; String walk = "talk"; String talk = "feel"; String feel = "walk"; claim(feel, song, feel); // to walk the walk is good claim(good, hear, song); // to hear the good is bad claim(talk, "song", feel); // to feel the walk is song claim("claim", talk, walk); // to claim the talk is feel } public static void claim(String hear, String good, String song) { System.out.println("to " + hear + " the " + song + " is " + good); } }
A return value is when a method sends a value back to the code that called it.
public static type name(parameters) { // declare
...
return expression;
}
variableName = methodName(parameters); // call
Example:
public static double fToC(double tempF) { return (tempF - 32) * 5.0 / 9.0; } ... double bodyTemp = fToC(98.6); // bodyTemp stores 37.0 double freezing = fToC(32); // freezing stores 0.0
Method | Description | Example |
---|---|---|
Math.abs
|
absolute value |
Math.abs(-308) returns 308
|
Math.ceil
|
ceiling (rounds upward) |
Math.ceil(2.13) returns 3.0
|
Math.floor
|
floor (rounds downward) |
Math.floor(2.93) returns 2.0
|
Math.max
|
max of two values |
Math.max(45, 207) returns 207
|
Math.min
|
min of two values |
Math.min(3.8, 2.75) returns 2.75
|
Math.pow
|
power |
Math.pow(3, 4) returns 81.0
|
Math.round
|
round to nearest integer |
Math.round(2.718) returns 3
|
Math.sqrt
|
square root |
Math.sqrt(81) returns 9.0
|
Write the results of each expression.
Use the proper type (such as .0
for a double
).
Note that a variable's value changes only if you re-assign it using the =
operator.
double grade = 2.7; Math.round(grade); // grade = 2.7 grade = Math.round(grade); // grade = 3.0 double min = Math.min(grade, Math.floor(2.9)); // min = 2.0 double x = Math.pow(2, 4); // x = 16.0 x = Math.sqrt(64); // x = 8.0 int count = 25; Math.sqrt(count); // count = 25 count = (int) Math.sqrt(count); // count = 5 int a = Math.abs(Math.min(-1, -3)); // a = 3
Consider the following method for converting milliseconds into days:
// converts milliseconds to days public static double toDays(double millis) { return millis / 1000.0 / 60.0 / 60.0 / 24.0; }
Write a similar method named area
that takes as a parameter the radius of a circle and that returns the area of the circle.
For example, the call area(2.0)
should return 12.566370614359172
.
Recall that area can be computed as π times the radius squared and that Java has a constant called Math.PI
.
Write a method named pay
that accepts two parameters: a real number for a TA's salary, and an integer for the number of hours the TA worked this week.
The method should return how much money to pay the TA.
For example, the call pay(5.50, 6)
should return 33.0
.
The TA should receive "overtime" pay of 1 ½ normal salary for any hours above 8.
For example, the call pay(4.00, 11)
should return (4.00 * 8) + (6.00 * 3) or 50.0
.
String
methodsMethod name | Description |
---|---|
charAt(index)
|
character at given index |
indexOf(str)
|
index where the start of the given String appears in this
string (-1 if not found)
|
length()
|
number of characters in this String
|
replace(str1, str2)
|
a new string with all occurrences of str1 changed to str2 |
substring(index1, index2) or substring(index1) |
the characters in this string from index1 (inclusive) to index2 (exclusive); if index2 is omitted, grabs till end of string |
toLowerCase()
|
a new string with all lowercase letters |
toUpperCase()
|
a new string with all uppercase letters |
Write the results of each expression with String
s in
"quotes" and characters in single quotes ('a'
)
// index 0123456789012345
String str1 = "Frodo Baggins";
String str2 = "Gandalf the GRAY";
str1.length() |
13 |
|
str1.charAt(7) |
'a' |
|
str2.charAt(0) |
'G' |
|
str1.indexOf("o") |
2 |
|
str2.toUpperCase() |
"GANDALF THE GRAY" |
|
str1.toLowerCase().indexOf("B") |
-1 |
|
str1.substring(4) |
"o Baggins" |
|
str2.substring(3, 14) |
"dalf the GR" |
|
str2.replace("a", "oo") |
"Goondoolf the GRAY" |
|
str2.replace("gray", "white") |
"Gandalf the GRAY" |
|
"str1".replace("r", "range") |
"strange1" |
If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook.
You can view an exercise, type a solution, and submit it to see if you have solved it correctly.
Choose some problems from the book and try to solve them!