1 Task 1 Character and String Class Methods Please apply the professors suggestions to your program. 2 Task 2

(1) Task #1 Character and String Class Methods
– Please apply the professors suggestions to your program.
(2) Task #2 String.split and the StringBuilder Class
I was working on Task #1. However, I got stuck. My program did not work correctly.
I got advice from a professor about my program.
Please apply the above suggestion to your program.
Professors advice.
• Consider using the isDigit(char ch) boolean Character class method for testing char values found in Table 9-1 on page 558.
• Assume that you are checking to see if the score on a test is not in the range from 0 to 100. Further, assume that the integer variable named score has already been declared correctly and has been defined by user input. You might use code similar to the code snippet below to detect an out of range value.
if (score 100)
{

System.out.println(“The value you entered “ + score + “ is not in the range from 0 to 100.”);
}
Please apply the above suggestion to your program.
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
My Program
public class Time
{
public static void main(String[] args)
{

private int hours; //Conventional hours

private int minutes; //Conventional minutes

private boolean afternoon; // Flag for afternoon

/**

Constructs a customary 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 (militaryTime.length()!=5)

{


System.out.println(militaryTime + is not a valid miliary time. );

}

else

{


// Check to make sure the colon is in // the correct spot


if (militaryTime.charAt(2)!=:)


{


System.out.println(militaryTime + is not a +





valid miliary time. );


}


// Check to make sure all other characters // are digits


else if (!(militaryTime.charAt(0)>=0 && militaryTime.charAt(0)<=9))


{


System.out.println(militaryTime + is not a valid military time.);


}




else if (!(militaryTime.charAt(1)>=0 && militaryTime.charAt(1)<=9))


{


System.out.println(militaryTime + is not a valid miliary time. );


}


else if (!(militaryTime.charAt(3)>=0 && militaryTime.charAt(3)<=9))


{


System.out.println(militaryTime + is not a valid miliary time. );


}


else if (!(militaryTime.charAt(4)>=0 && militaryTime.charAt(4)<=9))


{


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


this.hours=Integer.valueOf(militaryTime.substring(0, 2));


this.minutes=Integer.valueOf(militaryTime.substring(3));


// 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;

}
}
}

 

"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"

Connect with a professional writer in 5 simple steps

Please provide as many details about your writing struggle as possible

Academic level of your paper

Type of Paper

When is it due?

How many pages is this assigment?

Place Order