Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators.

Similar presentations


Presentation on theme: "1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators."— Presentation transcript:

1 1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators

2 Assignment 3>> Review from Last Week

3 Naming constants as FINAL>>3 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Assignment 3 Review on website http://gecko.gc.maricopa.edu/~jsbattis/assignment3CSC110.htm

4 Example>> 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;

5 Mini exercise>> FINAL MODIFIER EXAMPLE public class Average3 { public static final int DIVISOR = 3; public static void main(String[] args) { …….. average = average/DIVISOR; ………. }

6 Coding>> FIRST THINGS FIRST 1.Create folder TonightsWork 2.Include SavitchIn.class 3.Start with empty “main” class

7 Tonight's lecture>>7 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); }

8 Class lab type casting>>8 CSC 110AA Lecture: Type Casting

9 Code>>9 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?

10 10 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>>

11 END11 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); } Class Lab Type Casting Characters


Download ppt "1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators."

Similar presentations


Ads by Google