Date value set to current datetime, after recall the date value is null

M

moonhk

Output as below

D:\Example\javaux\OO>java appmotor
The engine is now on. Tue Oct 10 23:44:41 CST 2006 (Yamaha RZ350)
Engine Start date = Tue Oct 10 23:44:41 CST 2006

Detail:
Model is Yamaha RZ350
Color is Yello
Year is 2003
Engine Started on null


D:\Example\javaux\OO>


"Engine Started" should not null , should be Tue Oct 10 23:44:41 CST
2006.

What wrong as below two java program ?

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 String getEngineStartDate() {

return "xx";

}

void showDetail() {
System.out.println("\nDetail:");
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 +
"\n");

}
public void startEngine() {
if (engineStatus == true)
System.out.println("The engine is alreay on. " + EngineStartDate +
" (" + this.Model + ")");
else {
engineStatus = true;
Date EngineStartDate = new Date();
System.out.println("The engine is now on. " + EngineStartDate +
" (" + this.Model+ ")");
System.out.println("Engine Start date = " + EngineStartDate);
}
}
}

/* eof */




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;
}
}

/* Main Part */
public class appmotor {
public static void main (String args[]) {

M2 m = new M2();

m.setModel ("Yamaha RZ350");
m.setColor ("Yello");
m.setYear (2003);

m.startEngine();
m.showDetail();

// m.startEngine();
// m.showDetail();


}
}
 
O

Oliver Wong

moonhk said:
"Engine Started" should not null , should be Tue Oct 10 23:44:41 CST
2006.

What wrong as below two java program ?
[code snipped]

It might be easier for you to find the bug if you tried to make your
program as small as possible while still reproducing the problem:
http://mindprod.com/jgloss/sscce.html

For example, you can probably get rid of inheritance by getting rid of
the M2 class, and the problem will still be there.

- Oliver
 
M

moonhk

opalpa said:
Date EngineStartDate = new Date();

delete the first word in that line. Cheers.

opalpa
(e-mail address removed)
http://opalpa.info/


try to OK Now. Thank a lot. What problem ?

Change as below

public void startEngine() {
if (engineStatus == true)
System.out.println("\nThe engine is alreay on. " + EngineStartDate
+ " (" + this.Model + ")");
else {
engineStatus = true;
EngineStartDate = new Date(); /* New */
System.out.println("\nThe engine is now on. " + EngineStartDate +
" (" + this.Model+ ")");
System.out.println("Engine Start date = " + EngineStartDate);
}
}
}

D:\Example\javaux\OO>java appmotor

The engine is now on. Wed Oct 11 01:26:06 CST 2006 (Yamaha RZ350)
Engine Start date = Wed Oct 11 01:26:06 CST 2006

Details:
Model is Yamaha RZ350
Color is Yello
Year is 2003
Engine Started on Wed Oct 11 01:26:06 CST 2006
Speed is 100 mph
Year is 2003
Engine Started on null
Speed is 100 mph
D:\Example\javaux\OO>
 
O

opalpa opalpa

try to OK Now. Thank a lot. What problem ?

Change as below

public void startEngine() {
if (engineStatus == true)
System.out.println("\nThe engine is alreay on. " + EngineStartDate
+ " (" + this.Model + ")");
else {
engineStatus = true;
EngineStartDate = new Date(); /* New */
System.out.println("\nThe engine is now on. " + EngineStartDate +
" (" + this.Model+ ")");
System.out.println("Engine Start date = " + EngineStartDate);
}
}
}

These are all the changes? Did you change anything else?

D:\Example\javaux\OO>java appmotor

The engine is now on. Wed Oct 11 01:26:06 CST 2006 (Yamaha RZ350)
Engine Start date = Wed Oct 11 01:26:06 CST 2006

Details:
Model is Yamaha RZ350
Color is Yello
Year is 2003
Engine Started on Wed Oct 11 01:26:06 CST 2006
Speed is 100 mph
Year is 2003
Engine Started on null
Speed is 100 mph
D:\Example\javaux\OO>

Using your source from original post: Its main method call showDetail()
once. The above output has more lines. The first problem looks
resolved. Repost source code.


opalpa
(e-mail address removed)
http://opalpa.info/
 
M

moonhk

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();


}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top