University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Arrays and Class Design Lecture 18, Tue Mar 14 2006

Slides:



Advertisements
Similar presentations
1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Advertisements

Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
1 Various Methods of Populating Arrays Randomly generated integers.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Interfaces, Polymorphism Lecture 20, Tue Mar
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 1 Interfaces, Polymorphism II Lecture 21, Thu 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
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Arrays II Lecture 15, Thu Mar
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Lecturer: Fintan Costello Welcome to Hdip 001 Introduction to Programming.
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.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 20, Fri Mar
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops I Lecture 17, Fri Feb
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
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 30, Mon Mar
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 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 Lecture 12, Mon Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design II Lecture 31, Wed Mar
Chapter 7 & 8- Arrays and Strings
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 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
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.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
CSC Programming I Lecture 8 September 9, 2002.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
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!
CS 101: Introduction to computer programming and utilization Abhiram Ranade.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
1 CS 177 Week 12 Recitation Slides Running Time and Performance.
Homework #2: Functions and Arrays By J. H. Wang Mar. 20, 2012.
WEEK 6 Class Activities Lecturer’s slides.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Lecture 7: Arrays Tami Meredith. Roadmap Variables Arrays Array indices & Using arrays Array size versus length Two dimensional arrays Sorting an array.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Two Dimensional Arrays. Students will be able to: code algorithms to solve two- dimensional array problems. use 2-D arrays in programs. pass two-use 2-D.
OOP Basics Classes & Methods (c) IDMS/SQL News
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Problem of the Day  Why are manhole covers round?
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
For Friday Finish reading chapter 9 WebCT quiz 17.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design II Lecture 7, Thu Jan
Lecture 22: Number Systems
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
Presentation transcript:

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Arrays and Class Design Lecture 18, Tue Mar based on slides by Kurt Eiselt

2 News n Midterm 2: Thu Mar 16, 6:30pm n Woodward 1&2 n hour-long exam, reserve 6:30-8 time slot n for buffer in case of fire alarms etc n Assignment 2 was due Friday 5pm n Internet crash at 4:45pm led to 24-hr extension to 5pm Sat n hardcopy was due Mon noon n no labs/tutorials this week n but one TA will be in lab during normal lab hours to answer questions

3 Midterm 2 n coverage: through arrays (Chap 8) n includes/builds on material covered in previous midterm n reading n Chap 1, 2, 3, 4, 6, 7 (not 5) n Chap 8.1, n study tips n write and test programs, not just read book n try programming exercises from book!

4 Reading n This week: ,

5 n now that we know 2D, we can do nD! n any number of dimensions: 3D, 4D… n up to 255D, actually n example: student quiz scores over multiple terms n row: students n col: quiz scores n stack: term Recap/Correct: Multidimensional Arrays Term Term Term Term2

6 Let's assume that your computer could make 1 billion (1,000,000,000) comparisons per second. That's a lot of comparisons in a second. And let's say your computer was using selection sort to sort the names of the people in the following hypothetical telephone books. Here's some mathematical food for thought. phone book number of N 2 number of people (N) seconds needed to sort Vancouver 544, ,284,262, or 5 minutes Canada 30,000, ,000,000,000, ,000 or 10.4 days People's Republic 1,000,000,000 1,000,000,000,000,000,000 1,000,000,000 or 31.7 years of China World 6,000,000,000 36,000,000,000,000,000,000 36,000,000,000 or 1142 years Recap: Sorting in N^2 Time

7 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 unchnged) 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

8 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 >

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

10 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]); }

11 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.

12 Favorite Colors (If Time…) n record everybody's favorite color n how can we do "averages" per row?