Escolar Documentos
Profissional Documentos
Cultura Documentos
Views
Use case view: describes behaviour
Useful for analysts, users and testers
A Programming Paradigm
Object-oriented programming is frequently referred to as a programming paradigm. Other programming paradigms include the imperative-programming paradigm, the logic programming paradigm, and the functionalprogramming paradigm.
Object Orientation
Traditional V Objects
A single Object
What Is an Object?
An object is a software bundle of related state and behaviour. Software objects are often used to model the real-world objects that you find in everyday life. This lesson explains how state and behaviour are represented within an object, introduces the concept of data encapsulation, and explains the benefits of designing your software in this manner.
What Is a Class?
A class is a blueprint or prototype from which objects are created. This section defines a class that models the state and behaviour of a real-world object. It intentionally focuses on the basics, showing how even a simple class can cleanly model state and behavior.
What Is Inheritance?
Inheritance provides a powerful and natural mechanism for organizing and structuring your software. This section explains how classes inherit state and behaviour from their super-classes, and explains how to derive one class from another using the simple syntax provided by the Java programming language.
Bank Account.1
public class Account { protected double balance; // Constructor to initialise balance public Account( double amount ) { balance = amount;} // Overloaded constructor for empty balance public Account() { balance = 0.0;} public void deposit( double amount ) { balance += amount; } public double withdraw( double amount ) { // See if amount can be withdrawn if (balance >= amount) { balance -= amount; return amount; } else // Withdrawal not allowed return 0.0;} public double getbalance() { return balance;}}
Bank Account.2
class AccountDemo { public static void main(String args[]) { // Create an empty account Account my_account = new Account(); // Deposit money my_account.deposit(250.00); // Print current balance System.out.println ("Current balance " + my_account.getbalance()); // Withdraw money my_account.withdraw(80.00); // Print remaining balance System.out.println ("Remaining balance " + my_account.getbalance()); }}
Savings Account
class BankAccount { private double balance; public BankAccount( ) { balance = 0; } public void BankAccount(double init ) { balance = init; } public void deposit(double amount) { balance = balance + amount; } public void withdraw(double amount) { balance = balance - amount; } public double getBalance( ) { return balance; }}
class SavingsAccount extends BankAccount { private double interestRate; // instance variable public void SavingsAccont(double rate) // constructor { interestRate = rate; } public void addInterest( ) // instance method { double interest = getBalance() * interestRate / 100; deposit(interest);}}
Park
Park
Run Park
public class RunPark { //Creates a new Park object. public static void main(String args[]) { Park myPark = new Park(); } }
Park
public class Park { // ------------------- Object variables --------------------private GrassArea grass; // Grass private PlayArea play; // Swings and slides private FlowerArea flowers; // Flower bed. private Path path; // Path running through park. // --------------------- Constructor -----------------------//Creates the park with its plants and entertainments. public Park() { // Create new objects (composition). grass = new GrassArea(); play = new PlayArea(); flowers = new FlowerArea(); path = new Path(); grass.grow(); }}
Flower Area
public class FlowerArea { // -------------- Class and object variables ----------------private Path path; // A winding path through the flowers.
/** Initialises the flower bed by creating a single rose. */ public FlowerArea() { path = new Path(); }
}
Grass Area
public class GrassArea{ // ------------------- Object variables --------------------private Tree oakTree; // A lovely old oak tree. private Path grassyPath; // Path running through park. // --------------------- Constructor -----------------------// Creates a grassy area complete with path and tree. public GrassArea() { oakTree = new Tree(); grassyPath = new Path(); } // ------------------------ Methods ------------------------// Grows the grass and any other features of the grass area. public void grow() { oakTree.grow(); oakTree.displayDetails(); } }
Path
public class Path { // ------- Constructor ---------// Creates a new path. public Path() { System.out.println("The builders have just finished laying the path"); } }
Tree.1
public class Tree{ // ------------------- Object variables --------------------private float height; // Stores heght of tree. private int age; // Age of tree. // Coordinates of location of tree. private int locationX,locationY; // --------------------- Constructor -----------------------//Creates a new tree and initialises its state. public Tree() { height = 1; age = 2; locationX = 0; locationY = 0; displayDetails(); }
Tree.2
// ----------------------- Methods --------------------------// Grows the tree by 5% and ages it by a year. public void grow() { height = height + (0.05f * height); age = age + 1; } // Displays details of the tree. public void displayDetails() { System.out.println("Tree is " + age + " years old, "); System.out.print(height + "m tall and found at ("); System.out.println(locationX + "," + locationY + ")"); } }