Question

/************************************************************************************ * Program: PRG/420 Week 5 * Purpose: Week 5 Coding Assignment * Programmer: TYPE YOUR...

/************************************************************************************
* Program:  PRG/420 Week 5 
* Purpose:     Week 5 Coding Assignment
* Programmer:  TYPE YOUR NAME HERE        
* Class:       PRG/420       
* Creation Date:  TYPE TODAY'S DATE HERE
*************************************************************************************
* Program Summary:   
*       This program converts a given date to a string.    
*       The code includes exception handling for a ParseException. 
************************************************************************************/

package prg420week5_codingassignment;

import java.util.*;     // wildcard to import all the util. classes 
import java.text.*;     // wildcard to import all the text classes   

public class PRG420Week5_CodingAssignment {

    public static void main(String[] args){

    // The getInstance() method returns a Calendar object whose calendar fields have been initialized with the current date and time.
    Calendar calendar = Calendar.getInstance(); {

    LINE 1. BEGIN THE TRY BLOCK.
    
                String str_date="01-Nov-17"; // Declare a string that we will use later to format a date like this: ##-XXX-##
                DateFormat formatter;  // Declare an object of type DateFormat so that we can call its parse() method later
                Date myFormattedDate; // Declare a variable of type Date to hold the formatted date
                
                formatter = new SimpleDateFormat("dd-MMM-yy");    // Assign a specific date format to the formatter variable

                // The given date is taken as a string that is converted into a date type by using 
                // the parse() method 

                 myFormattedDate = (Date)formatter.parse(str_date);             // setting up the format
                
                System.out.println("The formatted date is " + myFormattedDate);
                System.out.println("Today is " +calendar.getTime() );
            


        LINE 2. WRITE THE CATCH BLOCK TO CATCH EXCEPTIONS OF TYPE ParseException (TO HANDLE EXCEPTION, SIMPLY PRINT THE EXCEPTION)


  
    }
  }
}

For this assignment, you will apply what you learned in analyzing a simple Java™ program by writing your own Java™ program. The Java™ program you write should do the following:

  • Organize the code capable of throwing an exception of type ParseException as a tryblock.
  • Include a catch block to handle a ParseException error thown by the try block.
  • Include a hard-coded error that results in a ParseException to prove that the code can catch and handle this type of exception.

Complete this assignment by doing the following:

  1. Download and unzip the linked Week 5 Coding Assignment Zip File.
  2. Add comments to the code by typing your name and the date in the multi-line comment header.
  3. Replace the following lines with Java™ code as directed in the file:
  • LINE 1
  • LINE 2
  1. Replace the value assigned with one of the variables so that the program throws an exception.
  2. Comment each line of code you add to explain what you intend the code to do. Be sure to include a comment for the replacement value you added in Step 4 that causes the program to throw an exception.
  3. Test and modify your Java™ program until it runs without errors and produces the results described above.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main {
        public static void main(String[] args) {

                // The getInstance() method returns a Calendar object whose calendar fields have
                // been initialized with the current date and time.
                Calendar calendar = Calendar.getInstance();

                try {
                        
                        // 01-Jack-17 is not a valid date.. hence an exception will be thrown
                        String str_date = "01-Jack-17"; // Declare a string that we will use later to format a date like this:
                                                                                        // ##-XXX-##
                        
                        // String str_date = "01-Jan-17";  // this is a valid date, so no exception
                        
                        DateFormat formatter; // Declare an object of type DateFormat so that we can call its parse() method
                                                                        // later
                        java.util.Date myFormattedDate; // Declare a variable of type Date to hold the formatted date

                        formatter = new SimpleDateFormat("dd-MMM-yy"); // Assign a specific date format to the formatter variable

                        // The given date is taken as a string that is converted into a date type by
                        // using
                        // the parse() method

                        myFormattedDate = (java.util.Date) formatter.parse(str_date); // setting up the format

                        System.out.println("The formatted date is " + myFormattedDate);
                        System.out.println("Today is " + calendar.getTime());

                } catch (ParseException ex) {
                        // catch and print the exception
                        System.out.println(ex);
                }
        }
}



please upvote. Thanks!











