Re: Date value set to current datetime, after recall the date value is null
Source Code as below
I add some coding
such as in
public void showDetails() {
// calls the superclass by prefixing it with a super
super.showDetails();
// Add New Item
System.out.print("\tSpeed is " + speed + " mph");
}
Also, do you how to call Motorcycle.showDetails() ?
Motorcycle.java
============
import java.util.*;
import java.text.*;
class Motorcycle {
private String Model;
private String Color;
private int Year;
private Date EngineStartDate;
private boolean engineStatus = false;
void setModel (String loName) {
this.Model = loName;
}
void setColor(String loColor) {
this.Color = loColor ;
}
void setYear (int loYear) {
this.Year = loYear;
}
public void showDetails() {
System.out.println("\nDetails:");
System.out.println("\tModel is " + this.Model);
System.out.println("\tColor is " + this.Color);
System.out.println("\tYear is " + this.Year);
System.out.println("\tEngine Started on " + EngineStartDate );
}
public void startEngine() {
if (engineStatus == true)
System.out.println("\nThe engine is alreay on. " + EngineStartDate
+ " (" + this.Model + ")");
else {
engineStatus = true;
EngineStartDate = new Date();
System.out.println("\nThe engine is now on. " + EngineStartDate +
" (" + this.Model+ ")");
}
}
}
/* eof */
appmotor.java
==========
import java.lang.*;
/* Testing Inheritance */
class M2 extends Motorcycle {
private int speed;
public void setSpeed(int loSpeed) {
this.speed = loSpeed ;
}
public int getSpeed() {
return this.speed;
}
public void showDetails() {
// calls the superclass by prefixing it with a super
super.showDetails();
// Add New Item
System.out.print("\tSpeed is " + speed + " mph");
}
}
/* Main Part */
public class appmotor extends Thread {
public static void main (String args[]) {
M2 m = new M2();
m.setModel ("Yamaha RZ350");
m.setColor ("Yello");
m.setYear (2003);
m.setSpeed(100);
m.startEngine();
m.showDetails();
try {
sleep (4000);
} catch ( InterruptedException e) {}
m.startEngine();
m.showDetails();
}
}