Presentation is loading. Please wait.

Presentation is loading. Please wait.

Help Session How To Get Started Design Parameters

Similar presentations


Presentation on theme: "Help Session How To Get Started Design Parameters"— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 Let There Be LiteBrite


Download ppt "Help Session How To Get Started Design Parameters"

Similar presentations


Ads by Google