Core Competency DOO (Design Object-Oriented)

Similar documents
Remaining Enhanced Labs

Session 14 March 31, 2018

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

EXAM Computer Science 1 Part 1

Array Based Lists. Collections

CSCI 355 LAB #2 Spring 2004

CIS 162 Project 4 Farkle (a dice game)

Midterm Exam 2 CS 455, Spring 2011

Object- Oriented Analysis, Design and Programming

int main() { int account = 100; // Pretend we have $100 in our account int withdrawal;

CSE 142 Su01 Final Exam Sample Solution page 1 of 7

CSCI 355 Lab #2 Spring 2007

CIS 110 Spring 2014 Introduction to Computer Programming 12 May 2014 Final Exam Answer Key

CS112 Lecture: Defining Instantiable Classes

CS2102, D15 Exam 1. Name:

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

CSC Java Programming, Fall Java Data Types and Control Constructs

Practice Midterm Examination

Lab 2: Object-Oriented Design 12:00 PM, Jan 31, 2018

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

The Hong Kong Polytechnic University

CS 116 Week 8 Page 1

CIS 121 Data Structures and Algorithms with Java Spring 2018

CS 455 Midterm Exam 1 Fall 2010 [Bono] Sept. 29, 2010

UNDERSTANDING CLASS DEFINITIONS CITS1001

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

ASSIGNMENT 5 Data Structures, Files, Exceptions, and To-Do Lists

An Introduction to Subtyping

Defensive Programming

CS 211 Programming Practicum Fall 2018

Objectives: 1. Introduce the course. 2. Define programming 3. Introduce fundamental concepts of OO: object, class

Objectives for this class meeting. 1. Conduct review of core concepts concerning contracts and pre/post conditions

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

Classwork 7: Craps. N. Duong & R. Rodriguez, Java Crash Course January 6, 2015

SEMESTER 1, 2011 EXAMINATIONS. CITS1200 Java Programming FAMILY NAME: GIVEN NAMES:

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

CMSC 201 Spring 2018 Project 3 Minesweeper

Software Design and Analysis for Engineers

CS 455 Final Exam Fall 2013 [Bono] December 12, 2013

Assignment 5: Part 1 (COMPLETE) Sprites on a Plane

Outline for Today CSE 142. Programming a Teller Machine. CSE142 Wi03 I-1. ATM Algorithm for Dispensing Money

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

1 Inheritance (8 minutes, 9 points)

Computer Sciences 302 Exam 2 Information & Sample Exam

Lab 1: Setup 12:00 PM, Sep 10, 2017

CMPSCI 187 / Spring 2015 Hangman

CS 455 Midterm Exam 1 Fall 2010 [Bono] Sept. 29, 2010

Dartmouth College Computer Science 10, Winter 2012 Final Exam

Lab ACN : C++ Programming Exercises

CS2102, B11 Exam 2. Name:

CS 1302 Chapter 9 (Review) Object & Classes

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

Interlude. Object Oriented Design

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

Part III Appendices 165

COS 126 Written Exam 2 (Spring 2015)

Outline. Announcements. Homework 2. Boolean expressions 10/12/2007. Announcements Homework 2 questions. Boolean expression

CS 307 Midterm 2 Spring 2008

CS 455 Midterm 2 Fall 2017 [Bono] Nov. 7, 2017

Prelim 1. CS 2110, March 15, 2016, 5:30 PM Total Question Name True False. Short Answer

Project 3: Implementing a List Map

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

CS61B Lecture #7. Announcements:

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

CS61B Lecture #5: Arrays and Objects

CS201 - Assignment 3, Part 2 Due: Wednesday March 5, at the beginning of class

Programming a Bank Database. We ll store the information in two tables: INTEGER DECIMAL(10, 2)

Mutating Object State and Implementing Equality

WES-CS GROUP MEETING #9

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

VALLIAMMAI ENGINEERING COLLEGE

CPS122 Lecture: Defining a Class

About this exam review

Chapter 3. Iteration

CSE 142 Sp02 Final Exam Version A Page 1 of 14

CS 455 Final Exam Fall 2012 [Bono] Dec. 17, 2012

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

Ascending. Load. Descending. Save. Exit. Numbers

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

CS 134 Programming Exercise 9:

Soda Machine Laboratory

CIT 590 Homework 10 Battleship

Lists using ArrayList

METHODS EXERCISES GuessNumber and Sample run SumAll Sample Run

Introduction to Java Programs for Packet #4: Classes and Objects

CS 455 Midterm Exam 1 Spring 2013 [Bono] Feb. 21, 2013

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Programming Project 1

Object-oriented Programming and Software Engineering CITS1001. Multiple-choice Mid-semester Test

Java Puzzle Ball Nick Ristuccia

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CS 455 Midterm Exam 1 Fall 2015 [Bono] Thursday, Oct. 1, 2015

CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, :30pm)

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015

