CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Written by: Dr. JJ Shepherd
Road Map Introduction to object oriented programming. Classes
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Lecture From Chapter 6 & /8/10 1 Method of Classes.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
CPSC1301 Computer Science 1 Chapter 11 Creating Classes part 1.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
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.
CSC 142 Computer Science II Zhen Jiang West Chester University
Agenda Object Oriented Programming Reading: Chapter 14.
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.
10-Nov-15 Java Object Oriented Programming What is it?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
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.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
More Methods and Arrays Material from Chapters 5 to 7 that we didn’t cover in 1226.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Creating Classes part 2 Barb Ericson Georgia Institute of Technology June 2006.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Written by: Dr. JJ Shepherd
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
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.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Comp1004: Building Better Objects II Encapsulation and Constructors.
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.
Class Definitions and Writing Methods Chapter 3 10/12/15 & 10/13/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Some Eclipse shortcuts
Class Definitions and Writing Methods
Barb Ericson Georgia Institute of Technology Dec 2009
Anatomy of a class Part I
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Encapsulation and Constructors
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Workshop for Programming And Systems Management Teachers
Barb Ericson Georgia Institute of Technology June 2005
JAVA CLASSES.
Method of Classes Chapter 7, page 155 Lecture /4/6.
More on Creating Classes
Agenda Warmup Lesson 2.2 (parameters, etc)
Barb Ericson Georgia Institute of Technology Oct 2005
LCC 6310 Computation as an Expressive Medium
Review for Midterm 3.
Anatomy of a class Part I
CSG2H3 Object Oriented Programming
Presentation transcript:

CSC1401 Classes - 2

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 Using our class

What methods will we want to add? A method to show the slide show A method to add a picture to the slide show One or more constructors To initialize the class variables, as well as doing other class initialization Required by Java

The Constructor Is a method Named the same as the class name We do not specify public/private/protected The return type

What will our constructor initialize? The size of the array The array itself And, maybe some other things as well We can continue adding initialization instructions to the constructor later

A default constructor No parameters How big should the array be? Some default value, say 10 What will we initialize? The size The array This we need to declare 2 class-level variables to represent these values!

Default constructor class SlideShow { int size; Picture [] cells; SlideShow() { this.size = 10; this.cells = new picture [size]; } // ends the default constructor } // ends the class

Adding a second constructor, to specify the size of the array class SlideShow { int size; Picture [] cells; SlideShow() { this.size = 10; this.cells = new Picture [size]; } // ends the default constructor SlideShow (int howManyPictures) { this.size = howManyPictures; this.cells = new Picture [size]; } // ends constructor } // ends the class Note that the two methods have the same name! The book calls this “overloading constructors” Other methods may be overloaded as well

What if we also wanted to be able to initialize the wait time? class SlideShow { int size; Picture [] cells; int waitTime; // specified in msec SlideShow() { this.size = 10; this.waitTime = 2000; this.cells = new Picture [size]; } // ends the default constructor SlideShow (int howManyPictures) { this.size = howManyPictures; this.cells = new Picture [size]; this.waitTime = 2000; } // ends constructor SlideShow (int howLongToWait) { this.size = 10; this.cells = new Picture [size]; this.waitTime = howLongToWait; } // ends constructor } // ends the class What is the problem with our class? How can we fix it?

Creating a method to add a picture to our slide show What extra class variables do we need? The number of the cell Writing this method Make sure we don’t add more pictures than can fit in the array How do we handle trying to add more pictures? Not allowing it Resizing the array (using a second private method)

A method for 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

Create a Method Exercise Create a method show that will loop through the pictures in the array Showing the current picture Waiting till the wait time has passed Hiding the current picture

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 main System.out.println(showObj.cells); 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

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; }

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

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

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 index isn't valid return null

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

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

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 (if the title is non-null) and show that as the first picture in the slide show

Testing the class Add a class and containing a main method public static void main(String[] args) throws Exception { SlideShow vacShow = new SlideShow(); vacShow.addPicture(“23beach.jpg“); vacShow.addPicture(“23blueShrub.jpg“); vacShow.addPicture(“23butterfly.jpg“); vacShow.addPicture(“23church.jpg“); vacShow.addPicture(“23redDoor.jpg“); String title = “My vacation”; vacShow.addTitle(title); vacShow.show(); }

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) {} The book places the main method into the SlideShow class – while this is ok to do, we’ll generally keep the main in a separate class

Assignment Read Media Computation Chapter 11, Sections 3-6, 8 Please read the text, as it uses different examples!