Chapter 5 Part 1 COSI 11a 2006-11-06 Prepared by Ross Shaull.

Slides:



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

Written by: Dr. JJ Shepherd
Road Map Introduction to object oriented programming. Classes
Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.
1 What’s a class People are confused with nested classes and anonymous classes. Experiments in a recitation last week revealed that the problems were probably.
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)
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Chapter 5 Part 2 COSI 11a Prepared by Ross Shaull.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n.
Writing Classes (Chapter 4)
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Recitation 2 Main Method, API & Packages, Java Basics.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
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!
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point 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.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
As you arrive… FIRST: Grab the handout SECOND: Snarf the code for today’s class THIRD: Explain what’s going on in this code (It’s Example 1 in the code.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Catie Welsh March 23,  Lab 6 due Friday by 1pm 2.
Written by: Dr. JJ Shepherd
COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Topics Instance variables, set and get methods Encapsulation
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
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.
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: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Classes and Objects.
3 Introduction to Classes and Objects.
Static data members Constructors and Destructors
Object Oriented Programming
Lecture 6 D&D Chapter 7 & 8 Intro to Classes and Objects Date.
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Lecture 11 C Parameters Richard Gesick.
Defining Your Own Classes Part 1
Classes & Objects: Examples
Encapsulation and Constructors
Object Oriented Programming in java
Visibilities and Static-ness
Corresponds with Chapter 5
CSG2H3 Object Oriented Programming
Presentation transcript:

Chapter 5 Part 1 COSI 11a Prepared by Ross Shaull

Objectives for the Week Review methods and parameters Finish Chapter 5 –Constructors –Packages –Testing and the top-down/bottom-up techniques Write some interesting code together this week

Methods A method is an action that a class can take. They can be static methods or object methods. Who remembers the difference?

Method Parameters A parameter is like a variable declared in the body of a method, but you decide what value it gets by passing a value into the parameter when you call the method. This is on page 246.

Method Parameter Example public class TestClass { public doStuff(String s) { System.out.println(s.toUpperCase()) } public TestClassDriver { public static void main(String[] args) { TestClass testClass = new TestClass(); testClass.doStuff( ); } “hello world!”

Method Parameter Example public class TestClass { public doStuff(String s) { String s = ; System.out.println(s.toUpperCase()) } public TestClassDriver { public static void main(String[] args) { TestClass testClass = new TestClass(); testClass.doStuff( ); } “hello world!” “hello world!” Don’t actually write this, it is just an Illustration Behind the scenes, this local variable is created for you

Static Methods Methods that don’t have access to instance variables. Call them directly from class (i.e. Math.round). Write them when you want some functionality to be available without instantiating an object.

Constructors We’ve seen them in two recitations What does one look like? Notice: no return type! public ClassName(type1 param1, …, type2 param2) { // initialize stuff here in // body of constructor }

Constructors are methods? Special methods with no return type! You can’t call them directly, only with the “new” operator. But, they do have parameters like other methods. Also like other methods, they can be overloaded (who remembers what that means?)

Default Constructors 1 Simply a constructor with no parameters. Useful for specifying default values for instance variables.

Default Constructors 2 Sometimes there are no reasonable defaults for an instance variable, in that case don’t include a default parameter. It’s okay to omit the default constructor if you feel that your class requires parameters to the constructor. Example: a Person class; are there instance variables that wouldn’t have a sensible default?

Default Constructors 3 What will happen if someone tries to construct an instance of your class using a constructor with no arguments, AND you have no default constructor? See page 379.

Calling a constructor Scanner s = new Scanner(System.in); String s = “Hello World!”; Counter counter = new Counter();

Different from an initializer method? Remember when we wrote initializers on quizzes and the midterm? Is a constructor different from one of these methods? Yes, an initializer (or setter) is not called automatically, so you must always remember to call it after instantiation, making your class less safe and more difficult to use.

Code Interlude A return to the Counter class

Gotcha: Information Leakage Objects are always passed/returned by reference. If your class has an instance variable of non- primitive type, then returning it from a getter will allow users of your class to modify a private variable. Who can explain why? Who can tell me why this won’t matter with String objects?

Gotcha: Information Leakage Private instance variables that are of object type (instead of primitive type) are stored as references, so returning the reference out of a getter method allows a malicious user to make unauthorized changes inside your object. String objects are an exception to this rule because they are immutable (this means literally that they have no mutator methods, you cannot change the contents of a String, only create new Strings.

Another Code Interlude The CadetClass example from page 381.

Code Conclusion Lets start writing a word guessing game It will play a bit like “hangman” We will write it together today, Wednesday, and Thursday It will use many of the techniques and tools we’ve been learning up to now, including: –String manipulation –Loops and branching –Multiple interacting classes –Methods with parameters –Static methods –Constructors –Bottom-up and top-down testing techniques