Second Examination Solution

ArrayList. Introduction. java.util.arraylist

ASSIGNMENT 5 Objects, Files, and a Music Player

Transcription:

Here is the documentation for the rest of the semester. This document includes specification of the remaining Core Competencies, specifications for the final two Core Labs, and specifications for a number of Enhanced Labs from which you will pick just a few to complete. Note that there are only two more Core Labs, along with a bunch of Enhanced Labs from which you can pick the ones you choose to do. The new Enhanced Labs are not worth very many lab points, but the more you do, the better prepared you will be for the Final Exam, taking CS 2050, and actually using what you learn in this course for some future purpose even if you don t take any more computer science courses. No sections of the Zybook beyond Chapter 6 are assigned for credit, but you will want to read some parts of the Zybook as detailed in the following. Core Competency DOO (Design Object-Oriented) Given an English description of a scenario, be able to identify the different classes involved, and for each class, be able to figure out what instance variables it should have, be able to write code for each instance variable including appropriate data type and name, and be able to figure out what instance methods the class should have. An answer will be considered correct if it shows understanding of the ideas we won t require all the members to be identified, just some. Sample Problem: Suppose you are writing a program to computerize the game of Risk. Here is a simplified description of the game. Risk is a simulation of international warfare. The world is organized into 42 countries. Each country has a name, some other countries that border it, and a continent that it belongs to. Also, each country has to have at least one army on it, and typically has a bunch more. And, at all times each country is owned by some player. When a player destroys all the armies on a country owned by another player, that player takes ownership of the country. Players take turns in order (first, second, third, fourth, first, etc.). Each player owns some countries. When battles take place (decided by throwing dice), one or two armies sometimes need to be removed from a country. When a player s turn is about to start, they get some new armies (depending on how many and which countries they own) that they can place on any of the countries they own. At the end of a player s turn, they get to move any number of armies across a single border from one of their countries to another of their countries. CS 1050 Spring 2018 Page 74

