Intermezzo: Problem solving with data structures - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Intermezzo: Problem solving with data structures

Description:

The user can ask for a list of all songs on the CD. 12-3. Step 1: Identify Objects ... This code is not guaranteed to be error free! And it is not complete. ... – PowerPoint PPT presentation

Number of Views:79
Avg rating:3.0/5.0
Slides: 11
Provided by: watt50
Category:

less

Transcript and Presenter's Notes

Title: Intermezzo: Problem solving with data structures


1
Intermezzo Problem solving with data structures
  • How to layout your program
  • How to use the data structures
  • This was an ad-hoc lecture, in which I wrote a
    simple (not complete) program using BlueJ.

2
Example Problem
  • You are asked to implement a jukebox.
  • The jukebox contains a number of CDs.
  • The user can select a CD.
  • The user can ask for a list of all CDs in the
    jukebox.
  • Each CD contains a number of songs.
  • The jukebox starts playing the CD from its first
    song.
  • The user can ask for the next or previous song.
  • The user can ask for a list of all songs on the
    CD.

3
Step 1 Identify Objects
  • Objects
  • Jukebox
  • CD
  • Song
  • Make a class for each object. In BlueJ, I
    created three classes Song, CD, Jukebox.
  • Start with the simplest class.Because Jukebox
    contains CDs and CDs contain Songs, the simplest
    is the Song class.

4
Step 2 Class Song
  • public class Song
  • // Data we want about songs (suggested by
    students)
  • private String title private int length
    // how many minutes
  • // Constructor for objects of class Song
  • public Song(String t, int l) this.length
    l this.title t
  • public void Play() System.out.print("Currently
    playing....") System.out.println(this.title)

5
Step 3 Class CD
  • public class CD
  • // Data we want about CDs
  • private String titleprivate Song songs
    new Song5private int current
  • // Constructor for objects of class CD
  • public CD(String t) // Simplified all CDs
    will have the same songs.. this.title
    t songs0 new Song(My way, 4) songs1
    new Song(yesterday, 2) songs2 new
    Song("no way", 1) songs3 new
    Song("horrible", 7) songs4 new
    Song("boring", 2)
  • public void Start() this.current
    0 songsthis.current.Play()

6
Step 3 Class CD continued
  • public void Next() // First tried it without
    the If statement, // and you get an
    ArrayIndexOutOfBounds error, when // using Next
    to go to a non-existing slot in the // songs
    array
  • if (this.current lt songs.length - 1)
    this.current this.current
    1 songsthis.current.Play() else
    System.out.println("No next track!")
  • // Can add a method Previous in the same way..

7
Step 3 Class CD improved
  • Rather than hard-coding the songs in the CD class
    (which leads to all CDs having the same songs, we
    want the constructor method to take a parameter
  • // Constructor for objects of class CD
  • public CD(String t, Song s) // Still
    simplified // assuming all CDs to have five
    songs.. this.title t songs0
    s0 songs1 s1 songs2 s2
    songs3 s3 songs4 s4

8
Step 4 Class Jukebox
  • public class Jukebox
  • //Data we want about the Jukeboxprivate CD
    cds new CD2private int current
  • // Constructor for objects of class Jukebox
  • public Jukebox() Song s1 new
    Song5 s10 new Song("my way", 4) s11
    new Song("yesterday", 4) s12 new Song(no
    way", 2) s13 new Song(boring", 7) s14
    new Song(horrible", 3) this.cds0 new
    CD("rubbish", s1)
  • Song s2 new Song5 s20 new
    Song("no more way", 4) s21 new Song(no
    more energy", 4) s22 new Song(still
    boring", 7) s23 new Song(no more music",
    4) s24 new Song(very horrible",
    7) this.cds1 new CD("more rubbish", s2)

9
Step 4 Class Jukebox continued
  • public void Play (int y) // Very simplified
    // no error handling for numbers out of
    range // Would be better if user could select
    from list this.cdsy.Start() this.current
    y
  • public void NextTrack() this.cdsthis.curren
    t.Next()

10
Comments at end
  • This code is not guaranteed to be error free!
  • And it is not complete..
  • But it may give you some idea of how to start
    your assignment (if you have not done so yet).
  • Just think of a Castle as being a bit like a
    JukeBox, and a Room as being a bit like a CD
Write a Comment
User Comments (0)
About PowerShow.com