Commit 04bdd5ba authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch10] Initial code from the book

parent 88145a0f
Loading
Loading
Loading
Loading
+8 −0
Changes for src/main/java/ch10/NoStatic.java: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch10;

class NoStatic {
  public static void main(String[] args) {
    System.out.println("sqrt " + Math.sqrt(2.0));
    System.out.println("tan " + Math.tan(60));
  }
}
 No newline at end of file
+75 −0
Changes for src/main/java/ch10/Snippets.java: 75 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch10;

import java.util.ArrayList;

public class Snippets {
  void rounding() {
    int x = Math.round(-24.8f);  // returns -25
    int y = Math.round(24.45f);  // returns 24
    long z = Math.round(24.45);  // returns 24L
  }

  void wrapInt() {
    int x = 32;
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(x);
    int num = list.get(0);
  }

  void wrappers() {
    boolean b = true;
    Boolean bool = b;
    char c = 'a';
    Character chr = c;
    byte bt = 0;
    Byte bte = bt;
    short s = 1;
    Short sht = s;
    int i = 2;
    Integer intr = i;
    long l = 3;
    Long lng = l;
    float f = 4;
    Float flt = f;
    double d = 5;
    Double dbl = d;
  }

  static void parse() {
    String s = "2";
    int x = Integer.parseInt(s);
    double d = Double.parseDouble("420.24");

    boolean b = Boolean.parseBoolean("True");
  }

  static void badParse() {
    String t = "two";
    int y = Integer.parseInt(t);

    System.out.println(y);
  }

  void doubleToString1() {
    double d = 42.5;
    String doubleString = "" + d;
  }

  void doubleToString2() {
    double d = 42.5;
    String doubleString = Double.toString(d);
  }

  void doubleToString3() {
    double d = 42.5;
    String doubleString = String.valueOf(d);
  }

  static void format() {
    String.format("I have %.2f, bugs to fix.", 476578.09876);
  }

  public static void main(String[] args) {
    format();
  }
}
+17 −0
Changes for src/main/java/ch10/TestBox.java: 17 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch10;

public class TestBox {
  private Integer i;
  private int j;

  public static void main(String[] args) {
    TestBox t = new TestBox();
    t.go();
  }

  public void go() {
    j = i;
    System.out.println(j);
    System.out.println(i);
  }
}
 No newline at end of file
+9 −0
Changes for src/main/java/ch10/TestFormats.java: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch10;

public class TestFormats {
  public static void main(String[] args) {
    long myBillion = 1_000_000_000;
    String s = String.format("%,d", myBillion);
    System.out.println(s);
  }
}
 No newline at end of file
+20 −0
Changes for src/main/java/ch10/WithStatic.java: 20 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch10;

import java.util.function.Predicate;
import java.util.stream.Stream;

import static java.lang.Math.sqrt;
import static java.lang.Math.tan;
import static java.lang.System.out;

class WithStatic {
  public static void main(String[] args) {
    out.println("sqrt " + sqrt(2.0));
    out.println("tan " + tan(60));
  }

  void otherStaticImports() {
    Stream.of("string")
          .filter(Predicate.not(String::isEmpty));
  }
}
 No newline at end of file