CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a...
-
Upload
gabriel-lamb -
Category
Documents
-
view
214 -
download
0
Transcript of CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a...
![Page 1: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/1.jpg)
CSC1401Classes - 1
![Page 2: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/2.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/3.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/4.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/5.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/6.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/7.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/8.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/9.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(); }
}
![Page 10: CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/10.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/11.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/12.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/13.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/14.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/15.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/16.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/17.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/18.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/19.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/20.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/21.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/22.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062517/56649f0e5503460f94c231c6/html5/thumbnails/23.jpg)
Assignment
Read Media Computation Chapter 11, Sections 1-2