Help Session How To Get Started Design Parameters

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 41 Defining Classes and Methods Chapter 4.
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
UML Basics & Access Modifier
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 8 More Object Concepts
Writing Classes (Chapter 4)
PARAMETERS Making Messages Specific Setting Up Associations Actual and Formal Parameters Return Types Accessor and Mutator Methods.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Local Variables Garbage collection. © 2006 Pearson EducationParameters2 of 10 Using Accessors and Mutators Together What if you want to get a property.
CH 8 : Enhancing Classes - Review QUICK REVIEW : A Class defines an entity’s (Object’s) data and the actions or behaviors (Methods) associated with that.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
ECE122 Feb. 22, Any question on Vehicle sample code?
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
Chapter 5 Introduction to Defining Classes
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 ArrayLists Section 9.9.
1 Introduction to Object Oriented Programming Chapter 10.
Chapter 11 An introduction to object-oriented design.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Java Programming: Guided Learning with Early Objects
Intro To Classes Review
Chapter 3: Using Methods, Classes, and Objects
Clicker quiz 10/1/13 CSE 1102 Fall 2013.
Lecture 14 Writing Classes part 2 Richard Gesick.
Using local variable without initialization is an error.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Java Programming with BlueJ
Defining Your Own Classes Part 1
Corresponds with Chapter 7
Defining Classes and Methods
Classes, Encapsulation, Methods and Constructors (Continued)
Defining Classes and Methods
Classes, Objects, Methods and Strings
Defining Classes and Methods
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
CSG2H3 Object Oriented Programming
Day 11 The Last Week!.
Chapter 4 Test Review First day
Presentation transcript:

Help Session How To Get Started Design Parameters 1 Help Session How To Get Started Design Parameters Accessor and Mutator Methods

LiteBrite Help Session 2 Okay, so now what? In your last lab we introduced you to writing your own class. Now you are going to have to write several of your own classes. Before you start, make sure you understand how to design your entire program. Identify the nouns and verbs in the program specification to determine the objects (nouns) and methods (verbs) you will need in the program. First lab helped get students familiar, but now its time to apply it. We are going to go over the design during the help session. What are some of the objects and methods needed? LiteBrite Help Session 2 of 14 September 21, 2014 2

Which ones become classes? 3 Getting Started Assignment Specification (The Specs)‏ Make LiteBrite! When the user clicks on the grid, a colored peg should be inserted at that position. There should be a palette with at least two color choices in the form of LiteButtons. The palette should have a current color specified by whichever LiteButton was clicked last. When a peg is added to the grid it should correspond to the palette’s current color. Underline the nouns that might need to be made into classes. List the nouns. Which ones become classes? LiteBrite Help Session 3 of 14 September 21, 2014 3

Which ones become classes? 4 Getting Started Assignment Specification (The Specs)‏ Make LiteBrite! When the user clicks on the grid, a colored peg should be inserted at that position. There should be a palette with at least two color choices in the form of LiteButtons. The palette should have a current color specified by whichever LiteButton was clicked last. When a peg is added to the grid it should correspond to the palette’s current color. Underline the nouns that might need to be made into classes. List the nouns. Which ones become classes? LiteBrite Help Session 3 of 14 September 21, 2014 4

LiteBrite Help Session And then there was Lite... Support Code We are providing you with partially written (“stencil”) Grid and Palette classes as well as completely written support classes cs015.prj.LiteBriteSupport.Lite and cs015.prj.LiteBriteSupport.LiteButton for the pegs and color buttons, respectively. Their methods are described in the “Support Classes” section of your assignment handout. For this program, you will have to: Fill in the Grid and Palette classes Call on the support code (you never have to edit it, and cannot ever edit it) Create a simple top-level class containing Grid and Palette These are the support classes we give you, and we also give a stencil of grid and palette. Top Level class may seem redundant for this program, but it’s a good idea to get into a habit of making it for future assignments. Use the support code. LiteBrite Help Session 4 of 14 September 21, 2014

Support Code (Continued) LiteBrite Help Session And then there was Lite... Support Code (Continued) As with any CS15 program, you will need an App class to get things started! This class is partially written for you, but you will have to fill in the rest. You should not do anything except instantiate the top-level object in your program! LiteBrite Help Session 5 of 14 September 21, 2014

LiteBrite Help Session 7 A (not so) Great Design Problem: How do you make the pegs the same color as the current palette color? One Possible Design: --Have the Grid know about the Palette’s current color when the Grid is created --The Grid can store this color in an instance variable and use it to set the peg colors This is a bad design because the grid only knows about the palette’s color when it is created. What if the palette changes its color later? The grid will have no idea and keep making all pegs the same color. How about associating the grid with the palette so the grid can always ask the palette what its current color is? But, there are problems with this design, such as... --How will the Grid know when the user selects a new color on the Palette? LiteBrite Help Session 6 of 14 September 21, 2014 7

