COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

Similar documents
Making New instances of Classes

COMP 250. inheritance (cont.) interfaces abstract classes

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

CS 113 PRACTICE FINAL

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

ITI Introduction to Computing II

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

ITI Introduction to Computing II

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Inheritance (Deitel chapter 9)

CS1150 Principles of Computer Science Objects and Classes

Reusing Classes. Hendrik Speleers

Reviewing OO Concepts

CS-202 Introduction to Object Oriented Programming

Inheritance and Polymorphism

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Chapter 11 Inheritance and Polymorphism

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Class, Variable, Constructor, Object, Method Questions

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Comments are almost like C++

Questions Answer Key Questions Answer Key Questions Answer Key

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Practice for Chapter 11

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Methods Common to all Classes

Introductory Programming Inheritance, sections

Chapter 9 - Object-Oriented Programming: Polymorphism

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

Reviewing OO Concepts

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

CS Week 13. Jim Williams, PhD

Big software. code reuse: The practice of writing program code once and using it in many contexts.

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Object-Oriented Programming

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

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

Object Oriented Programming. Java-Lecture 11 Polymorphism

Rules and syntax for inheritance. The boring stuff

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

CH. 2 OBJECT-ORIENTED PROGRAMMING

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

Simple Java Reference

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Chapter 9 - Object-Oriented Programming: Inheritance

COMP 250: Java Object Oriented Programming

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

Chapter 11 Inheritance and Polymorphism

Learn more about our research, discover data science, and find other great resources at:

Programming 2. Inheritance & Polymorphism

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Java Comparable interface

Inheritance (Part 5) Odds and ends

Chapter 5 Object-Oriented Programming

Some client code. output. subtype polymorphism As dynamic binding occurs the behavior (i.e., methods) follow the objects. Squarer

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

index.pdf January 21,

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Java Classes, Inheritance, and Interfaces

Java Object Oriented Design. CSC207 Fall 2014

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Lecture 10. Overriding & Casting About

Chapter 6: Inheritance

Full file at Chapter 2 - Inheritance and Exception Handling

ITI Introduction to Computing II

ITI Introduction to Computing II

The Java language has a wide variety of modifiers, including the following:

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP CS Unit 6: Inheritance Notes

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

Language Features. 1. The primitive types int, double, and boolean are part of the AP

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

JAVA MOCK TEST JAVA MOCK TEST II

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

Object-Oriented Programming More Inheritance

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Inheritance (continued) Inheritance

CS 200 More Classes Jim Williams, PhD

Overview of the lecture. Some key issues in programming Java programming. CMPT Data and Program Abstraction. Some of the key programming issues

Exception-Handling Overview

Java Fundamentals (II)

CMSC 132: Object-Oriented Programming II

Chapter 3: Inheritance and Polymorphism

CIS 110: Introduction to computer programming

Java Magistère BFA

INHERITANCE AND EXTENDING CLASSES

CS Programming I: Inheritance

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

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

Questions Answer Key Questions Answer Key Questions Answer Key

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

Transcription:

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes Study the following class and answer the questions that follow: package shapes3d; public class Circular3DShape { private double radius; public Circular3DShape(double radius) { this.radius = radius; public double getradius() { return radius; public void setradius(double radius) { this.radius = radius; public double diameter(){ return radius * 2; public double circumference(){ return 2 * Math.PI * radius; public double area(){ return Math.PI * radius * radius; 1. Write a sub-class of Circular3DShape called Cylinder. The class has an additional attribute (private double height) to represent the height of the cylinder. Besides the usual constructor/s and getters and setters, you should have the following additional methods: Override the area() method to return the surface area of the cylinder (surface area of a cylinder is given by (2 π r 2 ) + (2 π r height)). Write an additional method in the class volume() that returns the volume of the cylinder. The volume of a cylinder is calculated as follows: π r 2 height. Override the Object class s equals method to return true if the object input is equal to this cylinder. Two cylinders are equal iff they have equal heights and equal radii. (15) 1

public class Cylinder extends Circular3DShape { private double height; * @param radius * @param height public Cylinder(double radius, double height) { super(radius); this.height = height; * @return the height public double getheight() { return height; * @param height the height to set public void setheight(double height) { this.height = height; public double area(){ return (2 * Math.PI * super.getradius() * super.getradius()) + (2 * Math.PI * super.getradius() * height); public double volume(){ return Math.PI * super.getradius() * super.getradius() * height; public boolean equals(object o){ if(o instanceof Cylinder){ return ((Cylinder)o).getRadius() == super.getradius() && ((Cylinder)o).height == height; return false; COMP200 2 2012

2. Assume that the above mentioned classes Circular3DShape and Cylinder both compile without error and have the methods described above. A class Shapes3DApp with a main method is written within the package: For each of the following state whether the sets of statements will compile without error or not if they are added independently to the main method. If there are errors, explain the error briefly. 2.1.) Circular3DShape shape1 = new Cylinder(2,2); shape1.getradius(); 2.2.) Circular3DShape shape1 = new Cylinder(2,2); Cylinder shape2 = shape1; 2.3.) Circular3DShape shape1 = new Cylinder(2,2); double vol = shape1.volume(); 2.4.) Circular3DShape shape1 = new Cylinder(2,2); if (shape1 instanceof Cylinder ) double vol = shape1.volume(); 2.5.) Object o = new Cylinder(2,2); o.getradius(); 1. no error 2. error: shape1 is of superclass type. Cannot store a reference of this type in a subclass variable. 3. The method volume() is undefined for the type Circular3DShape 4. The method volume() is undefined for the type Circular3DShape. Need a typecast. 5. The method getradius() is undefined for the type Object. 3. Give the output of each of the if statements. If there will be no output, due to an error, state why that code snippet will result in an error. (NB. This section will be negatively marked - you will lose half a mark for each incorrect answer - SO no guessing!) (5) package shapes3d; public class Shapes3DApp { public static void main(string[] args) { Circular3DShape shape0 = new Circular3DShape(2); Circular3DShape shape1 = new Cylinder(2,2); Cylinder shape2 = new Cylinder(2,2); Object o = new Circular3DShape(2); if (shape1.equals(shape0) ) System.out.println( 1. equal ) ; System.out.println( 1. not equal ) ; if (shape1.equals(shape2) ) System.out.println( 2. equal ) ; System.out.println( 2. not equal ) ; COMP200 3 2012

if (shape1 == shape2 ) System.out.println( 3. equal ) ; System.out.println( 3. not equal ) ; if(shape0.equals(o)) System.out.println( 4. equal ) ; System.out.println( 4. not equal ) ; shape0 = shape2; if (shape1.equals(shape0) ) System.out.println( 5. equal ) ; System.out.println( 5. not equal ) ; 1. not equal 2. equal 3. not equal 4. not equal 5. equal 4. We want to create a subclass of Circular3DShape called Cylinder. The class Sphere has not additional attributes. Given the definition of the class Circular3DShape above: State whether each of the following code snippets can be added to the class Sphere without causing a compiletime or a run-time error. If it cannot be added give reasons why it will result in an error. (10) 4.1.) public Sphere(double radius) { super(); super.setradius(radius); 4.2.) public Sphere(double radius) { super.setradius(radius); 4.3.) public int circumference(){ return (int) (super.circumference()); 4.4.) public double area (double x){ return super.area() * x; 4.5.) double area(){ return super.area(); COMP200 4 2012

1. super() is undefined in the superclass. 2. implicit call to super() fails because super() is undefined in the superclass 3. override so cannot change the return type. 4. legal overload 5. Cannot reduce the visibility of the inherited method from Circular3DShape COMP200 5 2012