230 likes | 250 Views
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 semester Picture beachscene = new Picture(“beach.jpg”);
E N D
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 semester Picture 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 task • What 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 nouns • Slide 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 code public 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 code public 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 file • With the same name as the class: SlideShow.java • Class names • Are singular (SlideShow not SlideShows) • Start with an uppercase letter • The rest of the word is lowercase • Uppercase 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.java • Click 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 • Syntax • visiblity type name; • visibility type name = expression; • Usually use private for the visibility • So that other classes can’t access it directly • The type is any of the primitive types, a class name , or an interface name • Arrays 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 = 0 • Objects = 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 following • SlideShow 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 class • Add extends Parent to the class declaration public class SlideShow extends Object • A declaration of public class SlideShow • Is the same as public class SlideShow extends Object
Summary • Object-oriented programs • Have interacting objects • To decide what classes to create • Identify the objects doing the action or being acted upon • And classify them (what type of thing are they?) • To declare a class • public class SlideShow{} • To declare a field • private type fieldName;
Assignment • Read Media Computation Chapter 11, Sections 1-2