Visibility, Static, and Exception Handling. June 19, 2017

Similar documents
Tutorial 8 Date: 15/04/2014

CS 61B Discussion 5: Inheritance II Fall 2014

Classes, Objects, and OOP in Java. June 16, 2017

Exceptions - Example. Exceptions - Example

What can go wrong in a Java program while running?

Exceptions. Errors and Exceptions. Dealing with exceptions. What to do about errors and exceptions

STATIC, ABSTRACT, AND INTERFACE

CS159. Nathan Sprague

Errors and Exceptions

Chapter 3. Selections

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

16-Dec-10. Consider the following method:

CSC207 Week 3. Larry Zhang

CS 3 Introduction to Software Engineering. 3: Exceptions

1.Which four options describe the correct default values for array elements of the types indicated?

Programming II (CS300)

CISC 323 (Week 9) Design of a Weather Program & Java File I/O

Lecture 14 Summary 3/9/2009. By the end of this lecture, you will be able to differentiate between errors, exceptions, and runtime exceptions.

Programming II (CS300)

Lecture 4. Types, Memory, Exceptions

AP CS Unit 6: Inheritance Notes

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

Exception-Handling Overview

Simple Java Input/Output

Enums, Generics, Nested Types. June 23, 2017

Java Object Oriented Design. CSC207 Fall 2014

CIS265 - Spring Exam 2 First Name Last Name CSU#

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

Object Oriented Programming. Week 7 Part 1 Exceptions

CSC Java Programming, Fall Java Data Types and Control Constructs

CIS 120 Final Exam May 3, Name (printed): Pennkey (login id):

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Recitation: Loop Jul 7, 2008

COMP-202 Unit 9: Exceptions

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

Lecture 14: Exceptions 10:00 AM, Feb 26, 2018

Inheritance and Substitution גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

For more details on SUN Certifications, visit

Exceptions. Examples of code which shows the syntax and all that

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Need to store a list of shapes, each of which could be a circle, rectangle, or triangle

CMSC131. Exceptions and Exception Handling. When things go "wrong" in a program, what should happen.

CS 200 File Input and Output Jim Williams, PhD

To Think About. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

Exception Handling. General idea Checked vs. unchecked exceptions Semantics of... Example from text: DataAnalyzer.

Full file at Chapter 2 - Inheritance and Exception Handling

CS1150 Principles of Computer Science Boolean, Selection Statements

Exceptions and Error Handling

7. Java Input/Output. User Input/Console Output, File Input and Output (I/O)

Chapter 4 Defining Classes I

9. Java Errors and Exceptions

C# Programming for Developers Course Labs Contents

CS61B Lecture #12. Programming Contest: Coming up Saturday 5 October. See the contest announcement page, here.

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

Vendor: Oracle. Exam Code: 1Z Exam Name: Java SE 7 Programmer I. Version: Demo

10/22/2018 Programming Data Structures

Sri Vidya College of Engineering & Technology Question Bank

CSC207 Winter Section L5101 Paul Gries

CS61B Lecture #12. Today: Various odds and ends in support of abstraction.

Exception Handling Generics. Amit Gupta

Assignment 17 Deadline: Oct pm COSC211 CRN15376 Session 14 (Oct. 29)

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

SECTION-1 Q.1.Which two code fragments are most likely to cause a StackOverflowError? (Choose two.)

d. If a is false and b is false then the output is "ELSE" Answer?

Programming Languages and Techniques (CIS120e)

CSE 143 Java. Exceptions 1/25/

Correctness and Robustness

EXCEPTIONS. Fundamentals of Computer Science I

CS 200 Command-Line Arguments & Exceptions Jim Williams, PhD

Internal Classes and Exceptions

CS Introduction to Data Structures Tuesday, February 2, 2016

COMP-202 Unit 9: Exceptions

CS365 Midterm -- Spring 2019

Recreation. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

THE UNIVERSITY OF AUCKLAND

6. Java Errors and Exceptions

Recommendation: Play the game and attempt to answer the questions yourself without looking at the answers. You ll learn much less if you just look at

6. Java Errors and Exceptions. Errors, runtime-exceptions, checked-exceptions, exception handling, special case: resources

Exam Questions 1z0-853

Instructions. Grading Box 2. /10 4. /10 1. /10 3. /10 5. /10. Total /50

CS S-22 Exceptions 1. Running a web server, don t want one piece of bad data to bring the whole thing down

