Lecture 6, CS 302-7 and 8, February 3

 

  1. Reminders
    1. OO change – Thursday now 1:30-2:30
    2. First codelab assignment posted – announcements on course website
  2. Review
    1. Constants
    2. Comments
    3. I/O
    4. Arithmetic
  3. Arithmetic
    1. Expression

                                                               i.      Operand

                                                             ii.      Operator

    1. Modulo
    2. Integer division

                                                               i.      3/2=1

                                                             ii.      3.0/2=1.5

    1. Powers, roots – Math.pow, Math.sqrt

                                                               i.      Math.pow(a,b); //a^b

                                                             ii.      Math.sqrt(a); //sqrt(a)

                                                            iii.      Squared - Might be easier just to do x * x

                                                           iv.      Can use Math.pow to do cube root, cube, etc

    1. Order of operations

                                                               i.      Like standard math

                                                             ii.      ()

                                                            iii.      Will discuss more once we introduce a few more operators

    1. Example –Quad.java
  1. Random Class
    1. What are random numbers?

                                                               i.      pseudorandom vs random

1.      True randomness: hotbits - http://www.fourmilab.ch/hotbits/

    1. Use Random like Scanner
    2. Step 1 : import java.util.Random();
    3. Step 2: Random <name> = new Random();

                                                               i.      Seed - > new Random(a);

    1. Step 3: methods
    2. .nextInt(a);

                                                               i.      Returns an int between 0 and a (exclusive)

1.      So, .nextInt(4) can return 0,1,2, or 3

    1. .nextInt();

                                                               i.      returns any int in range (-2,147,483,648 to 2,147,483,648)

    1. .nextDouble();

                                                               i.      returns a double between 0.0 and 1.0

    1. Example – Dice.java
  1. Strings
    1. Strings are sequences of characters.  Examples:

                                                               i.      “ab”

                                                             ii.      “c”

                                                            iii.      “1”

                                                           iv.      “&”

    1. String name=”Rob”

                                                               i.      Name is variable, “Rob” is literal

    1. String length

                                                               i.      name.length()

    1. Empty String – “”

                                                               i.      String empty=””;

                                                             ii.      empty.length(); // 0

    1. Concatenating strings

                                                               i.      Like addition, for Strings

                                                             ii.      String x=”x”;

                                                            iii.      String y=”y”;

                                                           iv.      String z=x+y;

                                                             v.      S.o.p(z); //xy

1.      (S.o.p is shorthand for System.out.println() – you still have to write it out in your programs)

  1. HW
    1. Read 3.1-3.2