Download CS112 PSET4 Walk-Through - 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
CS112PSET4Walk-Through
May,Yuxuan
YaleUniversity
[email protected]
Part1:AnimationandInteractivity
Part2.Cashier
AnimationandInteractivity
• ImplementaParameterizedFigure
- atleastthreecomponents&twocolors
- themethodshouldincludexandytospecifythelower-leftposition
- size50- 100
• Inputfromtheconsole
-
dimensionofthecanvas
initialx,y,speed,angleoftheParameterized Figure(Scanner,Mathfunctions)
useanimagefile(needStdDraw.java;usenext()/nextLine()?toreadfilename)
useanaudiofile(needStdAudio.java;usenext()ornextLine()toreadfilename)
• CountdownScene
- drawtheclock(locatethehand)
- calculatethepositionoftwofigures
• BouncingAnimation[Bonus](ifstatement)
ProgramStructure
• Createjavaprojectwithsrc/binatthesamedir
• SaveStdDraw.java
• SaveStdAudio.java
• Saveyourimagefile
• Saveyouraudiofileinthesamedirectory
ImplementaParameterizedFigure
StdDraw
textI/O:UsingScanner
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);
sizeofcanvas,
x,y,speed,angleoffigures
angle:Math.toRadians();
ImportantTestingHint
• Donotinputtheparametersforeachrun
• Useconstantsorfiletousefixedtestparameters
textI/O:readimage
includetheimage,
useconsole.nextLine()toreadtheimage:
example:StdDraw.picture(x,y,"angry-bird-r.png");
next()/nextLine()?
next():skipthewhitespacetostart
collectingthecharacter,untilthenext
whitespace
nextLine():collectsanyinputcharacter
intoastringuntilthefirstnewlineand
discardsthenewline
readaudio
includeStdAudio.java andtheaudio
example:duck-quack.wav
0
1
8
(cx,cy+r)
Example:countDown =8
7
(cx+rcos(2*P/n*1),
cy+rsin(2*P/n*1))
r
2
Drawhand->StdDraw.line
t=countDown -1,
6
(cx,cy)
5
3
4
• Question:Howtoshowhandineachsecond?
Show(T);->1000ms
Clear();
• Extend:Howtoshowthefigureandimage?
Show(T);->50ms
Clear();
• countbysecond->useforloop
Bonus:BouncingAnimation
vx
vy
positionY <=0
v
-vy
v
vx
positionValue >=screenSize &&speed>0
positionValue <=0&&speed<0
AnimationStructure
• CarLaunch vsusingdelta
• Forbouncing,wesuggestyouusethedeltastructure
for(...){
//setspeed
//computedeltaofdisplacementinFRAME_TIME
pos +=delta;
}
Theif/else Statement
• Anelse clause canbeaddedtoanif statementtomake
itanif-else statement:
if ( test ) {
statement1;
}
else {
statement2;
}
q If the condition is true, statement1 is
executed; if the condition is false,
statement2 is executed
q One or the other will be executed, but not
both
14
Part2.Cashier
Cashier
• 2partsoftheprogram:
1. Gettinginput(green)
2. Calculatingchange(red)
• Tocalculatechange,implement
static String getChanges(double owed, double paid)
inordertogetchange.
• ThisshouldreturnaStringwiththe
texttoprint(i.e.everythinginthe
redbox).
GettingInput
• UsetheScanner class
• Importjava.util.Scanner
• UseScanner.nextDouble() toreadinputasadouble
• Example:
Scanner scan = new Scanner(System.in);
firstInput = scan.nextDouble();
secondInput = scan.nextDouble();
Recap:String.format
• Allowsyoutoinsertdouble,int,etc valuesintoaStringaccordingtoa
specifiedformat
• Example:
double decimals = 0.55555;
String twoPlaces = String.format(“Two decimal places:
%.2f.”, decimals);
System.out.println(twoPlaces);
• Output:
Two decimal places: 0.56.
CalculatingChange
• Calculatechangeusingintegers
torepresentthenumberof
cents
• Thisallowsustouseinteger
division(/)andmodulo(%)
operators
• Ifa,bareintegers:
• a/bgivesintegerpartofquotient
• a%b givesremainderafter
dividingabyb
Example:
int priceInCents = 80;
int quarters = priceInCents/25;
int change = priceInCents%25;
// quarters = 3
// change = 5
Recap:Methodswithreturn
• Methodscanbemadetoreturnavalue
• 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:Methodswithreturn
• Canreturnanytype:String,int,double,float,etc.
• Mustdeclarereturntypeinmethoddeclaration:
public static double methodName()
public static int returnInteger()
public static String returnString()
• Afterthereturnstatement,therestofthecodeinthemethodisnot
executed.
public static int returnOne() {
return 1;
System.out.println(“This line will not be executed.”);
}