Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 91Generics, Enums, and Exceptions2/3/20092Opening Discussion■Do you have any questions about the quiz?■Let's look at solutions to the interclass problem.■Will we regularly run out of time before finishing slides?■What is the equivalent of scanf in Java?■Book corrections appreciated.■Do you have any questions about the reading?■Do you have any questions about the assignment?3Making General Typesafe■The problem with using the Object type for general polymorphism is that it many different type checks have to be done at runtime and you lose static type safety.■Using generics we can take our general function interface and make it static typesafe.■How can you combine these more general types of functions?4Enums■C had enums. What were they supposed to do? What was the problem with them?■Java includes enums as well. They serve the same goals, but lack the pitfalls.■Java enum syntax can get quite complex, but the basic form is simple and very similar to C.5Error Handling■How did you handle errors in C? (Consider the fopen function.)■What are some problems with this method?6Exceptions■Error handling in Java is done with exceptions, not return values or flags.■Normal exceptions can't be ignored and they don't propagate. Runtime exceptions don't propagate.7Syntax■For anything that isn't a RuntimeException you have to include handling code. For RuntimeExceptions it is optional.■If you want to deal with a possible exception in the current method do this:try {➔statements} catch(ExceptionType1 e) {➔statements} [catch(ExceptionType2 e) { ...} ...]■If this method can't handle it you add a throws clause to the method and it will go up to the calling method.Type name(args) throws ExType[,...] {...}8Additional Information■Exceptions also have the advantage that they can provide additional information.■Stack trace.■Informative message.■You can create your own exception classes. Strive to have them provide sufficient information for debugging.9Minute Essay■Do you have any questions about Java as a language? We are now moving from the language to libraries and problem solving.■Interclass Problem – Write a program that uses a Scanner to read the contents of a file. The hint is that you will have a line something like Scanner sc=new Scanner(new File(“filename.txt”));. You can decide what to do with the contents. Be creative. You might consider methods like
View Full Document