Results 1 to 7 of 7
  1. #1
    samd89 is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default Need help with blueJ

    I'm an undergrad and have been using blueJ for a couple months but im stuck on my assignment.

    Here are the questions.

    1.1 declare two private fields to represent the elements of time : hour and minute.

    1.2 write a constructor which has no parameters and initialises the time to midnight (0hr 0 min ).

    1.3 write a private mutator setHour which sets the value of the hour field from a suitable parameter. If the value of the parameter is within an appropriate range (between 0 - 23), the mutator should set the value of the hour field to the parameter value. If it is outside the appropriate range, the mutator sets the field value to 0.

    1.4 write a private method setMinute whose behaviour is similar to setHour but which uses the
    range appropriate for minutes (between 0 - 59).


    Here is the code

    Java Code:
    /**
     * Handles time in the format hour:minute.  
     * If an attempt is made to set hour or minute outside appropriate range of 0 - 59, 
     * component is set to 0
     * 
     * 
     * @version 9/11/07
     */
    public class Time
    {
        // Write your code here
    
    
    
    
    
    
        /** Returns the time elapsed in minutes between the object and the time given in the parameter
         *  @param that is represents the time used for comparison
         *  @return gives the minutes elasped between the two times
         */
        public int getElapsedTime(Time that)
        {
            int thisTime = this.getTotalMinutes();
            int thatTime = that.getTotalMinutes();
            if (thisTime > thatTime)
            {
                return thisTime - thatTime;
            }
            return thatTime - thisTime;
        }
        
        // Task 6.1
        /* Returns a time as a total number of minutes e.g. 3.40 is returned as 240
         */
        private int getTotalMinutes()
        {
            return 0;  // you must amend this code task 6.1 of Cwk 3
        }
        
       
        
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello, and welcome to the forum.

    So in essence what you've done is to give us your entire assignment without asking a question and without showing any evidence of effort on your part, and I'm afraid that in this situation there's not much that we can do for you other than to direct you to a tutorial or two.

    I suggest that you post your best solution that you've created so far, and ask as specific questions as possible, and we'll be much better able to help. If you really have no idea how to begin here, then please have a look here: Starting Writing a Program. It's a well-written article that can help.

    Much luck

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Just posting the assignment not encourage members to helping out. So be specific, are you stuck on anything? Let us know it with what you've tried, doesn't matter right or wrong.

  4. #4
    samd89 is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    ok then, no worries ive already answered the questions that ive posted up. But im a little stuck on one of the questions.

    1.7 Write an accessor getAsString which returns a String representation of the time using two digits each for hour and minute separated by a colon (: ) e.g. 14:35, 07:34, 02:03 and 00:00 for midnight.
    (Hint: look at Date class). If you can't get the formatting to work, just return the two numbers separated by a ":"

    here is the code i wrote for it, I thought it was right but its still doesnt compile.

    Java Code:
     public String getAsString ()
        {
            return as2Digits(hour) + ":" + as2Digits(minute);
        }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  6. #6
    samd89 is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    I've sorted my old problem but I got a new one. I get the error saying "illegal start of expression". And the part where it doesnt compile is "private void setHour(int minute)" I don't understand why I get this error as they are bothe identical.

    Java Code:
    /**1.3	write a private mutator setHour which sets the value of the hour field from a suitable parameter.
         *  If the value of the parameter is within an appropriate range (between 0 - 23), the mutator should set the value of the hour field to the parameter value.
         * If it is outside the appropriate range, the mutator sets the field value to 0.
         */
        private void setHour(int hour)
        {
            if ((replacementValue >= 0) & (replacementValue < 23)) {
                hour = replacementValue;
            }
            else{
            hour=0;
        } 
        
        /** 1.4	write a private method setMinute whose behaviour is similar to setHour but which uses the 
    	range appropriate for minutes (between 0 - 59). */
        private void setMinute(int minute)
        {
            if ((replacementValue >= 0) & (replacementValue < 23)) {
                minute = replacementValue;
            }
            else{
            minute=0;
        }

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. ArrayLists for BlueJ
    By heyit'skaye in forum New To Java
    Replies: 1
    Last Post: 09-01-2010, 05:15 AM
  2. Need Help in BlueJ... Cannot figure this out.
    By ERICAMAUVE4 in forum New To Java
    Replies: 2
    Last Post: 09-25-2009, 01:34 AM
  3. BlueJ help
    By Cid17 in forum New To Java
    Replies: 2
    Last Post: 06-29-2009, 08:38 PM
  4. help using BlueJ
    By zoe in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:07 AM
  5. BlueJ 2.2.0
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-07-2007, 03:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •