1 / 11

CSC 110AA Introduction to Computer Science for Majors - Spring 2003

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

emmly
Download Presentation

CSC 110AA Introduction to Computer Science for Majors - Spring 2003

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Review from Last Week Assignment 3>>

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

  4. 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>>

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

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

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

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

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

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

More Related