A Simple Object Oriented Program public class Simple { public static void main (String [] args) { System.out.println(“howdy”); } System.out is an object.

Slides:



Advertisements
Similar presentations
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Advertisements

CSCI 160 Midterm Review Rasanjalee DM.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
© 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.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
Classes and Objects CSC 171 FALL 2004 LECTURE 2. LABS Labs start this week. Go to your 2 nd (Wed or Thurs) lab Expect something like the programming exercises.
Chapter 2 Using Objects. These slides for CSE 110 Sections are based in part on the textbook-authors ’ slides, which are copyright 2003 by Cay Horstmann.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
1 Basic Object Oriented Concepts Overview l What is Object-Orientation about? l What is an Object? l What is a Class? l Constructing Objects from Classes.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Object-oriented analysis (OOA) techniques are used to (1) study existing objects to see if they can be reused or adapted for new uses, and (2) define new.
Chapter 2  Using Objects 1 Chapter 2 Using Objects.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
UML Basics & Access Modifier
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
More Object Concepts Chapter 4.  Our class is made up of several students and each student has a name and test grades  How do we assign the variables.
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Objects and Classes Mostafa Abdallah
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
More on Objects Mehdi Einali Advanced Programming in Java 1.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Methods.
Topics Instance variables, set and get methods Encapsulation
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
Review – Primitive Data What is primitive data? What are 3 examples of primitive data types? How is a piece of primitive data different from an object?
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Class Structure 15-Jun-18.
Chapter 2 Using Objects.
CSE 1030: Implementing Non-Static Features
Object Oriented Programming in java
Simple Classes in Java CSCI 392 Classes – Part 1.
AN INTRODUCTION TO OBJECTS AND CLASSES
Handout-2(a) Basic Object Oriented Concepts
Just Enough Java 17-May-19.
Methods/Functions.
Object Oriented Programming (OOP) Lecture No. 12
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

A Simple Object Oriented Program public class Simple { public static void main (String [] args) { System.out.println(“howdy”); } System.out is an object of class PrintStream, provided by the Java library println is a method of the PrintStream class

Objects and Classes  Object: entity that contains data and can performs actions in a program (by invoking methods)  Class: describes a type of object, a class must be defined in order to ‘create’ an object  So, each object belongs to a class  Class determines legal methods for an object System.out.fill(); // Error, no fill method System.out.println(“howdy”); // OK

Creating Objects The System.out object has already been created, and already exists for your use in a Java program. Typically, you must create an object before you can use it. In order to create an object, a class must exist which ‘describes’ this type of object. To use this class, you must be familiar with the creation methods (constructors) it provides. A constructor method always has the same name as the class.

One class which already exists for your use in the Java library is the Rectangle class. Let’s create a Rectangle object……… Two of the constructors available with the Rectangle class: Rectangle() Rectangle(int x, int y, int width, int height) provides class specs (see API specs)

Declare a rectangle reference variable: Rectangle myrec; Construct a rectangle: myrec = new Rectangle(5, 10, 20, 30); Adjust the size of this Rectangle object by calling the setsize method of the Rectangle class: myrec.setsize(45,15); Print the Rectangle object System.out.println(myrec); prints java.awt.Rectangle[x=5,y=10,width=45,height=15]

Attributes of a Rectangle Object

Syntax: Variable Definition TypeName variableName; TypeName variableName = expression; –Example: Rectangle myrec; Rectangle box = new Rectangle(); –Purpose: To define a new variable of a particular type and optionally supply an initial value

Syntax: Object Construction new ClassName(parameters) –Example: new Rectangle(5, 10, 20, 30) new Car(“red”, “Ford”,”Pinto”, 4.5) –Purpose: To construct a new object, initialize it with the construction parameters, and return a reference to the constructed object.

Object Variables Declare Rectangle crispyCrunchy; Declare and initialize: Rectangle cerealBox = new Rectangle(5, 10, 20, 30);  Once you have an object, you can apply methods: cerealBox.translate(15, 25); crispyCrunchy.translate(10,5); //error !!  Share objects: r = cerealBox;

Uninitialized and Initialized Variables Rectangle cerealBox; Rectangle cerealBox = new Rectangle(5,10,20,30);

These variables are references (like pointers) to objects, SO: Rectangle r; r = cerealBox; is NOT the same as: r = new Rectangle(5,10,20,30); In the first case, two variables will refer to the SAME object, in the second, an additional object is created and referred to by crispCrunchy.

Two Object Variables Referring to the Same Object

Writing a Test Program  Invent a new class, say MoveTest  Supply a main method (which makes this an application class)  Place instructions inside the main method  Import any library classes you need by specifying the package and class name: import java.awt.Rectangle;  You don't need to import classes in the java.lang package such as String and System

Syntax : Importing a Class from a Package import packageName.ClassName ; –Example: import java.awt.Rectangle; –Purpose: To import a class from a package for use in a program

File MoveTest.java import java.awt.Rectangle; public class MoveTest { public static void main(String[] args){ // declare and create the rectangle Rectangle cerealBox = new Rectangle(5,10,20,30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); }

Using Class Methods In order to call a class method successfully, you must examine it’s signature: // the following bold signature indicates how to use this // method of the Rectangle class public boolean contains (int x, int y) { //method body }

Using Class Methods  access specifier (such as public)  return type (such as String or void)  method name (such as contains or setSize)  list of parameters (number, type and purpose of parameters needed in call )

public boolean contains (int x, int y) { //method body } This method is ‘callable’, because its access specifier is public This method will return a true or false result, so the call should occur within an expression The method is called using the name ‘contains’ the call must provide 2 arguments Call: myrec.contains(6,7)