BP Forums
One of my first object oriented programs - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: Java (https://bpforums.info/forumdisplay.php?fid=19)
+---- Thread: One of my first object oriented programs (/showthread.php?tid=725)



One of my first object oriented programs - WitherSlayer - 01-03-2013

Copy these to codes into different eclipse classes
[code2=java]public class carPetrol
{

int tPetrol = 0;

public carPetrol(){

}
public carPetrol(int petrolAmount){
tPetrol = petrolAmount;
}



public void fillTank(int amount){
tPetrol = tPetrol + amount;

}
public void drive(int amount){
tPetrol = tPetrol - amount;

}
public int meterReading(){
return tPetrol;
}

}[/code2]

You will need to run the second program for this to work.

[code2=java]public class carPetrolTest {

public static void main(String[] args){
carPetrol petrolTank = new carPetrol(0);
petrolTank.fillTank(100);
petrolTank.drive(20);
System.out.println(petrolTank.meterReading() + " litres of gas remaining");
}
}[/code2]


Re: One of my first object oriented programs - brandonio21 - 01-03-2013

Ah, the classic "fill" program. Those are great for learning how to use OOP.

Good work!