Lecture 5, CS 302-6, September 14
int number=10;
number=6;
-> number equals six. The ten is forgotten
i. C++ - weak CS joke
i. final int FIELD_GOAL=3;
i. All caps
ii. _ to separate words
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.
final int NUMBER;
NUMBER=10;
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
i. Or, java.util.*;
ii. Scanner is one class in the util Package
i. Scanner in=new Scanner(System.in);
i. Use print instead of println so it’s on same line
i. in.nextInt() //Integer
ii. in.nextDouble() //Floating point
iii. in.next() //String
iv. Note – this prints a new line for us
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
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
i. For specifying the order that expressions are calculated
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
i. Truncation vs Rounding (Math.round)
1. Truncation: (int)(7.0/4.0)=1
2. Rounding: Math.round(7.0/4.0)=2
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
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) ]
i.
Math.sin
ii.
Math.cos
iii. Math.tan
iv. Math.log10
v. Math.abs //Absolute value