Question

In: Computer Science

java For this assignment, you will create a Time class that holds an hour value and...

java

For this assignment, you will create a Time class that holds an hour value and a minute value to represent a time. We will be using "military time", so 12:01 AM is 0001 and 1 PM is 1300. For this assignment, you may assume valid military times range from 0000 to 2359. Valid standard times range from 12:00 AM to 11:59 PM.

In previous assignments, we had a requirement that your class be named Main. In this assignment, the class is required to be named Time.

To get started, download the template file, Time.java. Your job will be to add the constructors and methods described in the following sections to the class, Time, that is declared in this template file.

Time should include two constructors:

Time() - Default constructor that sets the time to 0000.

Time(int h, int m) - If h is between 1 and 23 inclusive, set the hour to h. Otherwise, set the hour to 0. If m is between 0 and 59 inclusive, set the minutes to m. Otherwise, set the minutes to 0.

Time should include the following methods:

String toString() - Returns the time as a String of length 4 in the format: 0819. Notice that if the hour or minute is one digit, it should print a zero first. For example, 6 should print as 06.

String convert() - Returns the time as a String converted from military time to standard time. For example, 0545 becomes 5:45 AM and 1306 becomes 1:06 PM.

void increment() - Advances the time by one minute. Remember that 60 minutes = 1 hour. Therefore, if your time was 0359, and you add one minute, it becomes 0400. 2359 should increment to 0000.

To test your code, download the runner class student_time_runner.java into the same folder that holds your Time.java. Execute the method student_time_runner.main, and verify that the output matches the sample run listed below.

We will use a similar but different runner to grade the program. In order to pass all tests, you will need to change student_time_runner.java to test different values to make sure your program fits the requirements. Note: You will not be submitting student_time_runner.java. This file is provided to help you test your implementation of the class Time.

When you are done coding and testing, copy and paste your entire Time class into the Code Runner and press "Submit Answers" in order for your assignment to count as turned in.

Sample Run:

time1: 1456
convert time1 to standard time: 2:56 PM
time1: 1456
increment time1 five times: 1501

time2: 0012
increment time2 67 times: 0119
convert to time2 standard time: 1:19 AM
time2: 0119

time3: 0517
convert time3: 5:17 AM

time4: 1215
convert time4: 12:15 PM

time5: 0015
convert time5: 12:15 AM

time6: 0015
convert time6: 12:15 AM

time7: 2359
convert time7: 11:59 PM
increment time7: 0000
convert time7: 12:00 AM

NOTE: You must use the class name "Time" for this assignment. REMEMBER: You must SUBMIT your answer. Your assignment does not count as complete unless it has been submitted.

Solutions

Expert Solution

The following is the solution for the above problem :

Class : Time

Instance variable: h,m

These instance variables are declared as private by conforming with ususal coding norms, accompanied by their corresponding getters/setters for better accessibility.

toString() :

Method is returning a string by appending the required "0" and in order to minimize the code ternery operator has been brought into use:

condition ? statement/action if true : statement/action if false

increment() :

Method is incrementing the minutes by 1 unit. The use cases are:

  • if the minute exceeds 60 then the hour is also updated and minutes are set to 0.
  • if the hour equal to 24 (since we are taking input in military time) so it should also be updated likewise

convert()

Method returns a string after formatting the input with the provided use-cases:

  • If the user enters time>=0 or <12 then it is determined as AM and then the hour is set likewise
  • else it is determined as PM and the hour has to be adjusted to standard time from military time i.e.

time = 2355; ->

23 > 12 :=> 23-12 :=> 11

Hence, 11:55 PM

Following is the complete implementation of Time class :

