Project #46486 - two Java Homework

Homework :1

(Due Tonight in 8Hr)

 

 

One More Time

 

Objective:

 

Write a class converts 24-hour time to 12-hour time

  • First download the Lab17Driver and put it in your project
    • DO NOT ALTER THE DRIVER!
  • Write a class file called TimeFormatException that inherits from Exception
  • Create the following constructors
    • Default – calls the parent’s constructor and says something like “This is an incorrect time”
    • One that has a string parameter that passes that message to the parent’s constructor
  • Write a class file called Clock24 that DOES NOT HAVE a main method
  • Some of the attributes of Clock24 are
    • Hours
    • Minutes
    • isAM – whether or not it is AM or PM
  • Create the following Constructors
    • Default – sets everything to default values
      • Hours = 0 – we assume that the 24th hour is 0
      • Minutes = 0
      • isAM = true
    • One that has the parameters
      • Hours
      • Minutes
  • Accessors and Mutators for each variable
    • MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!
  • Create the following Methods
    • setTime – this method will take in an hours value and a minutes value and if those values are valid then they are converted to 12-hour time (Don’t forget to change from AM to PM and visa versa), and finally are set to the classes instance variables hours and minutes.  If they are not valid such as if they were negative, they throw a TimeFormatException.
    • setTime – this overloaded has a single string parameter formatted as “hours:minutes”, so for instance “17:45”.  This string value is to be broken down into separate hours and minutes and then those values are checked just like the other set time
      • Hint:  For the hours part, use a loop to look at each character in the string adding those characters to another string until it reaches the “:”.  Once it does reach “:” parse it.  Same thing for minutes but after the “:”, and it ends when it reaches the end of the string.
      • In the parsing process you need to handle the general Exception that comes when the user enters in something that is not a number.  Use a try catch for this.
    • printTime – prints out the 12-hour time formatted as hours:minutes time of day, so “1:45 PM”

 

 

 

Example Dialog:

Enter a time in the 24-hour notation or enter "Exit" to quit

2:45

2:45 AM

Enter a time in the 24-hour notation or enter "Exit" to quit

14:45

2:45 PM

Enter a time in the 24-hour notation or enter "Exit" to quit

-1:56

EXCEPTION!: The hour is incorrect

2:45 PM

Enter a time in the 24-hour notation or enter "Exit" to quit

asdf;lasdf

EXCEPTION!: Wrong format

EXCEPTION!: The hour is incorrect

2:45 PM

Enter a time in the 24-hour notation or enter "Exit" to quit

Exit

Goodbye!Finally:

Upload TimeFormatException and Clock24 file to the dropbox

 

Lab17Driver:

import java.util.*;
public class Clock24Driver {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Clock24 clock = new Clock24();
		Scanner keyboard = new Scanner(System.in);
		
		boolean quit = false;
		while(quit == false)
		{
			//Prompt the user
			System.out.println("Enter a time in the 24-hour notation or enter \"Exit\" to quit");
			//Gets the user input
			String input = keyboard.nextLine();
			if(input.equalsIgnoreCase("exit"))
			{
				System.out.println("Goodbye!");
				break;
			}
			try
			{
				clock.setTime(input);
				clock.printTime();
			}
			catch(TimeFormatException e)
			{
				System.out.println(e.getMessage());
			}
		}
	}


}




Homework :2

(Due in 5 Day)

 

 

Date Exceptions

 

Objective: 

Write a class called DateConverter which has a static method that takes in a date as a string (such as 01/21 or 1/21) and then returns a string with the date formatted with the alphanumeric month (such as January 21).  This method throws two different exceptions DayException andMonthException, which you need to write yourself.  The MonthException is thrown if the numeric month is not 1-12, and the DateException is thrown if the day is less than 0 or greater than the max possible day for that month.  In other words:

·     January 31 days

·     February 29 days

·     March 31 days

·     April 30 days

·     May 31 days

·     June 30 days

·     July 31 days

·     August 31 days

·     September 30 days

·     October 31 days

·     November 30 days

·     December 31 days

Finally write a driver DateConverterDriver that prompts the user for a date and then using the DateConverter either converts it or returns the exception message.  Keep running this until they choose to quit.

 

Example Dialog:

 

Welcome to the date converter!

Enter a numeric date formatted as month/day or “Exit” to quit.

1/21

The date is January 21

Enter a numeric date formatted as month/day or “Exit” to quit.

-1/30

Month Exception: Months must between 1 and 12 inclusively.

Enter a numeric date formatted as month/day or “Exit” to quit.

4/31

Day Exception: This day is in the wrong range for the month provided.

Enter a numeric date formatted as month/day or “Exit” to quit.

Quit

Good bye

 

 

Submit the files to the drop box

 



Subject Computer
Due By (Pacific Time) 11/05/2014 11:30 pm
Report DMCA
TutorRating
pallavi

Chat Now!

out of 1971 reviews
More..
amosmm

Chat Now!

out of 766 reviews
More..
PhyzKyd

Chat Now!

out of 1164 reviews
More..
rajdeep77

Chat Now!

out of 721 reviews
More..
sctys

Chat Now!

out of 1600 reviews
More..
sharadgreen

Chat Now!

out of 770 reviews
More..
topnotcher

Chat Now!

out of 766 reviews
More..
XXXIAO

Chat Now!

out of 680 reviews
More..
All Rights Reserved. Copyright by AceMyHW.com - Copyright Policy