CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Advertisements

Written by: Dr. JJ Shepherd
TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
H OW O BJECTS B EHAVE. O VERVIEW Methods use object state Arguments and return types in methods Java passes by value Getters and Setters Encapsulation.
Road Map Introduction to object oriented programming. Classes
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Conditionals-part11 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology Making Text for the Web part 4 Barb Ericson Georgia Institute of Technology March 2006.
Intro-Sound-part21 Introduction to Processing Digital Sounds part 2 Barb Ericson Georgia Institute of Technology Oct 2009.
Georgia Institute of Technology Speed part 3 Barb Ericson Georgia Institute of Technology May 2006.
CPSC1301 Computer Science 1 Chapter 11 Creating Classes part 1.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
The Java Programming Language
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
10-Nov-15 Java Object Oriented Programming What is it?
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
Georgia Institute of Technology Creating Classes part 4 Barb Ericson Georgia Institute of Technology May 2006.
CreatingClasses-SlideShow-part41 Creating Classes part 4 Barb Ericson Georgia Institute of Technology Dec 2009.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Georgia Institute of Technology Creating Classes part 2 Barb Ericson Georgia Institute of Technology June 2006.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
Georgia Institute of Technology More on Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
Written by: Dr. JJ Shepherd
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Intro-Sound-Mod10-part31 Introduction to Processing Digital Sounds part 3 while loop, tracing, for loop, parameters Barb Ericson Georgia Institute of Technology.
Georgia Institute of Technology Making Text for the Web part 2 Barb Ericson Georgia Institute of Technology March 2006.
ManipulatingPictures-part31 Manipulating Pictures, Arrays, and Loops part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 3
Creating and Modifying Text part 2
Barb Ericson Georgia Institute of Technology Dec 2009
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Georgia Institute of Technology
Barb Ericson Georgia Institute of Technology August 2005
CS1316: Representing Structure and Behavior
Arrays versus ArrayList
Barb Ericson Georgia Institute of Technology Oct 2005
Workshop for Programming And Systems Management Teachers
Manipulating Pictures, Arrays, and Loops
Barb Ericson Georgia Institute of Technology June 2005
Barb Ericson Georgia Institute of Technology May 2006
More on Creating Classes
Manipulating Pictures, Arrays, and Loops
Creating and Modifying Text part 3
Barb Ericson Georgia Institute of Technology Oct 2005
More on Creating Classes part 3
LCC 6310 Computation as an Expressive Medium
CSG2H3 Object Oriented Programming
Presentation transcript:

CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingClasses-SlideShow-part32 Learning Goals Computing concepts –Adding a method To show the pictures in the slide show –Creating accessors and modifiers That protect the fields in the object –Declaring a main method To execute the desired task

CreatingClasses-SlideShow-part33 Showing the Slide Show Now that a slide show has an array of slides we would like to –Show the pictures in the array We can loop through the elements of the array –And show the current picture –And wait for the wait time –Then hide the current picture We need to be careful of –A null pictureArray

CreatingClasses-SlideShow-part34 Thread.wait Use Thread.sleep(waitTime) to wait for waitTime number of milliseconds –1000 milliseconds is one second This can cause an exception –exceptional event - like if someone hits the reset button in DrJava while we are waiting –write the method to throw Exception by adding throws Exception public void show() throws Exception

CreatingClasses-SlideShow-part35 Create a Method Exercise Create a method show that will first check that the picture array isn't null –And if it isn't will loop through the pictures in the array Showing the current picture Waiting till the wait time has passed Hiding the current picture

CreatingClasses-SlideShow-part36 Accessing Fields from Other Classes Fields are usually declared to be private –So that code in other classes can’t directly access and change the data Try this in the interactions pane –System.out.println(showObj.pictureArray); You will get an exception –Short for exceptional event – error Outside classes can not use object.field to access the field value –Unless it is declared with public visibility

