1 / 14

Help Session

Help Session. How To Get Started Design Parameters Accessor and Mutator Methods. 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 (and a bit more Java code).

shani
Download Presentation

Help Session

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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 (and a bit more Java code). • -- Before you start, make sure you understand how to design your entire program. • -- You should pay attention to the nouns and verbs in the program specification to determine the objects (nouns) and methods (verbs) you will need in the program. LiteBrite Help Session 2 of 14 September 22, 2013

  3. Getting Started Assignment Specification (The Specs)‏ Create your own computerized LiteBrite. When the user clicks on the grid, colored pegs should be added at the proper location. There should be a palette with at least two color choices that are selected using 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 be the palette’s current color. List the nouns. Which ones will we want to make into classes? LiteBrite Help Session 3 of 14 September 22, 2013

  4. And then there was Lite... Support Code • We are providing you with partially written (“stencil”) Gridand Paletteclasses as well as completely written support classes cs015.prj.LiteBriteSupport.Liteand • 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 Gridand Paletteclasses • Call on the support code (you never have to edit it, and cannot ever edit it) • Create a simple top-level class LiteBrite Help Session 4 of 14 September 22, 2013

  5. And then there was Lite... Support Code (Continued) • As with any CS15 program, you will need an Appclass 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 22, 2013

  6. 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? LiteBrite Help Session 6 of 14 September 22, 2013

  7. The Preferred Design A better way to solve this problem: --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 --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) --Provide a method in the Palette class to “get” its current java.awt.Color --Then, the Grid can “get” the current color from the Palette by calling this accessor method Why is this better? -- What happens when the Palettecolor 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 22, 2013

  8. Containment Diagram App LiteBrite Grid Palette LiteButton Lite LiteBrite Help Session 8 of 14 September 22, 2013

  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)‏ Method Declaration public void buyCar(Brand carBrand) { //makes the car the specified brand } Method Invocation (from another class) _buyer.buyCar(bentley); --What’s the formal parameter? --What’s the actual parameter? LiteBrite Help Session 9 of 14 September 22, 2013

  10. 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; } } LiteBrite Help Session 10 of 14 September 22, 2013

  11. 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 11 of 14 9 of 12 September 17, 2010 September 12, 2013 LiteBrite Help Session

  12. Good Luck! Dinosaurs do it.

  13. Celebrities do it.

  14. You should do it, too.

More Related