constructors & an introduction to methods. defining constructor – car example public class car...

13
Constructors & An Constructors & An Introduction to Introduction to Methods Methods

Upload: daniella-lane

Post on 16-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

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

Constructors & An Introduction Constructors & An Introduction to Methodsto Methods

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

Defining Constructor – Car ExampleDefining Constructor – Car Example

Public class car {Public class car {String Model;String Model;double speed;double speed;String colour; String colour; {{Public Car ()Public Car ()}}{{Public Car (String make)Public Car (String make) {{

model = make;model = make;……

}}}}

Car car1 = new Car();Car car 2 = new car ("Audi");

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

ConstructorsConstructors A constructor is a method that creates an A constructor is a method that creates an

object. In java constructors are instance object. In java constructors are instance methods with the same name as their class. methods with the same name as their class. Constructors are invoked using the Constructors are invoked using the new new keywordkeyword

A constructor is a special method which is A constructor is a special method which is executed only when an object is createdexecuted only when an object is created

The purpose of a constructor is to setup (or The purpose of a constructor is to setup (or construct) the environment for the object construct) the environment for the object (variable values, etc) and to initialise an (variable values, etc) and to initialise an objects data when it is createdobjects data when it is created

A constructor method is easily recognised as it A constructor method is easily recognised as it must:must:– Have the same name as the classHave the same name as the class

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

Constructors: SummaryConstructors: Summary

A constructor is generally the first method in a A constructor is generally the first method in a class - it has the exact same name as the class class - it has the exact same name as the class (but has parentheses and optional arguments)(but has parentheses and optional arguments)

The arguments in a constructor are typically used The arguments in a constructor are typically used to initialise the to initialise the instance variablesinstance variables of the class. of the class.

Java has a default constructor if none is provided Java has a default constructor if none is provided which does not initialisation - this constructor is which does not initialisation - this constructor is called the called the no args constructor.no args constructor.

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

MethodsMethods

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

MethodsMethods

A method is a group of instructions (also called a A method is a group of instructions (also called a function or subroutine in other languages)function or subroutine in other languages)

A method can only be defined within a class A method can only be defined within a class definitiondefinition

Every java program must contain at least one Every java program must contain at least one methodmethod

Large programs can get very complex. Methods Large programs can get very complex. Methods help to reduce complexity by splitting programs help to reduce complexity by splitting programs into relatively isolated sections.into relatively isolated sections.

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

Method StructureMethod Structure

A method must take the following formA method must take the following form

returnType name (argument list){returnType name (argument list){

// method body// method body

}}

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

Defining Arguments for methodsDefining Arguments for methods

In the method definition:In the method definition:– The argument list contains zero or more formal The argument list contains zero or more formal

arguments separated by commas and enclosed in arguments separated by commas and enclosed in parenthesisparenthesis

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

Writing a method - ExampleWriting a method - Example

void printHi()void printHi()

{{

System.out.print(“hi”);System.out.print(“hi”);

}} This method produces no result after it This method produces no result after it

finishes, it requires no value (parameter) to finishes, it requires no value (parameter) to operate.operate.

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

Writing a method - ExampleWriting a method - Example

int addNumbers(int number1, int number2)int addNumbers(int number1, int number2){{int result = number1+number2;int result = number1+number2;return result;return result;}} The method produces an integer when it finishes. The method produces an integer when it finishes.

It also requires two integers to be given to it.It also requires two integers to be given to it. The method would therefore be used like this:The method would therefore be used like this:System.out.print(“answer=“+addNumbers(5,4));System.out.print(“answer=“+addNumbers(5,4));

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

Method DeclarationMethod Declaration

The compiler identifies methods by their The compiler identifies methods by their namename, , numbernumber andand typetype of of arguments.arguments.

The compiler also knows (from the return type in the declaration) the The compiler also knows (from the return type in the declaration) the return type of the methodreturn type of the method

There can be zero or more arguments passed to a method, but the There can be zero or more arguments passed to a method, but the parenthesis are mandatory (as is a name and return type)parenthesis are mandatory (as is a name and return type)

The return operator is required within every method unless the void The return operator is required within every method unless the void keyword is used in its declaration or unless it is a constructor.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, If the data type returned by the method does not match the return type, the class will not compile.the class will not compile.

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

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

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

Square ExampleSquare Example

class Square_Example{public static void main(String args[])

{Square s;s = new Square(10,20);System.out.println("result = " + s.area());

}//end main}