public class Time {

  

private int h;

private int m;

  

public Time() {

super();

}

public Time(int h, int m){

if(h>=1 && h<=23){

this.h = h;

}else{

this.h = 0;

}

if(m>=0 && m<=59){

this.m = m;

}else{

this.m = 0;

}

}

  

public int getH() {

return h;

}

public void setH(int h) {

this.h = h;

}

public int getM() {

return m;

}

public void setM(int m) {

this.m = m;

}

public String convert(){

String standardTime = "";

String flag = "";

if(h>= 0 && h < 12){

flag = " AM";

if(h == 0){

standardTime = Integer.toString(12);

}

else{

standardTime = Integer.toString(h);

}

}else{

flag = " PM";

if(h == 12){

standardTime = Integer.toString(12);

}

else{

standardTime = Integer.toString(h - 12);

}   

}

standardTime += ":";

standardTime += Integer.toString(m).toCharArray().length == 1 ? "0"+Integer.toString(m):Integer.toString(m);

standardTime += flag;

return standardTime;

}

public void increment() {

int min = this.m;

min++;

if(min == 60){

this.m = 00;

this.h++;

if(this.h == 24){

this.h = 0;

}

}else{

this.m = min;

}

}

public String toString() {

String hour = Integer.toString(h).toCharArray().length == 1 ? "0" + Integer.toString(h) : Integer.toString(h);

String minutes = Integer.toString(m).toCharArray().length == 1 ? "0" + Integer.toString(m) : Integer.toString(m);

return hour + minutes ;

}

}


Related Solutions

JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
JAVA FRACTIONS QUESTION: You will create a Fraction class in Java to represent fractions and to...
JAVA FRACTIONS QUESTION: You will create a Fraction class in Java to represent fractions and to do fraction arithmetic. To get you used to the idea of unit testing, this homework does not require a main method. You can create one if you find it useful, however, we will not be grading or even looking at that code. You should be comfortable enough with the accuracy of your test cases that you do not need to use print statements or...
in Java please For this assignment you are to write a class that supports the addition...
in Java please For this assignment you are to write a class that supports the addition of extra long integers, by using linked-lists. Longer than what is supported by Java's built-in data type, called long. Your program will take in two strings, consisting of only digits, covert each of them to a linked-list that represents an integer version on that string. Then it will create a third linked-list that represents the sum of both of the linked lists. Lastly, it...
Java program In this assignment you are required to create a text parser in Java/C++. Given...
Java program In this assignment you are required to create a text parser in Java/C++. Given a input text file you need to parse it and answer a set of frequency related questions. Technical Requirement of Solution: You are required to do this ab initio (bare-bones from scratch). This means, your solution cannot use any library methods in Java except the ones listed below (or equivalent library functions in C++). String.split() and other String operations can be used wherever required....
Java program. Need to create a class names gradesgraph which will represent the GradesGraphtest program. Assignment...
Java program. Need to create a class names gradesgraph which will represent the GradesGraphtest program. Assignment Create a class GradesGraph that represents a grade distribution for a given course. Write methods to perform the following tasks: • Set the number of each of the letter grades A, B, C, D, and F. • Read the number of each of the letter grades A, B, C, D, and F. • Return the total number of grades. • Return the percentage of...
Java Coding Background You will create a Java class that simulates a water holding tank. The...
Java Coding Background You will create a Java class that simulates a water holding tank. The holding tank can hold volumes of water (measured in gallons) that range from 0 (empty) up to a maximum. If more than the maximum capacity is added to the holding tank, an overflow valve causes the excess to be dumped into the sewer system. Assignment The class will be named HoldingTank. The class attributes will consist of two int fields – current and maxCapacity....
In java: -Create a class named Animal
In java: -Create a class named Animal
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
Programmed In Java In this assignment you will create a simple game. You will put all...
Programmed In Java In this assignment you will create a simple game. You will put all of your code in a class called “Game”. You may put all of your code in the main method. An example of the “game” running is provided below. Y ou will start by welcoming the user. 1. You should print "Welcome! Your starting coordinates are (0, 0).” 2. On the next line, you will tell the user the acceptable list of commands. This should...
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class....
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class. The vehicle class should contain at least 2 variables that could pertain to ANY vehicle and two methods. The truck class should contain 2 variables that only apply to trucks and one method. Create a console program that will instantiate a truck with provided member information then call one method from the truck and one method contained from the inherited vehicle class. Have these...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT