Winter 2019 CMPE212 4/17/2019 CMPE212 – Reminders

Slides:



Advertisements
Similar presentations
Today Quiz solutions are posted on the Grading page. Assignment 1 due today, 7pm. Arrays as Pointers, Cont. Aliasing & Passing by Reference null Winter.
Advertisements

Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Today Quiz solutions are posted on the Grading page. Assignment 2 is posted. Due the first Friday after Reading Week. All about null Start File Input/Output.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
Object Oriented Programming
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Introduction to Exceptions in Java CS201, SW Development Methods.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Today Review passing by reference and pointers. null pointers. What is an Object? Winter 2016CMPE212 - Prof. McLeod1.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Today… StringTokenizer class. Method Overloading. Catching Exceptions (and what they are!). Start Pointers and Aliasing. Winter 2016CMPE212 - Prof. McLeod1.
Generics, Exceptions and Undo Command
Last Time Math class Style and Documentation Loops And Exercises!
Java Programming Language
CS102 – Exceptions David Davenport Latest: May 2015
CSE 501N Fall ’09 17: Exception Handling
Winter 2018 CMPE212 9/18/2018 CMPE212 – Stuff…
Fall 2017 CISC124 9/18/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff…
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
CISC/CMPE320 - Prof. McLeod
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Exceptions 10-Nov-18.
Exception Handling Chapter 9.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Java Programming Language
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Exception Handling Chapter 9 Edited by JJ.
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
CISC124 Labs start this week in JEFF 155. Fall 2018
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
CMPE212 – Reminders The other four assignments are now posted.
CISC101 Reminders All assignments are now posted.
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 sample solution is posted.
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 3 due next Friday.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
CMPE212 – Reminders Course Web Site:
Last Time Some discussion of program “efficiency”. Arrays – 1D and 2D
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Winter 2019 CMPE212 5/3/2019 CMPE212 – Reminders
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
Exceptions 10-May-19.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
CISC101 Reminders Assignment 3 due today.
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Java Basics Exception Handling.
CMSC 202 Exceptions 2nd Lecture.
Exception Handling.
Presentation transcript:

Winter 2019 CMPE212 4/17/2019 CMPE212 – Reminders First quiz in the lab this week, Wednesday section still to write. More info in last Tuesday’s lecture. You are in two groups in onQ – your Lab and your Grader. Assignment 1 due this Friday. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Today StringTokenizer Demo Method Overloading Winter 2019 CMPE212 4/17/2019 Today StringTokenizer Demo Method Overloading Exceptions and Catching Them Objects in Memory – a Model Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Tokenizing Demo See SystemPropertiesDemo.java Winter 2019 CMPE212 4/17/2019 Tokenizing Demo See SystemPropertiesDemo.java Includes some old-fashioned string parsing code that uses String class methods only. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Fall 2013 CMPE212 Method Overloading A method can have the same name in many different classes (println(), for example). “Overloading” is when a method name is used more than once in method declarations within the same class. (also like println()…) The rule is that no two methods with the same name within a class can have the same number and/or types of parameters in the method declarations. (The “NOT” rule.) Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Method Overloading - Cont. Why bother? – Convenience! Java does not have default arguments. Allows the user to call a method without requiring him to supply values for all the parameters. One method name can be used with many different types and combinations of parameters. Allows the programmer to keep an old method definition in the class for “backwards compatibility”. Winter 2019 CMPE212 - Prof. McLeod

Method Overloading - Cont. How does it work? Java looks through all methods until the parameter types match with the list of arguments supplied by the user. If none match, Java tries to cast types in order to get a match. (Only “widening” casting like int to double, however.) Winter 2019 CMPE212 - Prof. McLeod

Method Overloading - Cont. Final notes on overloading: You can have as many overloaded method definitions as you want, as long as they are differentiated by the type and/or number of the parameters listed in the definition. Do not change the return type – that is tacky! See the getInt() and getDouble() overloaded methods in the Exercise 1 IOHelper class for example. Winter 2019 CMPE212 - Prof. McLeod

Fall 2013 CMPE212 Exceptions How can a method indicate that it is unable to return what it is supposed to return? How can a method deliver details about the error condition? How can you prevent the instantiation of an Object? The limitation of only returning a single “thing” means that you either designate error values for the “thing” or you have some other way to return the indication of an error. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Exceptions - Cont. The designers of Java followed conventions used by many other OOP languages - they allowed for another way to get something out of a method. However, an exception is thrown, not returned. Exceptions are Objects (big surprise!). When an error condition is encountered, a method can throw an instance of a pre-defined exception Object. A method can throw several exceptions, one for each possible kind of error condition. Winter 2019 CMPE212 - Prof. McLeod

