Military time and Standard time Java One of the assignments

Military time and Standard time, Java

One of the assignments given is to create a program that will essentially test Military time and Standard time. The Guidlines to the problem are as follows:

Each class should be in a separate file. All related classes should be in a package. The main class should not be in the package.

Create a Time class (abstract) that includes the following members:
* hour
* minutes
* seconds
* Include overloaded constructor and accessor/mutator functions   

Create a MilTime class derived from Time that includes the following members
* milHours
- stores military time as an integer
- 1:00 PM would be stored as 1300
- 4:30 PM would be stored as 1630
* accessor for MilHour
- override from
base class
- return milHours
* Mutator for milHour
* Overloaded constructor
- accept milHours and seconds
- convert milHours to hours and minutes and store in class variables
* setTime
- same as
constructor
* getStandHr
- return hour in
standard format
- call base class accessor

Create a TimeClock class derived from MilTime that includes:
* another milHours and seconds variables
- these hours represent an ending time
- the variables inherited from MilTime represent a starting time
* overloaded constructor
- accept start time and end time
- use
base class constructor to store start time
* accessor and mutator for new class variables
* TimeDifference
- calculate the number of hours between
start time and end time

Create a main program that uses a MilTIme object and a TimeClock object
* Test that all class member functions work properly
* Create the objects based on user input

The MilTime and TimeClock classes should validate that the data entered is within the proper ranges
* seconds should be between 0 and 59 (inclusive)
   * military time should be between 0 and 2359 (inclusive)
- the last two digits should never be between 60 and 99 (inclusive)
* numbers outside the range should be set to the closest valid number

=======================================================================================

I have created a total of 4 .java files so far; main.java, Time.java, MilTime.java, and TimeClock.java

My I decided to work on this problem from top to bottom, starting with Time.java.

/* Time.java */

package demo
abstract public class Time
{
private int hour, minute, second;
// Default constructor.
public Time()
{
hour = 0;
minute = 0;
second = 0;
}
// Overloaded constructor.
public Time(int getHour, int getMinute, int getSecond)
{
hour = getHour;
minute = getMinute;
second = getSecond;
}
// Accessor/Mutator functions.
public int getHour()
{
return hour;
}
public int getMinute()
{
return minute;
}
public int getSecond()
{
return second;
}
public void setHour(int hour)
{
this.hour = hour;
}
public void setMinute(int minute)
{
this.minute = minute;
}
public void setSecond(int second)
{
this.second = second;
}
}

This class I believe I have correct according to the assignment guide. However after this I am completely stuck.

/* MilTime.java */

package demo
public class MilTime extends Time

{
// Private instance variables.
private int milHours;
  
/**
* Accessor for milHour
* override from base class
* return milHours
* @return
*/
public int getMilHour()
{
return milHours;
}
  
/**
* Mutator for milHour
* @param milHours
*/
public void setMilHour(int milHours)
{
this.milHours = milHours;
}
  
/** Overloaded constructor
* Accept milHours and seconds
* convert milHours to hours and minutes and store in class variables
* @param milHours
* @param seconds
*/
public void setTime(int milHours, int seconds)
{
int hours=0, minutes=0;
if (milHours >= 1200) // time is PM
{
hours = ((milHours - 1200) / 100);
}
else
{
hours = (milHours / 100);
}
minutes = (milHours - (hours * 100));
if (hours == 0)
hours = 12;
Time ctime = new Time(){};
ctime.setHour(hours);
ctime.setMinute(minutes);
ctime.setSecond(seconds);
}
  
// Same as constructor?
public void setTime()
{
Time ctime = new Time(){};
ctime.setHour(milHours);
}
}

I have quickly become lost with how to accomplish certain tasks. mainly how everything is supposed to be properly organized. If I am just going about this the wrong way, please correct me. Thanks.

Solution

PROGRAM CODE:

Time.java

package time;

public class Time {

     

   private int hours;

   private int minutes;

   private int seconds;

  

   public Time(int hrs, int mins, int secs)

   {

       hours = hrs;

       minutes = mins;

       seconds = secs;

   }

   public Time(int hrs)

   {

       hours = hrs;

       minutes = 0;

       seconds = 0;

   }

  

   public int getHours() {

       return hours;

   }

   public void setHours(int hours) {

       this.hours = hours;

   }

   public int getMinutes() {

       return minutes;

   }

   public void setMinutes(int minutes) {

       this.minutes = minutes;

   }

   public int getSeconds() {

       return seconds;

   }

   public void setSeconds(int seconds) {

       this.seconds = seconds;

   }

  

  

}

MilTime.java

package time;

public class MilTime extends Time{

   private int milHours;

  

   public MilTime(int milHours, int seconds) {

       super(0,0,seconds);

       this.milHours = milHours;

       int hours=0, minutes=0;

       if (milHours >= 1200) // time is PM

       {

       hours = ((milHours - 1200) / 100);

       }

       else

       hours = (milHours / 100);

      

       minutes = (milHours %100);

       if (hours == 0)

       hours = 12;

       setHours(hours);

       setMinutes(minutes);

   }

