Name: CS 101G Spring 2004 Student ID: Monday, May 10. Final Exam

Similar documents
Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

COMPUTER SCIENCE. Paper 1 (THEORY)

Part I. Multiple Choice Questions (2 points each):

Introduction to Programming System Design. CSCI 455x (4 Units)

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

J a v a Quiz (Unit 3, Test 0 Practice)

CS 111 Classes I 1. Software Organization View to this point:

AP Computer Science Java Subset

Analysis of a Search Algorithm

Inheritance, overloading and overriding

Sample CSE8A midterm Multiple Choice (circle one)

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

CmpSci 187: Programming with Data Structures Spring 2015

Computing Concepts with Java Essentials

CMSC 202H. ArrayList, Multidimensional Arrays

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

AP Computer Science Java Mr. Clausen Program 9A, 9B

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

Object-Oriented Programming in Java

CS170 Lab 11 Abstract Data Types & Objects

PLV Goldstein 315, Tuesdays and Thursdays, 6:00PM-7:50PM. Tuesdays and Thursdays, 4:00PM-5:30PM and 7:50PM 9:30PM at PLV G320

Graduate Assessment Test (Sample)

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Introduction: Abstract Data Types and Java Review

Java CPD (I) Frans Coenen Department of Computer Science

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

10CS35: Data Structures Using C

Algorithms and Data Structures Written Exam Proposed SOLUTION

Habanero Extreme Scale Software Research Project

AP Computer Science Static Methods, Strings, User Input

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Ordered Lists and Binary Trees

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Object Oriented Software Design

Basic Programming and PC Skills: Basic Programming and PC Skills:

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Java Interview Questions and Answers

LINKED DATA STRUCTURES

Data Structures and Algorithms

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

CS193j, Stanford Handout #10 OOP 3

1 Hour, Closed Notes, Browser open to Java API docs is OK

Automatic generation of fully-executable code from the Domain tier of UML diagrams

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Java the UML Way: Integrating Object-Oriented Design and Programming

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

AP Computer Science A - Syllabus Overview of AP Computer Science A Computer Facilities

Iteration CHAPTER 6. Topic Summary

Glossary of Object Oriented Terms

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

RARITAN VALLEY COMMUNITY COLLEGE ACADEMIC COURSE OUTLINE. CISY 105 Foundations of Computer Science

PES Institute of Technology-BSC QUESTION BANK

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

7.1 Our Current Model

Java EE Web Development Course Program

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Java Application Developer Certificate Program Competencies

java Features Version April 19, 2013 by Thorsten Kracht

Advanced OOP Concepts in Java

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Project 4 DB A Simple database program

Intruduction to Groovy & Grails programming languages beyond Java

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

ADTs,, Arrays, Linked Lists

Java Programming Language

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Software Development with UML and Java 2 SDJ I2, Spring 2010

Java Classes. GEEN163 Introduction to Computer Programming

Moving from CS 61A Scheme to CS 61B Java

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

CompSci-61B, Data Structures Final Exam

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Java Software Structures

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

CS506 Web Design and Development Solved Online Quiz No. 01

DATA STRUCTURES USING C

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Chapter 7: Sequential Data Structures

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Data Structures. Level 6 C Module Descriptor

Computer Programming I

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Transcription:

Name: CS 101G Spring 2004 Student ID: Monday, May 10 Final Exam Note 1: Exam is closed book and notes. Note 2: Read questions carefully. Note 3: Ask instructor if you have any questions regarding the exam. Note 4: To enable grading for partial credit, show your work and provide as much information as possible. 1.) Definitions and Concepts (15 points): For each definition/description in the left column, mark the number of the term from the right column that best defines it. Note: Terms from the right column may be used 0, 1, or more times. a. a class (or set of classes) for data abstraction where the amount of data may vary dynamically in size b. the ancestor for a particular class c. Java code for a web browser d. the portion of the execution stack corresponding to a single dynamic instance of a method e. the condition upon which recursion usually terminates f. waterfall method g. good programming style h. ability of a class to execute methods as if they were defined in that class itself i. Java code that does not require a main() function j. a first-in, first-out data structure k. ability of a method call to perform different functions based on its class type l. iterative programming style in which a method calls itself m. spiral method n. a class (or set of classes) for data abstraction that defines an interface and its associated behavior o. storage location for method s return address, input arguments, and local variables 1. abstract data type 2. HTML 3. software life cycle 4. applet 5. inheritance 6. base case 7. recursion 8. polymorphism 9. association 10. activation record 11. queue 12. superclass 13. self-documenting code 14. data structure 15. abstraction

2.) Procedural Abstraction (25 points): (a) (7 points) public String bar (int a, int n) { int z = (int) ((double) (Math.pow (2,32) 1.0) / (double) a); for (i = 0; ((i < n) && (i <= z)); i++) result = result + + (a * Math.pow (2,n)); return result; Given the above procedure, what will be the output for the following statement? System.out.println (bar (5, 12)); What is the purpose of the variable z? (b) (8 points) public class ListItem { int number; ListItem next; public ListItem (double number, ListItem next) { this.number = number; this.next = next; Given the above class definition for ListItem, and the ListItem head corresponding to the head a linked list, give the Java code for a procedure that inserts a new ListItem, x, into the list before the tail item of the list. The procedure should return the head of the resulting list. public ListItem InsertBeforeTail (ListItem head, ListItem x) { // add your code here...

2.) cont (b) (10 points) public class ListItem { int number; ListItem next; public ListItem (double number, ListItem next) { this.number = number; this.next = next; public ListItem foo (int n) { if (this == null) return null; else if (this.number == n) return this; else return next.foo(); There is an error in the above procedure. - What is this error? - How can you fix this error? (please show the working code) - What is the intended purpose of this procedure?

3.) Data Abstraction (20 points): public class ListItem { double number; ListItem next; public ListItem (double number, ListItem next) { this.number = number; this.next = next; public class Equation { double b; public Equation (double b) { this.b = b; In this problem you are to define an Equation ADT. Given above are two classes, the standard ListItem class we ve been using in this course, and a basic equation class, which defines the function: f ( x, y) = b Given these two classes, construct an Equation ADT that supports: a 1-D polynomial (for any power of x) e.g. a 2-D polynomial e.g. a logarithmic function e.g. 4 2 f ( x) a x + a x + a x + b = 4 2 1 2 2 f ( x, y) = a x + a xy + a xy + b 20 f ( x) = a ln x + b 11 Define three classes for these types of equations, building off of the existing classes given above. Use inheritance whenever possible. For these classes, you only need to give the instance variables, constructors, and the addterm() method for the 1-D and 2-D polynomials. You may assume that a method append() exists (in the 1-D and 2-D polynomial classes) for appending ListItems to a linked list. For the 1-D and 2-D polynomial, it is recommended that you store each term s powers (of x and/or y) as well as each term s coefficient (e.g. for a 2 x 2, store a2, 2 for the power of x, and 0 for the power of y). Also draw the class diagram of the resulting class hierarchy. 12

3.) cont

4.) Data Structures (20 points): (a) (9 points) Given the Relation ADT, and the Set ADT, we can form a Set of Relations. We can use this Set of Relations to have a set of employees, with the Relation for each employee associating the employee s name with his/her salary. For example, (Jeremy, 50000) (Ron, 65000) (Terry, 38000) (Fred, 90000) (Sabrina, 15000) (Theresa, 42500) Given this example, what would the binary tree containing these relations look like, when: 1) Organizing the nodes of the tree according to the Relation's domain. 2) Organizing the nodes of the tree according to the Relation's range. Show each of these trees. Assume the tree is initially empty, and the Relations are inserted in the order given above. After a few years, Ron leaves the company for a better job. Show the two tree after the Relation for Ron is deleted.

4.) cont (b) (5 points) As discussed in class, the Set ADT can be implemented a variety of ways. - Which implementation (among those discussed in class) is best for the insert function? - Which is best for the intersection function? - Why doesn't the implementation that is best for the insert function also provide the best implementation for intersection function? (c) (6 points) You are creating a Java project that requires an array of X bins, where each bin is an element of class Bin (the Bin ADT), with the name Bin {# associated with it (e.g. Bin 1, Bin 2, Bin 3,, Bin X ). Write the Java procedure createbins(int X), which creates this array of X bins. Assume the constructor for class Bin receives one input parameter, String name. Use iteration wherever possible.

5.) Object-Oriented Design (20 points): Consider the following Java code and then answer the questions. public class Game { String name; double avgreturn; public Game (String name, double avgreturn) { this.name = name; this.avgreturn = avgreturn; public GameResult play (double bet) { GameResult currentbet; double rand = Math.random (); if (rand < (avgreturn / 2)) currentbet = new GameResult (true, "Won", bet); else currentbet = new GameResult (false, "Lost", -bet); return currentbet; public String getname () { return name; public String tostring () { return getname (); public class GameResult { boolean won; double winnings; String result; public GameResult (boolean won, String result, double winnings) { this.won = won; this.result = result; this.winnings = winnings; public boolean getwon () { return won; public double getwinnings () { return winnings; public String getresult () { return result; public String tostring () { if (won) return "" + result + "! Won $" + winnings; else return "" + result + ". Lost -$" + (-winnings);

public class Slots extends Game { double payoff; public Slots (double avgreturn, double payoff) { super ("the slot machines", avgreturn); this.payoff = payoff; public GameResult play (double bet) { GameResult currentbet; double rand = Math.random (); if (rand < (avgreturn / payoff)) currentbet = new GameResult (true, "Won", bet * payoff); else currentbet = new GameResult (false, "Lost", -bet); return currentbet; public class Blackjack extends Game { public Blackjack () { super ("Blackjack", 1); // 'avgreturn' not used by Blackjack public GameResult play (double bet) { GameResult currentbet; double rand = Math.random (); if (rand <.1) currentbet = new GameResult (true, "Blackjack", 1.5 * bet); else if (rand <.4) currentbet = new GameResult (true, "Won", bet); else currentbet = new GameResult (false, "Lost", -bet); return currentbet; public class Person { String name; double winnings; double bet; double lastbet; double winincrease; public Person (String name, double winincrease) { this.name = name; this.winincrease = winincrease; winnings = 0.0; bet = 1.0; lastbet = 0.0; public GameResult playgame (Game game) { return playgame (game, this.bet);

public GameResult playgame (Game game, double bet) { this.bet = bet; lastbet = bet; GameResult result = game.play (bet); winnings += result.getwinnings(); if (result.getwon()) this.bet += bet * winincrease; return result; public String getname () { return name; public double getlastbet () { return lastbet; public double getwinnings () { return winnings; (a) (3 points) What methods are inherited in each of the classes of this class hierarchy? (b) (3 points) What methods use polymorphism in each of the classes of this class hierarchy?

5.) cont (c) (14 points) Given the above class hierarchy, the following code computes 10 random numbers and updates each person s winnings accordingly. Given that the 10 random values in a particular execution of the code are as follows, what is the output of the code? 0.659, 0.652, 0.850, 0.757, 0.562, 0.287, 0.805, 0.231, 0.501, 0.594 public class Casino { public static void personplaygame (Person person, Game game) { GameResult result = person.playgame (game); System.out.println ("Sally bet $" + person.getlastbet() + " at " + game + ". " + result); public static void personplaygame (Person person, Game game, double bet) { GameResult result = person.playgame (game, bet); System.out.println ("Sally bet $" + person.getlastbet() + " at " + game + ". " + result); public static void printpersonwinnings (Person person) { System.out.println (""); System.out.print (person.getname() + "'s winnings total "); if (person.getwinnings() >= 0) System.out.println ("$" + person.getwinnings()); else System.out.println ("-$" + (-sally.getwinnings())); public static void main(string[] args) { GameResult result; Person sally = new Person (.5); Person josh = new Person (1.0); Person peter = new Person (0.0); Game doublemoney = new Game ("Double Your Money", (Math.random() *.08) +.9); Game fiveslots = new Slots ((Math.random() *.1) +.85, 5); Game tenslots = new Slots ((Math.random() *.06) +.9, 10); Game blackjack = new Blackjack (); personplaygame (sally, doublemoney, 10); personplaygame (peter, tenslots, 2); personplaygame (sally, doublemoney); personplaygame (sally, fiveslots, 10); personplaygame (josh, blackjack, 25); personplaygame (peter, fiveslots); personplaygame (josh, doublemoney); printpersonwinnings (sally); printpersonwinnings (josh); printpersonwinnings (peter);

5.) cont