Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Help Session How To Get Started Design Parameters Accessor and Mutator Methods."— Presentation transcript:

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

2 LiteBrite Help Session 2 of 14 September 21, 2014 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.

3 LiteBrite Help Session 3 of 14 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 LiteButton s. 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. List the nouns. Which ones become classes? September 21, 2014

4 LiteBrite Help Session 3 of 14 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 LiteButton s. 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. List the nouns. Which ones become classes? September 21, 2014

5 LiteBrite Help Session 4 of 14 And then there was Lite... Support Code September 21, 2014 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

6 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 And then there was Lite... Support Code (Continued)

7 LiteBrite Help Session 6 of 14 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 But, there are problems with this design, such as... --How will the Grid know when the user selects a new color on the Palette ? September 21, 2014

8 LiteBrite Help Session 7 of 14 The Preferred Design Associate the Grid with the Palette o Do this by passing the Palette as a parameter to the Grid ‘s constructor, as outlined in the stencil code setColor method - mutator o make sure the current color is “set” in the setColor method already outlined by the stencil code o for you inquisitive folk: when the user clicks one of the LiteButton s, this method magically gets invoked by our support code getColor method - accessor o make a method in the Palette class to “get” its current java.awt.Color o the Grid can “get” the current color from the Palette by calling this 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. September 21, 2014

9 LiteBrite Help Session 8 of 14 Containment Diagram App LiteBrite Grid Palette Lite LiteButton September 21, 2014

10 LiteBrite Help Session 9 of 12 Formal vs. Actual Parameters public void add(int a, int b) { //arithmetic elided } --What’s the formal parameter? --What’s the actual parameter? _calculator.add(4,2); 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)‏ September 21, 2014 Method Invocation (from another class) Method Declaration

11 LiteBrite Help Session 10 of 12 On your mark, “GET” “SET”, go! 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; } September 21, 2014

12 LiteBrite Help Session 9 of 12 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(); } September 17, 2010 LiteBrite Help Session 11 of 12 September 21, 2014 What is the value of _whatsPlaying when the Car is instantiated?

13 Let There Be LiteBrite


Download ppt "Help Session How To Get Started Design Parameters Accessor and Mutator Methods."

Similar presentations


Ads by Google