Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
One of my first object oriented programs
#1
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]
#2
Ah, the classic "fill" program. Those are great for learning how to use OOP.

Good work!
My Blog | My Setup | My Videos | Have a wonderful day.


Possibly Related Threads…
Thread Author Replies Views Last Post
  Where is a programs JAR file? Derek275 10 33,234 05-08-2013, 09:25 PM
Last Post: andrei.xd23

Forum Jump:


Users browsing this thread: 1 Guest(s)