UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Similar documents
Chapter 10 Classes Continued. Fundamentals of Java

Chapter 6 Introduction to Defining Classes

Chapter 11 Classes Continued

APCS Java Lesson 10: Classes Continued Modifications by Mr. Dave Clausen Updated for Java 1.5

Programming II (CS300)

Encapsulation. Mason Vail Boise State University Computer Science

Programming II (CS300)

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Chapter 14 Abstract Classes and Interfaces

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

Intro to Computer Science 2. Inheritance

COP 3330 Final Exam Review

Mathematics/Science Department Kirkwood Community College. Course Syllabus. Computer Science CSC142 1/10

CS-202 Introduction to Object Oriented Programming

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CS 11 java track: lecture 3

CH. 2 OBJECT-ORIENTED PROGRAMMING

Polymorphism. return a.doublevalue() + b.doublevalue();

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

public class Account { private int id; private static int nextaccountid = 0; private String name; private double balance;

Type Hierarchy. Lecture 6: OOP, autumn 2003

Full file at Chapter 2 - Inheritance and Exception Handling

Selected Java Topics

Self-review Questions

Curriculum Map Grade(s): Subject: AP Computer Science

More On inheritance. What you can do in subclass regarding methods:

C# Programming for Developers Course Labs Contents

Inheritance (P1 2006/2007)

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Java Object Oriented Design. CSC207 Fall 2014

1 Shyam sir JAVA Notes

CMSC 132: Object-Oriented Programming II

What about Object-Oriented Languages?

CST242 Object-Oriented Programming Page 1

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Use the scantron sheet to enter the answer to questions (pages 1-6)

Introduction to Programming Using Java (98-388)

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Inheritance Advanced Programming ICOM 4015 Lecture 11 Reading: Java Concepts Chapter 13

Declarations and Access Control SCJP tips

Introduction to Computing II (ITI 1121) Final Examination

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

Chapter 5 Object-Oriented Programming

25. Generic Programming

Do not start the test until instructed to do so!

Testing Object-Oriented Software. COMP 4004 Fall Notes Adapted from Dr. A. Williams

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Chapter 15. Exception Handling. Chapter Goals. Error Handling. Error Handling. Throwing Exceptions. Throwing Exceptions

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

Inheritance. Transitivity

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

More about inheritance

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

Outline. Computer Science 331. Information Hiding. What This Lecture is About. Data Structures, Abstract Data Types, and Their Implementations

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class


Assertions, pre/postconditions

VIRTUAL FUNCTIONS Chapter 10

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. January 11, 2018

Inheritance (continued) Inheritance

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Inheritance and Interfaces

More Relationships Between Classes

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Chapter Goals. Chapter 9 Inheritance. Inheritance Hierarchies. Inheritance Hierarchies. Set of classes can form an inheritance hierarchy

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

An exception is simply an error. Instead of saying an error occurred, we say that an.

CS 251 Intermediate Programming Inheritance

C18a: Abstract Class and Method

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Inheritance and Polymorphism

Java for Non Majors Spring 2018

EXAMINATIONS 2012 END-OF-YEAR SWEN222. Software Design. Question Topic Marks 1. Design Quality Design Patterns Design by Contract 12

Chapter 4 Defining Classes I

Operators and Expressions

Exception Handling Introduction. Error-Prevention Tip 13.1 OBJECTIVES

Lecture 18 CSE11 Fall 2013 Inheritance

Inheritance, Polymorphism, and Interfaces

COMP1008 Exceptions. Runtime Error

Chapter 4.!Data Abstraction: The Walls! 2011 Pearson Addison-Wesley. All rights reserved 4-1

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Computer Science II (20073) Week 1: Review and Inheritance

INSTRUCTIONS TO CANDIDATES

Data Abstraction: The Walls

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

Class, Variable, Constructor, Object, Method Questions

INHERITANCE. Spring 2019

ECE 122. Engineering Problem Solving with Java

School of Informatics, University of Edinburgh

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. February 10, 2017

Program Correctness and Efficiency. Chapter 2

Transcription:

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED EXERCISE 11.1 1. static public final int DEFAULT_NUM_SCORES = 3; 2. Java allocates a separate set of memory cells in each instance for the instance variables of a class. By contrast, only one set of cells is allocated for the class variables. 3. First, a static method can be included to allow the user to call the method by sending a message to the class. This method can serve as a public accessor or mutator of a class variable. Second, a static method can be included to perform some useful calculation. The Math class methods are good examples. Third, a static method can be included in a set of cooperating helper methods to decompose responsibilities for tasks in a class. 4. The reference to instvar in the static method is a syntax error because static methods cannot reference instance variables. 5. A public constant is accessible to all clients, and a static constant is accessible by using the class name as a reference. EXERCISE 11.3 1. The purpose of an interface is to provide clients with enough information to be able to use the methods of a class. An interface includes just a set of method signatures, whereas a class includes the implementations of these methods and perhaps some data as well. 2. static private void drawsquare(pen p, int x, int y, int length){ p.up(); p.move(x, y); p.down(); p.setdirection(270); for (int i = 1; i <= 4; i++){ p.move(length); p.turn(90); EXERCISE 11.4 1. public interface Account{ public double getbalance(); public void deposit(double amount); public void withdraw(double amount); 1

2. The expression implements <an interface> causes the compiler to check the implementing class for the presence of all the methods in the specified interface. If any of them are missing, a syntax error occurs. EXERCISE 11.5 1. A class hierarchy is a set of classes that are organized by the relationship of subclass and superclass. An example is the hierarchy Object/Circle/Wheel, where Circle is a subclass of Object and Wheel is a subclass of Circle. 2. The expression extends <a class> is used to inherit the data and behavior of another class. 3. To run a constructor of the same form in the superclass, one uses the form super(<parameters>) at the beginning of the corresponding constructor in the subclass. 4. With methods other than constructors, one uses the form super.<method name> (<parameters>). 5. The visibility modifier protected allows subclasses in a hierarchy to access instance and class variables or methods of superclasses without allowing other clients in the system to access these items. 6. There is a syntax error. The method setspokes is not included in the Shape interface. The programmer attempts to send this message to an instance of Wheel, but this object is masquerading as a Shape. The variable s must be cast to a Wheel before sending it the setspokes message. EXERCISE 11.6 1. a. Syntax error: the array element, as an Object, does not understand the indexof message. b. Syntax error: same as a. c. Correct. d. Runtime error: ClassCastException because a Student is cast to a String. EXERCISE 11.7 1. An abstract class serves as a repository of data and behavior common to a set of subclasses. Because this class is abstract, it cannot be instantiated. An example is the AbstractShape class, which is abstract and defines the behavior common to circles and rectangles. 2. We declare abstract methods for two possible reasons. First, the abstract class in which they are declared implements an interface, but the implementation of these methods is deferred to the concrete classes. Second, even if there is no interface, 2

these methods must be implemented in all subclasses, so declaring them abstract enforces this. 3. A final method cannot be overridden in a subclass. Examples are the accessors for variables that are defined in an abstract class. These methods should not be overridden by subclasses. 4. A protected variable is accessible throughout the defining class and any of its subclasses. An example is a balance for all subclasses of an abstract bank account class. EXERCISE 11.8 1. Generally, to locate the right method to execute, the JVM first looks in the class of the receiver object. If the method is not found there, the JVM look at this class s superclass, if there is one. This process is repeated until a method is found or an exception is thrown (if the method is not eventually found in the Object class). In this example, the server s method somemethod is executed first. Within this method, the JVM executes a call to the method somemethod in the superclass. 2. Student answers will vary, but here are some examples. The main application class in a terminal-based program depends on the Scanner class for input operations. A phone book class aggregates objects that consist of a name, address, and phone number. A colored point inherits data and behavior from a point. EXERCISE 11.9 1. The rule for passing a parameter object is the same as the rule for assignment, namely, the class of the actual parameter must be the same as or less inclusive than the class of the formal parameter. 2. The answer is b. EXERCISE 11.10 1. Preconditions describe assumptions that must be true before a method can perform its task correctly. For example, a method that uses a parameter as a divisor must expect that number to not be zero. 2. Postconditions describe assumptions that must be true after a method executes correctly. For example, a correctly executed method to deposit money in a bank account will leave behind a balance that is the sum of the balance before the deposit was made and the amount deposited. EXERCISE 11.11 3

1. A method should throw an exception when its preconditions are violated. For example, if an integer parameter must be less than a certain value, the method should throw an exception if the parameter is greater than or equal to that value. 2. When a method throws an exception, control is passed immediately to the calling method and from there goes on up the call chain to the main method, where the JVM halts execution with a trace of this call chain. 3. An exception can be caught and handled by using a try-catch statement. 4. The @param tag marks a parameter of a method. The @return tag marks the returned value of a method. The @throws tag marks an exception thrown by the method. EXERCISE 11.12 1. The programmer should use equals instead of == when comparing two objects because == is too restrictive. In cases where two distinct objects are structurally the same (have the same values for all their instance variables), == would return false because == returns true only when the operands refer to one object. 2. true false false 3. When an object is assigned to a variable, that variable contains a reference to the object. 4. To obtain a true copy of an object, you must create a new instance of that object s class and then copy the original object s instance variables to the new object s variables. EXERCISE 10.13 1. The advantages of using turtle graphics are that it s thoroughly object-oriented and there is no forgetful bitmap to worry about. The disadvantages are that shapes cannot be filled and the drawing is a bit slow. The advantages of using the standard graphics classes are that the methods are very fast and the shapes can be filled. The main disadvantage is that the programmer must refresh the forgetful bitmap. REVIEW QUESTIONS Fill in the Blank 1. class, static 2. super 3. protected 4. abstract 5. abstract 4

6. preconditions, postconditions 7. equals, clone 5