Classes and Methods Write a class called C1 containing: A static variable SV of type int. A static method SM with one parameter, of type C1, and a void.

Slides:



Advertisements
Similar presentations
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
Advertisements

Recitation 4. 2-D arrays. Exceptions. Animal[] v= new Animal[3]; 2 declaration of array v v null Create array of 3 elements a6 Animal[] null Assign.
How do Methods Work?. Let’s write a method that adds two integer values together and returns the result.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
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.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 4: Writing Classes
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Introduction to arrays Data in economy size packages.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
1 Executing Method Calls This lecture tells you precisely how method calls are executed (a few details will have to wait until we get to classes and objects).
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Slides prepared by Rose Williams, Binghamton University Chapter 8 Polymorphism Part I.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational.
Homework #4: Operator Overloading and Strings By J. H. Wang May 8, 2012.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
ECE122 Feb. 22, Any question on Vehicle sample code?
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
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.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Java Generics – Wildcards By: Anupam Chanda. 2 Generics and Subtyping We start to run into some new issues when we do some things that seem “normal”.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Building java programs, chapter 3 Parameters, Methods and Objects.
1 Week 8 l Methods l Parameter passing Methods. 2 Using Methods l Methods are actions that an object can perform. l To use a method you invoke or call.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
1 Introduction to Object Oriented Programming Chapter 10.
Methods.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
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.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Chapter VII: Arrays.
Topic: Classes and Objects
Classes and Objects.
Introduction to Programming Using C
9.1 Class (static) Variables and Methods
Arrays 2/4 By Pius Nyaanga.
Chapter 4: Writing Classes
Methods.
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.
March 29th Odds & Ends CS 239.
Stack Memory 2 (also called Call Stack)
Classes & Objects: Examples
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Group Status Project Status.
Classes, Encapsulation, Methods and Constructors (Continued)
Anatomy of a Method.
Java Lesson 36 Mr. Kalmes.
CS2011 Introduction to Programming I Methods (II)
JAVA Constructors.
CS2011 Introduction to Programming I Methods (I)
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)
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Which best describes the relationship between classes and objects?
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Classes and Methods Write a class called C1 containing: A static variable SV of type int. A static method SM with one parameter, of type C1, and a void return type. A constructor with no parameters which does nothing. An instance method IM with no parameters, an int return type, and a local variable LV of type C1.

Object Creation, Method Invocation Write a class C2 having a main method which: Creates an object of type C1 (previous slide) and assigns it to variable V. calls method SM with V as its argument. Sends message IM to the object bound to V and prints out the returned value.

Messages Suppose class C3 has a static method S3() and an instance method I3(), and class C4 has a static method S4(), an instance method I4(), and a static variable V4 whose type is C3. 1.What message(s) can be sent to the object bound toV4 ? 2.Write an expression that sends a message to the object bound to V4. 3.If you wanted to be able to send another message, M(), to the object bound to V4, where would you put method M ?

What is wrong ? public class C5 { public static Integer M5(int i) { Integer x = this.M6(i); return x ; } public Integer M6(int w) { return new Integer(w); }

Can you tell the type of “this” ? public class C6 { public void M8(String s) { System.out.print(this.M7(s.trim()) ); } public String M7(String t) { String s = t.toUpperCase(); return s ; }

Exercises about previous slide Write a main method that –Binds the String object “ Fred” to variable X. –Creates an object capable of receiving the M8 message, binds it to variable V, and sends it the M8 message with X as its argument. –Prints out X. Trace the execution of your program.

Identify the local variables and parameters public class C7 { public String M9(String s, String t) { String a ; a = this.M10(s) ; String b = this.M10(t) ; t = a.concat(b) ; return t ; } public String M10(String t) { String s = t.toUpperCase(); return s ; } a.concat(b) creates a new String with a copy of a followed by a copy of b

Exercises about previous slide Write a main method that –Binds the string object “sss” to variable s, and “tt” to variable t. –Creates an object capable of receiving the M9 message, binds it to variable V, sends it the M9 message with s and t as its arguments, and stores the result in variable Y. –Prints out Y, s, and t. Trace the execution of your program. –What are the initial bindings of the local variables the second time M10 is invoked ? Would anything be different if the main method put the arguments s and t in the opposite order ?

Write a few lines of code that… Creates an Integer object, binds two variables to the object,and then changes one of them so that it is bound to no object.

Trace this String s = “aaa” ; System.out.println(s.length()); s = “” ; System.out.println(s.length()); s = null ; System.out.println(s.length());