Results 1 to 8 of 8
  1. #1
    thomase is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default Accessor help needed (I think...- little confussed)

    Hi Guys, I'm new to java and I'm struggling a bit this semester as we've introduced using a main file and then a driver file.

    At the moment I'm trying to get my head around accessor methods.

    I've written this code to convert time in seconds into "hours:minutes:seconds" format.



    If in another file I set something (either via a scanner, or just a plan int variable with a value of seconds), how can I send it to the TimeDisplay class to return the correctly formatted time?

    Many thanks in advance :)
    Last edited by thomase; 03-12-2009 at 01:49 PM. Reason: removing private code

  2. #2
    thomase is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default

    It's gone off the first past so I assume it's okay to give it a little bump?

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

    Default

    If all this class is, is a place to hold your small utility or helper method, this is one of the few places where I think it's OK to use static methods: in other words, you can have a public static method that takes an int (or long?) as input and returns a String as output, and then call it as you would any static method -- on the class, not on an object.

  4. #4
    thomase is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    If all this class is, is a place to hold your small utility or helper method, this is one of the few places where I think it's OK to use static methods: in other words, you can have a public static method that takes an int (or long?) as input and returns a String as output, and then call it as you would any static method -- on the class, not on an object.
    So with that I'd keep what I've got wouldn't I? Then in the main file what would I put? I've had a look on the net for a tutorial, I'm just unsure of how to send the int (not long) from this 'main' file to the TimeDisplay class and then from there, to output it back in the 'main' file.

    I'd love a little code snipit or a link to a site where with a bit of work I could decode some code that would do this. Problem at the moment is I see websites and I'm not sure if they do have relevant code, but I've no problem working out how to do it from a code.

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

    Default

    similar to what you have, except for no constructor and the method is static and takes an int parameter, seconds.

  6. #6
    thomase is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    similar to what you have, except for no constructor and the method is static and takes an int parameter, seconds.
    hmm, still a little confused I think.

    Java Code:
    package albumlist;
    public class Main {
    
        public static void main(String[] args) {
            
           int seconds = 23423123;
                    
        }
    }

    Cheers for the help so far! Rep Added :)

  7. #7
    anteup is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    Java Code:
    // ok in the application class ...
    	public static void main(String[] args)
     {
    	
    		 int seconds = 10001;
    
                   //here you are declaring and instantiating an object   //of the TimeDisplay class and naming it time, you are passing 
    // the seconds variable to the paramaterized constructor
    		 TimeDisplay time = new TimeDisplay(seconds);
    
    //here you are displaying your output.... by calling the accessor //accessor method displayHrsMinsSecs() on the object reference
    //time
    		 System.out.println(time.displayHrsMinsSecs());
    
    		
    	}
    
    }

  8. #8
    thomase is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default

    Ah, Thank you!! Thinking back now, I actually remember seeing code like this in my lecture notes but didn't fully understand it! Thanks again for the help - I can now use my accessor method :)

Similar Threads

  1. help needed
    By amarchandar in forum New To Java
    Replies: 1
    Last Post: 02-18-2009, 12:38 PM
  2. help needed
    By amarchandar in forum New To Java
    Replies: 3
    Last Post: 02-17-2009, 02:59 PM
  3. Replies: 6
    Last Post: 12-03-2008, 12:15 AM
  4. a little help needed..
    By litojs in forum New To Java
    Replies: 1
    Last Post: 11-16-2008, 08:12 PM
  5. Help needed.
    By necro-1000 in forum AWT / Swing
    Replies: 2
    Last Post: 01-12-2008, 12:37 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
  •