Add a comment
Know the answer?
Add Answer to:
/************************************************************************************ * Program: PRG/420 Week 5 * Purpose: Week 5 Coding Assignment * Programmer: TYPE YOUR...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this assignment, you will apply what you learned in analyzing for, while, and do-while loops...

    For this assignment, you will apply what you learned in analyzing for, while, and do-while loops by writing these statements yourself. The Java™ program you write should do the following: Display a pyramid of asterisks onscreen (i.e., a nested for loop) Display the integers 10 to 1 in decreasing order, one number per line (i.e., a while/do-whlie loop) Add 7 until the sum becomes greater than 157, at which point the program should display both the sum and the number...

  • Resource: "The Locale Object" text file For this assignment, you will develop Java™ code that relies...

    Resource: "The Locale Object" text file For this assignment, you will develop Java™ code that relies on localization to format currencies and dates. In NetBeans, copy the linked code to a file named "Startercode.java". Read through the code carefully and replace all occurrences of "___?___" with Java™ code. Note: Refer to "Working with Dates and Times" in Ch. 5, "Dates, Strings, and Localization," in OCP: Oracle®Certified Professional Java® SE 8 Programmer II Study Guide for help. Run and debug your...

  • PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is...

    PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is the output of the program as it is written? (Program begins on p. 2) 2. Why would a programmer choose to define a method in an abstract class (such as the Animal constructor method or the getName()method in the code example) vs. defining a method as abstract (such as the makeSound()method in the example)? /********************************************************************** *           Program:          PRG/421 Week 1 Analyze Assignment *           Purpose:         Analyze the coding for...

  • What are the results of the program as written? What results would the program produce if...

    What are the results of the program as written? What results would the program produce if you changed the value of a from 0 to 3? ************************************************************************************* * Program Summary:    *   This code includes simple try/catch (exception handling) functionality. * We put some arithmetic code into a "try" block so that we can "catch" an exception * that is thrown during the course working through the arithmetic. * We need to know specifically what kind of exception we intend...

  • Modify your program in Lab Assignment 4A to throw an exception if the file does not...

    Modify your program in Lab Assignment 4A to throw an exception if the file does not exist. An error message should result.   Use the same file name in your program as 4A, however, use the new input file in 4B assignment dropbox. My code: import java.io.*; import java.util.*; public class Lab4B { public static void main(String[] args) throws IOException { final String input_file = "Lab_4A.txt"; final String output_file = "Lab_4A.txt"; Scanner x = null; FileWriter y = null; try {...

  • Java Your boss has just put you in charge of updating the company's client contact list....

    Java Your boss has just put you in charge of updating the company's client contact list. This file contains records in the following format: id,first_name,last_name,email 1,Norry,Killby,nkillby0@photobucket.com Your job is to reformat each record as follows: last_name, first_name, email Killby, Norry, <nkillby0@photobucket.com> Starter Code: package contactlistupdater; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Paths; import java.util.Formatter; import java.util.Scanner; import sun.reflect.generics.reflectiveObjects.NotImplementedException; public class ContactListUpdater { private static final String INPUT_FILENAME = "contacts.txt"; private static final String OUTPUT_FILENAME = "updated-contacts.txt"; /** * Transforms a String...

  • 3. Handling exceptions with a finally clause a. Download the following file from the class website:...

    3. Handling exceptions with a finally clause a. Download the following file from the class website: TestFinally.java b. Examine the code to determine what it does. c. Compile the code and notice the error. d. Modify TestFinally.java to handle the exception following these instructions (and those embedded in the code): i. Embedded in the code is a note showing the location for the try statement. ii. The first line of the catch block should look like this: catch(IOException e) {...

  • import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {              ...

    import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {               int total = 0;               int num = 0;               File myFile = null;               Scanner inputFile = null;               myFile = new File("inFile.txt");               inputFile = new Scanner(myFile);               while (inputFile.hasNext()) {                      num = inputFile.nextInt();                      total += num;               }               System.out.println("The total value is " + total);        } } /* In the first program, the Scanner may throw an...

  • I have my assignment printing, but I can find why the flowers is printing null and...

    I have my assignment printing, but I can find why the flowers is printing null and not the type of flower. Instructions Make sure the source code file named Flowers.java is open. Declare the variables you will need. Write the Java statements that will open the input file, flowers.dat, for reading. Write a while loop to read the input until EOF is reached. In the body of the loop, print the name of each flower and where it can be...

  • Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the...

    Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the following program? public class Quiz2B { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } } } Question 1 options: The program displays Welcome to Java two times. The program displays Welcome to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT