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:
Strings 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 mainxmain that were not
passed into printer as a parameter
public class Parameters {
public static void main() {
double bubble = 867.5309;
double x = 10.01;
double y = 5.3;
printer(double x, double y);
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 |