Oop in java (post 2)

  • Method

A method is just a chunk of code that does a particular job. But methods are set out in a certain way. You have a method header, and a method body. The header is where you tell Java what value type, if any, the method will return (an int value, a double value, a string value, etc). As well as the return type, you need a name for your method, which also goes in the header. You can pass values over to your methods, and these go between a pair of round brackets.

A simple Java method requires a minimum of three items:

Visibility : public, private, protected
Return Type: void, int, double, (etc.)
name: whatever you want to call the method

Visibility means who can access it. If it is public, anyone who has access to your class file can access the method. In some circumstances, this is perfectly fine. If the method is private, only the functions inside of that class can call the method. This is used as utility methods to do something you do not want just anyone who uses the class to do. Protected gives public function to all child classes.

Return type is void if you do not want the method to give you any data back. It would be used for such things as a method that prints out something. Any other return requires a return statement with the type of data it returns. For example, if you add two integers and want the results of that integer, your return type would be int.

Name of the method is anything you choose that is not already used in the class (unless you are overloading the method which is beyond the scope of this article)

If you want the method to do something with the data you supply it, you also need to include parameters within the ( ) You include what data type it is and give that parameter a name (ie: you are declaring a local variable for that method only)
examples:

access   return type   name   parameters
public     void             add     (int a, int b)

This would be written

public void add(int a, int b)
{
// do stuff here
}

Since the return type is void, you will have to write what you want the method to do inside of the method such as by printing it out from the method

public void add(int a, int b)
{
System.out.println(a+b);
}

  • Source Code:

Code that is written in high level language is called source code and in the context of java its called java code and its extension is .java and it’s not directly run on computer compiler is required to convert it in machine code.

  • IDE(BlueJ):

BlueJ is an integrated development environment (IDE) for the Java programming language, developed mainly for educational purposes, but also suitable for small-scale software development.

BlueJ was developed to support the learning and teaching of object-oriented programming, and its design differs from other development environments as a result. The main screen graphically shows the class structure of an application under development (in an UML-like diagram), and objects can be interactively created and tested. This interaction facility, combined with a clean, simple user interface, allows easy experimentation with objects under development. Object-oriented concepts (classes, objects, communication through method calls) are represented visually and in its interaction design in the interface.

OOP in Java

 

Java is an Object Oriented Language. As a language that has the Object Oriented feature Java supports the following fundamental concepts:

  • Polymorphism
  • Inheritance
  • Encapsulation
  • Abstraction
  • Classes
  • Objects
  • Instance
  • Method
  • Message Parsing

 

  • Class:

In the real world, you’ll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.

The following Bicycle class is one possible implementation of a bicycle:

 

class Bicycle {

 

int cadence = 0;

int speed = 0;

int gear = 1;

 

void changeCadence(int newValue) {

cadence = newValue;

}

 

void changeGear(int newValue) {

gear = newValue;

}

 

void speedUp(int increment) {

speed = speed + increment;

}

 

void applyBrakes(int decrement) {

speed = speed – decrement;

}

 

void printStates() {

System.out.println(“cadence:” +

cadence + ” speed:” +

speed + ” gear:” + gear);

}

}

 

  • Object:

Concept of object:

A typical Java program creates many objects, which as you know, interact by invoking methods. Through these object interactions, a program can carry out various tasks, such as implementing a GUI, running an animation, or sending and receiving information over a network. Once an object has completed the work for which it was created, its resources are recycled for use by other objects.

Creating Object:

Here’s a small program, called CreateObjectDemo, that creates three objects: one Point object and two Rectangle objects. You will need all three source files to compile this program.


public class CreateObjectDemo {

public static void main(String[] args) {

// Declare and create a point object and two rectangle objects.
Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Rectangle rectTwo = new Rectangle(50, 100);

// display rectOne's width, height, and area
System.out.println("Width of rectOne: " + rectOne.width);
System.out.println("Height of rectOne: " + rectOne.height);
System.out.println("Area of rectOne: " + rectOne.getArea());

// set rectTwo's position
rectTwo.origin = originOne;

// display rectTwo's position
System.out.println("X Position of rectTwo: " + rectTwo.origin.x);
System.out.println("Y Position of rectTwo: " + rectTwo.origin.y);

// move rectTwo and display its new position
rectTwo.move(40, 72);
System.out.println("X Position of rectTwo: " + rectTwo.origin.x);
System.out.println("Y Position of rectTwo: " + rectTwo.origin.y);
}
}

This program creates, manipulates, and displays information about various objects. Here’s the output:

Width of rectOne: 100
Height of rectOne: 200
Area of rectOne: 20000
X Position of rectTwo: 23
Y Position of rectTwo: 94
X Position of rectTwo: 40
Y Position of rectTwo: 72