A simple object oriented program
Download
1 / 18

A Simple Object Oriented Program - PowerPoint PPT Presentation


  • 120 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'A Simple Object Oriented Program' - judson


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
A simple object oriented program
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
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 objectSystem.out.fill(); // Error, no fill methodSystem.out.println(“howdy”); // OK


Creating objects
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)

www.java.sun.com provides class specs (see API specs)


Declare a rectangle library is the Rectangle class. Let’s create a Rectangle object………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); printsjava.awt.Rectangle[x=5,y=10,width=45,height=15]


Attributes of a rectangle object
Attributes of a Rectangle Object library is the Rectangle class. Let’s create a Rectangle object………


Syntax variable definition
Syntax: Variable Definition library is the Rectangle class. Let’s create a Rectangle object………

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
Syntax: Object Construction library is the Rectangle class. Let’s create a Rectangle object………

  • 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
    Object Variables library is the Rectangle class. Let’s create a Rectangle object………

    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
    Uninitialized and Initialized Variables library is the Rectangle class. Let’s create a Rectangle object………

    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.



    Writing a test program
    Writing a Test Program SO:

    • 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
    Syntax : Importing a Class from a Package SO:

    • import packageName.ClassName ;

      • Example:

  • import java.awt.Rectangle;

    • Purpose:

  • To import a class from a package for use in a program


  • File movetest java
    File MoveTest.java SO:

    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
    Using Class Methods SO:

    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 methods1
    Using Class Methods SO:

    • 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) SO: {

    //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)


    ad