Commit 2ac841a3 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch10] All chapter code added

parent 4a851176
Loading
Loading
Loading
Loading
+20 −0
Changes for src/main/java/ch10/Foof.java: 20 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch10;

class Foof {
  final int size = 3;
  final int whuffie;

  Foof() {
    whuffie = 42;
  }

  void doStuff(final int x) {
    // you can’t change x
  }

  void doMore() {
    final int z = 7;
    // you can’t change z
  }
}
     
+46 −0
Changes for src/main/java/ch10/MathMethods.java: 46 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch10;

public class MathMethods {

  static void random() {
    double r1 = Math.random();
    System.out.println("r1 = " + r1);
    int r2 = (int) (Math.random() * 5);
    System.out.println("r2 = " + r2);
  }

  static void abs() {
    int x = Math.abs(-240);       // returns 240
    double d = Math.abs(240.45);  // returns 240.45
  }

  static void round() {
    int x = Math.round(-24.8f);  // returns -25
    System.out.println("x = " + x);
    int y = Math.round(24.45f);  // returns 24
    System.out.println("y = " + y);

    long z = Math.round(24.45);  // returns 24L
    System.out.println("z = " + z);
  }

  static void min() {
    int x = Math.min(24,240);  // returns 24
    System.out.println("x = " + x);

    double y = Math.min(90876.5, 90876.49);  // returns 90876.49
    System.out.println("y = " + y);
  }

  static void max() {
    int x = Math.max(24,240);  // returns 240
    System.out.println("x = " + x);
    double y = Math.max(90876.5, 90876.49);  // returns 90876.5
    System.out.println("y = " + y);
  }


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

final class MyMostPerfectClass {
  // cannot be extended
}
 No newline at end of file
+8 −0
Changes for src/main/java/ch10/Poof.java: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch10;

class Poof {
  final void calcWhuffie() {
    // important things
    // that must never be overridden
  }
}
 No newline at end of file
+40 −1
Changes for src/main/java/ch10/Snippets.java: 40 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -3,6 +3,17 @@ package ch10;
import java.util.ArrayList;

public class Snippets {
  void mathMethods() {
    long x = Math.round(42.2);
    int y = Math.min(56, 12);
    int z = Math.abs(-343);
  }

  public static int min(int a, int b) {
    //returns the smallest of a and b
    return Math.min(a, b);
  }

  void rounding() {
    int x = Math.round(-24.8f);  // returns -25
    int y = Math.round(24.45f);  // returns 24
@@ -35,6 +46,14 @@ public class Snippets {
    Double dbl = d;
  }

  public void autoboxing() {
    int x = 32;
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(x);

    int num = list.get(0);
  }

  static void parse() {
    String s = "2";
    int x = Integer.parseInt(s);
@@ -69,7 +88,27 @@ public class Snippets {
    String.format("I have %.2f, bugs to fix.", 476578.09876);
  }

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

  static void formatDecimal() {
    System.out.println(String.format("%d", 42));
  }

  static void formatFloat() {
    System.out.println(String.format("%.3f", 42.000000));
  }

  static void formatHex() {
    System.out.println(String.format("%x", 42));
  }

  static void formatChar() {
    System.out.println(String.format("%c", 42));
  }

  public static void main(String[] args) {
    format();
    formatChar();
  }
}
Loading