# CS 201 Lecture 3:

W
Shared by:
Categories
Tags
-
Stats
views:
7
posted:
11/22/2011
language:
English
pages:
38
Document Sample

```							CS 201 Lecture 3:

John Hurley
Spring 2011
Cal State LA
Lab 2
 Make sure to use constants for the length of the pool and the
conversion factor.
 Don‟t just add 22.85 each time through the loop; do the
calculation!
Casting
       Casting converts data types
       You should rarely have to do this at this point
       For primitive types, the syntax looks like this:
       int x = (int) 5.1;
       Casting a floating point type to an integer truncates, doesn‟t
round!
       int x = (int) 1.6;
     sets x to 1, not 2!

4
Casting
      Casting the value back to the original type does not restore lost data, so if you need
the original value, go back to the original variable
public class Caster{
public static void main(String[] args){
double x = 1.6;
int y = (int) x;
System.out.println(x);
System.out.println(y);
System.out.println((double) y);
System.out.println(x);
}
}
outputs:
1.6
1
1.0
1.6

5
Constants

 Use all caps for constant names
 Use final keyword
 final double PI = 3.14159;
 …
 double circumference = 2 * PI * radius;
 See example

6
Naming Conventions
 Choose meaningful and descriptive names.
 Variables and method names:
 Use lowercase. If the name consists of several words,
concatenate all in one, use lowercase for the first word, and
capitalize the first letter of each subsequent word in the name.
For example, the variables radius and area, and the
method computeArea.

7
Naming Conventions, cont.
 Class names:
 Capitalize the first letter of each word in the
name. For example, the class name
ComputeArea.

 Constants:
 Capitalize all letters in constants, and use
underscores to connect words. For example, the
constant PI and MAX_VALUE

8
Part II: New Material
 So far, it has been easy to test our programs, since there is
only one sequence the computer can follow:
public class Power{
public static void main(String[] args){
for(int power = 0; power <= 10; power++){
System.out.println("2 ^ " + power + " = " + answer);
}
}
}
 Soon, we will start writing programs that take user input
 Users are human beings
 Program execution becomes more complex when unpredictable
human beings intervene
 Make sure to test thoroughly
 We have it easy. Testing involves saving, compiling, and running
on a machine that is at our fingertips
 It hasn‟t always been that way.
Escape Sequences
• Some characters will cause confusion in output
• What will happen if we try to execute
this:
•System.out.println(““Hi,Mom””);
• Others are used to control the format of
output
•Use escape characters for these cases

18
Escape Sequences
Description    Escape Sequence
Backspace      \b
Tab            \t
Linefeed       \n
Carriage return \r
(think of a typewriter)
Backslash      \\
Single Quote   \'

Double Quote    \"
19
Escape Sequences
public class Escape{
public static void main(String[] args){
System.out.println("A\tB\rB\tC
D\nDD\b E\\F \'Hi, Mom\' \"Hi, Mom\"");
}
}

20
More Fun With Data Types
 boolean
 Value that is either true or false
 inequalities and tests in loop statements evaluate to boolean
values
 1 < 2 is true, so
 2 < 1 is false
 2 == 2 is true

int i = 2;
System.out.println(i + " < " + 5 + "?" + (i < 5));

prints “true”
21
More Fun With Data Types
public class BooleanDemo{
public static void main(String[] args){
for(int i = 0; i < 10; i++) {
System.out.println(i + " < " + 5 + "? " + (i < 5));
System.out.println(i + " == " + 5 + "? " + (i == 5));
System.out.println(i + " > " + 5 + "? " + (i > 5) + "\n");
}

System.out.println(" (1 == 1) == (2 == 1)? " + ((1 == 1) == (2 == 1)));
System.out.println(" (1 < 2) == (2 < 3)? " + ((1 < 2) == (2 < 3)));   }

}

22
More Fun With Data Types
 Declare a boolean variable the same way you declare an int or floating point
type

public class BooleanDemo2{
public static void main(String[] args){
boolean lessThanFive;
for(int i = 0; i < 10; i++) {
lessThanFive = i < 5;
System.out.println(i + " < " + 5 + "? " + lessThanFive);
}
}
}

23
More Fun With Data Types
 char
 One character
 Set value using single quotes:
 char myChar = „A‟;
 „A‟ is not the same as „a‟

24
More Fun With Data Types
 String
 Sequence of zero or more characters.
 Note the capital S
 String is a class; we will learn what that means later.
 Set values using double quotes:
 String myString = “Get your own String, This one is mine.”
 “” is called a null String or empty String.

25
More Fun With Data Types
public class CharStringDemo{
public static void main(String[] args){
char myChar = 'a';
String myString = "John Hurley";
System.out.println(myChar);
System.out.println(myString);
}
}

26
String Concatenation
// Three strings are concatenated
String message = "Welcome " + "to " + "Java";

// String Supplement is concatenated with character B
String s1 = "Supplement" + 'B'; // s1 becomes SupplementB

To concatenate to existing String, create a new String and use
concat():
String myString = “Good”;
String myOtherString = myString.concat(“ Morning”);

27
Character Codes
 Computers store information in bits
 There are several code systems to assign numeric values to
characters
Character Codes
 ASCII
 developed in the early 1960s, when memory was much more
expensive than it is now
 128 (27) possible characters
 The first 33 are control characters, originally used to control
teletypes
Character Codes
 Unicode
 Used in Java
 Superset of ASCII
 65536 values
 The first 128 are the same as ASCII
 Adds other alphabets, mathematical symbols,etc.
Character Codes
public class ASCIIUnicodeDemo{
public static void main(String[] args){
for(int charNum = 0; charNum < 128; charNum++){
System.out.println("Character " + charNum + " is
" + (char) charNum);
}
}
}

 Why do we get a blank line after 10 and before 11?
Character Codes
 chars can be compared using <, =, > operators
 Apply the math operators to the Unicode values
 It‟s probably obvious that „a‟ < „b‟
 Not obvious that
 „A‟ < „a‟
 „0‟ < „a‟
 „/‟ < „a‟
 8 < „5‟ !!
Character Codes
public class UnicodeCompare{
public static void main(String[] args){
boolean isLessThan = false;
for(int charNum = 0; charNum < 128; charNum++){
char currChar = (char) charNum;
System.out.println("'" + currChar + "': " + " less than 'A'? "
+ (currChar < 'A'));

}
System.out.println("And by the way, it is " + (8 < '5') + " that 8 <
'5'");
}
}
Programming Errors
 Syntax Errors
 Detected by the compiler
 Runtime Errors
 Causes the program to abort
 Logic Errors
 Produces incorrect result

34
Errors
public class Errors{
public static void main(String[] args){
i += 1
System.out.println(i);
i/= 0;
}
}

35
Errors
public class Errors{
public static void main(String[] args){
int y = 10;
for(int x = 0; x <=10; x++){
y -= 1;
System.out.println(10 / y);

}
}
}

36
Logic Errors

public class PowerError{
public static void main(String[] args){
for(int power = 0; power <= 10; power++){
System.out.println("2 ^ " + power + " = " +

}
}
}
37
Quiz
 One question:

 What does the Java virtual machine do, and why do
we separate it from Java compilers?
 Answer in one well-constructed paragraph. Your
paragraph should have a topic sentence, a concluding
sentence, and about three sentences in between to
 Turn in on CSNS

```
Related docs
Other docs by 8z6a6gM
Manuale tecnico