+ All Categories
Home > Documents > CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the...

CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the...

Date post: 05-Jan-2016
Category:
Upload: stella-smith
View: 212 times
Download: 0 times
Share this document with a friend
23
CSC1401 Classes - 2
Transcript
Page 1: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

CSC1401Classes - 2

Page 2: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

Learning Goals

Computing conceptsAdding a method

To show the pictures in the slide show

Creating accessors and modifiersThat protect the fields in the object

Using our class

Page 3: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

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 constructorsTo initialize the class variables, as well as doing other class initialization

Required by Java

Page 4: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

The Constructor

Is a method

Named the same as the class name

We do not specifypublic/private/protected

The return type

Page 5: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

What will our constructor initialize?

The size of the array

The array itself

And, maybe some other things as wellWe can continue adding initialization instructions to the constructor later

Page 6: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

A default constructor

No parameters

How big should the array be? Some default value, say 10

What will we initialize?The size

The array

This we need to declare 2 class-level variables to represent these values!

Page 7: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

Default constructor

class SlideShow{int size;Picture [] cells;

SlideShow(){

this.size = 10;this.cells = new picture [size];

} // ends the default constructor} // ends the class

Page 8: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

Adding a second constructor, to specify the size of the array

class SlideShow{

int size;Picture [] cells;

SlideShow(){

this.size = 10;this.cells = new Picture [size];

} // ends the default constructor

SlideShow (int howManyPictures){

this.size = howManyPictures;this.cells = new Picture [size];

} // ends constructor} // ends the class

Note that the two methods have the same name!

The book calls this “overloading constructors”

Other methods may be overloaded as well

Page 9: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

What if we also wanted to be able to initialize the wait time?

class SlideShow{

int size;Picture [] cells;int waitTime; // specified in msec

SlideShow(){

this.size = 10;this.waitTime = 2000;this.cells = new Picture [size];

} // ends the default constructor

SlideShow (int howManyPictures){

this.size = howManyPictures;this.cells = new Picture [size];this.waitTime = 2000;

} // ends constructor

SlideShow (int howLongToWait){

this.size = 10;this.cells = new Picture [size];this.waitTime = howLongToWait;

} // ends constructor

} // ends the class

What is the problem with our class?

How can we fix it?

Page 10: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

Creating a method to add a picture to our slide show

What extra class variables do we need?The number of the cell

Writing this methodMake sure we don’t add more pictures than can fit in the array

How do we handle trying to add more pictures?Not allowing it

Resizing the array (using a second private method)

Page 11: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

A method for showing the slide show

Now that a slide show has an array of slides we would like to

Show the pictures in the array

We can loop through the elements of the array

And show the current pictureAnd wait for the wait time Then hide the current picture

Page 12: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

Create a Method Exercise

Create a method show that will loop through the pictures in the array

Showing the current picture

Waiting till the wait time has passed

Hiding the current picture

Page 13: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

Accessing Fields from Other Classes

Fields are usually declared to be privateSo that code in other classes can’t directly access and change the data

Try this in the mainSystem.out.println(showObj.cells);

You will get an exceptionShort for exceptional event – error

Outside classes can not use object.field to access the field value

Unless it is declared with public visibility

Page 14: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

Accessors and Modifiers

Accessors Are public methods that return data

In such a way as to protect the data for this objectSyntax

public fieldType getFieldName()Example

public String getName() { return this.name;}

ModifiersAre public methods that modify data

In such a way as to protect the data for this objectSyntax

public returnType setFieldName(type name);Example

public void setName(String theName) {this.name = theName; }

Page 15: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

Naming Conventions

Accessors – also called GettersUse getFieldName for non boolean fieldsUse isFieldName for boolean fields

Modifiers – also called Setters and Mutators

Use setFieldNameSometimes return a boolean value to indicate if the value was set successfully

ExamplesgetName and setName

Page 16: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

Creating SlideShow Accessors

Add a method to get the wait timepublic 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 arrayIt is safer to return the picture at an index

Then other classes can’t directly change the array of pictures

Page 17: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

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

Page 18: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

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 timeThe wait time must be > 0

Page 19: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

Wait Time Modifier

public void setWaitTime(int time)

{

// check that it is a valid wait time

if (time >= 0)

this.waitTime = time;

}

Page 20: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

Add a Field Exercise

Add a title field to the SlideShow class Add an accessor to get the value of this fieldAdd a modifier to set the value of this fieldModify the show method to first create a blank picture with the title on it (if the title is non-null) and show that as the first picture in the slide show

Page 21: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

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“);String title = “My vacation”;vacShow.addTitle(title);

vacShow.show(); }

Page 22: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

Summary

Classes have fields, constructors, and methods

Constructors are used to initialize fields in the object

Fields are usually declared to be privateTo 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 executionpublic static void main(String[] args) {}

The book places the main method into the SlideShow class – while this is ok to do, we’ll generally keep the main in a separate class

Page 23: CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

Assignment

Read Media Computation Chapter 11, Sections 3-6, 8

Please read the text, as it uses different examples!


Recommended