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

Help Session PowerPoint PPT Presentation


  • 97 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 (and a bit more Java code).

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


Help session

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


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

LiteBrite Help Session

4 of 14

September 22, 2013


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 22, 2013


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 22, 2013


Help session

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


Help session

Containment Diagram

App

LiteBrite

Grid

Palette

LiteButton

Lite

LiteBrite Help Session

8 of 14

September 22, 2013


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


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 14

September 22, 2013


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 14

9 of 12

September 17, 2010

September 12, 2013

LiteBrite Help Session


Help session

Good Luck!

Dinosaurs do it.


Help session

Celebrities do it.


Help session

You should do it, too.


  • Login