presentation9 - Objects Oriented Programming and Java...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Objects Oriented Programming and Java Soheil Hassas Yeganeh Computer Engineering Department Sharif University of Technology Class 2 Circle.java class Circle { } Class • You know how to create a class: 1. Create a .java fle. 2. Defne a class in the fle. 3. The class name and the fle name should be the same . 2 Circle.java class Circle { } Fields 3 Circle.java class Circle { double r; } Person.java class Person { Person parent; } Fields • Class felds is defned just like the local variables . • <TYPE> <FNAME>; • Field type can be either a primitive type or a class . • Field type can be set to its own container class . 3 Circle.java class Circle { double r; } Person.java class Person { Person parent; } Methods 4 Circle.java class Circle { double r; double getArea(){ return r*3.14*3; } void setRadius (double radius){ r = radius; } } Methods • Class methods can be defned as: • <TYPE> methodName (arg1, arg2, arg3, ..., arg2) • Arguments are defned like local variables • Methods are defned only in classes . 4 Circle.java class Circle { double r; double getArea(){ return r*3.14*3; } void setRadius (double radius){ r = radius; } } Objects 5 Circle.java class Circle { ... public static void main(String[] args) { Circle c = new Circle(); c.r = 1.1; } } Objects • Objects are instances of a class that can be created using new operator: • new <TYPE>() • Fields and methods of objects can be accessed using “.” . 5 Circle.java class Circle { ... public static void main(String[] args) { Circle c = new Circle(); c.r = 1.1; } } Objects 6 Circle.java class Circle { ......
View Full Document

Page1 / 42

presentation9 - Objects Oriented Programming and Java...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online