6.2 Classes “ A class is basically a structure with member functions as well as member data. Classes are central to the programming methodology known as.

Slides:



Advertisements
Similar presentations
Understanding an Apps Architecture ASFA Computer Science: Principles Fall 2013.
Advertisements

Java Card Programming: Overview Presented by: Raman Sharykin PISCES Group: Soumyadeb Mitra, Sruthi Bandhakavi, Ragib Hasan, Raman Sharikyn University.
6.6 – Finding Rational Zeros. The Rational Zero Theorem If f(x) = a n x n + … + a 1 x + a 0 has integer coefficients, then every rational zero of f has.
Slides modified by Erin Chambers Problem Solving and Algorithm Design.
Visual Basic: An Object Oriented Approach 2 – Designing Software Systems.
Software Design Deriving a solution which satisfies software requirements.
1 CSE1301 Computer Programming: Lecture 23 Algorithm Design (Part 1)
12 Pontoon1May Pontoon program CE : Fundamental Programming Techniques.
Representing a Game Board In a game, we represent the action taking place using an array – In a very simple game, we use individual variables to represent.
Chapter 3 Collections. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 3-2 Chapter Objectives Define the concept and terminology related.
Computer Science 1620 Programming & Problem Solving.
Chapter 1 Program Design
1 CSE1301 Computer Programming: Lecture 25 Software Engineering 2.
Chapter 9 Domain Models 1CS6359 Fall 2012 John Cole.
Excel Battleship.
CPT 140 Programming Constructs1 OBJECT ORIENTED TECHNOLOGY Terminology and Basic Concepts.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
1 CSC103: Introduction to Computer and Programming Lecture No 13.
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
How to make Space Invaders
OOD Case Study (For parallel treatment, see Chapter 2 of the text)
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
11 Finding Winners Using Arrays Session 8.2. Session Overview  Find out how the C# language makes it easy to create an array that contains multiple values.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
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.
1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: An Aside: The Quickest Tour through the UML that you will ever get.
Once the opposing player has scored a hit on all of the space for a particular ship, you must call out, “Hit…you sunk my ___________”. Once a player has.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Buffers Let’s go for a swim. Buffers A buffer is simply a collection of bytes A buffer is simply a collection of bytes – a char[] if you will. Any information.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order.
The Software Development Process
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Solving Kriegspiel endings with brute force: the case of KR vs. K Paolo Ciancarini Gian Piero Favini University of Bologna.
Software Development. Software Development Loop Design  Programmers need a solid foundation before they start coding anything  Understand the task.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
M1G Introduction to Programming 2 5. Completing the program.
Intermediate 2 Computing Unit 2 - Software Development.
Computer Game Design ActionScript is… Object-oriented programming Everything you do in ActionScript does something to some object* Some objects.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
Chapter 7 Problem Solving with Loops
CS Data Structures I Chapter 2 Principles of Programming & Software Engineering.
Physics Vector Battleship Activity (Also known as the ClassCraft Activity: “Conquest of the Seven Seas”)
Warship C++: An entity of Battleship
Problem Decomposition and Abstraction. Problem Solving Which one is easier:  Solving one big problem, or  Solving a number of small problems?
Alice in Action with Java Chapter 4 Flow Control.
1 The Software Development Process ► Systems analysis ► Systems design ► Implementation ► Testing ► Documentation ► Evaluation ► Maintenance.
Programming for Art: Arrays – 2D ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 16 Fall 2010.
Battleships. You need to know How to draw two 11 x 11 grids How to read off names of squares.
2-dimensional Arrays A 2-dimensional array has rows and columns It is actually an “array of arrays” A Tic-Tac-Toe board is an example of a 3 by 3 2-d array.
What About Alice? In Alice, we have seen that we have:  Objects (skater, snowman, snowwoman, camera)  Methods (move, turn, roll, move toward)  Properties.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Lab Instructor: Salem Othman
CMPT 120 Topic:  Case Study.
CS 326 Programming Languages, Concepts and Implementation
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Structures Cont. Dr. Xiao Qin Auburn.
Lesson Plan Title: Sea Battle (Paper-pencil Game) Level: Middle
Battleship
Topic 1: Problem Solving
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