Exceptions - Cont., Propagation If a method throws an exception, then that method is immediately halted and there is no need for any return value, even if the method is non-void. The invoking method then receives the exception – if it does not catch it, then it goes to the next invoking method – all the way to main, if necessary. Called “cascading”. Finally, if main does not catch the exception, your program crashes and a message is sent to the console window. Winter 2019 CMPE212 - Prof. McLeod

Exceptions - Cont., Message Handler How does an Exception Object contain information about the error condition? The type of the Object: IOException NumberFormatException FileNotFoundException ArrayIndexOutOfBoundsException … So, the method should throw a relevant exception object. Turns out that exceptions can also carry a String message. Winter 2019 CMPE212 - Prof. McLeod

Bearer of "Bad Tidings" Exceptions – Cont. An exception is just a: The exception itself cannot do anything about the problem. But it can act to stop your program! Winter 2019 CMPE212 - Prof. McLeod

Exceptions - Cont., Catching This is done with a “try/catch” block (see the next slide for the syntax). The compiler will force you to use try/catch blocks when you invoke methods that throw exceptions. (Eclipse can help build a try/catch: Select the code to be surrounded. Right click and choose “Surround with”, then “Try/catch block. The wizard will automatically choose all the possible exceptions.) Winter 2019 CMPE212 - Prof. McLeod

