Download pptx - Yale "Zoo"

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS112 PSET4 Walk-Through
May, Yuxuan
Yale University
[email protected]
Part 1: Animation and Interactivity
Part 2. Cashier
Animation and Interactivity
• Implement a Parameterized Figure
- at least three components & two colors
- the method should include x and y to specify the lower-left position
- size 50 - 100
• Input from the console
-
dimension of the canvas
initial x, y, speed, angle of the Parameterized Figure (Scanner, Math functions)
use an image file (need StdDraw.java; use next() / nextLine()? to read file name)
use an audio file (need StdAudio.java; use next() or nextLine() to read file name)
• Countdown Scene
- draw the clock (locate the hand)
- calculate the position of two figures
• Bouncing Animation [Bonus] (if statement)
Program Structure
• Create java project with src/bin at the same dir
• Save StdDraw.java
• Save StdAudio.java
• Save your image file
• Save your audio file in the same directory
Implement a Parameterized Figure
StdDraw
text I/O: Using Scanner
import java.util.Scanner;
Scanner console = new Scanner(System.in);
Method
nextInt()
Description
Returns an int from source
nextDouble()
Returns a double from source
next()
Returns a one-word String from source
nextLine()
Returns a one-line String from source
// Example: Typically print a prompt
System.out.print("How old are you? ");
int age = console.nextInt();
System.out.println("You typed " + age);
size of canvas,
x, y, speed, angle of figures
angle: Math.toRadians();
Important Testing Hint
• Do not input the parameters for each run
• Use constants or file to use fixed test parameters
text I/O: read image
include the image,
use console.nextLine() to read the image:
example: StdDraw.picture(x, y, "angry-bird-r.png");
next() / nextLine()?
next(): skip the whitespace to start
collecting the character, until the next
whitespace
nextLine(): collects any input character
into a string until the first new line and
discards the new line
read audio
include StdAudio.java and the audio
example: duck-quack.wav
0
1
8
(cx, cy + r)
Example: countDown = 8
7
(cx + r cos (2 * P / n *1),
cy + r sin (2 * P / n *1))
r
2
Draw hand -> StdDraw.line
t = countDown -1,
6
(cx, cy)
5
3
4
• Question: How to show hand in each second?
Show(T); -> 1000 ms
Clear();
• Extend: How to show the figure and image?
Show(T); -> 50 ms
Clear();
• count by second -> use for loop
Bonus: Bouncing Animation
vx
vy
positionY <= 0
v
-vy
v
vx
positionValue >= screenSize && speed > 0
positionValue <= 0 && speed < 0
Animation Structure
• CarLaunch vs using delta
• For bouncing, we suggest you use the delta structure
for ( ... ) {
// set speed
// compute delta of displacement in FRAME_TIME
pos += delta;
}
The if/else Statement
• An else clause can be added to an if statement to make
it an if-else statement:
if ( test ) {
statement1;
}
else {
statement2;
}
 If the condition is true, statement1 is
executed; if the condition is false,
statement2 is executed
 One or the other will be executed, but not
both
14
Part 2. Cashier
Cashier
• 2 parts of the program:
1. Getting input (green)
2. Calculating change (red)
• To calculate change, implement
static String getChanges(double owed, double paid)
in order to get change.
• This should return a String with the
text to print (i.e. everything in the
red box).
Getting Input
• Use the Scanner class
• Import java.util.Scanner
• Use Scanner.nextDouble() to read input as a double
• Example:
Scanner scan = new Scanner(System.in);
firstInput = scan.nextDouble();
secondInput = scan.nextDouble();
Recap: String.format
• Allows you to insert double, int, etc values into a String according to a
specified format
• Example:
double decimals = 0.55555;
String twoPlaces = String.format(“Two decimal places:
%.2f.”, decimals);
System.out.println(twoPlaces);
• Output:
Two decimal places: 0.56.
Calculating Change
• Calculate change using integers
to represent the number of
cents
• This allows us to use integer
division ( / ) and modulo ( % )
operators
• If a, b are integers:
• a/b gives integer part of quotient
• a%b gives remainder after
dividing a by b
Example:
int priceInCents = 80;
int quarters = priceInCents/25;
int change = priceInCents%25;
// quarters = 3
// change = 5
Recap: Methods with return
• Methods can be made to return a value
• Example:
public static int sum(int x, int y) {
return x+y;
}
public static void main(String[] args) {
int stripesOnZebra, blackStripes, whiteStripes;
stripesOnZebra = sum(blackStripes, whiteStripes);
}
Recap: Methods with return
• Can return any type: String, int, double, float, etc.
• Must declare return type in method declaration:
public static double methodName()
public static int returnInteger()
public static String returnString()
• After the return statement, the rest of the code in the method is not
executed.
public static int returnOne() {
return 1;
System.out.println(“This line will not be executed.”);
}