CSCE145 Test 2-Review 03/29/2015 Hongkai Yu

Similar documents
Tutorial 8 Date: 15/04/2014

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

CS Week 13. Jim Williams, PhD

CS 302 Week 9. Jim Williams

University of Palestine. Mid Exam Total Grade: 100

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

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

AP COMPUTER SCIENCE A

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

2. [20] Suppose we start declaring a Rectangle class as follows:

CS1150 Principles of Computer Science Objects and Classes

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Java Comparable interface

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

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

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

ITI Introduction to Computing II

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

Topic 7: Algebraic Data Types

Object Oriented Programming

CS 305j Midterm 1 Fall 2008

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 {

Object Oriented Programming 2015/16. Final Exam June 28, 2016

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

Java Object Oriented Design. CSC207 Fall 2014

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

Polymorphism CSCI 201 Principles of Software Development

AP CS Unit 6: Inheritance Programs

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

System.out.print(); Scanner.nextLine(); String.compareTo();

Recitation 3 Class and Objects

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

Inheritance and Polymorphism

Chapter 6 Class and Method

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Java and OOP. Part 2 Classes and objects

CS1083 Week 2: Arrays, ArrayList

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

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

Selected Questions from by Nageshwara Rao

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

Principles of Object Oriented Programming. Lecture 4

CH. 2 OBJECT-ORIENTED PROGRAMMING

Objects and Classes (1)

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Abstract Class (2) Abstract Classes and Interfaces. Abstract Class (1) Abstract Class (3) EECS2030: Advanced Object Oriented Programming Fall 2017

CS 101 Fall 2006 Midterm 3 Name: ID:

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

BSc. (Hons.) Software Engineering. Examinations for / Semester 2

Notes on Chapter Three

Java Review. Fundamentals of Computer Science

Reviewing OO Concepts

Abstract Classes and Interfaces

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

Lesson11-Inheritance-Abstract-Classes. The GeometricObject case

Chapter 5: Classes and Objects in Depth. Introduction to methods

CSC 102 Lecture Notes Week 2 Introduction to Incremental Development and Systematic Testing More Jav a Basics

Chapter 5: Classes and Objects in Depth. Information Hiding

Solutions to Sample JAC444 Midterm Test

ESC101 : Fundamental of Computing

In our classes, we've had

1 Method Signatures and Overloading (3 minutes, 2 points)

CS 1302 Chapter 9 (Review) Object & Classes

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Introduction to Java Unit 1. Using BlueJ to Write Programs

Object Oriented Relationships

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

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

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Midterm Solutions. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

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

Practice Questions for Chapter 9

Chapter 14 Abstract Classes and Interfaces

ITI Introduction to Computing II

ITI Introduction to Computing II

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ June Exam

Page 1 / 3. Page 2 / 18. Page 3 / 8. Page 4 / 21. Page 5 / 15. Page 6 / 20. Page 7 / 15. Total / 100. Pledge:

CS100B: Prelim 3 Solutions November 16, :30 PM 9:00 PM

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

This exam is open book. Each question is worth 3 points.

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

(a) Write the signature (visibility, name, parameters, types) of the method(s) required

CSCI 212 Practice Final Exam Summer Instructor: Krishna Mahavadi

Programming 2. Inheritance & Polymorphism

COMP 250. Lecture 29. interfaces. Nov. 18, 2016

CS-202 Introduction to Object Oriented Programming

F I N A L E X A M I N A T I O N

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

Introduction to Computer Science Unit 4B. Programs: Classes and Objects

AP Computer Science Unit 1. Writing Programs Using BlueJ

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Second Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 11 November 2010

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

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

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

Classes. Classes as Code Libraries. Classes as Data Structures

Transcription:

CSCE145 Test 2-Review 03/29/2015 Hongkai Yu 1. What results are printed when the main method in TestBase is executed? public class Base private int value; public Base(int x) value = x; System.out.println( Constructed Base: + value); public class TestBase extends Base private Base val; public TestBase(int x, int y) super(x); val = new Base(y); public static void main(string[] args) TestBase d = new TestBase(11, 28); Constructed Base: 11 Constructed Base: 28 2. Given the following classes named Rectangle and Point, write an equals method for the Rectangle class that returns true if two rectangles have the same area, and returns false otherwise: public class Point public double x; // Because these are public, you can access them public double y; // directly without getters and setters public class Rectangle private Point ll; // the lower left corner of the rectangle private Point ur; // the upper right corner of the rectangle public boolean equals(rectangle r1) double arear1 = (r1.ur.x - r1.ll.x)*(r1.ll.y - r1.ur.y); double areathis = (this.ur.x - this.ll.x)*(this.ll.y - this.ur.y); if (arear1 == areathis) return true; else return false

3. The Building class is defined in terms of the following private instance variables: a String owner and an int numberofrooms. Assume there are accessor methods for the instance variables, called getnumberofrooms() and getowner(). Write a method equals for the Building class that determines whether an instance of the Building class has the same values for its instance variables as another instance of the Building class. public boolean equals(building b) if (this.getnumberofrooms() == b.getnumberofrooms() && this.getowner.equals(b.getowner)) return true; else return false; 4. Given an array of Building objects, where Building is defined as in problem #1, write a Java method that will accept the array of Building objects as a parameter and will display, i.e., print, the owner and numberofrrooms of each Building. public void print(building[] b) for(int i=0; i< b.length; i++) System.out.println( Owner is + b[i].getowner() + and # of room = + b[i].getnumberofrooms()); 5. Given the array from problem #3, write a Java method that will accept the array of Building objects as a parameter and will return the total number of rooms in all of the buildings in the array. public int gettotalrooms(building[] b) int total = 0; for (int i=0; i< b.length; i++) total += b[i].getnumberofrooms(); 6. The class House is derived from the base class Building (from problem #1) because it has an additional private instance variable, a boolean hasgarage. Write a constructor for House that takes three

parameters for owner, numberofrooms, and hasgarage, and that makes use of the following constructor for Building: public Building(String own, double n) owner = own; numberofrooms = n; public House(String owner, int numberofrooms, boolean hasgarage) super(owner, numberofrooms); this.hasgarage = hasgarage; // Problem 7 to 9 are together. 7. We have a class called 2DPoint like this: public class 2DPoint private double x; private double y; Please write default constructor and parameterized constructor for this class. 8. Write a new class called 3DPoint derived from the class 2DPoint. 3DPoint class has an additional private instance variable, a double type z. You do not need to write accessor methods(get and set). Default constructor and parameterized constructor have to be defined in your 3DPoint class. 9. Create an object/instance for the base class 2DPoint and another object/instance for the sub class 3DPoint. Use their parameterized constructors to initialize the object of 2DPoint to be (10,11) and the object of 3DPoint to be (100,110,120)

10. Suppose we have two overloading methods: public double ADD(double a, double b) return a+b; public double ADD(double a, String b) return -9999; If we call ADD(3.0, 4.0) in the main method, what will be the result? If we call ADD(3.0, 4.0 ) in the main method, what will be the result? 11. Please explain the difference of overloading method and overriding method in JAVA? 12. Polymorphism question: We have the following three classes: public class Person public void Test () System.out.println ("Base Class Person"); public class Doctor extends Person public void Test () System.out.println ("Sub Class Doctor"); public class Professor extends Person

public void Test () System.out.println ("Sub Class Professor"); Given the above classes, what will be the printout of the following code? public static void main(string[] args) Person[] people = new Person[4]; people [0] = new Doctor(); people [1] = new Professor(); people [2] = new Doctor(); people [3] = new Person(); for (Person p : people) p.test(); System.out.println(); 13. Question for interface: What is wrong with the following interface? And fix it. public interface SomethingIsWrong void amethod(int avalue) System.out.println("Hi Mom"); Answer: It has a method implementation in it. Fix the interface: public interface SomethingIsWrong void amethod(int avalue); 14. Please explain the idea of interface in JAVA.

15. Suppose we have an interface called Measurable, which defines the rule to measure the 2D shapes in Perimeter and Area. public interface Measurable /** Returns the perimeter. */ public double getperimeter (); /** Returns the area. */ public double getarea (); Write a class called Rectangle which implements the interface Measurable. Some parts of the class Rectangle are missed, please make up for it. public class Rectangle //do not need to worry about accessor methods(get and set) and constructors here private double Width; private double Height; public Rectangle (double w, double h) Width = w; Height = h; 16. Please explain abstract class and abstract methods. You need to understand them.