Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: small problem to do with 'this.'! help and explanation much appreciated!

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default small problem to do with 'this.'! help and explanation much appreciated!

    So this is my secondary class
    public class Tuna{
       private int hour = 2;
       private int minute = 2;
       private int second = 3;
     
       public void setTime(int hour, int minute, int second){
    	  this.hour = 4;
    	  this.minute = 5;
    	  this.second = 6;
     
       }
       	public String toMilitary(){
       		return String.format("%02d:%02d:%02d", hour, minute, second );
       	}
     
       	public String toString(){
       		return String.format("%d:%02d:%02d %s",((hour==0||hour==12)?12:hour%12), minute, second, (hour <12? "AM": "PM"));
       	}
    }

    and this is my main class
    public class Apples{
        public static void main (String args[]){
        	Tuna tunaObject = new Tuna();
        	System.out.println(tunaObject.toMilitary());
        	System.out.println(tunaObject.toString());
        	System.out.println(tunaObject.toMilitary());
        	System.out.println(tunaObject.toString());
     
        }
    }

    The effect i wanted was (for all the printlns) it would use the numbers in the setTime(method?constructor? im not sure) but it uses the local 'private' variables.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: small problem to do with 'this.'! help and explanation much appreciated!

    Try calling the setTime() method from your main() method before the println() statements, something like:

    tunaObject.setTime( 4, 5, 6 );

    Once you see how that works, you may want to change the setTime() method to actually use the parameters passed to it.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    nikolaiL (May 21st, 2014)

  4. #3
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: small problem to do with 'this.'! help and explanation much appreciated!

    thank you

Similar Threads

  1. Average of an array! help and explanation much appreciated! :)
    By nikolaiL in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 14th, 2014, 02:44 PM
  2. [SOLVED] Small problem
    By keepStriving in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 23rd, 2013, 06:31 AM
  3. Big Rookie Question - New to Java, small problem Help appreciated!
    By Knighter in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 24th, 2013, 01:55 AM
  4. Problem with my code, help would be appreciated.
    By Bobba in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 1st, 2012, 06:31 PM
  5. Small problem, help please?
    By Jonathansafc in forum What's Wrong With My Code?
    Replies: 33
    Last Post: September 3rd, 2011, 06:46 PM