public class Droid{
//instance fields
int batteryLevel;
String name;
//constructor method
public Droid(String droidName){
name = droidName;
batteryLevel = 100;
}
//tostring method
public String toString(){
return "hello, I'm the droid: " + name;
}
//peform task method
public void performTask(String task){
System.out.println(name+" is performing task: "+ task);
batteryLevel = batteryLevel - 10;
}
// method to print energy levels
public void energyReport(){
System.out.println(batteryLevel);
}
//public main method
public static void main(String[] args){
Droid Codey = new Droid("Codey");
System.out.println(Codey);
Codey.performTask("Dancing");
Codey.energyReport();
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter