110 likes | 211 Views
CSC 110AA Introduction to Computer Science for Majors - Spring 2003. Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators. Review from Last Week. CSC 110AA Introduction to Computer Science for Majors - Spring 2003. Assignment 3 Review on website
E N D
CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators
Review from Last Week Assignment 3>>
CSC 110AA Introduction to Computer Science for Majors - Spring 2003 • Assignment 3 • Review on website • http://gecko.gc.maricopa.edu/~jsbattis/assignment3CSC110.htm Naming constants as FINAL>>
Defining Named Constants public—no restrictions on where this name can be used static—must be included, but explanation has to wait final—the program is not allowed to change the value • The remainder of the definition is similar to a variable declaration and gives the type, name, and initial value. • A declaration like this is usually at the beginning of the file and is not inside the main method definition. public static final double PI = 3.14159; Example>>
FINAL MODIFIER EXAMPLE public class Average3 { public static final int DIVISOR = 3; public static void main(String[] args) { …….. average = average/DIVISOR; ………. } } Mini exercise>>
FIRST THINGS FIRST • Create folder TonightsWork • Include SavitchIn.class • Start with empty “main” class Coding>>
Try to change RATE and observe error messages public class Junk { public static final double RATE = 2.765; public static void main(String[] args) { RATE = 4.768; System.out.println(RATE); } } Tonight's lecture>>
CSC 110AA Lecture: Type Casting Class lab type casting>>
Class Lab Type Casting • Create several variables in a program with different types (i.e. int and double) • Perform several mathematical calculations on the numbers (i.e. multiplication, division) and output the results • Type cast results and output the numbers • Observe error messages • Note loss of precision • What happens with different casts on same expression? Code>>
public class TypeCast { public static void main(String[] args) { int x = 4; int y = 7; int z = 22; double a = 14.27; double b = 8.88; double c = 8.67988; int intAnswer; double doubleAnswer; intAnswer = (int)(a/c); System.out.println(intAnswer); doubleAnswer = x/c; System.out.println(doubleAnswer); } }//Type cast a character>>
Class Lab Type Casting Characters public class TypeCastCharacter { public static void main(String[] args) { char letter = ‘z’; int letterValue; letterValue = (int)letter System.out.println(“The letter is “ + letter); System.out.println(“The integer value is “ + letterValue); } } END