carnegie mellon school of computer sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8...

62
1

Upload: others

Post on 08-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

1

Page 2: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

2

••–

•–

Page 3: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

3

•–––––

Page 4: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

4

https://travis-ci.org/images/logos/TravisCI-Mascot-1.pnghttps://www.unixmen.com/use-git-commands-linux-terminal/

https://bit.ly/2ZAnuPf\https://github.githubassets.com/images/modules/logos_page/Octocat.png

https://junit.org/junit4/images/junit5-banner.png

Page 5: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

5

Page 6: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

6

V.

Page 7: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

7

class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello world!");

}

}

Page 8: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

8

class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello world!");

}

}

class

Page 9: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

9

class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello world!");

}

}

● main●● public static void main

Page 10: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

10

class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello world!");

}

}

Page 11: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

11

class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello world!");

}

}

main

Page 12: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

12

class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello world!");

}

}

System

Page 13: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

13

http://images.slideplayer.com/21/6322821/slides/slide_9.jpghttps://www.jonbell.net/wp-content/uploads/2015/10/Screen-Shot-2015-10-08-at-2.16.41-PM.png

https://www.theverge.com/2012/10/18/3524036/os-x-update-removes-java-web-browsers

class HelloWorld { public static void main(String[] args) { System.out.println("Hello world!"); }}

HelloWorld.java (HelloWorld.class)

$ javac HelloWorld $ java HelloWorld

Page 14: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

14

••–

•– psvm public static void main

•–

• …–

Page 15: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

15

V.

Page 16: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

16

int long shortchar boolean bytefloat double

●●●

●●●

Page 17: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

17

int x = 5;

int y = 5;

System.out.println(x == y);

---------------------------

String x = “foo”;

String y = x;

System.out.println(x == y);

---------------------------

String x = “foo”;

String y = “foo”;

System.out.println(x == y);

---------------------------

Page 18: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

18

int x = 5;

int y = 5;

System.out.println(x == y);

---------------------------

true

String x = “foo”;

String y = x;

System.out.println(x == y);

---------------------------

String x = “foo”;

String y = “foo”;

System.out.println(x == y);

---------------------------

Page 19: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

19

int x = 5;

int y = 5;

System.out.println(x == y);

---------------------------

true

String x = “foo”;

String y = x;

System.out.println(x == y);

---------------------------

true

String x = “foo”;

String y = “foo”;

System.out.println(x == y);

---------------------------

Page 20: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

20

int x = 5;

int y = 5;

System.out.println(x == y);

---------------------------

true

String x = “foo”;

String y = x;

System.out.println(x == y);

---------------------------

true

String x = “foo”;

String y = “foo”;

System.out.println(x == y);

---------------------------

false

Page 21: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

21

x == y x y••

x.equals(y) x y

String x = “foo”;

String y = “foo”;

System.out.println(x == y); // false

System.out.println(x.equals(y)); // true

Page 22: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

22

• int• long• byte• short• char• float• double• true false

Page 23: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

23

• byte short int– byte

• float double–

Page 24: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

24

•••–

•–––

Page 25: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

25

•––

•– ≈– ≈

Page 26: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

26

Object

Object

BicycleInstrument

MountainBikeGuitar

extendsextends

extends

extends

Page 27: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

27

•––

––

Page 28: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

28

••––

interface Comparator {boolean compare(int i, int j);

}class AscendingComparator implements Comparator {

public boolean compare(int i, int j) { return i < j; }}class DescendingComparator implements Comparator {

public boolean compare(int i, int j) { return i > j; }}

Page 29: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

29

•– .length

String[] answers = new String[42];

if (answers.length == 42) {

answers[42] = "no"; // ArrayIndexOutOfBoundsException

}

Page 30: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

30

•– …

enum Planet { MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE; }

Planet location = …;if (location.equals(Planet.EARTH)) { System.out.println("Honey, I'm home!");}

Page 31: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

31

•@Override, @Deprecated, @SuppressWarnings

class Bicycle { ... @Override public String toString() { return ...; }}

Page 32: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

32

int long shortchar boolean bytefloat double

Integer Long ShortCharacter Boolean ByteFloat Double

●○ HashSet<Integer>

●●

Page 33: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

33

public class BoxOfTricks {

public static Integer n;

public static void main(String [] args) {

if (n == 0)

System.out.println(“That looks okay?”);

else

System.out.println(“I think not.”);

}

}

Page 34: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

34

public class BoxOfTricks {

public static Integer n = null;

public static void main(String [] args) {

if (n == 0) // throws NullPointerException

System.out.println(“That looks okay?”);

else

System.out.println(“I think not.”);

}

}

Page 35: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

35

