Ch6 - Rectangle box = new Rectangle() ; i = box.outcode(i,...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
/** Illustrates enumeration type in Java */ import java.awt.* ; import java.util.* ; enum WeekDays {M, T, W, R, F} enum Days {M, T, W, R, F, S, Z} public class Ch6 { public static void main(String[] args) { //enum Days {M, T, W, R, F, S, Z} enum types must not be local Days today ; WeekDays meeting ; //today = F ; // cannot find symbol today = Days.F ; //meeting = today ; //incompatible types meeting = WeekDays.F ; //if (meeting == today) {} // incomparable types //today ++ ; // operator cannot be applied to Days //Testing type checking int i = 3 ; double d = 3.5 ; float f = 3.5F ; d = i ; //i = d ; possible loss of precision //f = d ; possible loss of precision System.out.println(i + f) ; d = Math.sqrt(i) ; //works even though defined with double arg. doInt(i) ; //doInt(f) ; // cannot be applied to (float) doFloat(f) ; doFloat(i) ; //Unsuccessfully trying to make a type error that goes undetected
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Rectangle box = new Rectangle() ; i = box.outcode(i, i) ; Student s = new Student() ; Employee e = new Employee() ; Person p = s ; //e = (Employee) p ; //compiles but ClassCastException Person people = new Employee[5] ; //covariance of arrays . . compat. //people[0] = new Student() ; //compiles but ArrayStoreException Employee employees = (Employee) people ; //Illustrating that implicit coercion decreases reliability System.out.println("sum of 4 and 5 is " + 4 + 5) ; System.out.println("f = " + f) ; f = f + i/(2*i) ; System.out.println("f = " + f) ; } public static void doInt(int i) { } public static void doFloat(float f) { } } class Person { String name ; int height ; } class Employee extends Person { double wage ; } class Student extends Person { double gpa ; }...
View Full Document

This note was uploaded on 09/29/2010 for the course COMPUTER S cps615 taught by Professor Pro during the Spring '10 term at Randolph College.

Page1 / 2

Ch6 - Rectangle box = new Rectangle() ; i = box.outcode(i,...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online