Exceptions – Cont. Syntax of a “try-catch block”: try { // block of statements that might // generate an exception } catch (exception_type identifer) { // block of statements }[ catch (exception_type identifer) { … }][ finally { }] Winter 2019 CMPE212 - Prof. McLeod

Exceptions – Cont. You must have at least one “catch block” after the “try block” (otherwise the try block would be useless!) You can have many catch blocks, one for each exception you are trying to catch. The code in the “finally” block is always executed, whether an exception is thrown, caught, or not. Winter 2019 CMPE212 - Prof. McLeod

Checked vs Unchecked Exceptions Checked exceptions must either be caught in a method or thrown from that method (using the throws clause in the method header). Examples: IOException, FileNotFoundException, ClassNotFoundException You should not catch an un-checked exception or an Error. Examples: OutOfMemoryError, StackOverflowError, VirtualMachineError Winter 2019 CMPE212 - Prof. McLeod

Checked vs Unchecked Exceptions, Cont. The compiler will ensure that checked exceptions are handled properly. Unchecked exceptions occur only at runtime and the compiler does not care about them. Unchecked exceptions are all sub-classes of java.lang.Error Winter 2019 CMPE212 - Prof. McLeod

Try With Resources This new syntax is very useful with Java 7 & newer versions’ improved file I/O syntax. More on this topic in exercise 4. Winter 2019 CMPE212 - Prof. McLeod

Try With Resources – Cont. Syntax of a “try-with-resources block”: try (instantiation; instantiation; …) { // other statements that might // generate an exception }[ catch (exception_type identifer) { // block of statements }][ catch (exception_type identifer) { … }][ finally { }] Winter 2019 CMPE212 - Prof. McLeod

Try With Resources – Cont. The instantiation(s) inside the set of ( ) immediately after the try keyword are declared resources that must be local to the try/catch block. Note that there is no ; at the end of the list and that there does not have to be any catch blocks. These resources must all implement the AutoCloseable interface, which means that the try block can close the resource when it is finished. Resources will be closed whether or not an exception is thrown because their scope is forced to be in the try block only. As a result, all use of the resource must also take place in the try block. Winter 2019 CMPE212 - Prof. McLeod

One-Dimensional Arrays - Declaration Fall 2013 CMPE212 One-Dimensional Arrays - Declaration As we have already seen - To create an array to hold 10 integers: int[] testArray = new int[10]; testArray now points to an area of memory that holds locations for 10 integers. It also points to one location that holds testArray.length which is an attribute of the array, that is equal to the number of elements. Arrays are Objects in Java. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Aside – Java 10 Array Declaration This: int[] testArray = new int[10]; Can now be simplified to: var testArray = new int[10]; Reduces redundant declarations in the instantiation statement. Winter 2019 CMPE212 - Prof. McLeod

Modelling Pointers Use the array as an example to help model the behaviour of a pointer. Note that an array is an example of a mutable object – object contents can be changed by accessing the object’s contents through the pointer. Object design or class design determines mutability. Winter 2019 CMPE212 - Prof. McLeod

One-Dimensional Arrays - Declaration, Cont. 0480ff 0180ff … int[] testArray 0480ff As a “pointer”, testArray points to an area of memory that contains a series of int values as well as the attribute length. 10 .length Winter 2019 CMPE212 - Prof. McLeod

One-Dimensional Arrays - Declaration, Cont. The java statement above can be split into two: int[] testArray; testArray = new int[10]; The first statement creates a variable of type int[] - that is to say that it will be an array of int’s. The variable, testArray is now an object of type int[] that contains an “object reference” or a pointer. The object reference is null after the first statement. Winter 2019 CMPE212 - Prof. McLeod

A “Null Pointer” After int[] testArray; int[] testArray null 0180ff … Winter 2019 CMPE212 - Prof. McLeod

After the new Keyword After testArray = new int[10]; int[] testArray 0480ff 0180ff … int[] testArray 0480ff 10 Winter 2019 CMPE212 - Prof. McLeod

One-Dimensional Arrays - Cont. The array indices allow mutability. Memory locations are calculated by offsets from the base address: testArray[0] testArray[1] testArray[2] testArray[3] testArray[4] testArray[5] testArray[6] testArray[7] testArray[8] testArray[9] testArray.length 10 Array indices Winter 2019 CMPE212 - Prof. McLeod

Multi-Dimensional Arrays Consider: int[][] exArray = new int[3][5]; 1002fc int[][] exArray 0480ff int[] exArray[0] 1002fc exArray[1] 1010fc exArray[2] 1201ab 1010fc 1201ab Winter 2019 CMPE212 - Prof. McLeod

Multi-Dimensional Arrays - Cont. So exArray points to three one dimensional arrays: exArray[0] exArray[1] exArray[2] Each of these arrays has the same length: exArray[2].length // returns 5 Yes, you can refer to these arrays in code, just like this. Winter 2019 CMPE212 - Prof. McLeod

Multi-Dimensional Arrays - Cont. int[][] twoDArray = new int[10][20]; The above is equivalent to: int[][] twoDArray; twoDArray = new int[10][]; for (int i = 0; i < 10; i++) twoDArray[i] = new int[20]; As shown above: twoDArray.length // gives 10 twoDArray[0].length // gives 20 Winter 2019 CMPE212 - Prof. McLeod

Multi-Dimensional Arrays - Cont. “Ragged Arrays” are not “square”, and are legal in Java: int[][] raggedArray = new int[5][]; raggedArray[0] = new int[5]; raggedArray[1] = new int[3]; raggedArray[2] = new int[2]; raggedArray[3] = new int[8]; raggedArray[4] = new int[12]; Winter 2019 CMPE212 - Prof. McLeod

Multi-Dimensional Arrays - Cont. Array initializer for two-D array, for example: int[][] twoDArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; System.out.println(twoDArray.length); // 4 System.out.println(twoDArray[0].length); // 3 Winter 2019 CMPE212 - Prof. McLeod

Aliasing Objects - Array Example int[] first = {1, 2, 3, 4, 5}; int[] second = {10, 20, 30, 40, 50, 60, 70}; 1 2 3 4 5 10 20 30 40 50 60 70 0480ff 0960ff int[] first 0480ff int[] second 0960ff .length 5 .length 7 Winter 2019 CMPE212 - Prof. McLeod

Aliasing Objects - Array Example, Cont. second = first; // Aliasing! 1 2 3 4 5 10 20 30 40 50 60 70 0480ff 0960ff int[] first 0480ff int[] second 0480ff .length 5 .length 7 Winter 2019 CMPE212 - Prof. McLeod

Aliasing Objects - Array Example, Cont. // after garbage collection Poof! 1 2 3 4 5 0480ff int[] first 0480ff int[] second 0480ff .length 5 Winter 2019 CMPE212 - Prof. McLeod

Aside – “Garbage Collection” in Java Some computer programming languages require you to indicate when you are done with variables so the memory they are occupying can be released back to the OS. Called “Garbage Collection”. (Fortunately!) Java has an automatic Garbage Collection system: Variables are garbage collected once you move outside their scope. Object contents are garbage collected when there are no pointers pointing to the contents. Winter 2019 CMPE212 - Prof. McLeod

Aliasing Objects - Array Example, Cont. first[4] = 500; // second[4] is also 500 1 2 3 4 500 0480ff int[] first 0480ff int[] second 0480ff .length 5 Winter 2019 CMPE212 - Prof. McLeod

Aliasing Objects - Array Example, Cont. Fall 2013 CMPE212 Aliasing Objects - Array Example, Cont. So, setting one array to equal another as in: array1 = array2; sets array1 to point to the same data memory location that was (and still is) pointed to by array2. Now, changing the value of an element in array2 will change that same element in array1, or visa-versa - this makes sense since both array Objects point to the same set of data values in memory! Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Aliasing Objects Passing an Object into a method results in the method’s parameter being aliased to the Object passed. Called “Passing by Reference”! Winter 2019 CMPE212 - Prof. McLeod