   public int getMilHours() {

       return this.milHours;

   }

   public void setMilHours(int milHours) {

       this.milHours = milHours;

       int hours=0, minutes=0;

       if (milHours >= 1200) // time is PM

       {

       hours = ((milHours - 1200) / 100);

       }

       else

       hours = (milHours / 100);

       minutes = (milHours - (hours * 100));

       if (hours == 0)

       hours = 12;

       setHours(hours);

       setMinutes(minutes);

   }

  

   public int getStandHr()

   {

       return getHours();

   }

}

TimeClock.java

package time;

public class TimeClock extends MilTime{

   private int milHours;

   private int seconds;

  

   public TimeClock(int startMilHours, int startSeconds, int endMiltime, int endSecondsTime) {

       super(startMilHours, startSeconds);

       this.milHours = endMiltime;

       this.seconds = endSecondsTime;

   }

   public int getEndMilHours() {

       return milHours;

   }

   public void setEndMilHours(int milHours) {

       this.milHours = milHours;

   }

   public int getEndSeconds() {

       return seconds;

   }

   public void setEndSeconds(int seconds) {

       this.seconds = seconds;

   }

  

   public int getStartMilHours() {

       return getMilHours();

   }

   public void setStartMilHours(int milHours) {

       super.setMilHours(milHours);

   }

   public int getStartSeconds() {

       return getSeconds();

   }

   public void setStartSeconds(int seconds) {

       super.setSeconds(seconds);

   }

  

  

   public double timeDifference()

   {

      

       return (getEndMilHours() - getStartMilHours())/100;

   }

}

TimeTester.java

import java.util.Scanner;

public class TimeTester {

  

   public static int getHours()

   {

       int hrs;

       Scanner keyboard = new Scanner(System.in);

       System.out.print(\"Enter the military hours: \");

       hrs = keyboard.nextInt();

       if(hrs < 0)

           hrs = 0;

       else if(hrs > 2359)

           hrs = 2359;

       return hrs;

   }

  

   public static int getSeconds()

   {

       int sec;

       Scanner keyboard = new Scanner(System.in);

       System.out.print(\"Enter the seconds: \");

       sec = keyboard.nextInt();

       if(sec < 0)

           sec = 0;

       else if(sec > 59)

           sec = 59;

       return sec;

   }

  

   public static void main(String args[])

   {

       int hrs = -1, sec = -1, smilHours = -1, sseconds = -1, emilHours = -1, eseconds = -1;

       MilTime miltime;

       TimeClock clock;

       hrs = getHours();

       sec = getSeconds();

       miltime = new MilTime(hrs, sec);

       System.out.println(\"Military time: \"+ miltime.getMilHours());

       System.out.println(\"Hours: \" + miltime.getStandHr());

       System.out.println(\"Minutes: \" + miltime.getMinutes());

       System.out.println(\"Seconds: \" + miltime.getSeconds());

      

       System.out.println(\"Enter start time: \");

       smilHours = getHours();

       sseconds = getSeconds();

       System.out.println(\"Enter the end time: \");

       emilHours = getHours();

       eseconds = getSeconds();

      

       clock = new TimeClock(smilHours, sseconds, emilHours, eseconds);

       System.out.println(\"Start Time is \" + clock.getStartMilHours());

       System.out.println(\"End Time is \" + clock.getEndMilHours());

       System.out.println(\"Time difference: \" + clock.timeDifference());

   }

}

OUTPUT:

Enter the military hours: 2345

Enter the seconds: 3

Military time: 2345

Hours: 11

Minutes: 45

Seconds: 3

Enter start time:

Enter the military hours: 1200

Enter the seconds: 20

Enter the end time:

Enter the military hours: 1300

Enter the seconds: 20

Start Time is 1200

End Time is 1300

Time difference: 1.0

Military time and Standard time, Java One of the assignments given is to create a program that will essentially test Military time and Standard time. The Guidli
Military time and Standard time, Java One of the assignments given is to create a program that will essentially test Military time and Standard time. The Guidli
Military time and Standard time, Java One of the assignments given is to create a program that will essentially test Military time and Standard time. The Guidli
Military time and Standard time, Java One of the assignments given is to create a program that will essentially test Military time and Standard time. The Guidli
Military time and Standard time, Java One of the assignments given is to create a program that will essentially test Military time and Standard time. The Guidli
Military time and Standard time, Java One of the assignments given is to create a program that will essentially test Military time and Standard time. The Guidli
Military time and Standard time, Java One of the assignments given is to create a program that will essentially test Military time and Standard time. The Guidli
Military time and Standard time, Java One of the assignments given is to create a program that will essentially test Military time and Standard time. The Guidli
Military time and Standard time, Java One of the assignments given is to create a program that will essentially test Military time and Standard time. The Guidli
Military time and Standard time, Java One of the assignments given is to create a program that will essentially test Military time and Standard time. The Guidli

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site