Help Session
This presentation is the property of its rightful owner.
Sponsored Links
1 / 13

Help Session PowerPoint PPT Presentation


  • 60 Views
  • Uploaded on
  • Presentation posted in: General

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 .

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Help session

Help Session

  • How To Get Started

  • Design

  • Parameters

  • Accessor and Mutator Methods


Help session

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.

LiteBrite Help Session

2 of 14

September 21, 2014


Help session

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.

List the nouns.

Which ones become classes?

LiteBrite Help Session

3 of 14

September 21, 2014


Help session

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.

List the nouns.

Which ones become classes?

LiteBrite Help Session

3 of 14

September 21, 2014


Help session

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 containing Grid and Palette

LiteBrite Help Session

4 of 14

September 21, 2014


Help session

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 21, 2014


Help session

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 21, 2014


Help session

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

  • setColormethod - 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

  • getColormethod - 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?

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 21, 2014


Help session

Containment Diagram

App

LiteBrite

Grid

Palette

LiteButton

Lite

LiteBrite Help Session

8 of 14

September 21, 2014


Help session

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 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


Help session

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 12

September 21, 2014


Help session

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 12

9 of 12

September 17, 2010

September21, 2014

LiteBrite Help Session


Help session

Let There Be

  • LiteBrite


  • Login