Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
moonhk - 10 Oct 2006 16:50 GMT
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();

}
}
Oliver Wong - 10 Oct 2006 17:30 GMT
> "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
opalpa opalpa@gmail.com http://opalpa.info - 10 Oct 2006 17:41 GMT
Date EngineStartDate = new Date();

delete the first word in that line.  Cheers.

opalpa
opalpa@gmail.com
http://opalpa.info/
moonhk - 10 Oct 2006 18:30 GMT
> Date EngineStartDate = new Date();
>
[quoted text clipped - 3 lines]
> 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>
opalpa opalpa@gmail.com http://opalpa.info - 10 Oct 2006 18:57 GMT
> try to OK Now. Thank a lot. What problem ?
>
[quoted text clipped - 13 lines]
>  }
> }

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

> D:\Example\javaux\OO>java appmotor
>
[quoted text clipped - 11 lines]
>         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/
moonhk - 11 Oct 2006 05:45 GMT
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();

}
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.