Here is one reasonable answer to this problem: public class Country { private String name; // name of the country private int numarmies; // number of armies on that country private ArrayList<Country> neighbors; // list of countries connected // to this country private Player owner; // the current owner of this country // rough headers for some instance methods: public void addarmies(int num) public void removearmies(int num) public void changeowner(player p) public class Player { private int turn; // holds 1, 2, 3, 4,... private ArrayList<Country> owned; // holds list of owned countries // rough headers for some instance methods: public int computearmiesearned() public void conquer(country c) Practice Problem: (to be done in class working in small groups) In the game of Monopoly (if you have any questions about the rules of this game, ask your group members), one very important kind of object is the lot. Given the title deed for a few lot instances, perform Core Competency DOO on the class Lot. Write your answer on a whiteboard in preparation for whole group discussion. You will also identify other classes that would be needed in this game, because they are needed as the type of a Lot instance variable or as a parameter type in a Lot instance method. Core Competency WMH (Write Method Headers) Given method calls, with enough context to pin down the types of the arguments and the return type, infer the most likely method header, with arbitrary parameter names. Sample Problem: (headers already shown off to the right below) For the fragment of code below, write down after each method call the most appropriate corresponding method header, including static or not, correct return type, method name, and parameter list (use whatever names you like for the parameters, but the types must be appropriate): Assume that all the methods being called are members of the Clown class. CS 1050 Spring 2018 Page 75

Ball a, b; Clown c, d, e; double x, y, z; // lots of code omitted that gives these variables meaningful values c = new Clown( 13.2, "Bozo" ); ----> Clown(double x, String s) z = f(x); ----> double f(double x) e = Clown.flip( c, d ); ----> static Clown flip(clown x, Clown y) b = c.flib( b, d ); ----> Ball flib(ball b, Clown d) d.foop(); ----> void foop() x = e.mop( 37.0, a ); ----> double mop(double a, Ball b) Practice Problem: For the fragment of code below, write down to the right of each method call the most appropriate corresponding method header, including static or not, correct return type, method name, and parameter list (use whatever names you like for the parameters, but the types must be appropriate). Assume that all the methods (including constructors) being called are members of the Dog class. Dog x, y; Food r, s, t; Cat a, b; int result; // lots of code omitted that gives these variables meaningful values x = new Dog( 17.2, "Rowdy" ); ----> r = x.produce(); ----> result = Dog.torment( a, b ); ----> x.chase( a, y ); ----> CS 1050 Spring 2018 Page 76

Core Competency WMB (Write Method Bodies) Given an English description of a new kind of object, with specified behaviors, code for the instance variables in the class, and headers for the constructor(s) and instance methods, write the bodies of the constructor(s) and instance methods. Scenario: Suppose you want to implement a Java class that holds information for a student in a course. Specifically, suppose that a student has a name and scores on three tests as whole number values representing the percentage score on each test. Write the bodies for all the constructors and instance methods as documented in the file Student.java, using the given instance variables. Answer for this Core Competency: Practice Problem: Scenario: public class Bottle { See the class Student.java in the Labs folder we will answer this problem as a whole group during class-time. Suppose we are starting work on a program for a game where players try to fill and empty various bottles. We figure out that a bottle has two instance variables how much it can hold (its capacity ) and how much it currently holds (its volume ). Write the bodies (off to the side with arrows showing where the code should be inserted) for all the constructors and instance methods as documented, using the given instance variables. // instance variables: private int cap; private int vol; // construct a new bottle with // default capacity 12, // assuming that a new bottle starts empty public Bottle() { // construct a new bottle with the given capacity // c, assuming that a new bottle starts empty public Bottle(int c) { CS 1050 Spring 2018 Page 77

// add the given amount to the volume of this // bottle, except don t go past the capacity public void add(int amount) { // empty this bottle and return the // volume it had public int empty() { // transfer the volume in this bottle to the // other bottle, not exceeding its capacity, // and empty this bottle public void transferto(bottle other) { // Bottle Core Lab 13.1 [2 lab points] The purpose of this Lab is to give you some hands-on experience with the try-catch exception mechanism in Java. See Section 13.1 (and later sections if you are interested) in the Zybook Here are the basics of exceptions: Many method calls in Java classes throw exceptions. If you look in the Java API at one such, say the method parseint in the Integer class, you will see throws NumberFormatException as part of its header. If a method throws an exception, you have to handle it somehow. By far the best way is to use a try-catch statement around the method call so the possible exception can be handled right there (as opposed to putting throws Exception in the header of all the methods in the chain leading to the one that throws the exception). Here is the basic template for a try-catch statement: try { // put code here that includes the method call that can // throw an exception // if an exception is thrown, execution jumps below to // the corresponding catch block CS 1050 Spring 2018 Page 78

catch (Exception e) { // put code here to report the problem // or otherwise handle it For example, see lines 67 91 in the Noteboard.java file in the folder Noteboard in the folder Session18Shultz. Download the file LabCh13GetInt.java and write the body of the method getint so that it behaves as documented in the file and the test case below. Hints: your getint method will have a while loop with a try-catch statement in its body. This is a good place to use a boolean flag variable, say named done, to exit the loop. Test your code by running LabCh13GetInt and repeatedly typing whatever nonsense you like when the program asks you to enter an integer value. When you finally type a legal int value, the program should compute and display its square. Here is a sample run, with the user inputs shown in italic font:. Enter an integer to be squared: two Error, please enter a legal int: 2 3 Error, please enter a legal int: 2.7 Error, please enter a legal int: 2 The square is 4.. Core Lab 7.1 [4 lab points] In this Lab you will create a bank simulation starting from scratch. This will give you hands-on experience with object-based features of Java, use of ArrayList, and review of pretty much everything in the course. See your instructor s examples (the Noteboard application for Professor Shultz and the Staff applications for Professor Kramer) of applications that load a bunch of objects from a data file into an ArrayList, allow the user to interactively modify individual objects and the list, and save the list to a data file when the user wants to quit. You will need to make two classes for this Lab. The ATM class simulates the behavior of a simple automatic teller machine. The Account class encapsulates the behavior of. CS 1050 Spring 2018 Page 79

a single customer s account. The ATM class should have as its core instance variables an ArrayList<Account> instance, known as list in the following specifications, and an integer index current holding the location in list of the current account, if there is one, or 1 if there is no current account. You will get to practice designing a class with instance variables, instance methods, and constructors as you create the Account class. Your first step will be to figure out the instance variables for the Account class, like in the upcoming Core Competency DOO (Design Object-Oriented). Your main method in the ATM class will load information from a data file into list. (you will have to create the initial file). Then it will use the pattern as in Core Competency WML (write menu loop) to let the user interactively issue a series of commands. Finally, it will save the current list back to the data file. You can use whatever data file name you prefer. At the top of the loop a menu showing the possible commands should be displayed, along with the name of the current account or a statement that there is no current account. Here are the specifications for the commands to be implemented in the menu-loop (for user convenience, some of the commands are short): Command Mnemonic Actions q quit Ask for the managerial password, and if the input matches the managerial password, save the account data to the data file and halt the program. If the user doesn t enter the correct managerial password, display an appropriate error message. n new account Ask the user for the password they want to use (any string), and for the initial deposit they are making in their savings account (the checking account starts at $0). Find the next available account number and construct a new Account instance with the obvious data. Display on-screen a welcome message telling the customer their account number and asking them to memorize it. The new account should be set as the current account, in case the customer wants to do some transactions. CS 1050 Spring 2018 Page 80

Command Mnemonic Actions i login Ask for an account number (a positive integer). If there is no account in the list with that account number, display an error message. Otherwise, ask for the password for that account. If the password matches the account password, make this account the current account. Otherwise, display an error message and leave the current account unchanged. Note: at the beginning, or after the current account is logged out, the current account location should be set to 1 to signify that there is no current account. After logging in successfully, the current account location is set to the index of that account in the list. All of the commands below should display an error message rather than doing what is described if there is no current account. o log out Set the current account location to 1. ws withdraw savings Ask the user for an amount to withdraw from savings. If the amount is reasonable (positive and not bigger than the savings balance), reduce the savings balance by that amount and display the new balance; otherwise display an error message. wc withdraw checking Same as ws but with the checking account of the current account. CS 1050 Spring 2018 Page 81

Command Mnemonic Actions ds deposit savings Ask the user for an amount to deposit to savings. If the amount is positive, increase the savings balance by that amount and display the new balance; otherwise display an error message. dc deposit checking Same as ds but with the checking account. t transfer Ask the user for an amount to transfer from savings to checking, and if the amount is reasonable, transfer it and display the new balances; otherwise display error message. c close account Display a message showing the total funds being given to the user due to account closure, and then remove the current account from list. After this current should be set to 1. You should use your getint method from Core Lab 13.1 (copy and paste it into the ATM class) to obtain all monetary amounts as integer numbers of dollars. You can choose whatever managerial password you wish. Be sure to thoroughly test your application before asking to have it checked. Be sure to test all the things that should produce error messages, and all the transactions. Be sure to have at least five accounts in your testing. CS 1050 Spring 2018 Page 82