COP 3330 Final Exam Review

Mutating Object State and Implementing Equality

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Defensive Programming

CS 455 Midterm Exam 2 Fall 2015 [Bono] Nov. 10, 2015

Video 2.1. Arvind Bhusnurmath. Property of Penn Engineering, Arvind Bhusnurmath. SD1x-2 1

Introduction to Programming Using Java (98-388)

Error Handling. public int divide(double a, double b) { if (b==0) return -1; // error double result = a/b; return 0; // success }

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

Transcription:

Visibility, Static, and Exception Handling June 19, 2017

Reading Quiz

What is Printed? System.out.print("A"); try { System.out.print("B"); // SomeExceptionType is thrown } except (SomeExceptionType e1) { System.out.print("C"); } except (OtherExceptionType e2) { System.out.print("D"); A. "ABCDEF" B. "ABF" C. "ABCF" D. "ABEF" E. "ABCEF" } finally { System.out.print("E"); } System.out.print("F");

What is the Purpose of Exceptions? A. To indicate a performance issue B. To indicate an uncommon error has occurred, that the programmer should handle C. To provide additional information to the compiler, for optimization D. To provide the programmer an alternate control flow option

What is the Result? public class A { private String amethod() { return "Private"; } } A mya = new A(); System.out.print(myA.aMethod()); A. Won't compile B. MethodNotFoundException throw C. "Private" printed D. null printed

What does "Static" do? A. To indicate a value won't change during the program's execution B. To indicate a variable will change frequently during the program's execution C. To indicate that a method or variable is connected to the class, and not instances of the class D. To indicate that a method or variable is connected to the instance, and not the class itself

What is Printed? public class Parent { public String amethod() { return "Parent"; } } public class Child extends Parent { public String amethod() { return "Child"; } } A. "Parent" B. "Child" C. "ChildParent" D. "ParentChild" Child achild = new Child(); String result = achild.amethod(); System.out.print(result);

Done!

Housekeeping Debugging SSH / git password issues SSH keys and ~/.ssh/config Abbreviated Office Hours Today Homework 2

Homework 2 Animal - Beluga - Chameleon - Dog - GermanShepard - Mammal - Orca 1. iswarmblooded 2. islivingunderwater 3. isnamedaftereuropeancountry 4. canchangecolor 5. isblackandwhite - Reptile - ShibaInu - Snake - Whale

Class Visibility

Problem Classes represent data and functionality Some functionality is for "users" of code Some functionality is "internal" or "sensitive"

Credit Card Account Credit Account Charge Account

Credit Card Account Credit Account - Code to verify status of account (ping charge) - Code to send money to account Charge Account - Code to verify status of account (ping charge) - Code to take money to account

Credit Card Account Credit Account - Call "Verify Account" - Send money to account Charge Account - Call "Verify Account" - Take money from account Verify Account - Code to verify status of account (ping charge)

Credit Card Account Credit Account - Call "Verify Account" - Send money to account Charge Account - Call "Verify Account" - Take money from account Verify Account!!!!!!!!! - Code to verify status of account (ping charge)

Java's Solution Public Internal and external access Private Internal class access only Properties and Methods

CreditCardAccount.java -->

Credit Card Account Credit Account Charge Account Verify Account

Subclassing Credit Card Account Credit Account Visa Credit Card Account Charge Account Verify Account Credit Account

Subclassing Credit Card Account Credit Account Visa Credit Card Account Charge Account Verify Account Credit Account Subclass "Credit Account" cannot see "Verify Account"

Java's Solution Public Internal and external access Private Internal class access Protected Internal class and subclass access

CreditCardAccount.java -->

Discussion Other examples for public / private / protected methods or properties Security Shared Functionality When subclasses want to share internal functionality / data

"Static"

Problem Instances describe functionality and data of a type Where to put code and data shared between instances? Constants, aggregators, "Importing", etc

Constants Circle property: π method: Area method: Radius

Circle Circle property: π method: Area method: Radius property: π method: Area method: Radius Circle Circle property: π property: π property: π method: Area method: Area method: Area method: Radius method: Radiu

Circle Circle property: π method: Area method: Radius property: π method: Area method: Radius Circle Circle property: π property: π property: π method: Area method: Area method: Area method: Radius method: Radiu

Aggregation Circle Circle Circle Circle Circle property: property: π property: π property: π property: π π method: method: Area method: Area method: Area method: Area Area method: method: Radius method: Radius method: Radius method: Radius Radius We want to find the area of multiple circles We want to store that somewhere in our code Could create a "CircleAdder" class awkward

Converters TheirCircle We're using someone else's code We want to convert their types into our types Could create a "CircleConverter" class OurCircle awkward

Java's Solution Static: Attach functionality and data to classes (instead of instances) Can also be public / private / protected Same access rules as with instances Properties and Methods

Static vs Instance Live on Can Access Number in system Instance Each instance The fields in the same instance Arbitrarily many Static The class Only other static values Just one

Circle.java -->

Exceptions

Problem Errors happen, all the time (Sod's Law) Robust programs need to deal with these errors Some errors are REALLY bad Programmers are lazy Programming languages should nudge in the right direction

Error Example writestufftodisk(byte[] lotsofstuff) What to do when things go wrong? Return boolean boolean writestufftodisk(byte[] lotsofstuff) Return "error code" int writestufftodisk(byte[] lotsofstuff) Multiple values ErrorAndResultObject writestufftodisk(byte[] lotsofstuff) Out parameter boolean writestufftodisk(byte[] lotsofstuff, Error errorobject)

Error Example writestufftodisk(byte[] lotsofstuff) What to do when things go wrong? Return boolean boolean writestufftodisk(byte[] lotsofstuff) Return "error code" int writestufftodisk(byte[] lotsofstuff) Multiple values ErrorAndResultObject writestufftodisk(byte[] lotsofstuff) Out parameter boolean writestufftodisk(byte[] lotsofstuff, Error errorobject)

Error Example writestufftodisk(byte[] lotsofstuff) What to do when things go wrong? Return boolean boolean writestufftodisk(byte[] lotsofstuff) Return "error code" int writestufftodisk(byte[] lotsofstuff) Multiple values ErrorAndResultObject writestufftodisk(byte[] lotsofstuff) Out parameter boolean writestufftodisk(byte[] lotsofstuff, Error errorobject)

Error Example writestufftodisk(byte[] lotsofstuff) What to do when things go wrong? Return boolean boolean writestufftodisk(byte[] lotsofstuff) Return "error code" int writestufftodisk(byte[] lotsofstuff) Multiple values ErrorAndResultObject writestufftodisk(byte[] lotsofstuff) Out parameter boolean writestufftodisk(byte[] lotsofstuff, Error errorobject)

Error Example writestufftodisk(byte[] lotsofstuff) What to do when things go wrong? Return boolean boolean writestufftodisk(byte[] lotsofstuff) Return "error code" int writestufftodisk(byte[] lotsofstuff) Multiple values ErrorAndResultObject writestufftodisk(byte[] lotsofstuff) Out parameter boolean writestufftodisk(byte[] lotsofstuff, Error errorobject)

Problems With these Approaches?

Problems With these Approaches Not enough information Large, non-meaningful lookup tables Easy to ignore

Java's Solution Force programmers to deal with errors through the compiler try / catch / finally try I'm about to do something risky catch Here's how I'll handle this bad thing that could happen finally Good or bad, do this last thing Part of the method signature

Exceptions Example public FileReader(String filename) throws FileNotFoundException

Exceptions Example public FileReader(String filename) throws FileNotFoundException FileReader secretsreader = new FileReader("secrets.txt");

Exceptions Example public FileReader(String filename) throws FileNotFoundException FileReader secretsreader = new FileReader("secrets.txt"); What could go wrong?

Exceptions Example public FileReader(String filename) throws FileNotFoundException try { FileReader secretsreader = new FileReader("secrets.txt"); } catch (FileNotFoundException error) { } System.out.println("Welp, seems we couldn't get the file ");

Exceptions Example public FileReader(String filename) throws FileNotFoundException public int read(char[] cbuf) throws IOException try { FileReader secretsreader = new FileReader("secrets.txt"); char[] secrets = new char[1000]; secretsreader.read(secrets); } catch (FileNotFoundException error) { System.out.println("Welp, seems we couldn't get the file "); } catch (IOException othererror) { System.out.println("Got the file, but couldn't read it "); }

URL.java -->

In Groups https://www.cs.uic.edu/~psnyder/cs342- summer2017/ic/student.java Walk through the code Discuss how this code could be improved using visibility modifiers and static

In Groups Code up another example Split into two files, Main.java and Student.java Implement discussed changes