LiteBrite Help Session 8 The Preferred Design Associate the Grid with the Palette Do this by passing the Palette as a parameter to the Grid‘s constructor, as outlined in the stencil code setColor method - mutator make sure the current color is “set” in the setColor method already outlined by the stencil code for you inquisitive folk: when the user clicks one of the LiteButtons, this method magically gets invoked by our support code getColor method - accessor make a method in the Palette class to “get” its current java.awt.Color the Grid can “get” the current color from the Palette by calling this accessor method -- Why is this better? Using this design, a new Grid won’t have to be constructed each time the Palette’s color changes, and the Grid will have accurate information about the Palette’s current color. The Grid will be able to use its association with the Palette to ask what its current color is each time a new peg is created, and then color the peg accordingly. -- What would happen if the Grid knew directly about the color? It is considered very bad design to allow direct access to other classes’ variables. This is why we declare classes’ instance variables as PRIVATE. If the Grid knew directly about the Palette’s color, it would be able to accidentally alter the variable’s value. Instead, we use encapsulation -- that is, we make the Palette’s current color variable reachable only through an accessor method. Why is this better? What happens when the Palette color changes? What would happen if the Grid knew directly about the color? Think about how this affects encapsulation. LiteBrite Help Session 7 of 14 September 21, 2014 8

LiteBrite Help Session 9 Containment Diagram App LiteBrite Here is the Containment Diagram you should model your LiteBrite after. A filled in diamond means that the top class contains more than one instance of the bottom one (e.g. Grid contains many Lites). The arrow means Grid has a reference to palette Grid Palette Lite LiteButton LiteBrite Help Session 8 of 14 September 21, 2014 9

Formal vs. Actual Parameters Two types of parameters --Formal --Actual Formal parameters are used when you are declaring a method Like x in: f (x) = 3x^2 + 5x + 2 Actual parameters are used when actually calling the method Like 5 in: y = f (5)‏ Many methods in Java take parameters. There are two types - formal and actual. Note that the corresponding formal and actual parameters for a method often do not have the same name. However, the actual parameter must be of the same type as the formal parameter, or you will get an error when compiling. --What are the formal/actual parameters? The formal parameter is the parameter named in the method declaration: in this code, it is “Brand carBrand.” The actual parameter is located where the method is actually invoked: here, it is “bentley.” This method call should work, assuming that bentley is a variable of type Brand. Understanding the difference between formal and actual parameters is important for correctly using accessor and mutator methods (see next slide …)‏ Method Declaration public void add(int a, int b) { //arithmetic elided } Method Invocation (from another class) _calculator.add(4,2); --What’s the formal parameter? --What’s the actual parameter? LiteBrite Help Session 9 of 12 September 21, 2014

On your mark, “GET” “SET”, go! LiteBrite Help Session Accessor (get) and Mutator (set) methods -Used to “get” (access) and “set” (mutate or change) variables. public class CDPlayer { private CD _currentCD; public CDPlayer(CD myCD) { _currentCD = myCD; } /** * This is a mutator! */ public void setCD(CD newCD) { _currentCD = newCD; * This is an accessor! public CD getCD() { return _currentCD; By encapsulating _currentCD within the CDPlayer class as private, we know that CDPlayer is the only class which can access (get) and mutate (set) _currentCD. This is beneficial because we don’t want other classes to have the ability to change the _currentCD without the CDPlayer “knowing” about it; hence, only the CDPlayer should have control over and access to its _currentCD. In this case, the setCD method is the Mutator because other classes can call setCD and provide a new CD for the CDPlayer to have as its _currentCD. The getCD() method is the Accessor because other classes can get what the _currentCD is by calling getCD() which returns a CD. LiteBrite Help Session 10 of 12 September 21, 2014

What is the value of _whatsPlaying when the Car is instantiated? Accessor Example public class Car { private CDPlayer _myPlayer; private CD _jazzCD, _classicalCD, _whatsPlaying; public Car() { _jazzCD = new CD(); _classicalCD = new CD(); _myPlayer = new CDPlayer(_jazzCD); _myPlayer.setCD(_classicalCD); this.seeWhatsPlaying(); } public void seeWhatsPlaying() { _whatsPlaying = _myPlayer.getCD(); What is the value of _whatsPlaying when the Car is instantiated? LiteBrite Help Session LiteBrite Help Session 11 of 12 9 of 12 September 17, 2010 September 21, 2014

Let There Be LiteBrite