CSC1401 Classes 3 - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

CSC1401 Classes 3

Description:

Creating an object of type SlideShow. Invoking methods on it. Accessing Fields from Other Classes ... Creating SlideShow Accessors. Add a method to get the ... – PowerPoint PPT presentation

Number of Views:437
Avg rating:3.0/5.0
Slides: 16
Provided by: Wanda6
Category:

less

Transcript and Presenter's Notes

Title: CSC1401 Classes 3


1
CSC1401Classes - 3
2
Learning Goals
  • Computing concepts
  • Reviewing methods
  • Using our class

3
What methods will we want to add?
  • A method to show the slide show
  • A method to add a picture to the slide show
  • One or more constructors
  • To initialize the class variables, as well as
    doing other class initialization
  • Required by Java

4
Discussion
  • Looking at the variables in our class, we see
    that some are class-level (for use by any
    method in the class), and some are local (for use
    only within a method)
  • What is the difference?

5
Using our class
  • Creating an object of type SlideShow
  • Invoking methods on it

6
Accessing Fields from Other Classes
  • Fields are usually declared to be private
  • So that code in other classes cant directly
    access and change the data
  • Try this in the main
  • System.out.println(showObj.cells)
  • You will get an exception
  • Short for exceptional event error
  • Outside classes can not use object.field to
    access the field value
  • Unless it is declared with public visibility

7
Accessors and Modifiers
  • Accessors
  • Are public methods that return data
  • In such a way as to protect the data for this
    object
  • Syntax
  • public fieldType getFieldName()
  • Example
  • public String getName() return this.name
  • Modifiers
  • Are public methods that modify data
  • In such a way as to protect the data for this
    object
  • Syntax
  • public returnType setFieldName(type name)
  • Example
  • public void setName(String theName)
  • this.name theName

8
Naming Conventions
  • Accessors also called Getters
  • Use getFieldName for non boolean fields
  • Use isFieldName for boolean fields
  • Modifiers also called Setters and Mutators
  • Use setFieldName
  • Sometimes return a boolean value to indicate if
    the value was set successfully
  • Examples
  • getName and setName

9
Creating SlideShow Accessors
  • Add a method to get the wait time
  • public int getWaitTime()
  • What about a method to get the array of
    pictures?
  • If someone gets the array s/he can directly
    change the pictures in the array
  • It is safer to return the picture at an index
  • Then other classes cant directly change the
    array of pictures

10
Exercise
  • Create a method that returns the wait time
  • Create a method that returns the picture at a
    given index in the array
  • If the index isn't valid return null

11
Creating Slide Show Modifiers
  • We need public methods
  • That let other classes set the time to wait
    between pictures
  • Our class is responsible for making sure this
    only happens in such a way
  • as to keep the data valid and not cause errors
  • Setting the wait time
  • The wait time must be 0

12
Wait Time Modifier
  • public void setWaitTime(int time)
  • // check that it is a valid wait time
  • if (time 0)
  • this.waitTime time

13
Testing the class
  • Add a class and containing a main method
  • public static void main(String args) throws
    Exception
  • SlideShow vacShow new SlideShow()
  • vacShow.addPicture(23beach.jpg)
    vacShow.addPicture(23blueShrub.jpg)
  • vacShow.addPicture(23butterfly.jpg)
    vacShow.addPicture(23church.jpg)
    vacShow.addPicture(23redDoor.jpg)
  • vacShow.show()

14
Summary
  • Classes have fields, constructors, and methods
  • Constructors are used to initialize fields in the
    object
  • Fields are usually declared to be private
  • To protect the data from misuse by other classes
  • So you need to provide public accessor (getter)
    and modifier (setter) methods
  • That still protect the data
  • Use a main method to begin execution
  • public static void main(String args)
  • The book places the main method into the
    SlideShow class while this is ok to do, well
    generally keep the main in a separate class

15
Assignment
  • Read Media Computation Chapter 11, Sections 3-6,
    8
  • Please read the text, as it uses different
    examples!
Write a Comment
User Comments (0)
About PowerShow.com