Methods CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Slides:



Advertisements
Similar presentations
Computer Science 320 Clumping in Parallel Java. Sequential vs Parallel Program Initial setup Execute the computation Clean up Initial setup Create a parallel.
Advertisements

C++ Classes & Data Abstraction
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Constructors And Instantiation. Constructor Basics Every class must have a constructor Even abstract classes!! No return types Their names must exactly.
1 Class Constructor Is a specific method Used to initialize the class’s fields Having the same name as the declaring classclass Doesn’t have return type(even.
Lecture 2: Object Oriented Programming I
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
SCOPE & I/O CSC 171 FALL 2004 LECTURE 5. CSC171 Room Change Thursday, September 23. CSB 209 THERE WILL BE A (group) QUIZ! - topic: the CS department at.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
MIT AITI 2003 Lecture 7 Class and Object - Part I.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Saravanan.G.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Object Oriented Programming: Java Edition By: Samuel Robinson.
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.
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Generics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Multi-Threaded Programming Design CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Parallel Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Programming in Java CSCI-2220 Object Oriented Programming.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Semaphores CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Object Oriented Programming I ( ) Dr. Adel hamdan Part 03 (Week 4) Dr. Adel Hamdan Date Created: 7/10/2011.
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Topics Instance variables, set and get methods Encapsulation
Software Technology - I Classes Objects Methods (Cont.) A Baby is a Monkey.
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.
Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Thread Pools CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Producer/Consumer CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Reading Parameters CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D.
Modern Programming Tools And Techniques-I
Static data members Constructors and Destructors
Examples of Classes & Objects
Principles of Software Development
Overloading and Overriding
Classes & Objects: Examples
Principles of Software Development
METHOD OVERRIDING in JAVA
Java – Inheritance.
Chapter 14 Abstract Classes and Interfaces
Chapter 5 Classes.
Presentation transcript:

Methods CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Outline USC CSCI 201L2/9 ▪M▪Methods ▪P▪Program

Method Description ▪A method in Java is equivalent to a function in C++ ▪All methods in Java must exist within a class ▪Other than constructors (which have no return type), all methods must return a value or have a return type of void ›If a method has a return type, it must specify that as the word immediately preceding the name of the method (i.e. all other modifiers must be before the return type) ›The method must also return a value along all paths of execution through it or the code will not compile ▪To call a method, you must have an instance of the class in which the method is declared, unless the method is declared static ▪If a method calls itself, it is called a recursive method USC CSCI 201L3/9 Methods

Method Example 1 public class Lemonade { 2 private int numLemons; 3 public int getNumLemons() { 4 return numLemons; 5 } 6 public void setNumLemons(int numLemons) { 7 this.numLemons = numLemons; 8 } 9 public static void main(String [] args) { 10 Lemonade glass = new Lemonade(); 11 glass.setNumLemons(3); 12 System.out.println(“lemons = “ + glass.getNumLemons()); 13 } 14 } USC CSCI 201L4/9 Methods

Method/Variable Modifiers ▪Static (methods and variables) ›Only one instance of static variables or methods exist in memory ›You do not need an instance of a class to access static members of a class The access modifiers still apply though ›Non-static variables can only be accessed by non-static methods Why? ▪Final (methods and variables) ›A final variable can only be initialized once, either inline or in the constructor ›A final method cannot be overridden by a subclass ▪Abstract (methods and classes) ›An abstract method must be overridden in a subclass or the subclass becomes abstract ▪Synchronized (methods) ›A synchronized method obtains a lock on the object so no other synchronized method can execute until the first one terminates ▪Volatile (variables) ›A volatile variable will not allow a cached value to be used in threads – all threads will get the same value when accessing a volatile variable ▪Transient (variables) ›A transient variable will not persist when using serialization USC CSCI 201L5/9 Methods

Constructors ▪Constructors are called when a class is instantiated ▪Constructors typically initialize member variables, though they can do anything ▪Constructors can be overridden ›If any constructor is explicitly created in the class, the default constructor is no longer created by the compiler ▪Constructors have no return type USC CSCI 201L6/9 Methods

Constructor Example 1 public class Apple { 2 private String color; 3 private int numApples; 4 public Apple(String color, int numApples) { 5 this.color = color; 6 this.numApples = numApples; 7 } 8 public Apple(String color) { 9 this.color = color; 10 this.numApples = 1; 11 } 12 public static void main(String [] args) { 13 Apple greenApples = new Apple(“green”, 3); 14 Apple bushel = new Apple(“red”, 126); // medium-sized :) 15 Apple yellowApples = new Apple(“yellow”); 16 Apple app = new Apple(); // ? 17 } 18 } USC CSCI 201L7/9 Methods

Outline USC CSCI 201L8/9 ▪M▪Methods ▪P▪Program

Program ▪Write a program that iteratively and recursively finds the Fibonacci number specified by the user when prompted. Here is a sample execution with user input bolded. USC CSCI 201L9/9 Program c:\>java csci201.Fibonacci What Fibonacci number would you like? 8 Iteratively: Fibonacci number 8 is 21. Recursively: Fibonacci number 8 is 21. c:\>