Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/http://creativecommons.org/licenses/by-nc-

Slides:



Advertisements
Similar presentations
Intro to CS – Honors I More Objects and Methods GEORGIOS PORTOKALIDIS
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Information Hiding and Encapsulation
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
Writing Classes (Chapter 4)
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.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
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!
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
CSE8A Lecture2 Check the class web page (and discussion board, linked form webpage) for news and info: cse8a.weebly.com Lab starts this week. Discussion.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
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.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
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.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
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.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CSE 8A Lecture 17 Reading for next class: None (interm exam 4)
Intro To Classes Review
Chapter 3: Using Methods, Classes, and Objects
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
CS 302 Week 11 Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
Introduction to Computer Programming
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)
Creative Commons Attribution Non-Commercial Share Alike License
Object-Oriented Programming
Creative Commons Attribution Non-Commercial Share Alike License
CS 200 Creating Classes Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
Barb Ericson Georgia Institute of Technology Oct 2005
ITE “A” GROUP 2 ENCAPSULATION.
Object-Oriented Design AND CLASS PROPERTIES
CSG2H3 Object Oriented Programming
Presentation transcript:

Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/ sa/3.0/ Original Developer: Beth Simon, 2009

CSE8A Lecture 25 Read next class: Re-read Chapter 11 No class, No lab Wed –PSA8 due Tues 11:59 (individual – Sound collage) Week 10 –PSA9 due WED 11:59pm (pair – Make a class for Participant in medical study) –Lecture all week, Exam Wed of finals week 3-6pm –Yes, there’s a quiz Friday Week 10 Exam: Wed 3-6pm –Individual and Group

By the end of today’s class you should be able to… LG50: Describe how a Java class is made up of instance variables or fields, constructors, and methods and brainstorm a given class design. LG51: Identify common errors made by novices in defining their own classes. LG53: Identify common structure of “getter” and “setter” methods. LG54: Be able to draw the memory model of an object (at a more detailed level than before) – based on what happens in a constructor. LG55: Identify legal and illegal instances of method overloading, so you can know what “variations” on methods you can write. LG56: Create arrays of objects of any length or type and be able to use good software design for using arrays as fields

Chapter 11: Creating Classes Classes (and class definitions) are the primary programming support for object- oriented programming languages. Classes are comprised of –Data (we need to know about object) Fields or instance variables –Constructors (to create new objects) –Methods (actions we need to be able to perform on objects) Constructors are “special” methods

Let’s look around in Picture.java Wait, there’s nothing – because we didn’t want to scare you in Week 2 –So we hid things away in SimplePicture.java –Let’s look there. And in Pixel.java

Class, Field, Method? 1.automobile 2.numberDoors 3.isExpired 4.door 5.bodyColor 6.getEfficiency 7.color 8.miles/gallon 9.licensePlate 10. setPlate 11.getRating

A Class example for “class” Class: Species Fields/Instance Variables: –name –population –growthRate Methods: –1: Constructors (2 versions) –2: Getters (get values in instance variables) –3: Setters/Mutators (change values in instance variables)

How many errors are there in this code (and what are they) A.2 B.3 C.4 D.5 E.>=6 public class Species private String name; { public static void main(String[] args) { double population; double growthRate; } public Species() { String name = “No Name Yet”; double[] population = 0; growthRate = 33.3; }

Which of following would you select for a “getter” method header for Species class? public void getName(); public void getPopulation(); public void getGrowthRate(); public String getName(); public int getPopulation(); public double getGrowthRate(); public void getName(String newName); public void getPopulation(int newPop); public void getGrowthRate(double newGrowthRate); private void getName(); private void getPopulation(); private void getGrowthRate();

Which of following would you select for a “setter” method header for Species class? public void setName(); public void setPopulation(); public void setGrowthRate(); public String setName(); public int setPopulation(); public double setGrowthRate(); public void setName(String newName); public void setPopulation(int newPop); public void setGrowthRate(double newGrowthRate);

“Best Practice” Getters/Setters A reason we make all out fields/instance variables private is we want to protect our code from malicious or “silly” users –Including other “users” on our development team Classes provide encapsulation and data protection –Put safeguards on how the data can be changed –What is legal and illegal

Which is the BEST Setter Method for population instance variable? public void setPopulation(int newPop) { population = newPop; } public void setPopulation(int newPop) { if (newPop >= 0) population = newPop; } public boolean setPopulation(int newPop) { if (newPop >= 0) { population = newPop; return false; } return true; }

Better technique: Setters/Modifiers The best return type for many setters/modifiers is… A.void: a setter’s job is to change an instance variable value B.void: a setter’s job is to change an instance variable value as long as it is legal (if it’s not it will print out an error message) C.The type of the value that is being changed: a setter should return the value that was changed D.boolean: a setter should return true if the setting was successful and false if not

A MODIFIED Class example for “class” Class: Species Fields/Instance Variables: –name –Population on 5 continents –growthRate public class Species { private String name; private double[] population; private double growthRate; public Species() { String name = “No Name Yet”; double[] population = {0,0,0,0,0}; growthRate = 33.3; }

Constructors: Under the hood Constructors “automatically execute” some “hidden” code for you. public Species() { String name = “No Name Yet”; double[] population = {0,0,0,0,0}; growthRate = 33.3; }

Our Species class example public class Species { ///////// fields //////////// private String name; private int[] population; private double growthRate; /////// constructors /////////// /////// methods //////////////// }

Draw what happens here… (hint pg 357) public Species(String newName, int[] newPop, double newGR) { name = newName; for (int i=0; i< this.population.length;i++) population[i] = newPop[i]; growthRate = newGR; } DO YOU UNDERSTAND? There’s a missing line that causes an error – what is it?

Overloading: Which are legal overloads? A.1 B.2 C.3 D.1 and 3 E.1 and 2 public Species() public Species(String newName); public boolean setGrowthRate(double gr) public void setGrowthRate(double gr) public void setPopulation(int a, int b, int c, int d int e) public void setPopultion(int[] a) 1)Solo: (45 sec) 2)Discuss: (2 min) 3)Group: (20 sec)

Terminology Check 1.Declaration 2.Instantiation 3.Initialization double [] foo; for (int i = 0; i < foo.length; i++) { foo[i] = -11.5; } foo = new double[5];

Keeping our data secure (pg 356 is insecure) public Species(String newName, int[] newPop, double newGR) { name = newName; population = new int[newPop.length]; for (int i=0; i< this.population.length;i++) population[i] = newPop[i]; growthRate = newGR; } public Species(String newName, int[] newPop, double newGR) { name = newName; //BAD DESIGN… INSECURE population = newPop; growthRate = newGR; }