1. Copy the files Time.java (Code Listing 9.1) and TimeDemo.java (Code Listing9.2) from the StudentCD or as directed by your instructor. 2. In the Time.java file, add conditions to the decision structure which validatesthe data. Conditions are needed that will:a.Check the length of the Stringb. Check the position of the colonc.Check that all other characters are digits. 3. Add lines that will separate the String into two substrings containing hours andminutes. Convert these substrings to integers and save them into the instancevariables. 4. In the TimeDemo class, add a condition to the loop that converts the user’sanswer to a capital letter prior to checking it. 5.Compile, debug, and run. Test out your program using the following valid input: a.00:00  b.12:00  c.04:05 d.10:15   e.23:59   f.00:35 And the following invalid input: a.7:56  b. 15:78   c.08:60   d. 24:00  e. 3e:33  f. 1:111

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Task #1 Character and String Class Methods

1. Copy the files Time.java (Code Listing 9.1) and TimeDemo.java (Code Listing9.2) from the StudentCD or as directed by your instructor.

2. In the Time.java file, add conditions to the decision structure which validatesthe data. Conditions are needed that will:a.Check the length of the Stringb. Check the position of the colonc.Check that all other characters are digits.

3. Add lines that will separate the String into two substrings containing hours andminutes. Convert these substrings to integers and save them into the instancevariables.

4. In the TimeDemo class, add a condition to the loop that converts the user’sanswer to a capital letter prior to checking it.

5.Compile, debug, and run. Test out your program using the following valid input:

a.00:00  b.12:00  c.04:05 d.10:15   e.23:59   f.00:35

And the following invalid input:

a.7:56  b. 15:78   c.08:60   d. 24:00  e. 3e:33  f. 1:111  

 

/**
Represents time in hours and minutes using
the customary conventions.
*/

public class Time
{
private int hours; // Conventional hours
private int minutes; // Conventional minutes
private boolean afternoon; // Flag for afternoon

/**
Constructs a cutomary time (12 hours, am or pm)
from a military time ##:##
@param militaryTime Time in the military
format ##:##
*/

public Time(String militaryTime)
{
// Check to make sure something was entered
if (militaryTime == null)
{
System.out.println(militaryTime +
" is not a " +
"valid miliary time." );
}
// Check to make sure there are 5 characters
else if (// CONDITION TO CHECK LENGTH OF STRING)
{
System.out.println(militaryTime +
" is not a " +
"valid miliary time." );
}
else
{
// Check to make sure the colon is in
// the correct spot
if (//CONDITION TO CHECK COLON POSITION)
{
System.out.println(militaryTime +
" is not a " +
"valid miliary time." );
}
// Check to make sure all other characters
// are digits
else if (// CONDITION TO CHECK FOR DIGIT)
{
System.out.println(militaryTime +
" is not a " +
"valid miliary time." );
}
else if (// CONDITION TO CHECK FOR DIGIT)
{
System.out.println(militaryTime +
" is not a " +
"valid miliary time." );
}
else if (//CONDITION TO CHECK FOR DIGIT)
{
System.out.println(militaryTime +
" is not a " +
"valid miliary time." );
}
else if (//CONDITION TO CHECK FOR DIGIT)
{
System.out.println(militaryTime +
" is not a " +
"valid miliary time." );
}
else
{
// SEPARATE THE STRING INTO THE HOURS
// AND THE MINUTES, CONVERTING THEM TO
// INTEGERS AND STORING INTO THE
// INSTANCE VARIABLES

// Validate hours and minutes are valid values
if(hours > 23)
{
System.out.println(militaryTime +
" is not a " +
"valid miliary time." );
}
else if(minutes > 59)
{
System.out.println(militaryTime +
" is not a " +
"valid miliary time." );
}
// Convert military time to conventional time
// for afternoon times
else if (hours > 12)
{
hours = hours - 12;
afternoon = true;
System.out.println(this.toString());
}
// Account for midnight
else if (hours == 0)
{
hours = 12;
System.out.println(this.toString());
}
// Account for noon
else if (hours == 12)
{
afternoon = true;
System.out.println(this.toString());
}
// Morning times do not need converting
else
{
System.out.println(this.toString());
}
}
}
}

/**
The toString method returns a conventional time.
@return A conventional time with am or pm.
*/
public String toString()
{
String am_pm;
String zero = "";

if (afternoon)
am_pm = "PM";
else
am_pm = "AM";
if (minutes < 10)
zero = "0";

return hours + ":" + zero + minutes + " " + am_pm;
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Knowledge Booster
Concept of pointer parameter
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education