View
214
Download
0
Category
Preview:
Citation preview
CSC1401Classes - 1
Learning Goals
Computing conceptsIdentifying 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
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?
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
How to create a slide show?
We can use an array of pictures
Then, we can loop through the pictures, displaying each picture
An array of Pictures
Recall how we had arrays of Pixels (when first examining pictures
We can do the same thing here:Picture [] pictureList;
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”);
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(); }
}
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
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
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();
} }
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!
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
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
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
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
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
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!
Next
We need to decide what methods/functions this class will have, and to write them
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
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;
Assignment
Read Media Computation Chapter 11, Sections 1-2
Recommended