Lecture 5, CS 302-6, September 14

 

  1. Review
    1. Correction from last time – int is 4 bytes, long is 8 bytes
    2. Variables – parking lot analogy
    3. Variable naming
    4. Identifiers and reserved words
    5. Comments
  2. Variable assignment
    1. Variables store only one value

int number=10;

number=6;

-> number equals six.  The ten is forgotten

    1. ++, --, +=

                                                               i.      C++ - weak CS joke

    1. Note – can only Declare variables once (can’t go int counter; int counter;) – variable names (identifiers) are unique
  1. Constants
    1. Declare a variable as final.

                                                               i.      final int FIELD_GOAL=3;

    1. Naming

                                                               i.      All caps

                                                             ii.      _ to separate words

    1. Why?

                                                               i.      Same reasons we use comments – to indicate something about this value.

                                                             ii.      You should use Constants wherever you have a ‘magic number’ in code. – a value that you use many times, but that is not going to change.

    1. Java will actually let you declare the final variable, and store the value later.  Usually not good practice, though.

final int NUMBER;

NUMBER=10;

  1. Input
    1. I/O – Input/Output

                                                               i.      For humans:

1.      Input - senses (sight, hearing, etc)

2.      Output - speech, writing, etc.

                                                             ii.      On a computer:

1.      Input – keyboard, mouse, etc

2.      Output – monitor, printer, etc

                                                            iii.      In Java:

1.      Input – Scanner

2.      Output – System.out.println(), etc

    1. Step 1 – import java.util.scanner;  //This goes at the very top of the program, even above the class declaration

                                                               i.      Or, java.util.*;

                                                             ii.      Scanner is one class in the util Package

    1. Step 2 – declare scanner object

                                                               i.      Scanner in=new Scanner(System.in);

    1. Step 3 - prompt user for input, using System.out.print().

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

    1. Step 4 – Based upon what kind of data we want, use:

                                                               i.      in.nextInt()  //Integer

                                                             ii.      in.nextDouble()  //Floating point

                                                            iii.      in.next()   //String

                                                           iv.      Note – this prints a new line for us

  1. Arithmetic
    1. +, - ,* , /         //Addition, subtraction, multiplication, division

                                                               i.      For multiplication and division, why no x, dot, [blank], division bar, division sign, etc?  …because there’s no button for them on the keyboard

    1. Expressions

                                                               i.      Basically, just equations.  1+1=2 is an expression.

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

                                                            iii.      Operand

1.      one of the inputs – 1, 2, 3, etc.

2.      Can also be a variable – eg intValue

                                                           iv.      Operator

1.      +, -, =, etc

    1. Parenthesis

                                                               i.      For specifying the order that expressions are calculated

    1. Division – integers and floating point numbers

                                                               i.      Int/int=int

                                                             ii.      Int/double=double

                                                            iii.      Double/int=double

                                                           iv.      etc

                                                             v.      Make sure you’re calculating what you expect – not always intuitive

    1. Converting floating point to integers

                                                               i.      Truncation vs Rounding (Math.round)

1.      Truncation: (int)(7.0/4.0)=1

2.      Rounding: Math.round(7.0/4.0)=2

    1. Remainder – modulo operator - % (note that this does not mean percent)

                                                               i.      You can use integer division to get the whole part, and % to get remainder

                                                             ii.      Example:

1.      7.0/4.0=1.75

2.      7/4=1

3.      7%4=3

4.      Therefore, 7/4 = 1 remainder 3, or 1+3/4

    1. Powers, roots – Math.pow(a,b), Math.sqrt(a)

                                                               i.      Squaring - Might be easier just to do a*a in code

                                                             ii.      Can use Math.pow to do cube root, etc, or negative powers [ 3^-2=1/(3^2) ]

    1. Other useful Math functions:

                                                               i.      Math.sin

                                                             ii.      Math.cos

                                                            iii.      Math.tan

                                                           iv.      Math.log10

                                                             v.      Math.abs //Absolute value

  1. HW
    1. Read 3.1,3.2