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

  • Follow


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


}
}

0
Reply moon_ils-se (53) 10/10/2006 3:50:58 PM

"moonhk" <moon_ils-se@yahoo.com.hk> wrote in message 
news:1160495457.928202.31910@k70g2000cwa.googlegroups.com...
>
> "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 


0
Reply owong (5281) 10/10/2006 4:30:33 PM


   Date EngineStartDate = new Date();

delete the first word in that line.  Cheers.

opalpa
opalpa@gmail.com
http://opalpa.info/

0
Reply opalpa (277) 10/10/2006 4:41:08 PM

opalpa opalpa@gmail.com http://opalpa.info wrote:
> Date EngineStartDate = new Date();
>
> delete the first word in that line.  Cheers.
>
> opalpa
> opalpa@gmail.com
> 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>

0
Reply moon_ils-se (53) 10/10/2006 5:30:05 PM

> 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
opalpa@gmail.com
http://opalpa.info/

0
Reply opalpa (277) 10/10/2006 5:57:54 PM

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


}
}

0
Reply moon_ils-se (53) 10/11/2006 4:45:00 AM

5 Replies
23 Views

(page loaded in 0.081 seconds)

Similiar Articles:












7/15/2012 10:07:16 PM


Reply: