CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

Similar documents
COE318 Lecture Notes Week 10 (Nov 7, 2011)

Full file at Chapter 2 - Inheritance and Exception Handling

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Chapter 9. Exception Handling. Copyright 2016 Pearson Inc. All rights reserved.

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

CS159. Nathan Sprague

Exceptions and Libraries

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Chapter 13. Inheritance and Polymorphism. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Class, Variable, Constructor, Object, Method Questions

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Introduction to Java Written by John Bell for CS 342, Spring 2018

Introduction to Java

Exception Handling. Chapter 8. Chapter 8 1

16-Dec-10. Consider the following method:

Inheritance (Part 5) Odds and ends

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse

CSE 142 Su 04 Computer Programming 1 - Java. Objects

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

Exception Handling. Chapter 9

Chapter 11 Handling Exceptions and Events. Chapter Objectives

Exceptions. Author: Boaz Kantor The Interdisciplinary Center, Herzliya Introduction to Computer Science Winter Semester

Inheritance (Outsource: )

Making New instances of Classes

Programming II (CS300)

Object-Oriented Design. March 2005 Object Oriented Design 1

CH. 2 OBJECT-ORIENTED PROGRAMMING

Announcements. Final exam. Course evaluations. Saturday Dec 15 10:20 am -- 12:20 pm Room: TBA

CS 11 java track: lecture 3

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

EXCEPTIONS. Java Programming

Fundamentals of Object Oriented Programming

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

COMP1008 Exceptions. Runtime Error

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

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

Objects and Iterators

Exceptions. What exceptional things might our programs run in to?

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

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

WES-CS GROUP MEETING #9

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

BBM 102 Introduction to Programming II Spring Exceptions

SSE3052: Embedded Systems Practice

Introduction to Java. Handout-3a. cs402 - Spring

Objects as a programming concept

Abstract Classes and Interfaces

Programming II (CS300)

AP CS Unit 6: Inheritance Notes

APCS Unit 5 Exam. Assuming all four classes have a default constructor, which of the following statements would result in an error from the compiler?

CMSC 202. Exceptions

1.00 Lecture 14. Exercise: Plants

Exceptions and Error Handling

15CS45 : OBJECT ORIENTED CONCEPTS

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CS159. Nathan Sprague

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Java Errors and Exceptions. Because Murphy s Law never fails

TaxiBot New attributes Variables Math! TaxiBot

Week 3 Classes and Objects

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

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

Program Fundamentals

Midterm Exam CS 251, Intermediate Programming March 12, 2014

CS 116 Week 8 Page 1

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

Java Programming Training for Experienced Programmers (5 Days)

Chapter 8. Exception Handling

CS 162, Lecture 25: Exam II Review. 30 May 2018

6.Introducing Classes 9. Exceptions

COMP-202 Unit 9: Exceptions

