Task #1Character and String Class Methods 2. In the Time.java file,add condition
ID: 3634641 • Letter: T
Question
Task #1Character and String Class Methods2. In the Time.java file,add conditions to the decision structure which validates
the data. Conditions are needed that will
a) Check the length of the string
b) Check the position of the colon
c) Check that all other characters are digits
3. Add lines that will separate the string into two substrings containing hours and
minutes. Convert these substrings to integers and save them into the instance
variables.
4. In the TimeDemo class,add a condition to the loop that converts the user’s
answer to a capital letter prior to checking it.
5. Compile,debug,and run. Test out your program using the following valid
input:00:00,12:00,04:05,10:15,23:59,00:35,and the following invalid input:
7:56,15:78,08:60,24:00,3e:33,1:111.
Task #2 StringTokenizer and StringBuilder classes
1. Copy the file secret.txt(code listing 10.3) from www.aw.com/cssupportor as
directed by your instructor. This file is only one line long. It contains 2
sentences.
2. Write a main method that will read the file secret.txt,separate it into word
tokens.
3. You should process the tokens by taking the first letter of every fifth word,
starting with the first word in the file. These letters should converted to capitals,
then be appended to a StringBuilder object to form a word which will be printed
to the console to display the secret message.
Time.java
//Represents time in hours and minutes using
//the customary conventions
public class Time
{
//hours in conventional time
private int hours;
//minutes in conventional time
private int minutes;
//true if afternoon time, false if morning time
private boolean afternoon;
//Constructs a cutomary time (12 hours, am or pm)
//from a military time ##:##
public Time(String militaryTime)
{
//Check to make sure something was entered
if (militaryTime == null)
{
System.out.println(
"You must enter 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 && minutes == 0)
{
hours = 12;
System.out.println(this.toString());
}
//morning times don't need converting
else
{
System.out.println(this.toString());
}
}
}
}
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;
}
}
TimeDemo.java
import java.util.Scanner;
public class TimeDemo
{
public static void main (String [] args)
{
Scanner keyboard = new Scanner(System.in);
char answer = 'Y';
String enteredTime;
String response;
while (//CHECK ANSWER AFTER CONVERTING TO CAPITAL)
{
System.out.print(
"Enter a miitary time using the ##:## form ");
enteredTime = keyboard.nextLine();
Time now = new Time (enteredTime);
System.out.println(
"Do you want to enter another (Y/N)? ");
response = keyboard.nextLine();
answer = response.charAt(0);
}
}
}
// secret.txt
January is the first month and december is the last. Violet is a purple color as are lilac and plum.
Explanation / Answer
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.