Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Modify Time2 class to show seconds since midnight

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am new to Java and have been given an assignment to:
Modify the Time2 class to implement the time as the number of seconds since midnight. The class should have one data field (an int with the number of seconds since midnight) instead of three. This change should not affect the arguments, behavior, or output of the public methods.
Create a Driver class with a main method to test your Time2 class. This program should ask the user to input the number of hours, minutes, and seconds past midnight, creating a Time2 object and using the mutator methods. The program should then use the toString() method to print out the time.

I believe that I have covered what has been requested by getting rid of int hour, int minute and int second and replacing it with int totalSeconds, creating a Time2 Object, using the get/set methods and finally the toString method.  I have added in the questions to prompt the user to enter the hours, minutes and seconds but my output is all zeroes - 00:00:00.  I think my error is occurring in the return but being such a novice I'm not sure where the error is occurring
Enter number of hours past midnight:
10
Enter number of minutes past midnight:
5
Enter number of seconds past midnight:
6
00:00:00
In addition, I'm having difficulty with my driver class in that it just terminates with the error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method printf(Locale, String, Object...) in the type PrintStream is not applicable for the arguments (int, int, int)

at Time2Test.main(Time2Test.java:8)
Here is my code so far along with my Driver class to test it:
// Time2 class declaration with overloaded constructors.

import java.util.Scanner;

public class Time2 {

  public static void main(String[] args) { //main method
      Scanner input = new Scanner(System.in);
      System.out.println("Enter number of hours past midnight: "); //prompt user to enter hours
          input.nextLine();
      System.out.println("Enter number of minutes past midnight: "); //prompt user to enter minutes
          input.nextLine();
      System.out.println("Enter number of seconds past midnight: "); //prompt user to enter seconds
      input.nextLine();
      System.out.printf(String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond()));
  }

private static int totalSeconds; // // no-argument constructor initializes totalSeconds to zero;

public Time2() {
totalSeconds = 0;
}

public Time2(int hour) { //Time 2 constructor: hour given, minute and seconds have a default of 0
setTime(hour, 0, 0);
}

public Time2(int hour, int minute) { //Time 2 constructor: hour given, minute given and second has a default of 0
setTime(hour, minute, 0);
}

public Time2(int hour, int minute, int second) { // Time2 constructor: hour, minute and second given
setTime(hour, minute, second);
}

public Time2(Time2 time) { //Time 2 constructor: additional Time2 object given
setTime(Time2.getHour(), Time2.getMinute(), Time2.getSecond());
}

public void setTime(int hour, int minute, int second) { //method to set Time
setHour(hour);
setMinute(minute);
setSecond(second);
}

public void setHour(int hour) { // set hour value
int hours = (hour >= 0 && hour < 24) ? hour : 0;

totalSeconds = (hours * 3600) + (getMinute() * 60) + getSecond();
} // end method setHour

public void setMinute(int minute) { // set minute value
int minutes = (minute >= 0 && minute < 60) ? minute : 0;

totalSeconds = (getHour() * 3600) + (minutes * 60) + getSecond();
} // end method setMinute

public void setSecond(int second) { // set second value
int seconds = (second >= 0 && second < 60) ? second : 0;

totalSeconds = (getHour() * 3600) + (getMinute() * 60) + seconds;
} // end method setSecond

public static int getHour() { // get hour value
return (totalSeconds / 3600);
} // end method getHour

public static int getMinute() { // get minute value
return ((totalSeconds % 3600) / 60);
} // end method getMinute

public static int getSecond() { // get second value
return ((totalSeconds % 3600) % 60);
} // end method getSecond

public String toString() { // convert to String in format (HH:MM:SS)
return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond());
} // end method toString

public void Time2() {
// TODO Auto-generated method stub

}
} // end method toString
// end class Time2
Test class
//Testing for class Time2
public class Time2Test {

public static void main(String[] args) {

Time2 application = new Time2();
application.Time2();
System.out.printf(Time2.getHour(), Time2.getMinute(), Time2.getSecond());
} //end main
} //end class Time2Test

Any assistance would be greatly appreciated.
 
Rancher
Posts: 259
13
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you need to learn how to debug.
the first thing to learn is how to use a stack-trace to help find where your error is. you will have to post a copy of it here if you want help to read it.

the second thing to learn is how to use the debugging feature of your IDE. then you can step through the program line-by-line and watch what happens to the variables as the program executes. this should easily and quickly show you why your time function is always zeros.
 
Marshal
Posts: 76464
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should also read this FAQ. Have a look at your IDE if you get an

Unresolved compilation problem:

error message. Not only will there be a red mark against the offending line of code, but the error message tells you where to look for details of the error. You have used format() before, and given it the correct arguments.

Also please remind yourself how the code button works; I corrected your code tags on your first ever post.
 
We must storm this mad man's lab and destroy his villanous bomb! Are you with me tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic