csc1401 classes - 1. learning goals computing concepts identifying objects and classes declaring a...

23
CSC1401 Classes - 1

Upload: gabriel-lamb

Post on 04-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

CSC1401Classes - 1

Page 2: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

Learning Goals

Computing conceptsIdentifying objects and classes

Declaring a class

Declaring fields

Default field values

Page 3: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

Objects and Classes

We have been using them all semesterPicture beachscene = new Picture(“beach.jpg”);

A big advantage of the picture class was that any method we wrote could be used in any program that had a picture object!We will explore how to create a class from scratch, rather than modifying an existing class

Page 4: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

Identifying Objects and Classes

Object-oriented programs Consist of interacting objects

Which are defined by and created by classes

To identify the objects in a taskWhat are the things that are doing the work or being acted upon?How do you classify them?What data (fields) do they need to know to do the task?What procedures (methods) do they need?

Page 5: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

Identifying the Objects and Classes

Say that we want to write a program to do a slide show

A series of pictures shown one after the other with some time waiting between the pictures

One way to start is to underline the nounsSlide show, picture, wait time

A slide show has pictures and a time to wait between pictures

Page 6: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

How to create a slide show?

We can use an array of pictures

Then, we can loop through the pictures, displaying each picture

Page 7: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

An array of Pictures

Recall how we had arrays of Pixels (when first examining pictures

We can do the same thing here:Picture [] pictureList;

Page 8: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

An array of pictures – allocating memory

To have the picture list contain 5 pictures:pictureList = new Picture [5];

To set the first element in the picture list to be the beach scene:

pictureList[0] = new Picture (“beach.jpg”);

Page 9: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

The resulting codepublic static void main(String[] args) {

Picture[] pictureList = new Picture[5]; pictureList[0] = new Picture(“23beach.jpg"); pictureList[1] = new Picture(“23blueShrub.jpg"); pictureList[2] = new Picture(“23church.jpg"); pictureList[3] = new Picture(“23redDoor.jpg"); pictureList[4] = new Picture(“23butterfly.jpg"); int index; for (index = 0; index < 5; index ++) {

pictureList[index].show(); }

}

Page 10: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

A problem

All 5 pictures show up at once

In a slide show, the pictures are supposed to show up one at a time, and there is a brief pause to wait while the picture is being displayed

Page 11: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

Causing a wait for a Slide Show

Use Thread.sleep(waitTime) to wait for waitTime number of milliseconds

For wait time we can use integer to hold the number of milliseconds to wait

This can cause an exception so write the method to throw Exception by adding throw Exception

Page 12: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

The modified codepublic static void main(String[] args) throws Exception {

Picture[] pictureList = new Picture[5]; pictureList[0] = new Picture(“23beach.jpg"); pictureList[1] = new Picture(“23blueShrub.jpg"); pictureList[2] = new Picture(“23church.jpg"); pictureList[3] = new Picture(“23redDoor.jpg"); pictureList[4] = new Picture(“23butterfly.jpg"); int index; for (index = 0; index < 5; index ++) {

pictureList[index].show();Thread.sleep(2000);pictureList[index].hide();

} }

Page 13: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

Creating a class

This slide show is neat, but I can only use it in the current program

I’d like to be able to reuse it, like we did with the methods in the Picture class

So, let’s create a slide show class!

Page 14: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

Class Definition

Each class is defined in a fileWith the same name as the class: SlideShow.java

Class namesAre singular (SlideShow not SlideShows)Start with an uppercase letterThe rest of the word is lowercaseUppercase the first letter of each additional word

The syntax for a class definition is:visibility class Name {}

Inside the class definition goes:Fields, constructors, and methods

Page 15: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

Class Declaration

To declare a SlideShow class Click on the File-New-Java button in jGRASP

Type in:public class SlideShow

{

}

Save it in SlideShow.javaClick on File then Save

Compile the file

Page 16: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

Class contents

Classes contain variables (state) and methods

We have been writing methods for classes (e.g. the Picture class)

What variables might our class need?Two different kinds of variables within a class:

Local to a method

Class-wide

Page 17: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

Declaring Fields

Syntaxvisiblity type name;visibility type name = expression;

Usually use private for the visibilitySo that other classes can’t access it directly

The type is any of the primitive types, a class name , or an interface nameArrays are declared with [] after the type or after the name

type[] name; or type name[];Names start with a lowercase letter

The first letter of each additional word is uppercased

Page 18: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

Default Field Values

If you don’t specify an initial value for a field It will get one anyway when it is created

Numbers = 0Objects = null (not referring to any object yet)boolean = false

public class SlideShow{ //////////////// fields ///////////////////////////////////////////

private Picture[] pictureArray; private int waitTime;}

Initial value will be null

Page 19: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

Testing the SlideShow Class

Add the fields to the class definition and compile it

Try the followingSlideShow slideShowObj = new SlideShow();

Don’t worry, the class isn’t supposed to do anything yet!

Page 20: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

Next

We need to decide what methods/functions this class will have, and to write them

Page 21: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

All Classes Inherit from Object

If you don’t specify the parent class when you declare a class

The class with inherit from java.lang.Object

You can specify the parent classAdd extends Parent to the class declaration

public class SlideShow extends Object

A declaration of public class SlideShow

Is the same aspublic class SlideShow extends Object

Page 22: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

Summary

Object-oriented programsHave interacting objects

To decide what classes to createIdentify the objects doing the action or being acted upon

And classify them (what type of thing are they?)

To declare a classpublic class SlideShow{}

To declare a field private type fieldName;

Page 23: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values

Assignment

Read Media Computation Chapter 11, Sections 1-2