COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Feb 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
MSc IT Programming Methodology (2). number name number.
Tetris – Genetic Algorithm Presented by, Jeethan & Jun.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Chapter 5 Part 1 COSI 11a Prepared by Ross Shaull.
COMP 110 Loops Tabitha Peck M.S. February 11, 2008 MWF 3-3:50 pm Philips
Chapter 10 Arrays. Topics Declaring and instantiating arrays Array element access Arrays of objects Arrays as method parameters Arrays as return values.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
1 COMP 110 Designing and Overloading Methods Tabitha Peck M.S. March 26, 2008 MWF 3-3:50 pm Philips 367.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
CSC 212 – Data Structures Lecture 3: Fields, Methods, & Constructors.
MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal.
Lecture 7 Polymorphism. Review: Constructors ●Parenthesis after constructor, not in class declaration ●The constructor makes the SamBot – do not “ new.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Week71 APCS-AB: Java Control Structures October 17, 2005.
Make Tetris Using Java By bearzx.
CIS 199 Test 01 Review. Computer Hardware  Central Processing Unit (CPU)  Brains  Operations performed here  Main Memory (RAM)  Scratchpad  Work.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
CSC 221: Computer Programming I Fall 2001  top-down design  approach to problem solving, program design  focus on tasks, subtasks, …  reference parameters.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
The Java Programming Language
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
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.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Week 3 – Wednesday.  What did we talk about last time?  ADTs  List implementation with a dynamic array.
Data Types & Operations 1 Last Edited 1/10/04CPS4: Java for Video Games Data Types.
COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014.
Elementary C++. Procedural Programming Split your problem into simpler parts then solve each part separately Recognize common parts and solve them only.
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
COMP 110 Worksheet review, debugger Luv Kohli September 29, 2008 MWF 2-2:50 pm Sitterson 014.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
LECTURE 20: RECURSION CSC 212 – Data Structures. Humorous Asides.
Catie Welsh March 23,  Lab 6 due Friday by 1pm 2.
Error Handling Tonga Institute of Higher Education.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
1 CSC 221: Computer Programming I Fall 2005 simple conditionals and expressions  if statements, if-else  increment/decrement, arithmetic assignments.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
COMP 110 More arrays, 2D arrays, Program 4 Luv Kohli November 10, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110 Arrays Luv Kohli November 5, 2008 MWF 2-2:50 pm Sitterson 014.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Problem of the Day  Why are manhole covers round?
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Questions? Math Class Wrapper Classes Writing / Testing Methods.
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
More About Objects and Methods Chapter 5 Programming with Methods
Presentation transcript:

COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014

Announcements Lab 6 due Friday, November 7, 2pm 2

Questions? 3

Today in COMP 110 Designing methods Overloading methods 4

Tetris 5

Get into small groups Decide what high-level tasks are required for Tetris gameplay to work Assume the graphical display code is taken care of for you 6

Tetris high-level gameplay tasks Choose a random tetromino to give the user User-controlled tetromino manipulation Game-controlled tetromino manipulation (automatically falling) Remove full horizontal lines of blocks Increase user score, level, and speed of falling blocks Check if game is over 7

User-controlled tetromino manipulation High-level task: manipulate tetromino based on user input How can a tetromino be manipulated? ◦ Move ◦ Rotate 8

Moving a tetromino How? Subtasks ◦ Move left ◦ Move right ◦ Move down 9

Rotating a tetromino Subtasks ◦ Rotate left ◦ Rotate right 10

Imagine a Tetromino class public class Tetromino { private int x; private int y; // some other stuff describing this Tetromino’s shape public void moveLeft() { x--; } public void moveRight() { x++; } public void moveDown() { y--; } 11 +y +x

Top-down design Divide and conquer Start with a big problem Decompose problem into smaller subtasks Decompose big subtasks into even smaller subtasks Solve subtasks to solve big problem 12

Game-controlled tetromino manipulation How can we implement automatically falling tetrominoes? What are we trying to do at a high level? ◦ Every n amount of time, make a tetromino move down one space ◦ Need a timer 13

Using the Tetromino class in a game loop public class TetrisGame { private Tetromino userTetr; // gameUpdate() is called once per game loop public void gameUpdate() { //...do some stuff here // check user input, assume userTetr has been properly // instantiated if (userInput == LEFT) userTetr.moveLeft(); else if (userInput == RIGHT) userTetr.moveRight(); else if (userInput == DOWN) userTetr.moveDown(); applyAutoFalling(userTetr); // do some other stuff here } 14

applyAutoFalling method public void applyAutoFalling(Tetromino tetr) { double timeSinceLastAutoFall = // some code to figure out the time since the last fall if (timeSinceLastAutoFall > 0.5) { tetr.moveDown(); } 15

What if we see this behavior? Imagine that we have run the game ◦ A new tetromino appears ◦ The user does not provide any input ◦ The tetromino does not automatically fall, it simply stays where it is What could the problem be? 16

Let’s check applyAutoFalling public void applyAutoFalling(Tetromino tetr) { double timeSinceLastAutoFall = // some code to figure out the time since the last fall if (timeSinceLastAutoFall > 0.5) { tetr.moveDown(); } } What if we had this code? double timeSinceLastAutoFall = 0.0; 17

The problem could be elsewhere What if we had this code inside the class Tetromino? public void moveDown() { y = y; } The moveDown() method does not do what it is supposed to 18

Testing If a subtask (method) does not work, your solution is incorrect Test EVERY method you write 19

Bottom-up testing How do we determine if the error is in applyAutoFalling or moveDown? Test each method individually ◦ If method A calls method B, fully test method B before testing method A ◦ In this case, fully test moveDown before testing applyAutoFalling 20

Driver programs Simple program for only you to test with ◦ Run by you, not your user Call methods with different inputs ◦ Test cases, edge conditions  Positive, negative, zero  true, false  Strings, characters MathUtils example in jGRASP 21

Overloading Using the same method name for two or more methods within the same class We have seen this for constructors already Parameter lists must be different ◦public double average(double n1, double n2) ◦public double average(double n1, double n2, double n3) Java knows what to use based on the number and types of the arguments 22

Method signature A method’s name and the number and types of its parameters signature does NOT include return type Cannot have two methods with the same signature in the same class 23

Gotcha Overloading and automatic type conversion Imagine we have this constructor defined: public Pet(double initialWeight) We create a Pet like this: Pet myPet = new Pet(35); What happens? 24

Gotcha Imagine we have these two constructors defined: public Pet(int initialAge) public Pet(double initialWeight) We create a Pet like this: Pet myPet = new Pet(35); What happens? ◦ We create a pet with age 35, instead of weight

Wednesday Arrays 26