Lecture 4, CS 302-7 and 8, January 30

 

  1. Reminders
    1. Labs start Tuesday, January 31 (tomorrow)

                                                               i.      Note – labs aren’t posted online until day of

    1. Lab consultants – schedule posted online

                                                               i.      http://pages.cs.wisc.edu/~cs302/resources/conSchedule.html

    1. Exam conflicts – online form (lab – this week)
  1. Review
    1. Printing – numbers and strings
    2. Errors

                                                               i.      Compile-time

                                                             ii.      Run-time

  1. Variables
    1. Variables store values.
    2. Analogy to labeled parking spot – has a unique location, and holds something.
    3. Three components:

                                                               i.      Type

                                                             ii.      Name

                                                            iii.      Value

    1. Variables must be declared:

                                                               i.      <type> <name>;

    1. And then initialized

                                                               i.      <name> = <value>;

    1. Variables must be declared and initialized before we try to do something with them
    2. Declaring and initializing variables are statements – need to have a ; afterwards
    3. You can also declare and initialize variables on the same line

                                                               i.      <type> <name> = <value>;

    1. Football.java
  1. Types of variables
    1. Parking spot analogy – different kinds of cars, bikes, motorcycles, trucks, etc – different kinds go in different spots
    2. int

                                                               i.      (integer) – for those familiar with integers from math

                                                             ii.      Whole numbers – negative, positive, or zero

                                                            iii.      Note – integers have a maximum size (demo with 3 billion)

1.      MaxInt.java

2.      System.out.println(1000000000*3);

3.      Can use double or long instead

    1. long

                                                               i.      Like an int, but can store bigger numbers

                                                             ii.      int is 4 bytes, long is 8 bytes

    1. double

                                                               i.      stores non-integer values, such as 0.5 or –1.2

    1. Literals

                                                               i.      If you just use the number instead of a variable, it is referred to as a literal

                                                             ii.      Literal:

System.out.println(1+2);

                                                            iii.      Variable:

int a=1+2;

System.out.println(a);

  1. Variable naming
    1. Requirements vs Standards
    2. Variable names must:

                                                               i.      Start with letter or _.  Cannot start with number

                                                             ii.      Contain letters, numbers, and _

                                                            iii.      No spaces, ?, %

                                                           iv.      Case Sensitive

                                                             v.      Not use reserved words

1.      For instance, public, static, …

    1. Variable names should:

                                                               i.      Begin with a lower case letter

1.      Why?  So that we don’t confuse them with classes, since classes should start with upper case

                                                             ii.      Start new words with upper case

1.      This is called ‘camel case’

                                                            iii.      Be descriptive.

1.      ‘x’ vs ‘fieldGoal’ - fieldGoal is a better name?

a.       In case you forget what it means

b.      In case anyone else ever has to work with your code

  1. Constants
    1. Declare a variable as final.

                                                               i.      final int FIELD_GOAL=3;

    1. Why?

                                                               i.      Should use Constants wherever you have a ‘magic number’ in code.

1.      Say, we had a complicated football program, and field goal is referenced a bunch of places in it.

2.      Now, say the rules change, so a field goal is worth four points.

3.      If we use a constant, we just have to change the value once, when we declare the constant.

4.      If we were using 3, we would have to search our program for everywhere that uses 3 like that and change it.

    1. Java will actually let you declare the final variable, and store the value later.  Usually not good practice, though.
  1. HW – 2.5-end of 2