6.2 Classes “ A class is basically a structure with member functions as well as member data. Classes are central to the programming methodology known as object-oriented programming.”

Procedural Programming In procedural programming, we break down a program in terms of functions. Top-down design principles start with the problem at hand and finds a solution by breaking down the problem into steps that can be used to solve it. We begin with the major steps, and then break those down into smaller steps. This process is called stepwise-refinement.

Result The result is a program in which functions play the major role. Data is merely passed to and from functions. Data may be structured (arrays, structs), but is secondary to the flow of control of the program. This is the procedural programming paradigm.

Object-Oriented Design In object-oriented design, instead of thinking in terms of functions, we think in terms of objects. Objects are collections of related data along with the functions that apply to that data. Objects occur naturally in many problems, and so by thinking about what objects are needed to solve a problem, we often can create better solutions.

OOD - Example Let's take a simple example: A clock. What is the data associated with a clock? What functions do we normally perform on that data?

OOD – Another Example A second example is a bank account. Let's take a simple form of a bank account – a checking account with interest. What is the data? balance, APR What are the functions? deposit, withdrawal, check balance, modify APR, check APR

Implementation In object-oriented programming, we call the functions that are part of an object its methods. The data is stored in fields or components.

Object-Oriented Design In object-oriented design the problem is decomposed into different objects that are inherent in the problem. The program relies on the interaction between the objects. The objects can call each others' methods. This is called message passing. A message is the name (and parameters) of the method to be called. Different types of objects may have methods with the same name, so which method is actually run depends on the object and the message.

OOD Example – Battleship Game Consider the game Battleship. An example can be found at This is a two player game, where each player has a board full of ships, unseen by the other player. The players take turns shooting at squares on their opponents board. The opponent indicates when a ship is hit and when a ship is sunk (all spots are hit). The game is over when one player sinks all of the opponent's ships.

Battleship Implementation What objects are necessary? Ship Water Hit Miss Board Player?

Ships and Other Objects Ship objects would have the following fields: name, position?, size, number of hits, symbol (for non-GUI version)‏ Ship objects would have the following methods: shootAt, place?, anything else? Shooting a ship would increment the number of hits, and if the size is reached, sink the ship. The other classes – Water, Hit, and Miss, would also have a shootAt method.

Board Object The board would have a two dimensional array of Things (where a Thing is a Ship, Water, Hit, or Miss). The board would also have to remember the number of ships sunk. The board would have the following methods: shootAt(x,y), display, placeShips.

Object Interaction The main program would prompt the user for a shot, say (x,y), then call the board's shootAt(x,y) method (send the shootAt(x,y) message to the board). The board object would, in turn, look up what object is at (x,y) (a Ship, Water, Hit, or Miss) and send a shootAt message to that object. The board does not care what object is there. It sends the same shootAt message to the object, and depending on the object, the appropriate method is called.

Ship ShootAt A ship object would react to the shootAt method by incrementing its number of hits (numHits) field, and if numHits == size, send a message to the board to increment its number of ships sunk (incrShipsSunk). The object would also return an object that is to replace it on the board, in this case, a Hit object, which looks different, and avoids the problem of counting a second hit if the player shoots at the same spot more than once.

Pseudo-code The Ship's shootAt method would look like: if(++numHits == size)‏ send the incrShipsSunk message to the board, return Hit; The Board's shootAt(x,y) method would look like: send the shootAt message to what's at (x,y), and set that position to be whatever is returned. Water's shootAt method would just return a Miss. Hit and Miss would just return a Hit and Miss object, respectively.

Main Program The main program would first create the board and place the ships, and then have a loop which: Reads in the player's move – (x,y)‏ Sends the shoot(x,y) message to the board Displays the result Continues until the board says that all the ships are sunk (this would mean asking the board if all the ships are sunk)‏