University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 30, Mon Mar 29 2010

Slides:



Advertisements
Similar presentations
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Interfaces, Polymorphism Lecture 20, Tue Mar
Advertisements

CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Inheritance II Lecture 23, Thu Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Mathematical Operations, Static Methods Lecture 9, Thu Feb 2.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Interfaces, Polymorphism II Lecture 21, Thu Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Interfaces, Inheritance Lecture 22, Tue Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Advanced Class Design Lecture 19, Thu Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Class Design Lecture 9, Mon Jan
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 11, Fri Jan
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Arrays II Lecture 15, Thu Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Mathematical Operators, Static Methods Lecture 14,
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design Lecture 6, Tue Jan
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 2D Arrays, Sorting Lecture 16, Tue Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Static Methods, Conditionals Lecture 15, Mon Feb 8.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 20, Fri Mar
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops I Lecture 17, Fri Feb
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Arrays II Lecture 22, Wed Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
COMP2004 Programming Practice Sam Holden Department of Computer Science University of Sydney.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops Lecture 12, Tue Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Interfaces, Polymorphism Lecture 29, Fri Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 2D Arrays, Sorting Lecture 23, Fri Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Arrays and Class Design Lecture 18, Tue Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Arrays Lecture 21, Mon Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design II Lecture 31, Wed Mar
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Objects, Class Design Lecture 8, Fri Jan
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Yet More Array Practice Lecture 24, Mon Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Interfaces, Polymorphism Lecture 28, Wed Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 13, Wed Feb
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops II Lecture 18, Mon Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design III, Parameter/Scope Review Lecture.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
Moving from C to Java. The Java Syntax We Know Java Types Some of the types we know and love are still there  Integer: byte, short, char, int, long.
Week 2 - Monday.  What did we talk about last time?  Exceptions  Threads  OOP  Interfaces.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Lecture 9.4 Java Interfaces. © 2006 Pearson Addison-Wesley. All rights reserved Java does not support multiple inheritance. Interface Characteristics...
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Homework #3: Classes and Constructors By J. H. Wang Apr. 24, 2015.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
OOP Basics Classes & Methods (c) IDMS/SQL News
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Arrays and Sorting. Process Open a file that contains integers, one per line. Read each line, convert to short and store each into an array Sort the array.
Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;
Problem of the Day  Why are manhole covers round?
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design II Lecture 7, Thu Jan
Lecture 13: More Arrays Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
slides created by Ethan Apter
Java Programming Language
CSE 143 Lecture 2 ArrayIntList, binary search
slides created by Ethan Apter
Presentation transcript:

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 30, Mon Mar borrowing from slides by Kurt Eiselt

2 News n midterm 2 exam papers handed back today n raw and scaled scores were made visible Fri n to reach me, send real to n please do NOT use the WebCT/Vista Mail n A3 will be out Wed since A2 due date extension to Tue

3 News: Labs Reminder n week 10 is standard lab n week 11 optional midterm review/correction n solutions handed out at end of week 12 labs n week 12 is standard lab

4 News: n To reach me, send real to n please do NOT use the WebCT/Vista Mail

5 Reading n Weeklies due either this Wed 3/31 or next Wed 4/7 (since no class Fri, Mon) n This week: n (3rd ed) n (2nd ed)

6 Recap: Interfaces as Contract n Can write code that works on anything that fulfills contract n even classes that don’t exist yet! n Example: Comparable n useful if you need to sort items n compareTo(object) n returns int < 0 if this object less than parameter n returns 0 if same n returns int > 0 if this object greater than parameter

7 Recap: Comparable n sort method that works on array of objects of any type that implements Comparable n type guaranteed to have compareTo method n we need to sort n Bunny n Giraffe n String n...

8 Recap: Wrappers n Many classes implement Comparable interface n Byte, Character, Double, Float, Integer, Long, Short, String n each implements own version of compareTo n Wrapper classes n wraps up (encapsulates) primitive type n Double: object wrapping primitive double n No: sort( double[] myData ); n Yes: sort( Double[] myData );

9 Recap: Multiple Interfaces n Classes can implement more than one interface at once n contract to implement all abstract methods defined in every interface it implements public class MyClass implements Interface1, Interface2, Interface3 { }

