Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

© Vinny Cahill 1 Classes in Java. © Vinny Cahill 2 Writing a Java class Recall the program to calculate the area and perimeter of a rectangle of given.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
COMP More About Classes Yi Hong May 22, 2015.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
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.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
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.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
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.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Introduction to Computer Programming
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
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.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Catie Welsh February 23,  Lab 4 due on Friday  Lab 5 will be assigned on Friday 2.
Chapter 6 Methods Chapter 6 - Methods.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations.
Creating and Using Class Methods. Definition Class Object.
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.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
Introduction To Java Programming 1.0 Basic Concepts of Java Programming 2.0 Classes, Polymorphism and Inheritance 3.0 Exception handling 4.0.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Classes - Intermediate
Object Oriented Programming I ( ) Dr. Adel hamdan Part 03 (Week 4) Dr. Adel Hamdan Date Created: 7/10/2011.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Methods.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Features of JAVA PLATFORM INDEPENDENT LANGUAGE JAVA RUNTIME ENVIRONMENT (JRE) JAVA VIRTUAL MACHINE (JVM) JAVA APP BYTE CODE JAVA RUNTIME ENVIRONMENT.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.
Chapter 9: Value-Returning Functions
Introduction to Modular Programming
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Methods.
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
CSC 113 Tutorial QUIZ I.
Pemrograman Dasar Methods PTIIK - UB.
An Introduction to Java – Part I, language basics
Functions.
Classes Lecture 7 from Chapter /1/11.
JAVA Constructors.
Introduction to Object-Oriented Programming
Corresponds with Chapter 5
Methods Coding in Java Copyright © Curt Hill.
Presentation transcript:

Constructors & An Introduction to Methods

Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car () }{ Public Car (String make) { model = make; … }} Car car1 = new Car(); Car car 2 = new car ("Audi");

Constructors  A constructor is a method that creates an object. In java constructors are instance methods with the same name as their class. Constructors are invoked using the new keyword  A constructor is a special method which is executed only when an object is created  The purpose of a constructor is to setup (or construct) the environment for the object (variable values, etc) and to initialise an objects data when it is created  A constructor method is easily recognised as it must: –Have the same name as the class

Constructors: Summary  A constructor is generally the first method in a class - it has the exact same name as the class (but has parentheses and optional arguments)  The arguments in a constructor are typically used to initialise the instance variables of the class.  Java has a default constructor if none is provided which does not initialisation - this constructor is called the no args constructor.

Methods

Methods  A method is a group of instructions (also called a function or subroutine in other languages)  A method can only be defined within a class definition  Every java program must contain at least one method  Large programs can get very complex. Methods help to reduce complexity by splitting programs into relatively isolated sections.

Method Structure  A method must take the following form returnType name (argument list){ // method body }

Defining Arguments for methods  In the method definition: –The argument list contains zero or more formal arguments separated by commas and enclosed in parenthesis

Writing a method - Example void printHi() {System.out.print(“hi”);}  This method produces no result after it finishes, it requires no value (parameter) to operate.

Writing a method - Example int addNumbers(int number1, int number2) { int result = number1+number2; return result; }  The method produces an integer when it finishes. It also requires two integers to be given to it.  The method would therefore be used like this: System.out.print(“answer=“+addNumbers(5,4));

Method Declaration  The compiler identifies methods by their name, number and type of arguments.  The compiler also knows (from the return type in the declaration) the return type of the method  There can be zero or more arguments passed to a method, but the parenthesis are mandatory (as is a name and return type)  The return operator is required within every method unless the void keyword is used in its declaration or unless it is a constructor.  If the data type returned by the method does not match the return type, the class will not compile.

class Square { int length; int breadth; public Square (int length, int breadth){ this.length = length; this.breadth = breadth; } int area(){ int answer = length*breadth; return answer; } }//end class Square

Square Example class Square_Example { public static void main(String args[]) { Squares; s = new Square(10,20); System.out.println("result = " + s.area()); }//end main }