public Candy() { ingredients = new ArrayList<String>(); ingredients.add("sugar");

CS 251, Intermediate Programming Midterm Exam October 9, 2013

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

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

CS 61B Discussion 5: Inheritance II Fall 2014

In this lab we will practice creating, throwing and handling exceptions.

Defensive Programming

Polymorphism: Inheritance Interfaces

Computer Components. Software{ User Programs. Operating System. Hardware

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Comp 249 Programming Methodology Chapter 9 Exception Handling

BM214E Object Oriented Programming Lecture 7

CSE 143 Java. Exceptions 1/25/

public static boolean isoutside(int min, int max, int value)

I. True/False: (2 points each)

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

Exceptions, try - catch - finally, throws keyword. JAVA Standard Edition

Object-Oriented Concepts

EECS2030 Week 7 worksheet Tue Feb 28, 2017

Checked and Unchecked Exceptions in Java

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

EXCEPTIONS. Objectives. The try and catch Statements. Define exceptions. Use try, catch and finally statements. Describe exception categories

CS11 Introduction to C++ Fall Lecture 6

Transcription:

CISC-124 20180215 These notes are intended to summarize and clarify some of the topics that have been covered recently in class. The posted code samples also have extensive explanations of the material. Please be sure to download and run the code samples, and experiment with them by running them, modifying them, and writing your own similar methods. Please also make use of the textbook as a learning resource. Passing Parameters A Java method cannot change the value of any of the arguments passed to its parameters. public static void main(string[] args){ int y = 3; System.out.println( y = +y); methoda(y); System.out.println( y = +y); void methoda(int x){ System.out.println( x = +x); x = 12; System.out.println( x = +x); In this example the output is y = 3 x = 3 x = 12 y = 3

The point is that when we change the value of x, it doesn t change the value of y Now what if the parameter is a reference (link) to an Object? public static void main(string[] args){ Dog dog1 = new Dog( Astro ); System.out.println( dog1.name = +dog1.getname()); methodb(dog1); System.out.println( dog1.name = +dog1.getname()); void methodb(dog d){ System.out.println( d.name = +d.getname()); d.setname( Scoobydoo ); System.out.println( d.name = +d.getname()); The output is dog1.name = Astro d.name = Astro d.name = Scoobydoo dog1.name = Scoobydoo The methodb method was able to change the name attribute of the Dog - so it looks like the method was able to change the value of the argument it was given. It really didn t though, because the argument given is the link (reference) to that Dog object, and that link didn t get changed the method is not able to make dog1 refer to a different Dog object, just as methoda was not able to change the value of y. However, methodb is able to change the contents of the Dog object that the argument links (refers) to.

There is an ever-lasting argument among Java programmers about the proper terminology to describe how parameters are passed. Some say it is passing by reference because the parameter refers to the same Object that the argument refers to. Others say it is passing by value because the the parameter is always given the value of the argument and that value is either a primitive type or it is a link (reference) to an object. As you read about and study Java programming, you will encounter both descriptions. It really doesn t matter which terminology you use, so long as you understand exactly how it works. No matter which description you use, you will eventually find someone who disagrees with it! Exceptions We use exceptions to respond to problems that occur during runtime. Exceptions are simply Objects that we can use to hold information that identifies the problem. Java has many predefined Exceptions such as FileNotFoundException(), and we can also define our own (see the most recent project that includes the Dog class). If one of our methods (or a pre-defined method) detects a problem that it doesn t know how to solve such as an invalid parameter it can create and throw an Exception. Creating the Exception is done with the new keyword, just as for any Object. Throwing an exception terminates the method immediately no value is returned. Suppose methoda calls methodb, and methodb throws an exception. The exception is thrown to the point in methoda at which methodb is called. Then methoda must either deal with the exception (this is where we use a try/catch structure) or throw it back to whatever method called methoda this is done by putting throws... in the methoda header. Every exception that your methods can encounter or create must be caught. It may be thrown, and thrown, and thrown again all the way up the chain of methods that called other methods, but somewhere along the line some method has to catch it. The main method is not allowed to throw any exceptions so main is the final point at which exceptions can be caught.

Inheritance When we declare that class ClassB extends ClassA, we are stating that objects of type ClassB inherit all attributes and methods of ClassA. ClassB objects can also have attributes and methods that do not come from ClassA. If an attribute of ClassA is private, it is still inherited by ClassB but ClassB objects cannot see it. However, ClassB objects can access private attributes using public accessor methods If an attribute of ClassA is protected, it is inherited by ClassB and ClassB objects can see it. ClassB can executed the constructor method from ClassA using the super command. ClassB can override methods from ClassA, which is just a fancy word for redefine. We introduced the instanceof keyword that lets us determine if an object is an instance of a particular class. Note that if an object is an instance of ClassB, it is also an instance of ClassA, and of any class above ClassA. Every class in Java is an extension of the basic Object class, and inherits some methods from that class. One of those is equals() - which means that we can try to compare any two objects to see if they are equal... however in practice in any class where we need to test objects for equality we should override the default equals() method with a method that is appropriate for the particular class. Usually we determine equality of two objects of the same type by comparing the values of their attributes.

Javadoc Javadoc is a tool that we can use to produce web-pages that document our Java programs. When we run javadoc.exe on our programs, it produces a web-page for each of our classes each web-page lists the methods and attributes of the class, as well as other useful information. We can provide comments that will be included in the web-page by including them in comment blocks that look like this /** * This method does something useful * @param name String * @param age int * @returns double */ placed just above the header of the method. Eclipse will do a lot of the work of generating these comment blocks for us.