The ¡life ¡and ¡death ¡of ¡objects, ¡sta2cs ¡
CSCI ¡136: ¡Fundamentals ¡of ¡Computer ¡Science ¡II ¡ ¡• ¡ ¡Keith ¡Vertanen ¡
The life and death of objects, sta2cs CSCI 136: - - PowerPoint PPT Presentation
The life and death of objects, sta2cs CSCI 136: Fundamentals of Computer Science II Keith Vertanen Overview Where Java stores stuff The
CSCI ¡136: ¡Fundamentals ¡of ¡Computer ¡Science ¡II ¡ ¡• ¡ ¡Keith ¡Vertanen ¡
2 ¡
3 ¡
4 ¡
5 ¡
public ¡void ¡doStuff() ¡ { ¡ ¡ ¡ ¡boolean ¡b ¡= ¡true; ¡ ¡ ¡ ¡go(4); ¡ } ¡ ¡ public ¡void ¡go(int ¡x) ¡ { ¡ ¡ ¡ ¡int ¡z ¡= ¡x ¡+ ¡24; ¡ ¡ ¡ ¡crazy(); ¡ } ¡ ¡ public ¡void ¡crazy() ¡ { ¡ ¡ ¡ ¡char ¡c ¡= ¡'a'; ¡ } ¡
6 ¡
public ¡void ¡doStuff() ¡ { ¡ ¡ ¡ ¡boolean ¡b ¡= ¡true; ¡ ¡ ¡ ¡go(4); ¡ } ¡ ¡ public ¡void ¡go(int ¡b) ¡ { ¡ ¡ ¡ ¡int ¡c ¡= ¡b ¡+ ¡24; ¡ ¡ ¡ ¡crazy(); ¡ } ¡ ¡ public ¡void ¡crazy() ¡ { ¡ ¡ ¡ ¡char ¡c ¡= ¡'a'; ¡ } ¡
doStuff() ¡ boolean ¡b ¡→ ¡true ¡ doStuff() ¡ boolean ¡b ¡→ ¡true ¡ go() ¡ int ¡b ¡→ ¡4 ¡ int ¡c ¡→ ¡28 ¡
doStuff() ¡ boolean ¡b ¡→ ¡true ¡ go() ¡ int ¡b ¡→ ¡4 ¡ int ¡c ¡→ ¡28 ¡
crazy() ¡ char ¡c ¡→ ¡'a' ¡ doStuff() ¡ boolean ¡b ¡→ ¡true ¡ go() ¡ int ¡b ¡→ ¡4 ¡ int ¡c ¡→ ¡28 ¡
7 ¡
public ¡class ¡Factorial ¡ { ¡ ¡ ¡ ¡public ¡static ¡long ¡fact(long ¡n) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(n ¡<= ¡1) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡1; ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡n ¡* ¡fact(n ¡-‑ ¡1); ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡long ¡result ¡= ¡fact(4); ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("4! ¡= ¡" ¡+ ¡result); ¡ ¡ ¡ ¡} ¡ } ¡ ¡
main() ¡ ¡ fact(4) ¡ n ¡→ ¡4 ¡ ¡ fact(3) ¡ n ¡→ ¡3 ¡ ¡ fact(2) ¡ n ¡→ ¡2 ¡ ¡ fact(1) ¡ n ¡→ ¡1 ¡ ¡
8 ¡
public ¡class ¡Factorial ¡ { ¡ ¡ ¡ ¡public ¡static ¡long ¡fact(long ¡n) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(n ¡<= ¡1) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡1; ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡n ¡* ¡fact(n ¡-‑ ¡1); ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡long ¡result ¡= ¡fact(4); ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("4! ¡= ¡" ¡+ ¡result); ¡ ¡ ¡ ¡} ¡ } ¡ ¡
main() ¡ fact(4) ¡= ¡24 ¡ fact(4) ¡ n ¡→ ¡4 ¡ return ¡4 ¡* ¡fact(3) ¡ fact(3) ¡ n ¡→ ¡3 ¡ return ¡3 ¡* ¡fact(2) ¡ fact(2) ¡ n ¡→ ¡2 ¡ return ¡2 ¡* ¡fact(1) ¡ fact(1) ¡ n ¡→ ¡1 ¡ return ¡1 ¡
9 ¡
public ¡class ¡Factorial ¡ { ¡ ¡ ¡ ¡public ¡static ¡long ¡fact(long ¡n) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡n ¡* ¡fact(n ¡-‑ ¡1); ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡long ¡result ¡= ¡fact(4); ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("4! ¡= ¡" ¡+ ¡result); ¡ ¡ ¡ ¡} ¡ } ¡ ¡
main() ¡ fact(4) ¡ n ¡→ ¡4 ¡ fact(3) ¡ n ¡→ ¡3 ¡ fact(2) ¡ n ¡→ ¡2 ¡ fact(1) ¡ n ¡→ ¡1 ¡ fact(0) ¡ n ¡→ ¡0 ¡ fact(-‑1) ¡ n ¡→ ¡-‑1 ¡ fact(-‑2) ¡ n ¡→ ¡-‑2 ¡
Exception ¡in ¡thread ¡"main" ¡ java.lang.StackOverflowError ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ ... ¡
10 ¡
11 ¡
myDuck ¡
12 ¡
myDuck ¡
13 ¡
myDuck ¡
14 ¡
You're ¡so ¡going ¡to ¡ have ¡to ¡this ¡yourself ¡ in ¡data ¡structures. ¡
15 ¡
public ¡class ¡DuckKiller1 ¡ { ¡ ¡ ¡ ¡public ¡static ¡void ¡makeDuck() ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡Duck ¡d ¡= ¡new ¡Duck(); ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡makeDuck(); ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(true) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Do ¡something ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡} ¡ } ¡
16 ¡
00 ¡public ¡class ¡DuckKiller1b ¡ 01 ¡{ ¡ 02 ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ 03 ¡ ¡ ¡{ ¡ 04 ¡ ¡ ¡ ¡ ¡ ¡if ¡(args.length ¡>= ¡0) ¡ 05 ¡ ¡ ¡ ¡ ¡ ¡{ ¡ 06 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Duck ¡d ¡= ¡new ¡Duck(); ¡ ¡ ¡ ¡ 07 ¡ ¡ ¡ ¡ ¡ ¡} ¡ 08 ¡ ¡ ¡ ¡ ¡ ¡while ¡(true) ¡ 09 ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ 10 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Do ¡something ¡ 11 ¡ ¡ ¡ ¡ ¡ ¡} ¡ 12 ¡ ¡ ¡} ¡ 13 ¡} ¡
17 ¡
00 ¡public ¡class ¡DuckKiller1b ¡ 01 ¡{ ¡ 02 ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ 03 ¡ ¡ ¡{ ¡ 04 ¡ ¡ ¡ ¡ ¡ ¡if ¡(args.length ¡>= ¡0) ¡ 05 ¡ ¡ ¡ ¡ ¡ ¡{ ¡ 06 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Duck ¡d ¡= ¡new ¡Duck(); ¡ ¡ ¡ ¡ 07 ¡ ¡ ¡ ¡ ¡ ¡} ¡ 08 ¡ ¡ ¡ ¡ ¡ ¡while ¡(true) ¡ 09 ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ 10 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Do ¡something ¡ 11 ¡ ¡ ¡ ¡ ¡ ¡} ¡ 12 ¡ ¡ ¡} ¡ 13 ¡} ¡
18 ¡
00 ¡public ¡class ¡DuckKiller2 ¡ 01 ¡{ ¡ 02 ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ 03 ¡ ¡ ¡ ¡{ ¡ 04 ¡ ¡ ¡ ¡ ¡ ¡ ¡Duck ¡d ¡= ¡new ¡Duck(); ¡ 05 ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("quack!"); ¡ 06 ¡ ¡ ¡ ¡ ¡ ¡ ¡d ¡= ¡new ¡Duck(); ¡ 07 ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("quack!"); ¡ 08 ¡ ¡ ¡ ¡ ¡ ¡ ¡d ¡= ¡new ¡Duck(); ¡ 09 ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("quack!"); ¡ 10 ¡ ¡ ¡ ¡} ¡ 11 ¡} ¡
19 ¡
00 ¡public ¡class ¡DuckKiller2 ¡ 01 ¡{ ¡ 02 ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ 03 ¡ ¡ ¡ ¡{ ¡ 04 ¡ ¡ ¡ ¡ ¡ ¡ ¡Duck ¡d ¡= ¡new ¡Duck(); ¡ 05 ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("quack!"); ¡ 06 ¡ ¡ ¡ ¡ ¡ ¡ ¡d ¡= ¡new ¡Duck(); ¡ 07 ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("quack!"); ¡ 08 ¡ ¡ ¡ ¡ ¡ ¡ ¡d ¡= ¡new ¡Duck(); ¡ 09 ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("quack!"); ¡ 10 ¡ ¡ ¡ ¡} ¡ 11 ¡} ¡
20 ¡
00 ¡public ¡class ¡DuckKiller2 ¡ 01 ¡{ ¡ 02 ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ 03 ¡ ¡ ¡ ¡{ ¡ 04 ¡ ¡ ¡ ¡ ¡ ¡ ¡Duck ¡d ¡= ¡new ¡Duck(); ¡ 05 ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("quack!"); ¡ 06 ¡ ¡ ¡ ¡ ¡ ¡ ¡d ¡= ¡new ¡Duck(); ¡ 07 ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("quack!"); ¡ 08 ¡ ¡ ¡ ¡ ¡ ¡ ¡d ¡= ¡new ¡Duck(); ¡ 09 ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("quack!"); ¡ 10 ¡ ¡ ¡ ¡} ¡ 11 ¡} ¡
21 ¡
00 ¡public ¡class ¡DuckKiller3 ¡ 01 ¡{ ¡ 02 ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ 03 ¡ ¡ ¡ ¡{ ¡ 04 ¡ ¡ ¡ ¡ ¡ ¡ ¡Duck ¡d ¡= ¡new ¡Duck(); ¡ 05 ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("quack!"); ¡ 06 ¡ ¡ ¡ ¡ ¡ ¡ ¡d ¡= ¡null; ¡ 07 ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("quack!"); ¡ 08 ¡ ¡ ¡ ¡} ¡ 09 ¡} ¡
22 ¡
00 ¡public ¡class ¡DuckKiller3 ¡ 01 ¡{ ¡ 02 ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ 03 ¡ ¡ ¡ ¡{ ¡ 04 ¡ ¡ ¡ ¡ ¡ ¡ ¡Duck ¡d ¡= ¡new ¡Duck(); ¡ 05 ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("quack!"); ¡ 06 ¡ ¡ ¡ ¡ ¡ ¡ ¡d ¡= ¡null; ¡ 07 ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("quack!"); ¡ 08 ¡ ¡ ¡ ¡} ¡ 09 ¡} ¡
23 ¡
import ¡java.util.*; ¡ ¡ public ¡class ¡HeapDeath ¡ { ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ArrayList<Duck> ¡list ¡= ¡new ¡ArrayList<Duck>(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(true) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Duck ¡d ¡= ¡new ¡Duck(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡list.add(d); ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡} ¡ } ¡
Duck ¡reference ¡persists ¡
¡
24 ¡
byte ¡
short ¡
int ¡
long ¡
float ¡
double ¡
boolean ¡
char ¡
¡
hqp://righteousit.wordpress.com/tag/tcpip/ ¡ ¡
25 ¡
00 ¡public ¡class ¡HeapPuzzler ¡ 01 ¡{ ¡ 02 ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ 03 ¡ ¡ ¡ ¡{ ¡ 04 ¡ ¡ ¡ ¡ ¡ ¡ ¡Duck ¡d ¡= ¡new ¡Duck(); ¡ 05 ¡ ¡ ¡ ¡ ¡ ¡ ¡Duck ¡[] ¡a ¡= ¡new ¡Duck[4]; ¡ 06 ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡< ¡a.length; ¡i++) ¡ 07 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[i] ¡= ¡new ¡Duck(); ¡ 08 ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("quack!"); ¡ 09 ¡ ¡ ¡ ¡ ¡ ¡ ¡a[0] ¡= ¡null; ¡ 10 ¡ ¡ ¡ ¡ ¡ ¡ ¡a[1] ¡= ¡d; ¡ 11 ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("quack!"); ¡ 12 ¡ ¡ ¡ ¡} ¡ 13 ¡} ¡
04 ¡ 05 ¡ 08 ¡ 09 ¡ 10 ¡
Two ¡variables ¡that ¡refer ¡to ¡the ¡same ¡Duck ¡
26 ¡
public ¡class ¡Duck ¡ { ¡ ¡ ¡ ¡private ¡String ¡name ¡= ¡""; ¡ ¡ ¡ ¡private ¡double ¡weight ¡= ¡0.0; ¡ ¡ ¡ ¡ ¡public ¡Duck(Duck ¡other) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡this.name ¡ ¡ ¡= ¡other.name; ¡ ¡ ¡ ¡ ¡ ¡ ¡this.weight ¡= ¡other.weight; ¡ ¡ ¡ ¡} ¡ } ¡
27 ¡
¡
¡
public ¡class ¡Rabbit ¡ { ¡ ¡ ¡ ¡private ¡double ¡x ¡= ¡0.0; ¡ ¡ ¡ ¡private ¡double ¡y ¡= ¡0.0; ¡ ¡ ¡ ¡private ¡static ¡final ¡double ¡size ¡= ¡0.06; ¡ ¡ ¡ ¡ ¡public ¡Rabbit(double ¡x, ¡double ¡y) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡this.x ¡= ¡x; ¡ ¡ ¡ ¡ ¡ ¡ ¡this.y ¡= ¡y; ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡public ¡void ¡draw() ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡StdDraw.picture(x, ¡y, ¡"rabbit.png"); ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡public ¡boolean ¡intersect(double ¡x, ¡double ¡y) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡double ¡deltaX ¡= ¡(this.x ¡-‑ ¡x); ¡ ¡ ¡ ¡ ¡ ¡ ¡double ¡deltaY ¡= ¡(this.y ¡-‑ ¡y); ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡(Math.sqrt(deltaX ¡* ¡deltaX ¡+ ¡deltaY ¡* ¡deltaY) ¡< ¡size); ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡... ¡ ¡ ¡
28 ¡
¡ ¡ ¡public ¡Rabbit ¡breed() ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡Rabbit ¡baby ¡= ¡new ¡Rabbit(this.x ¡+ ¡(0.2 ¡-‑ ¡Math.random() ¡* ¡0.4), ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡this.y ¡+ ¡(0.2 ¡-‑ ¡Math.random() ¡* ¡0.4)); ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡baby; ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ArrayList<Rabbit> ¡rabbits ¡= ¡new ¡ArrayList<Rabbit>(); ¡ ¡ ¡ ¡ ¡ ¡ ¡rabbits.add(new ¡Rabbit(0.5, ¡0.5)); ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(true) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡rabbits.size() ¡-‑ ¡1; ¡i ¡>= ¡0; ¡i-‑-‑) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Rabbit ¡r ¡= ¡rabbits.get(i); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡r.draw(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(r.intersect(StdDraw.mouseX(), ¡StdDraw.mouseY())) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡rabbits.add(r.breed()); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡StdDraw.show(100); ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡} ¡ } ¡
29 ¡
30 ¡
31 ¡
public ¡static ¡int ¡round(float ¡a) ¡
Round ¡to ¡the ¡nearest ¡integer ¡
public ¡static ¡long ¡round(double ¡a) ¡ public ¡static ¡int ¡min(int ¡a, ¡int ¡b) ¡
Find ¡the ¡minimum ¡of ¡two ¡numbers ¡
public ¡static ¡long ¡min(long ¡a, ¡long ¡ ¡b) ¡ public ¡static ¡float ¡min(float ¡a, ¡float ¡b) ¡ public ¡static ¡double ¡min(double ¡a, ¡double ¡b) ¡ public ¡static ¡int ¡abs(int ¡a) ¡
Return ¡absolute ¡value ¡of ¡a ¡number ¡
public ¡static ¡long ¡abs(long ¡a) ¡ public ¡static ¡float ¡abs(float ¡a) ¡ public ¡static ¡double ¡abs(double ¡a) ¡ public ¡static ¡double ¡random() ¡
Get ¡a ¡random ¡number ¡in ¡[0.0, ¡1.0) ¡
public ¡static ¡double ¡sqrt(double ¡a) ¡
Take ¡the ¡square ¡root ¡of ¡a ¡number ¡
32 ¡
¡public ¡static ¡final ¡double ¡E ¡ ¡= ¡2.7182818284590452354; ¡ ¡public ¡static ¡final ¡double ¡PI ¡= ¡3.14159265358979323846; ¡ /** ¡ ¡ ¡* ¡Don't ¡let ¡anyone ¡instantiate ¡this ¡class. ¡ ¡*/ ¡ private ¡Math() ¡{} ¡
¡
33 ¡
int ¡x ¡= ¡Math.round(42.2); ¡ int ¡y ¡= ¡Math.min(56, ¡12); ¡ int ¡z ¡= ¡Math.abs(-‑343); ¡
Math ¡mathObj ¡= ¡new ¡Math(); ¡
Exception ¡in ¡thread ¡"main" ¡java.lang.Error: ¡Unresolved ¡ compilation ¡problem: ¡ ¡ ¡The ¡constructor ¡Math() ¡is ¡not ¡visible ¡
34 ¡
public ¡class ¡Cow ¡ { ¡ ¡ ¡ ¡private ¡String ¡name ¡= ¡""; ¡ ¡ ¡ ¡private ¡double ¡weight ¡= ¡0.0; ¡ ¡ ¡ ¡ ¡public ¡static ¡double ¡getWeight() ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡weight; ¡ ¡ ¡ ¡} ¡ } ¡
¡
Cow.java:8: ¡non-‑static ¡variable ¡weight ¡cannot ¡be ¡ referenced ¡from ¡a ¡static ¡context ¡ ¡return ¡weight; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡^ ¡
Cow ¡cow1 ¡= ¡new ¡Cow(); ¡ Cow ¡cow2 ¡= ¡new ¡Cow(); ¡ System.out.println("weight ¡= ¡", ¡Cow.getWeight()); ¡
35 ¡
public ¡class ¡Cow ¡ { ¡ ¡ ¡ ¡private ¡String ¡name ¡= ¡""; ¡ ¡ ¡ ¡private ¡double ¡weight ¡= ¡0.0; ¡ ¡ ¡ ¡ ¡public ¡double ¡getWeight() ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡weight; ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡public ¡static ¡boolean ¡isBig() ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡(getWeight() ¡> ¡500.0); ¡ ¡ ¡ ¡} ¡ } ¡
¡
Cow.java:13: ¡non-‑static ¡method ¡ getWeight() ¡cannot ¡be ¡referenced ¡from ¡a ¡ static ¡context ¡ ¡return ¡(getWeight() ¡> ¡500.0); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡^ ¡
36 ¡
public ¡static ¡final ¡double ¡PI ¡= ¡3.14159265358979323846; ¡
37 ¡
public ¡class ¡Cow ¡ { ¡ ¡ ¡ ¡private ¡String ¡name ¡= ¡""; ¡ ¡ ¡ ¡private ¡double ¡weight ¡= ¡0.0; ¡ ¡ ¡ ¡ ¡ ¡private ¡static ¡AudioFile ¡sound ¡= ¡new ¡AudioFile("cow.wav"); ¡ ¡ ¡ ¡ ¡public ¡void ¡makeNoise() ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡sound.play(); ¡ ¡ ¡ ¡} ¡ } ¡
AudioFile ¡object ¡is ¡created ¡on ¡
38 ¡
public ¡class ¡Cow ¡ { ¡ ¡ ¡ ¡private ¡String ¡name ¡= ¡""; ¡ ¡ ¡ ¡private ¡double ¡weight ¡= ¡0.0; ¡ ¡ ¡ ¡ ¡private ¡static ¡int ¡numCows ¡= ¡0; ¡ ¡ ¡ ¡ ¡public ¡Cow() ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡numCows++; ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡public ¡Cow(Cow ¡otherCow) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡this.name ¡ ¡ ¡= ¡otherCow.name; ¡ ¡ ¡ ¡ ¡ ¡ ¡this.weight ¡= ¡otherCow.weight; ¡ ¡ ¡ ¡ ¡ ¡ ¡numCows++; ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡public ¡static ¡int ¡getNumCows() ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡numCows; ¡ ¡ ¡ ¡} ¡ } ¡
39 ¡
Heap ¡