Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.
lab document created by Marty Stepp, Stuart Reges and Whitaker Brand
Goals for today:
for
loop table practice
Create a table of the number of stars on each line:
******* ***** *** * |
|
multiplier: How much does the # of stars change between lines? -2 shift: Given your multiplier, what must be added to get that many stars on line 1? 9 |
Test your loop expression in Practice-It! using the checkmark icon above. Use the form:
for (int stars = 1; stars <= multiplier * line + shift; stars++) {
public static void stars() { for (int i = 1; i <= 10; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } for (int j = 1; j <= 20 - 2 * i; j++) { System.out.print(" "); } for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } }
* * ** ** *** *** **** **** ***** ***** ****** ****** ******* ******* ******** ******** ********* ********* ********************
public static void mystery() { for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) { System.out.print(i * j + "\t"); } System.out.println(); } }
\t
represents a tab character.
Answer:
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
Write a program to produce the following output using nested for
loops. Use a table to help you figure out the patterns of characters on each line.
-----1----- ----333---- ---55555--- --7777777-- -999999999- |
|
|
Test your loop expressions in Practice-It! using the checkmark icon above. Use your expressions in the loop tests of the inner loops of your code.
Write a Java program in a class named SlashFigure
to produce the following output with nested for
loops. Use a loop table if necessary to figure out the expressions.
!!!!!!!!!!!!!!!!!!!!!! \\!!!!!!!!!!!!!!!!!!// \\\\!!!!!!!!!!!!!!//// \\\\\\!!!!!!!!!!////// \\\\\\\\!!!!!!//////// \\\\\\\\\\!!////////// |
|
Test your code in Practice-It! or the Output Comparison Tool.
A class constant is a global value that cannot be changed.
public static final type name = expression;
Example:
public static final int DAYS_PER_WEEK = 7; public static final double TAX_RATE = 0.10;
for
loop expressions w/ constant
When adding a class constant to a loop expression, it affects the constant that must be added in the expression.
Suppose we have the two loop expressions below for figure sizes of 5 and 9. The third line of the table shows the general formula that would be used if we turned our figure's size into a constant named SIZE
.
size | expression | relationship |
---|---|---|
5 |
8 * line + 16 |
16 = 3 * 5 + 1 |
9 |
8 * line + 28 |
28 = 3 * 9 + 1 |
SIZE |
8 * line + (3 * SIZE + 1) |
continued on the next slide ...
for
loop table w/ constant
You already found loop expressions for the slash figure at size 6. Now make a table at size 4 and use the two to generalize the loop expression in terms of a constant for the figure size.
!!!!!!!!!!!!!! \\!!!!!!!!!!// \\\\!!!!!!//// \\\\\\!!////// |
|
|
Add a class constant to your slash figure program so that it can be resized from its default of 6:
size 4 | size 7 |
---|---|
!!!!!!!!!!!!!! \\!!!!!!!!!!// \\\\!!!!!!//// \\\\\\!!////// |
!!!!!!!!!!!!!!!!!!!!!!!!!! \\!!!!!!!!!!!!!!!!!!!!!!// \\\\!!!!!!!!!!!!!!!!!!//// \\\\\\!!!!!!!!!!!!!!////// \\\\\\\\!!!!!!!!!!//////// \\\\\\\\\\!!!!!!////////// \\\\\\\\\\\\!!//////////// |
Test your code in the Output Comparison Tool.
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); } } |
for
loop (line 5). Do this by moving your cursor to the beginning of that line until you see a
stop-sign icon
and then clicking.
continued on the next slide...
i
and number
.
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, number = |
42 |
|
i = 2, number = |
9 |
|
i = 3, number = |
24 |
|
i = 4, number = |
64 |
continued on the next slide...
number
after the loop is done executing.
number
?
69
A variable's scope is the part of a program in which it exists. In Java, the scope of a variable starts when it is declared and ends when the closing curly brace for the block that contains it is reached. A variable is said to be in scope where it is accessible.
public class Example { public static void main(String[] args) { performTest(); } public static void performTest() { int count = 12; for (int i = 1; i <= 12; i++) { runSample(); System.out.print(count); } } public static void runSample() { System.out.print("sample"); } } |
In which of these two blocks is the variable count in scope?
|
A 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); } }
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); } }
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); } } |
String[] args
y
without declaring and
initializing itx
and y
in
the method callprinter
without the correct number of
parameters (2, in this case)printer
by passing the incorrect type
of parameters (double, in this case)z
: it is in scope
inside printer
, not main
x
y
are in the
wrong order (should be type, then name)x
in
the println
bubble
: it is in
scope inside main
, not printer
public class Parameters { public static void main(String[] args) { 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,y doubledouble y) { int z = 5; System.out.println("x = " +doublex + " and y = " + y);System.out.println("The value from main is: " + bubble);} }
When you want to divide a graphical program into multiple drawing methods, you must pass Graphics g as a parameter in addition to any other parameters. Example:
public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(400, 300); Graphics g = panel.getGraphics(); ... drawStuff(g, 13, 52, 7); } public static void drawStuff(Graphics g, int a, int b, int c) { g.drawLine(a, 45, b, c); ... }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import Java.*; public class PrettyPicture { public static void main(String[] args) { DrawingPanel panel = DrawingPanel(220, 150); setBackgroundColor(Color.YELLOW); Graphics g = panel.Graphics(); panel.setColor(new Color.BLUE); g.drawRectangle(50, 25); g.setColor("RED"); g.fillEllipse(130, 25, 42.1, 40.5); } } |
answer on next slide...
import
statement; should import java.awt.*
new
before 2nd occurrence of DrawingPanel
setBackground
panel.
before setBackground
getGraphics
setColor
method is part of object g
, not panel
new
before Color.BLUE
drawRect
drawRect
(width and height)
Color.RED
, not "RED"
fillOval
import java.awt.*; public class PrettyPicture { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(220, 150); panel.setBackground(Color.YELLOW); Graphics g = panel.getGraphics(); g.setColor(Color.BLUE); g.drawRect(50, 25, 10, 10); g.setColor(Color.RED); g.fillOval(130, 25, 42, 40); } }
Write a complete Java program that draws the following output:
import java.awt.*; public class Face1 { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(220, 150); Graphics g = panel.getGraphics(); g.drawOval(10, 30, 100, 100); // face outline g.setColor(Color.BLUE); g.fillOval(30, 60, 20, 20); // eyes g.fillOval(70, 60, 20, 20); g.setColor(Color.RED); // mouth g.drawLine(40, 100, 80, 100); } }
Consider the output at right. The first stair's top-left corner is at position (5, 5). The first stair is 10x10 px in size. Each stair is 10px wider than the one above it.
Fill in the table below with the coordinates and sizes of the first five stairs. Note which values change and which ones stay the same.
stair | x | y | width | height |
---|---|---|---|---|
1
|
5 |
5 |
10 |
10 |
2
|
5 |
15 |
20 |
10 |
3
|
5 |
25 |
30 |
10 |
4
|
5 |
35 |
40 |
10 |
5
|
5 |
45 |
50 |
10 |
Write a complete Java program to draw the stairs. Copy/paste the code template below into jGRASP and fill in your own expressions or values for each stair's x, y, width, and height.
Use your table from the previous slide to help you find the correct expressions.
The values that change for each stair should become expressions in terms of the loop counter variable, i
.
import java.awt.*; public class Stairs1 { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(110, 110); Graphics g = panel.getGraphics(); for (int i = 0; i < 10; i++) { g.drawRect(x, y, width, height); } } }
Modify your stairs program to draw one (or all) of the following outputs.
Modify only the body in your for
loop.
(You may want to make a new table to find the expressions for x, y, width, and height.)
To get each output, change the for
loop body to the following:
// output 2
g.drawRect(5, 5 + 10*i, 100 - 10*i, 10);
// output 3
g.drawRect(95 - 10*i, 5 + 10*i, 10 + 10*i, 10);
// output 4
g.drawRect(5 + 10*i, 5 + 10*i, 100 - 10*i, 10);
Suppose you have an existing program that draws the "face" figure at right. Let's modify the program using methods and parameters so that we can draw several faces at different locations.
continued on the next slide...
Modify the Face
program to draw the following output.
Write a parameterized method that draws a face at different positions.
import java.awt.*; public class Face2 { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(320, 180); Graphics g = panel.getGraphics(); drawFace(g, 10, 30); drawFace(g, 150, 50); } public static void drawFace(Graphics g, int x, int y) { g.setColor(Color.BLACK); g.drawOval(x, y, 100, 100); g.setColor(Color.BLUE); g.fillOval(x + 20, y + 30, 20, 20); g.fillOval(x + 60, y + 30, 20, 20); g.setColor(Color.RED); g.drawLine(x + 30, y + 70, x + 70, y + 70); } }
Modify your previous Java program to draw the following output.
Use a for
loop with your parameterized method to draw faces at different positions.
import java.awt.*; public class Face3 { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(520, 180); Graphics g = panel.getGraphics(); for (int i = 0; i < 5; i++) { drawFace(g, 10 + i * 100, 30); } } public static void drawFace(Graphics g, int x, int y) { g.setColor(Color.BLACK); g.drawOval(x, y, 100, 100); g.setColor(Color.BLUE); g.fillOval(x + 20, y + 30, 20, 20); g.fillOval(x + 60, y + 30, 20, 20); g.setColor(Color.RED); g.drawLine(x + 30, y + 70, x + 70, y + 70); } }
for
loops to produce the
following output:
000111222333444555666777888999 000111222333444555666777888999 000111222333444555666777888999
99999888887777766666555554444433333222221111100000 99999888887777766666555554444433333222221111100000 99999888887777766666555554444433333222221111100000 99999888887777766666555554444433333222221111100000 99999888887777766666555554444433333222221111100000
999999999888888887777777666666555554444333221 999999999888888887777777666666555554444333221 999999999888888887777777666666555554444333221 999999999888888887777777666666555554444333221
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
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
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!