Search code examples
javamethodsconstructorgetter-setter

How to properly set totalSeconds() (hours minutes seconds) in my line of code and return it


Hey guys my teacher gave me a clock program to make, it has to return information etc, so far I'm a little confused on how total seconds is supposed to work, I will show my driver and normal file below, please help. I have to make sure the Hours Minutes and Seconds return in total seconds. I will post live edits I make to my code

I have tried to return it by adding a variable and adding it in the driver to no avail.

{

  //Instance Variables
  private int Hours;
  private int Minutes;
  private int Seconds;
  private double Cost;
  private boolean IsOn;
  private int DLS;



  //Zero arg constructors
  public Clock()
  { // Start
    Hours = 10;
    Minutes = 52;
    Seconds = 23;   
    Cost = 20.00;
    IsOn = true;
    DLS = 11;
  } // End

  //Multi-Argument Constructors

  public Clock( int Hours, int Minutes, int Seconds, double Cost , boolean IsOn, int DSL)
  { // Start
    this.Hours = Hours;
    this.Minutes = Minutes;
    this.Seconds = Seconds;
    this.Cost = Cost;
    this.IsOn = IsOn;
    this.DLS = DLS;
  } // End

  public void setTime(int Hours)
  {
    System.out.println(Hours + 1);
  }

  public int convertDaylightSaving(int Hours)
  {
    return Hours;
  }



  //ToString Method

  public String toString()
  { //Start
    String output;
    output = "When I checked my watch the hour was: " + Hours + "\n" + 
      "When I checked my watch the minute was: " + Minutes + "\n" +
      "When I checked my watch the seconds were: " + Seconds + "\n" +
      "The Cost is: " + Cost + "\n" +
      "Is this the time right now?: " + IsOn;

    return output;

  }
} // End

public class ClockDriver
{ 
 public class ClockDriver
{ // Start

  public static void main(String[] args)
  { // Officially Start

    Clock Watch = new Clock( 11, 04, 16, 35.99, false, 11);
    System.out.println(Watch);
  } // End
} // Officially End

The expected results are for it to print out everything I've made, the result is that it errors.


Solution

  • Ok so I actually answered my own question by doing the following.

    
    
    public class Clock
    {
    
      //Instance Variables
      private int Hours;
      private int Minutes;
      private int Seconds;
      private double Cost;
      private boolean IsOn;
      private int DLS;
      int totalSeconds; 
    
    
    
      //Zero arg constructors
      public Clock()
      { // Start
        Hours = 10;
        Minutes = 52;
        Seconds = 23;   
        Cost = 20.00;
        IsOn = true;
        DLS = 11;
        totalSeconds = 9945;
      } // End
    
      //Multi-Argument Constructors
    
      public Clock( int Hours, int Minutes, int Seconds, double Cost , boolean IsOn, int DSL, int totalSeconds)
      { // Start
        this.Hours = Hours;
        this.Minutes = Minutes;
        this.Seconds = Seconds;
        this.Cost = Cost;
        this.IsOn = IsOn;
        this.DLS = DLS;
        this.totalSeconds = totalSeconds;
      } // End
    
      public void setTime(int Hours)
      {
        System.out.println(Hours + 1);
      }
    
      public int convertDaylightSaving(int Hours)
      {
        return Hours;
      }
        public int totalSeconds()
        {
          return totalSeconds + 5000;
        }
    
    
      //ToString Method
    
      public String toString()
      { //Start
        String output;
        output = "When I checked my watch the hour was: " + Hours + "\n" + 
          "When I checked my watch the minute was: " + Minutes + "\n" +
          "When I checked my watch the seconds were: " + Seconds + "\n" +
          "The Cost is: " + Cost + "\n" +
          "Is this the time right now?: " + IsOn + "\n" + 
          "The total seconds right now are: " + totalSeconds;
    
    
        return output;
    
      }
    } // End
    
    

    After that I made the driver and had it return

    public class ClockDriver
    { // Start
    
      public static void main(String[] args)
      { // Officially Start
    
        Clock Watch = new Clock( 11, 04, 16, 35.99, false, 11, 9945);
        System.out.println(Watch);
    }
      } // End