Lecture 5, CS 302-7 and 8, February 1

 

  1. Reminders
    1. Exam conflicts – online form – by NEXT FRIDAY 10 pm.

                                                               i.      Can update/view after creating

                                                             ii.      Make sure to use your cs login/password (not wisc login)

    1. Codelab

                                                               i.      Do you have to do it?  No

                                                             ii.      Useful and extra credit

1.      Best way to learn is by doing

2.      Register with wisc email and last/first name (so we know who you are)

    1. Download page

                                                               i.      Now active

                                                             ii.      Use versions of eclipse and java posted

                                                            iii.      If using a Mac on Leopard (or earlier) make sure to follow directions so that you have the right version of Java.

    1. Office hours – 4-5 M, 11-12 W, 1-2 Th

                                                               i.      In general: If my door’s open, come in!

  1. Review
    1. Variables
    2. Variable naming
    3. Constants
  2. Constants
    1. Declare a variable as final.

                                                               i.      final int FIELD_GOAL=3;

    1. Naming

                                                               i.      All caps

                                                             ii.      _ to separate words

    1. Why constants?

                                                               i.      Should use Constants wherever you have a ‘magic number’ in code. – like before with field goals

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 (initialize) later.  Usually not good practice, though.
  1. Comments
    1. Why use comments?

                                                               i.      Pretty much the same reason we use descriptive variable names

    1. Types of comments in Java:

                                                               i.      //

                                                             ii.      /*   */

                                                            iii.      /** */

1.      For documentation tools like JavaDoc

    1. When should you use comments?

                                                               i.      When something is unclear or confusing

                                                             ii.      In general, use ‘as needed’.  Don’t need to comment every line, but reasons for initial variable values, complicated equations etc are good places.

                                                            iii.      Will it slow down your program to use comments?

1.      No, because they are not compiled into the Java Byte Code

    1. Note – I won’t use too many comments in the code I write in class due to time constraints
  1. Variable assignments
    1. Think of ‘=’ as ‘set to’
    2. Variables store only one value

                                                               i.      You can overwrite this value

    1. ++, --

                                                               i.      C++ J

    1. Note – can only Declare variables once (can’t go int counter; int counter;) – identifiers are unique
  1. Identifiers and Reserved Words
    1. Identifier – aka variable name, etc.

                                                               i.      Variable, class, method names, etc

    1. Reserved words - Words that carry special significance in Java – for instance, double or Class
    2. Identifiers cannot have the same name as reserved words
  1. Input
    1. I/O (input/output)

                                                               i.      Input – keyboard, mouse, etc

                                                             ii.      Output – monitor, printer, etc

1.      eg System.out.println();

    1. How to get input from a user (step-by-step)

                                                               i.      Step 1 – import java.util.scanner;

1.      Or, java.util.*;

2.      This goes before any other code in your program

                                                             ii.      Step 2 – declare scanner object

1.      Scanner <name>=new Scanner(System.in);

2.      Example: Scanner in=new Scanner(System.in);

                                                            iii.      Step 3  - prompt user.  Describe what they should enter

1.      Use print instead of println so it’s on same line

                                                           iv.      Step 4 – in.nextInt(), in.nextDouble(),in.next()

1.      Store the value returned by one of the above in a variable.

2.      Example: int a=in.nextInt();

3.      Note – this prints a new line for us

                                                             v.      Example – Birthday.java

  1. Arithmetic
    1. +, - ,* , /
    2. Expression

                                                               i.      Note – we’ve been doing this without really thinking about it

                                                             ii.      Operand

1.      things we are acting on

2.      variable

3.      object of operator

                                                            iii.      Operator

1.      +,-, etc

    1. % operator – modulo

                                                               i.      Like a remainder

                                                             ii.      7%4=3 because, if you divide 7 by 4, you have 3 left over.

  1. HW
    1. Finish ch 2