Object Oriented Programing(OOP)

Objects

Object Method Attributes Constructor
An instance of a class Functions an object can do Variables within an object Method: creates the object and assigns attributes.

Here is my UML for my OOP project Tiposed Larenim. Also, my tiposedLarenim Class in Java.(toString and comment are removed for space)

TiposedLarenim

#population:int
#name : String
#longitude:double
#latitude:double
#submerged:boolean
#energy:double
#possibleOres : String[]
#age: int
#ores : String
+ TiposedLarenim()
+ TiposedLarenim(n:String, la:double, lo:double, w:double, e:double, age:int)
+ eat(food:String,heights:int[]):boolean
+ move():void
+ generateOres():void
+ setSubmerged(sub: boolean):void
+ harvestOres():String
+ breed(other: TiposedLarenim): boolean
+toString():String

Copy

import java.lang.Math;
public class TiposedLarenim {
    static int population=100;
    String name;
    double latitude=0;
    double longitude=0;
    double weight;
    boolean submerged=false;
    double energy;
    int age;
    String[] possibleOres = {"iron","gold","palladium","diamond","platinum","lithium"};
    String ores="";
    public TiposedLarenim(){
        name="greg";
        latitude=0;
        longitude=0;
        weight=200;
        energy=100;
        this.age=100;
    }  
    public TiposedLarenim(String n, double la,double lo, double w, double e, int age){
        name=n;
        latitude=la;
        longitude=lo;
        weight=w;
        energy=e;
        this.age=age;
    }
    public boolean eat(String food, int[] heights){
        if((food.equals("trees") && ! submerged) || (food.equals("coral") && submerged)){
            for(int i=0;i<heights.length;i++){
                if(energy>=100){
                    generateOres();
                    energy-=50;
                    weight+=5;
                }else{
                    energy+= Math.pow(heights[i],1.1);
                    weight++;
                }
            }
            move();
            return true;
        }
        return false;
    }
    public void move(){
        double movelat = Math.random()*10-5;
        double movelong = Math.random()*10-5;
        latitude+=movelat;
        longitude+=movelong;
        if(latitude>180 || latitude<-180){
            double newLatitude= latitude>180 ? latitude-movelat*2 : latitude+movelat*2;
            latitude=newLatitude*-1;
        }if(longitude>90 || longitude<-90){
            double newLongitude= latitude>90 ? latitude-movelat*2 : latitude+movelat*2;
            longitude=newLongitude*-1;
        }
    }
    public void generateOres(){
        int index= (int) Math.floor(Math.random()*(possibleOres.length-0.1));
        String ore= possibleOres[index];
        double amount = Math.round((Math.random()+1)*weight*2)/50;
        ores+=amount+" kgs of "+ore+", ";
    }
    public void setSubmerged(boolean sub){
        submerged=sub;
    }
    public String  harvestOres(){
        String harvestedOres=ores;
        ores="";
        return harvestedOres;
    }
    public boolean breed(TiposedLarenim other){
        if(Math.ceil(this.latitude) == Math.ceil(other.latitude) 
        && Math.ceil(this.longitude) == Math.ceil(other.longitude)){
            population++;
            return true;
        }
        return false;
    }
}

Modifiers

Access Modifers

Modifer Class Package Subclass Global
Public
Proctected
Default
Private
The difference, between protected and default is that protected can access subclasses outside of a package.

Keywords

Here are some Useful Keywords in OOP

this: Refers to the class, used to not create confusion between local variables and class attribtues

static: attributes or methods that belong to the class no the object (Population in TiposedLarenim Project)

final: Once an attribute is intialized it can no longer change.

abstract: It is a method that its function has not been defined. Classes that contains these methods are Abstract

Super: Refers to the super class. You can use it as a variable to access methods super.Area(); or use it for the constuctor super()