Except where otherwise noted, the contents of this document are Copyright 2010 Stuart Reges and Marty Stepp.
lab document created by Whitaker Brand and Marty Stepp
Goals for today:
String
s to represent and manipulate text data
Math
library to perform mathematical computations
public class MysteryNums { public static void main(String[] args) { int x = 15; sentence(x, 42); // 15 42 int y = x - 7; sentence(y, x + y); // 8 23 } public static void sentence(int num1, int num2) { System.out.println(num1 + " " + num2); } }
String
valuesString
constants we have been using since chapter 1 can
be stored in variables of type String
. What output is
produced by the following code?
String first = "James"; String last = "Kirk"; String middle = "T." System.out.println(last); // Kirk System.out.println("My name is " + first); // My name is James System.out.println(first + " " + last); // James Kirk System.out.println(last + ", " + first + " " + middle); // Kirk, James T. System.out.println(middle + " is for Tiberius"); // T. is for Tiberius
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(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); } }
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)); } }
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; } }
1 2 3 4 5 6 7 8 9 10 |
public class Numbers { public static void main(String[] args) { int number = 42; for (int i = 1; i <= 1000; i++) { number = number * 37 % 103; } int number2 = number * number; System.out.println("result = " + number2); } } |
continued on the next slide...
i
and number
.
When you want it to continue, you can hit the button that looks like the
standard "play" icon of a right-pointing arrow. Using this
approach, fill in the table below indicating what
value number
has when i
has the given value.
Keep in mind that you are figuring out what value number
has
just before it executes this line of code.
i = 1 |
42 |
|
i = 2 |
9 |
|
i = 3 |
24 |
|
i = 4 |
64 |
continued on the next slide...
number
after the loop is done executing. Again hit the
"play" button to start the programming running again. What value
does number
have at line 7?
69
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
|
continued on the next slide...
Write the results of each of the following expressions. Make sure to use
the proper type (.0 for a double
).
Math.ceil(9.17) |
10.0 |
|
Math.pow(2, 4) |
16.0 |
|
Math.sqrt(64) |
8.0 |
|
Math.floor(12.73) + Math.max(8,
5) |
20.0 |
|
Math.abs(Math.min(-1, -3)) |
3 |
|
Math.ceil(Math.random()) |
1.0 |
|
-Math.pow(2, 2) + Math.pow(-2, 3) +
Math.pow(2, -2) |
-11.75 |
|
Math.round(4.25) + Math.round(5.38) +
Math.round(6.49) |
15 |
String
s
Write a method called printStrings
that
accepts a String
and a number of repetitions as parameters
and prints that String
the given number of times. For
example, the call:
printStrings("abc", 5);
will print the following output:
abcabcabcabcabc
main
method.
Try calling your method several times from main
and see what
happens when you pass different values.
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum = sum + i;
}
System.out.println(sum); // 5050
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum = sum + i;
}
System.out.println(sum); // 5050
printPowersOf2
that
uses a cumulative algorithm to print out the powers of 2 up to
2x. It should take a parameter that
determines x; that is, the exponent of the highest power of 2 to
be printed. For example, the following call:
printPowersOf2(10);should produce the following output, because 210 = 1024:
1 2 4 8 16 32 64 128 256 512 1024(For this problem it's okay if your numbers have a
.0
at the
end of them.)
main
method. Call your method several times
in main
to ensure that it works. Also check your answer
using Practice-It by clicking the check-mark icon above.
n
printPowersOfN
that prints the powers of any given
number n up to n^x. For example, the
following call:
printPowersOfN(3, 5);should produce the following output, because 35 = 243:
1 3 9 27 81 243and the call:
printPowersOfN(2, 10);should produce the same output as
printPowersOf2(10);
:
1 2 4 8 16 32 64 128 256 512 1024
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
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 Chapter 3 and try to solve
them! Note that you won't be able to do every problem from Chapter 3;
some involve concepts we have not learned yet, such
as return
and Scanner
.