10 Recap: Selection Sort For Int Primitives // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); }

11 Finishing Comparable Code

12 Question 4: [15 marks] Now let’s use Java to simulate bunnies! (Why? Because everybody likes bunnies!) In our simulation, each bunny is on a grid at some location defined by an X-coordinate and a Y-coordinate. Also, each bunny has some number of energy units measured in carrot sticks. (X-coordinates, Y-coordinates, and the number of carrot sticks are integer values.) Bunnies can hop north, south, east, or west. When a bunny hops to the north, the bunny’s Y-coordinate is increased by 1, and the X-coordinate remains unchanged. When a bunny hops to the west, the bunny’s X-coordinate is decreased by 1, and the Y-coordinate remains unchanged. Same idea for hops east (X-coordinate increased by 1, Y-coordinate unchanged) and south (Y-coordinate decreased by 1, X-coordinate unchanged). Note that making one hop requires a bunny to eat one carrot stick, and when a bunny has eaten all of his or her carrot sticks, that bunny can 't hop. Use Java to create a Bunny class which can be used to generate Bunny objects that behave as described above. When a new Bunny object is created, the Bunny always starts at coordinates X = 10, Y = 10, and the Bunny has 5 carrot sticks. Your Bunny class definition must include a hop(int direction) method, and a displayInfo() method. The direction parameter is 12 for north, 3 for east, 6 for south, and 9 for west (like a clock face). The hop() method should test to make sure that the Bunny has not eaten all the carrot sticks – if the Bunny still has carrot sticks, the hop() method should update coordinates as explained above and print the message “hop”. If no carrot sticks remain, it should just print the message “This bunny can’t hop”. The displayInfo() method should print the Bunny’s location and number of remaining carrot sticks. Below is a simple test program that could be used to test your Bunny class definition, followed by the output we’d expect to see when using this test program with your Bunny class definition. Bunny Class Warmup

13 public class BunnyTest { public static void main(String[] args) { System.out.println("Testing Peter"); Bunny peter = new Bunny(); peter.displayInfo(); peter.hop(12); peter.hop(9); peter.displayInfo(); System.out.println("Testing Emily"); Bunny emily = new Bunny(); emily.displayInfo(); emily.hop(9); emily.hop(12); emily.hop(9); emily.hop12(); emily.displayInfo(); } > java BunnyTest Testing Peter This bunny is at position 10,10 This bunny has 5 carrot sticks remaining hop This bunny is at position 9,12 This bunny has 2 carrot sticks remaining Testing Emily This bunny is at position 10,10 This bunny has 5 carrot sticks remaining hop This bunny can't hop This bunny is at position 6,11 This bunny has 0 carrot sticks remaining >

14 More Bunnies How could we keep track of a herd of bunnies? We could make an array of bunnies.

15 More Bunnies public class BunnyTest1 { public static void main (String[] args) { Bunny[] myBunnyHerd = new Bunny[10]; myBunnyHerd[0] = new Bunny(3,6,4,"Foofoo"); myBunnyHerd[1] = new Bunny(7,4,2,"Peter"); myBunnyHerd[3] = new Bunny(9,2,3,"Ed"); for(int i = 0; i < myBunnyHerd.length; i++) { if (myBunnyHerd[i] != null) { myBunnyHerd[i].hop(3); System.out.println(myBunnyHerd[i]); }

16 Even More Bunnies Question 5: [16 marks] The world desperately needs better bunny management software, so please help by writing a BunnyHerd class. A BunnyHerd object holds an array of Bunny objects. Your BunnyHerd class definition should include the following four methods: constructor Expects two parameters, an integer representing the maximum number of bunnies in the herd, and a String for the name of the herd. addBunny(int xPos, int yPos, int carrots,String name) Expects four parameters, the X- and Y-coordinates of the bunny, the number of carrots, and the name. This method creates a new Bunny object and stores the reference to the object in the next available location in the BunnyHerd object. deleteBunny(String name) Expects one parameter, the name of the bunny. This method removes from the BunnyHerd object all references to bunnies with the given name by overwriting those references with the null pointer. This method does not change the pointer to the next available location in the BunnyHerd object. printHerd() This method uses the toString() method of the Bunny object to print information about every Bunny in the herd.