1_Dice[1]

1_Dice[1] - Problem: Create a class to represent "flat...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Problem: Create a class to represent "flat Problem: Create a dice". A flat die always rolls the same number. First, ask "What do flat dice have? What do flat dice do?" They have an integer: we will need one int int variable variable They roll and get shown on the screen, so we'll need at least 2 functions: roll() and show() 1 Problem: Create a class to represent "flat dice". Problem: Create a class to represent "flat dice". A flat die always rolls the same number. 2 An Instance of a class An Creating a class is like making a blueprint for a house It shows what the house will look like when you actually build it Designing a blueprint is different from building a house. It's the same with a class: The class shows what dice will have and do, when some are actually made. 3 new new When you make a new object, it is said to be an instance of its class In Java, you make instances of a class with the word new new FlatDie(); You can create a variable for a FlatDie(): FlatDie oneDie = new FlatDie(); oneDie int num = 5; num 4 The dot operator The dot operator Once you have made an instance of a class, you can access its parts with a dot FlatDie oneDie = new FlatDie(); FlatDie oneDie.roll(); oneDie.roll(); text("Roll is " + oneDie.num,20,20); oneDie.num 5 Now I'll add code to draw() so that every Now I'll add code to time the screen is drawn, the die shows up void draw() { FlatDie aDie = new FlatDie(); aDie.roll(); aDie.show(50,50); text("You rolled a " + aDie.num,10,90); } 6 Now I'll add code to draw() so that every Now I'll add code to time the screen is drawn, the die shows up void draw() { FlatDie aDie = new FlatDie(); aDie.roll(); aDie.show(50,50); text("You rolled a " + aDie.num,10,90); } 7 With a class writing a program with With a 13 flat dice isn't much harder 8 Here's what Here's what the whole program looks like 9 What would be the output of this applet? What would be the output of this applet? (Hint: it's a trick question) FlatDie aDie = new FlatDie(); //aDie.roll(); aDie.show(50,50); text("You rolled a " + aDie.num,10,90); 10 What would be the output of this applet? What would be the output of this applet? (Hint: it's a trick question) FlatDie aDie = new FlatDie(); //aDie.roll(); aDie.show(50,50); text("You rolled a " + aDie.num,10,90); Zero! Since aDie was never rolled, num was never initialized. 11 The Constructor The Constructor A Constructor is a special function that initializes the variables class FlatDie { int num; int FlatDie() FlatDie() { num=3; num=3; } //other java code not shown //other 12 Remember Pong? Remember Pong? What code should go in the Ball constructor? class Ball { int x,y; int boolean up,right; boolean Ball() Ball() { ??? ??? } //lots more java //lots 13 Each of the 4 Each of the 4 variables needs to be initialized class Ball { int x,y; boolean up,right; up Ball() Ball() { x = 150; 150; y = 50; 50; up=true; up=true right=false; right=false; } //lots more java //lots 14 Thing one; Thing two; Practice Quiz Practice Quiz void setup() { Question: Find one = new Thing(); one two = new Thing(); two the output println("1: " + one.nNum); println("1: two.nNum = one.nNum * 3; two.nNum println("2: " + two.nNum); println("2: one.nNum = two.nNum + one.nNum; one.nNum println("3: " + one.nNum); println("3: } class Thing { int nNum; int Thing() Thing() { nNum = 2; nNum } 15 } Practice Quiz Questions Practice Quiz Questions 1. 2. 1. True/False: A constructor is a function. A Fill in the blank so that num is initialized with one of the following random integers {1,2,3,4}: int num = __________________ ; Find the output of the following program: println("Testing " + 1 + 2 + 3); println("Testing " + (1 + 2 + 3)); 16 Problem: Write a program where Problem: Write a program where the dice are different random sizes 17 Problem: Write a program where Problem: Write a program where the dice are different random sizes Now, what does every die have? (think, what does it have that makes it different?) class Die { ?? ?? } 18 How many lines of code in the How many lines of code in the constructor? class Die { int dots, sideLength; int Die() Die() { ?? ?? } } 19 What else do we need? What else do we need? class Die { int dots, sideLength; int Die() Die() { dots = int(random(1,7)); dots sideLength = int(random(20,80)); sideLength } ?? ?? } 20 Now let's finish the program Now let's finish the program class Die { int dots, sideLength; int Die() Die() { dots = int(random(1,7)); dots sideLength = int(random(20,80)); sideLength } void show(int x, int y) { fill(255); fill(255); rect(x,y,sideLength,sideLength); rect(x,y,sideLength,sideLength); //code for dots not shown //code } 21 void setup() //not shown void void draw() { ?? ?? } class Die { int dots, sideLength; int Die() Die() { dots = int(random(1,7)); dots sideLength = int(random(20,80)); sideLength } void show(int x, int y) //not shown void } 22 void draw() void { while(??) while(??) { Die bob = new Die(); Die bob.show(??,??); bob.show(??,??); } } class Die { int dots, sideLength; int Die() Die() { dots = int(random(1,7)); dots sideLength = int(random(20,80)); sideLength } void show(int x, int y) //not shown void } 23 void draw() void { int xPosition = 0; int while(??) while(??) { Die bob = new Die(); Die bob.show(??,??); bob.show(??,??); } } 24 void draw() void { int xPosition = 0; int while(??) while(??) { Die bob = new Die(); Die bob.show(xPosition,150); bob.show(xPosition,150); } } 25 void draw() void { int xPosition = 0; int while(xPosition < 450) while(xPosition { Die bob = new Die(); Die bob.show(xPosition,150); bob.show(xPosition,150); ?? ?? } } 26 void draw() void { int xPosition = 0; int while(xPosition < 450) while(xPosition { Die bob = new Die(); Die bob.show(xPosition,150); bob.show(xPosition,150); xPosition = xPosition + ??; xPosition } } 27 void draw() void { int xPosition = 0; int while(xPosition < 450) while(xPosition { Die bob = new Die(); Die bob.show(xPosition,150); bob.show(xPosition,150); xPosition = xPosition + bob.sideLength; bob.sideLength; } } 28 Practice Quiz Question Practice Quiz Question Circle the picture that best matches the output void setup() { size(100,100); size(100,100); } void draw() { int yPos = 10; int while(yPos < 100) while(yPos { Widget widgie = new Widget(); Widget widgie.show(50,yPos); widgie.show(50,yPos); yPos = yPos + widgie.diam; yPos } } class Widget { int diam; int Widget() Widget() { diam = 12; diam } void show(int x, int y) void { ellipse(x,y,diam,diam); ellipse(x,y,diam,diam); } } 29 ...
View Full Document

This note was uploaded on 01/13/2012 for the course CS 12 taught by Professor Ma during the Spring '08 term at UC Davis.

Ask a homework question - tutors are online