1 / 23

CSC1401 Classes - 1

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”);

danielah
Download Presentation

CSC1401 Classes - 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC1401Classes - 1

  2. Learning Goals • Computing concepts • Identifying objects and classes • Declaring a class • Declaring fields • Default field values

  3. 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

  4. 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?

  5. 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

  6. How to create a slide show? • We can use an array of pictures • Then, we can loop through the pictures, displaying each picture

  7. An array of Pictures • Recall how we had arrays of Pixels (when first examining pictures • We can do the same thing here: Picture [] pictureList;

  8. 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”);

  9. 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(); } }

  10. 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

  11. 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

  12. 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(); } }

  13. 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!

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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!

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

  21. 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

  22. 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;

  23. Assignment • Read Media Computation Chapter 11, Sections 1-2

More Related