Consider letting the less experienced member of the group do the typing if it seems like there's a mismatch in experience or comfort levels. That will help make sure they don't get left behind. Worst case, flip a coin to see who does the typing. Or have Java generate a random boolean value. (Type (new java.util.Random()).nextBoolean()
in the codepad.)
At the conclusion of the lab, make arrangements for the typer to share a copy of the code with the other member(s) of the group if desired. (E.g. email it, or put it on a shared Google drive, etc.) My solutions to the lab will get posted as well.
package.bluej
file inside the project folder to open the project in BlueJ, or start BlueJ and use the Open menu item to select the project folder.
variableName = value;
where variableName is the name of a variable (instance variable or other) that you want to change, and value is an expression describing the new value to be stored in the variable. The expression on the right-hand side can be a literal value like 100, or involve the name of another variable. If it's a variable, you're telling Java to look inside that variable, retrieve its value, and store that value in the variable on the left-hand side. One good way to get practice with the assignment statement is to read existing code that uses assignment statements, and see if you can predict their results.
AsmtStmtPractice
class and spend a minute familiarizing yourself with its contents. Instances of the class each have three fields — x
, y
, and z
. The constructor calls another method, reset
, to give the fields initial values. (It was set up that way so that you could call reset
yourself to get back to those initial values at any time.) Create an instance of the class (right click on it and select the first item on the menu that pops up), then double-click the object (the red rounded rectangle) to open an object inspector before proceeding:
x
, y
, and z
at the end of the method after the assignment statements have been executed. Once your team has come to consensus on your prediction, test it by running the method and using the object inspector to peek at the final values. Call reset
on the object before moving on to the next test method. If the behavior of any of the cases seems unexpected, feel free to ask for more information.
test1
method in AsmtStmtPractice
so that it looks like the code below. (You can copy and paste from this web page to save some typing.)
public void test1() { int silly; // This declares a new local variable silly = -2; // We then give it an initial value x = 100; y = 5 + silly; // Should be able to use silly's value here }
AsmtStmtPractice
by right-clicking on AsmtStmtPractice
and using the drop-down menu as before.
silly
appear as part of the state of the object? Why, or why not, do you suppose?
test1
method on the object and then check again. Does it show up in the object inspector? Do you see any evidence that silly
's value was used within the test1
method?
AsmtStmtPractice
lab and edit test3
now: Change the x=z;
line so it says x=silly;
instead, then compile the code. You'll get an error, and if you hover over silly
the details emerge. Java doesn't think silly
is usable in test2
! It was defined inside test1
, so its scope is limited to test1
— it's not accessible outside that method.
test3
, and let's see what happens if we move silly's
declaration outside of test1
. Delete the int silly;
line from the top of test1
(but only that one line) and edit the fields at the top of the class so that silly
is declared there now instead as shown below. (We'll talk in class again about what "private" is doing for us, but it won't affect how the variables and scope work for this example.)
private int x; private int y; private int z; private int silly; // Add this line
silly
as a field. Note that it compiles this time! Declaring it at the top of the class makes it visible in all of the methods. Create an instance of the class as before and use the object inspector. Is silly
shown? Call test3
and test1
methods and make sure you understand how they interact.
PacMan
class and skimming its contents. Note that it contains a constructor (a method with the same name as the class that will run automatically when a PacMan object is created), and two other methods. None of the methods currently contains any code. (There are no Java statements between their open and close curly braces.)
Don't forget to copy the resulting Java statements from the terminal window and paste them into the constructor in the PacMan class before moving on!1 Create a new Circle named body 2 Create a new Circle named eye 3 Create a new Circle named powerball 4 Create a new Triangle named mouth 5 Make body visible 6 Make eye visible 7 Make mouth visible 8 Make powerball visible 9 Change body color to "yellow" 10 Change body size to 75 11 Move body up 12 Move body up 13 Change eye color to "black" 14 Change eye size to 5 15 Slow-Move eye horizontally by 58 16 Change mouth size to 75 (height) and 75 (width) 17 Move mouth horizontally by -150 18 Move mouth vertically by -85 19 Change mouth color to "white" 20 Change powerball color to "magenta" 21 Change powerball size to 20 22 Slow-Move powerball horizontally by 30 23 Move powerball down 24 Move powerball down
makeBlackAndWhite
method. The plan is for it to change the color of the objects to black so that the picture looks black and white, but we've got the same problem we ran into in class: The makeBlackAndWhite
method can't access eye
, or mouth
or any of the objects unless they've been stored in fields at the top of the class. Let's fix that..
PacMan
instance, and make sure it still looks ok. Then double-click on the object (the red rectangle) to open the object inspector and make sure you can see the fields, and that they're not null
.
makeBlackAndWhite
that changes the color of each of the objects as described in the comments in the body of the method. Try to write the code "by hand" rather than using the point-and-click interface and recording the results. Verify that the method works as expected before moving on.
makeInvisible
method. It should call makeInvisible()
on each of the objects in the PacMan picture. Verify that it works when you're done.
makeColor
method that could be used to turn a black-and-white PacMan back to its original colors.