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

Discussion in 'Java' started by moonhk, Oct 10, 2006.

  1. moonhk

    moonhk Guest

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


    }
    }
     
    moonhk, Oct 10, 2006
    #1
    1. Advertising

  2. moonhk

    Oliver Wong Guest

    "moonhk" <> wrote in message
    news:...
    >
    > "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
     
    Oliver Wong, Oct 10, 2006
    #2
    1. Advertising

  3. Date EngineStartDate = new Date();

    delete the first word in that line. Cheers.

    opalpa

    http://opalpa.info/
     
    opalpa http://opalpa.info, Oct 10, 2006
    #3
  4. moonhk

    moonhk Guest

    opalpa http://opalpa.info wrote:
    > Date EngineStartDate = new Date();
    >
    > delete the first word in that line. Cheers.
    >
    > opalpa
    >
    > 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>
     
    moonhk, Oct 10, 2006
    #4

  5. > 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

    http://opalpa.info/
     
    opalpa http://opalpa.info, Oct 10, 2006
    #5
  6. moonhk

    moonhk Guest

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


    }
    }
     
    moonhk, Oct 11, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    711
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    537
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    392
    John Machin
    Jul 28, 2006
  4. Martin
    Replies:
    0
    Views:
    338
    Martin
    Dec 27, 2008
  5. Replies:
    2
    Views:
    762
    M.-A. Lemburg
    Jan 6, 2009
Loading...

Share This Page