int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

int i;

int sum1 = 0;

for (i = 0; i < a.length; i++) {

sum1 += a[i];

}

int j;

int sum2 = 0;

for (j = 0; i < a.length; j++) {

sum2 += a[j];

}

System.out.println(sum1 - sum2);

Page 36: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

36

int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

int i;

int sum1 = 0;

for (i = 0; i < a.length; i++) {

sum1 += a[i];

}

int j;

int sum2 = 0;

for (j = 0; i < a.length; j++) { // Copy/paste error!

sum2 += a[j];

}

System.out.println(sum1 - sum2);

Page 37: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

37

…int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

int i;

int sum1 = 0;

for (i = 0; i < a.length; i++) {

sum1 += a[i];

}

int j;

int sum2 = 0;

for (j = 0; j < a.length; j++) {

sum2 += a[j];

}

System.out.println(sum1 - sum2); // Now prints 0, as expected

Page 38: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

38

…int sum1 = 0;

for (int i = 0; i < a.length; i++) {

sum1 += a[i];

}

int sum2 = 0;

for (int i = 0; i < a.length; i++) {

sum2 += a[i];

}

System.out.println(sum1 - sum2); // Prints 0

••

Page 39: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

39

int sum1 = 0;

for (int x : a) {

sum1 += x;

}

int sum2 = 0;

for (int x : a) {

sum2 += x;

}

System.out.println(sum1 - sum2); // Prints 0

••

Page 40: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

40

•–

•••–

Page 41: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

41

Page 42: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

42

•System.out.println("Hello World");

System.out.println("Radius: " + r);

System.out.println(r * Math.cos(theta));

System.out.println();

System.out.print("*");

Page 43: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

43

System.out.println(r * Math.cos(theta));

System.out.println();

Page 44: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

44

•System.out.println("Hello World");

System.out.println("Radius: " + r);

System.out.println(r * Math.cos(theta));

System.out.println();

System.out.print("*");

•System.out.printf(“Radius: %d%n”, r); // better!

System.out.printf("%d * %d = %d%n", a, b, a * b); // Varargs

Page 45: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

45

class Echo { public static void main(String[] args) { for (String arg : args) { System.out.print(arg + " "); } }}

$ java Echo The quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dog

Page 46: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

46

class Gcd { public static void main(String[] args) { int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); System.out.println(gcd(i, j)); }

static int gcd(int i, int j) { return i == 0 ? j : gcd(j % i, i); }}

$ java Gcd 11322 35298666

Page 47: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

47

class Wc { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long result = 0; while (sc.hasNext()) { // whitespace delimiter sc.next(); // consume token result++; } System.out.println(result); }}

$ java Wc < Wc.java32

Page 48: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

48

V.

Page 49: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

49

•–––

Page 50: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

50

Collection

QueueSet List

Deque

Map

Page 51: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

51

Set

LinkedHashSetHashSet TreeHashSet EnumSet

Page 52: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

52

public class Squeeze { public static void main(String[] args) { Set<String> s = new LinkedHashSet<>(); for (String word : args) s.add(word); System.out.println(s); }}

$ java Squeeze I came I saw I conquered[I, came, saw, conquered]

Page 53: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

53

public class Lexicon { public static void main(String[] args) { Set<String> s = new TreeSet<>(); for (String word : args) s.add(word); System.out.println(s); }}

$ java Lexicon I came I saw I conquered[I, came, conquered, saw]

Page 54: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

54

class Index { public static void main(String[] args) { Map<String, Integer> index = new TreeMap<>();

// Iterate backwards so first occurrence wins for (int i = args.length - 1; i >= 0; i--) index.put(args[i], i);

System.out.println(index); }}

$ java Index if it is to be it is up to me to do it{be=4, do=11, if=0, is=2, it=1, me=9, to=3, up=7}

Page 55: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

55

•–

•–

Page 57: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

57

Page 58: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

58

• Object– equals

– hashCode int

– toString

Page 59: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

59

Object

•– equals(Object o) true o

– hashCode() int

– toString()

• java.lang.Object@659e0bfd

Page 60: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

60

Object

• equals hashCode

––

• toString– println

Page 61: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

61

toString

toStringfinal class PhoneNumber { private final short areaCode; private final short prefix; private final short lineNumber; ... @Override public String toString() { return String.format("(%03d) %03d-%04d", areaCode, prefix, lineNumber); }}

Number jenny = ...;System.out.println(jenny);

(707) 867-5309

Page 62: Carnegie Mellon School of Computer Sciencecharlie/courses/17-214/2019-fall/... · 2019-08-29 · 8 class HelloWorld {public static void main(String[] args) {System.out.println("Hello

62

•••