Results 1 to 7 of 7
  1. #1
    Comp_Sci is offline Member
    Join Date
    Jul 2014
    Posts
    4
    Rep Power
    0

    Question Need Help with Java Code on Netbeans

    I have two classes: one with main [EditTime2Test] and the other with the methods [Time2]. I want to run EditTime2Test and for the Time2 class to run including the addTime method but the method will not give me the correct outcome. What am I doing wrong that I can not see. Thank you for your help.

    Also, I can not put any of the methods static except the main one.

    Java Code:
       public class Time2 {    
          
      // private int day; // > 24 hours
       private int hour; // 0 - 23
       private int minute; // 0 - 59
       private int second; // 0 - 59
       
       
       // Time2 no-argument constructor: 
       // initializes each instance variable to zero
       public Time2()
       {
          this( 0, 0, 0 ); // invoke Time2 constructor with three arguments
       } // end Time2 no-argument constructor
    
       // Time2 constructor: hour supplied, minute and second defaulted to 0
       public Time2( int h ) 
       { 
          this( h, 0, 0 ); // invoke Time2 constructor with three arguments
       } // end Time2 one-argument constructor
    
       // Time2 constructor: hour and minute supplied, second defaulted to 
       public Time2( int h, int m ) 
       { 
          this( h, m, 0 ); // invoke Time2 constructor with three arguments
       } // end Time2 two-argument constructor 
    
       // Time2 constructor: hour, minute and second supplied
       public Time2( int h, int m, int s ) 
       {
          setTime( h, m, s ); // invoke setTime to validate time
       } // end Time2 three-argument constructor 
    
       // Time2 constructor: another Time2 object supplied
       public Time2( Time2 time )
       {
          // invoke Time2 three-argument constructor
          this( time.getHour(), time.getMinute(), time.getSecond() );
       } // end Time2 constructor with a Time2 object argument
    
       // Set Methods
       // set a new time value using universal time; 
       // validate the data
       public void setTime( int h, int m, int s )
       {
          setHour( h ); // set the hour
          setMinute( m ); // set the minute
          setSecond( s ); // set the second
       } // end method setTime
    
       // validate and set hour 
       public void setHour( int h ) 
       { 
           if ( h >= 0 && h < 24 )
             hour = h; 
          else
             throw new IllegalArgumentException( "hour must be 0-23" );
       } // end method setHour
    
       // validate and set minute 
       public void setMinute( int m ) 
       { 
          if ( m >= 0 && m < 60 )
             minute = m;  
          else
             throw new IllegalArgumentException( "minute must be 0-59" );
       } // end method setMinute
    
       // validate and set second 
       public void setSecond( int s ) 
       { 
    
          if ( s >= 0 && s < 60 )
             second =  ( ( s >= 0 && s < 60 ) ? s : 0 ); 
          else
             throw new IllegalArgumentException( "second must be 0-59" );
       } // end method setSecond
       
       //  add time from user
      
       public void addTime(int h, int m, int s) 
        {
           {     
            getHour();
            int hr = getHour();
            
            getMinute();  
            int min = getMinute();
            
            getSecond();
            int sec = getSecond();
            
            System.out.printf("%d%d%d",hr,min,sec );
            System.out.printf("%d%d%d",h,m,s );
            
            int newHour = (hr + h) > 23 || (hr + h) < 0 ? -1 : hr + h;
            int newMin = (min + m) > 60 || (min + m) < 0 ? -1 : min + m;
            int newSec = (sec + s) > 60 || (sec + s) < 0 ? -1 : sec + s;
            if (newHour < 0)    // Validate hours
                throw new IllegalArgumentException(String.format("Hour input must be between -%d and %d", hr, 23 - hr));
    
            else if (newMin < 0)    // Validate minutes
                throw new IllegalArgumentException(String.format("Minute input must be between -%d and %d", min, 59 - min));
    
            else if (newSec < 0)    //Validate seconds
                throw new IllegalArgumentException(String.format("Second input must be between -%d and %d", sec, 59 - sec));
    
            else
                setTime(newHour, newMin, newSec);        
        }
           
        }
    
       // Get Method
       // get hour value
       public int getHour() 
       { 
          return  hour; 
         
       } // end method getHour
    
       // get minute value
       public int getMinute() 
       { 
         return minute; 
       } // end method getMinute
    
       // get second value
       public int getSecond() 
       { 
          return second; 
       } // end method getSecond
    
       // convert to String in universal-time format (HH:MM:SS)
       public String toUniversalString()
       {
          return String.format( 
             "%02d:%02d:%02d", getHour(), getMinute(), getSecond() );
       } // end method toUniversalString
       
       // convert to String in standard-time format (H:MM:SS AM or PM)
       public String toString()
       {
          return String.format( "%d:%02d:%02d %s", 
             ( (getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12 ),
             getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) );
       } // end method toString    
    }

    Java Code:
    import java.util.Scanner;
    
        public static void main(String[] args) {
          Time2 t1 = new Time2(); // 00:00:00
          Time2 t2 = new Time2( 2 ); // 02:00:00
          Time2 t3 = new Time2( 21, 34 ); // 21:34:00
          Time2 t4 = new Time2( 12, 25, 42 ); // 12:25:42
          Time2 t5 = new Time2( t4 ); // 12:25:42
          
    
          int h;
          int m;
          int s;
          
          Time2 aRef = new Time2();
    
            
            Scanner input = new Scanner(System.in);
         
           System.out.print("Please enter the amount of hour(s) you would like to add.\n");
           h = input.nextInt(); //reads hours needed to add
           System.out.print("Please enter the amount of minute(s) you would like to add.\n");
            m = input.nextInt(); //reads minutes needed to add
           System.out.print("Please enter the amount of second(s) you would like to add.\n"); 
            s = input.nextInt(); //reads minutes needed to add*/
     
             aRef.addTime(h, m, s);
    
          System.out.println( "Constructed with:" );
          System.out.println( "t1: all arguments defaulted" );
          System.out.printf( "   %s\n", t1.toUniversalString() );
          System.out.printf( "   %s\n", t1.toString() );
                  
          System.out.println( 
             "t2: hour specified; minute and second defaulted" );
         
          System.out.printf( "   %s\n", t2.toUniversalString() );
          System.out.printf( "   %s\n", t2.toString() );
    
          System.out.println( 
             "t3: hour and minute specified; second defaulted" );
          System.out.printf( "   %s\n", t3.toUniversalString() );
          System.out.printf( "   %s\n", t3.toString() );
    
          System.out.println( "t4: hour, minute and second specified" );
          System.out.printf( "   %s\n", t4.toUniversalString() );
          System.out.printf( "   %s\n", t4.toString() );
    
          System.out.println( "t5: Time2 object t4 specified" );
          System.out.printf( "   %s\n", t5.toUniversalString() );
          System.out.printf( "   %s\n", t5.toString() );
    
          // attempt to initialize t6 with invalid values
          try
          {
             Time2 t6 = new Time2( 27, 74, 99 ); // invalid values
          } // end try
          catch ( IllegalArgumentException e )
          {
             System.out.printf( "\nException while initializing t6: %s\n",
                e.getMessage() );
          } // end catch
       } // end main
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,775
    Rep Power
    5

    Default Re: Need Help with Java Code on Netbeans

    You haven't even told us what is wrong other than it doesn't work. What is it printing vs what should it print?
    Have you sprinkled print statements through out your program to see what the state of the
    variables are?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Comp_Sci is offline Member
    Join Date
    Jul 2014
    Posts
    4
    Rep Power
    0

    Default Re: Need Help with Java Code on Netbeans

    I have put print statements throughout the program and see if the values are printing correctly. However, in the addTime method when I call the getHour(), getMinute() and getSecond () methods to add the integers the user inputs, this methods (getHour(), getMinute() and getSecond ()) give me a value of 0 which is not correct. Am I calling them incorrectly?

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,775
    Rep Power
    5

    Default Re: Need Help with Java Code on Netbeans

    At the beginning of main, you create different instances of Time2. But the only one where addTime is called is 'aRef' which uses the
    default constructor, hence you set it to all 0's.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Comp_Sci is offline Member
    Join Date
    Jul 2014
    Posts
    4
    Rep Power
    0

    Default Re: Need Help with Java Code on Netbeans

    Okay now I understand that part. Likewise, can you help me understand in the addTime method why my getHour(), getMinute() and getSecond methods are not being called correctly and the value set equal to the int hr, min or sec respectively?

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,775
    Rep Power
    5

    Default Re: Need Help with Java Code on Netbeans

    I'm not certain you do understand. You do the following:

    Java Code:
    Time2 aRef = new Time2();
    aRef.addTime(h,m,s);
    So calling your methods will return all 0's.

    But take this example from your code:
    Java Code:
    Time2 t4 = new Time2( 12, 25, 42 ); // 12:25:42
    Now the methods should return 12, 25, and 42 when called via
    Java Code:
    t4.addtime(1,2,3); // or more appropriate arguments;
    Also you should get rid of the extra 'lone' method calls. They aren't doing much.

    Java Code:
    getHour();  // get rid of this
           int hr = getHour();
             
            getMinute();   // and this
            int min = getMinute();
             
            getSecond(); // and this
            int sec = getSecond();
    Lastly, it is more proper to use %n than \n in a printf statement.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Comp_Sci is offline Member
    Join Date
    Jul 2014
    Posts
    4
    Rep Power
    0

    Default Re: Need Help with Java Code on Netbeans

    All I need was an example. I am more of if I see it I know what is happening. Thank you so much.

Similar Threads

  1. Netbeans GUI source code
    By nksjolinder in forum New To Java
    Replies: 2
    Last Post: 09-21-2011, 10:24 PM
  2. How do I run this code in Netbeans?
    By TaxpayersMoney in forum New To Java
    Replies: 5
    Last Post: 07-28-2011, 07:47 AM
  3. NetBeans - error in code?
    By Sparky in forum NetBeans
    Replies: 4
    Last Post: 02-03-2011, 02:51 AM
  4. Replies: 9
    Last Post: 12-21-2010, 02:37 PM
  5. How to transfer my code to NetBeans IDE
    By ntagrafix in forum New To Java
    Replies: 18
    Last Post: 11-24-2009, 04:33 PM

Tags for this Thread

Posting Permissions

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