0

Im at the last few steps in my homework and need some help understanding/doing the last two steps. The last two things I have to do are:

Create a toString() method that returns the value as a formatted time String. Remember that values less than 10 must be padded with zeroes. For example, midnight will look like this returned from getMilitaryTime(): 00:00:00. Use the String zeroPad(int) method to pad the numbers - see the code below. Use a StringBuffer object to build the String in the toString() method, calling zeroPad(int) as necessary. Create a main method and construct the following times and print the time using System.out.println(clock): Call the default constructor (no parameters)
hour: 0, minute: 94, second: 56
hour: 14, minute: 63, second: 64
hour: 98, minute: 76, second: -64
hour: 5, minute: 8, second: 1
hour: 23, minute: 59, second: 59

under just the println at the end I get the error :Syntax error on token "println", = expected after this token. I'm not sure if fixing this will help me move onto the next steps or not. Is what I have for the most part correct when it come to making the toString method?

So Far this is what I have:

//Create a new class called Clock
public class Clock {
public static void main(String[] args){ 
}
//Create three integer attributes for hour, minute and second.
//Always store the hour in military time.       
private double MilitaryHour; 
private double Minute;
private double Second;
// Create setters for the hour, minute and second values.
public double getMilitaryHour() {
return MilitaryHour;
}
public void setMilitaryHour(double militaryHour) {
if(militaryHour < 0 || militaryHour > 23)
this.MilitaryHour = (militaryHour % 24);
this.MilitaryHour = militaryHour;
}


public double getMinute() {
return Minute;
}
public void setMinute(double minute) {
if(minute < 0 || minute > 59)
this.Minute = (minute % 60);
this.Minute = minute;
}
public double getSecond() {
return Second;
}
public void setSecond(double second) {
if(second < 0 || second > 59)
this.Second = (second % 60);
this.Second = second;
}
public Clock(double MilitaryHour,double Minute,double Second) {
this.MilitaryHour = MilitaryHour;
this.Minute = Minute;
this.Second = Second;

}
/default
public Clock(){
this(0.0, 0.0, 0.0);
}

public String toString()
{

String result = "Hour: " + getMilitaryHour()+"Minute: " + getMinute()+ "Seconds:" + getSecond();
return result;
}
System.out.println("" + result.toString());
}
BerlinUnited
  • 1
  • 1
  • 3
  • 3
    You need to (1) format your code properly (indentation matters since it for instance can show possible problems with scope) (2) describe problem you are having with your code - do you get wrong results/errors/exceptions? – Pshemo Oct 06 '15 at 19:23
  • under just the println at the end I get the error :Syntax error on token "println", = expected after this token. I'm not sure if fixing this will help me move onto the next steps or not. Is what I have for the most part correct when it come to making the toString method? – BerlinUnited Oct 06 '15 at 19:33
  • Problem descriptions should be part of question. Please [edit] your post and add this information there. – Pshemo Oct 06 '15 at 19:35
  • I have a pretty fancy idea: prefer to write code like `System.out.println("" + result.toString());` _inside_ a method, not _outside_. ;P. Oh and another fancy idea: reload the question before you edit it, so you won't ignore/rollback existing edits. – Tom Oct 06 '15 at 19:41

1 Answers1

0

A quick look shows me that your implementation contains many errors.

  1. Your statement System.out.println("" + result.toString()); is not placed correctly. It should be place inside a function, may be inside your main function.
  2. The result.toString() doesn't make sense. With what I understood from your problem, you need to create a Clock object like this:

    Clock clock = new Clock(1, 1, 1); Then,

    System.out.println(clock.toString());

To print the object. Like this:

public static void main(String[] args){ 
    Clock clock = new Clock(1, 1, 1);
    System.out.println(clock.toString());
}

I am not sure if this would be enough, but this should fix some of your issues.

Sushant Kafle
  • 446
  • 3
  • 8
  • `Clock clock = new Clock(1,1,1);` will create a clock object by calling the constructor [`public Clock(double MilitaryHour,double Minute,double Second)`] setting MilitaryHour = 1, Minute = 1 and Second = 1. – Sushant Kafle Oct 06 '15 at 20:08
  • If I set the 1's in new clock to MilitaryHour, Minutes, Seconds will it go back make it so a number greater than 24 will print as 0? – BerlinUnited Oct 06 '15 at 20:25
  • you are yet to implement the `zeroPad(int)` function mentioned in the description you posted. You need to implement that, and I think you are good to go! – Sushant Kafle Oct 06 '15 at 20:28