Write a class, TimedInstantiation, which will have a toString method which returns a string of the following form:

I was instantiated on 12 June 2008, 35 seconds after the previous instantiation of me.
if there have been previous instantiations, but if not (that is, if we are dealing with the first instantiation) it should return
I was instantiated on 12 June 2008, and I am the first instantiation.

The following class might be useful:
java.util.Date represents dates. Its toString() method returns the date and time in human readable form: getTime() returns a long representing the date in milliseconds, so that
(d1.getTime() - d2.getTime())/1000L
will be the time in seconds between dates d1 and d2. Finally, new Date() will return the current date and time.

Recommended Answers

All 7 Replies

and... ?

and... ?

how do i return the string using those methods,do i convert to string using those methods inorder to get the difference in the time in string

how you be sure about return form of toString()

The following class might be useful:
java.util.Date represents dates. Its toString() method returns the date and time in human readable form: getTime() returns a long representing the date in milliseconds, so that
(d1.getTime() - d2.getTime())/1000L
will be the time in seconds between dates d1 and d2. Finally, new Date() will return the current date and time.

This is just an explanation of what the Date class can do. What do you want, the toString method of the class TimedInstantiation do?

After reading your problem, I have a question: Have you ever workd with static variables? Try this example:

class Test {
   private static int count = 0;
   
   public Test() {
        count++;
   }

   public String toString() {
       if (count==1) {
              return "This my first time";
      } else {
           return "I was instatiated: "+count+" times";
      }
   }

   public static void main(String [] args) {
       Test t1 = new Test();
       System.out.println(t1);

       Test t2 = new Test();
       System.out.println(t2);

       Test t3 = new Test();
       System.out.println(t3);
   }
}

You will have to do the same but instead of count you will have a Date object and an int variable (for counting the seconds).
At the constructor you will create locally a new Date, calculate the difference and store that, to the variable you have for storing the difference. Then replace the value of the static Date object with the one that you created locally.
The tricky part is knowing when was first time the class was created in order to print the right message

i have tried,it stil doesnt work.

what variable type do i use to store the Date that i am meant to declare static?

below is the code;

import java.util.*;
import java.lang.*;
import java.util.Date.*;

public class Test1 {

private static int count = 0;
  private static Date d2 =new Date( 25 April,2009 15:03:10);
// or private static Date d2 = 25 April,2009 15:03:10;

public Test1(){
    count++;
    Date now = new Date();
    long D1= now.getTime();
    long TimeDifference=  D1-d2.getTime()/1000;    
}

public String toString(){        
    if (count ==1)
    {
        Date dee = new Date();
        long d1 = dee.getTime();
        return "This is the first intstantiation done at"+ d1;
    }    
    else{            
        return "This is the "+ count + "times" + TimeDifference;
    }
}

public static void main (String[] args)
{
    Test1 n1 = new Test1();
    System.out.println(n1);

    Test1 n2 = new Test1();
    System.out.println(n2);

    Test1 n3 = new Test1();
    System.out.println(n3);   
}    
}

The static variable should be Date. Check the API. And you will give it value at the constructor. The second Date object that you will create at the constructor will be local. You will only use it to get the "now" Date and calculate the difference. Then you will give its value to the static global Date.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.