java program need help!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: java program need help!

  1. #1
    Join Date
    Mar 2006
    Posts
    3

    java program need help!

    I need to do my assingmnet for class. If any one know how to help me i would appreciate it a lot. This is my assingment:




    Write a class to represent a time interval named ElapsedTime. This class represents elapsed time in days hours and minutes. It might be used by a shipping company to estimate and keep track od how long their vessels are at sea. If trip takes 7 1/2 days, elapsed time would be 7 days 12 hours and 0 minutes. Represent the current time interval as a number of minutes, stored in an int Java variable.


    Constructor : ElapsedTime (int days, int hours int minutes)
    This creates an object of the ElapsedTime class whose instance data is initialized according to the parameters of the constructor. For example, if the invocation of the constructor is ElapsedTime (0, 0, 0) an object is created representing a time interval of zero minutes. You may assume that all three arguments are never negative.

    Public methods:

    void setTime (int days int hours int minutes)
    setTime() sets the current time interval according to its arguments. For example if the method invocation is setTime (7, 3,22) the time interval is 10,282 minutes. All three arguments are never negative.

    void addTime (int days, hours minutes)
    addTime adds to the objects current time interval according to its arguments.For example if the method invocation si addTime (1,0,0) the time interval is incresed by 1440 minutes. One or all of the arguments may be negative or zero.
    Use the class Math.abs to make sure your instance data is always non-negative.

    int getDays()
    getDays returns the number of days not exceeded by the current time interval. This is never negative. For example if the current time is 1439 minutes getDays will return 0.

    int getHours()
    getHours returns number of hours in the current time interval after all whole days are taken out. This is never negative and never exceeds 23.

    int getMinutes()
    getMinutes returns the number of minutes in the current time interval after all whole days and all whole hours are taken out. This is never negative and never exceeds 59.

    String toString()
    to String returns a String object representing the current time interval in days hours and minutes. THe form of the String shuld be days, hours, minutes where days is at least one digit, hours is always two digit between 00 and 23 and minutes two digits between 00 and 59. You can use Decimal Format class with a pattern of "00" to force a number to be represented by a string with a leading zero.



    Part 2.
    Write a test program to test the ElapsedTime class. The program shuld be written as a seperate class that instantiates an ElapsedTime object and calls its methods. In the test program, test each method with arguments supplied by your program to see if it works. For each test, use JOptionPane.showMessageDialog() to output what the test is what the results are and what was expected.



    PLEASE IF ANYONE KNOW HOW TO DO THIS HELP ME!
    THANK YOU!

  2. #2
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    255
    Read the Java tutorial
    Happiness is good health and a bad memory.

  3. #3
    Join Date
    Mar 2006
    Posts
    3

    Problem !

    I set up the constructor and methods but i`m missing something in it. SO can you help me to correct it?

  4. #4
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    255
    Show us your code.
    Happiness is good health and a bad memory.

  5. #5
    Join Date
    Mar 2006
    Posts
    3
    public class ElapsedTime{
    private int timeInMinutes=0;
    public ElapsedTime(){

    }
    public void setTime (int days, int hours, int minutes, int timeInterval){
    timeInterval=(days*1440)+(hours*60)+minutes;

    }

    public void addTime (int days, int hours, int minutes, int time, int timeInterval){
    Math.abs(timeInMinutes=0);
    time=timeInterval+(days*1440)+(hours*60)+minutes;
    }
    public int getDays (int days, int timeInterval){
    days=timeInterval/1440;
    return days;
    }
    public int getHours (int timeInterval, int hours){
    hours=timeInterval/60;
    return hours;
    }
    public int getMinutes (int timeInterval, int minutes){
    minutes=timeInterval/1440;
    return minutes;
    }
    public String toString (){

    }

    }

    I`am not sure about the one string toString.

  6. #6
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Do you know how Java classes track or use "time"? Have you studied the "time" classes included with the Java API? Including the Date and Calendar classes?

    I think if I were approaching this project, I would save the "start" time - the current "time" when I create an object of the class. Then, whenever I query the object for its elapsed time, it would report back to me the difference between what I have stored as the start time and now. I could then make the various unit conversions to report the days, hours, minutes, seconds, etc. which have elapsed.

    The "toString" method just creates some representation of the current state of the class as a String object. What do you think is relevant for a user to know about the current state of the class?

  7. #7
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    well, there are several problems in your code: for example you have
    public int getDays (int days, int timeInterval){
    days=timeInterval/1440;
    return days;
    }
    but the local varaible days is not defined. correct would be:

    public int getDays (int days, int timeInterval){
    int days=(int)(timeInterval/1440);
    return days;
    }

    or shorter:
    public int getDays (int days, int timeInterval){
    return (int)(timeInterval/1440);
    }

    in none of your methods you have properly initialized/declared your local variables.

    then your toString method. it youst should generate a string representation of your
    elapsed time. eg.:

    public String toString (){
    return getDays()+" days "+getHours()+" hours "+getMinutes();
    }

    but you will recognize, that this methods will return the whole time in their measurement, eg. getMinutes will return the whole elapsed time in minutes.
    so you will have to splitt your time in minutes to the different measurements:

    on calculating the splitting to days, hours and minutes you have to consider the rest properly:

    int days = (int) (timerIntervall/1440);
    int rest = timeInterval-days*1440;
    int hours = (int) (rest/60);
    rest = rest-hours*60;
    int mins = rest;

Similar Threads

  1. Help! Simple Java Program.
    By SlickWilly440 in forum Java
    Replies: 2
    Last Post: 01-28-2006, 10:17 PM
  2. DevX does seem one sideded
    By Rob Abbe in forum Talk to the Editors
    Replies: 44
    Last Post: 01-13-2003, 03:57 PM
  3. Has Sun Given Up on the Desktop?
    By Lori Piquet in forum Talk to the Editors
    Replies: 114
    Last Post: 10-10-2002, 07:01 AM
  4. .NET vs. Enterprise Java: Who's Got Better Security?
    By Glen Kunene in forum Talk to the Editors
    Replies: 17
    Last Post: 03-23-2002, 01:43 AM
  5. Replies: 0
    Last Post: 11-16-2000, 01:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center