CreatingClasses-SlideShow-part37 Accessors and Modifiers Accessors –Are public methods that return data In such a way as to protect the data for this object Syntax public fieldType getFieldName() Example public String getName() { return this.name;} Modifiers –Are public methods that modify data In such a way as to protect the data for this object Syntax public returnType setFieldName(type name); Example public void setName(String theName) {this.name = theName; }

CreatingClasses-SlideShow-part38 Naming Conventions Accessors – also called Getters –Use getFieldName for non boolean fields –Use isFieldName for boolean fields Modifiers – also called Setters and Mutators –Use setFieldName –Sometimes return a boolean value to indicate if the value was set successfully Examples –getName and setName

CreatingClasses-SlideShow-part39 Creating SlideShow Accessors Add a method to get the wait time public int getWaitTime() What about a method to get the array of pictures? –If someone gets the array s/he can directly change the pictures in the array –It is safer to return the picture at an index Then other classes can’t directly change the array of pictures

CreatingClasses-SlideShow-part310 Exercise Create a method that returns the wait time Create a method that returns the picture at a given index in the array –If the array is null return null –If the index isn't valid return null

CreatingClasses-SlideShow-part311 Creating Slide Show Modifiers We need public methods –That let other classes set the time to wait between pictures –Our class is responsible for making sure this only happens in such a way as to keep the data valid and not cause errors Setting the wait time –The wait time must be > 0 Setting an array of pictures –We can decide if this can be changed or not when it isn’t null

CreatingClasses-SlideShow-part312 Set Picture Array Modifier Setting the array of pictures only if it is currently null public boolean setPictureArray(Picture[] theArray) { boolean result = false; if (this.pictureArray == null) { this.pictureArray = theArray; result = true; } return result; }

CreatingClasses-SlideShow-part313 Wait Time Modifier public void setWaitTime(int time) { // check that it is a valid wait time if (time >= 0) this.waitTime = time; }

CreatingClasses-SlideShow-part314 Add a Field Exercise Add a title field to the SlideShow class Add an accessor to get the value of this field Add a modifier to set the value of this field Modify the show method to first create a blank picture with the title on it and show that as the first picture in the slide show

CreatingClasses-SlideShow-part315 Adding a Main Method We have been typing stuff in the interactions pane in DrJava –To try out Java code and to try methods Most development environments make you write a main method to start execution –DrJava allows this too Each class can have a main method declared as follows: –public static void main(String[] args) It is public so that it can be called by other classes It is static because no object of the class exists when it is executed It doesn’t return anything so the return type is void You can pass several arguments to the main method and these are put in an array of strings

CreatingClasses-SlideShow-part316 Main Method Add a main method to SlideShow –Put the statements that you have been doing in the interactions pane in the main method public static void main(String[] args) throws Exception { Picture[] pictArray = new Picture[5]; pictArray[0] = new Picture(FileChooser.getMediaPath("beach.jpg")); pictArray[1] = new Picture(FileChooser.getMediaPath("blueShrub.jpg")); pictArray[2] = new Picture(FileChooser.getMediaPath("church.jpg")); pictArray[3] = new Picture(FileChooser.getMediaPath("eiffel.jpg")); pictArray[4] = new Picture(FileChooser.getMediaPath("greece.jpg")); SlideShow vacShow = new SlideShow(pictArray); vacShow.show(); }

CreatingClasses-SlideShow-part317 Execute the Main Method In DrJava you can run the main method in the class that is displayed in the definitions pane –By clicking on Tools then Run Document’s Main Method (or press key F2) It will do –java SlideShow –In the interactions pane –Which executes the main in the SlideShow class

CreatingClasses-SlideShow-part318 Summary Classes have fields, constructors, and methods Constructors are used to initialize fields in the object Fields are usually declared to be private –To protect the data from misuse by other classes –So you need to provide public accessor (getter) and modifier (setter) methods That still protect the data Use a main method to begin execution –public static void main(String[] args) {}