Question

In: Computer Science

For this assignment, you will apply what you learned in analyzing a simple Java™ program by...

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.

/************************************************************************************
* 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)


  
}
}
}

Solutions

Expert Solution

I am giving date in wrong format to show the exception in console output.

Please give date in right format to print the date and time .

import java.text.*;
import java.util.*;

public class PRG420Week5_CodingAssignment {
  
   public static void main(String[] args){
       Calendar calender=Calendar.getInstance();
      
       try{
           String str_date="2019-3-Nov"; // date is in wrong format
          
           DateFormat formatter; //Declaring Dateformatter
           Date MyFormattedDate;
           formatter=new SimpleDateFormat("dd-MMM-yy"); //Declaring Dateformat
           MyFormattedDate=(Date)formatter.parse(str_date); //formatting wrong date. this line will throw ParseException
           System.out.println("The formatted Date is "+MyFormattedDate); //printing Date
           System.out.println("Today is "+calender.getTime()); // printing Time
          
       }catch(ParseException e) {
e.printStackTrace();
           }
       }

}



Related Solutions

For this assignment, you will apply what you learned in analyzing a simple Java™ program by...
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: Display a prompt on the console asking the user to type in his or her first name Construct the greeting string "Hello, nameEntered!" Display the constructed greeting on the console Complete this assignment by doing the following: Download and unzip the linked zip file. Add comments to the code by...
For this assignment, you will apply what you learned in analyzing a simple Java™ program by...
For this assignment, you will apply what you learned in analyzing a simple Java™ program by writing your own Java™ program that creates and accesses an array of integers. The Java™ program you write should do the following: Create an array to hold 10 integers Ask the user for an integer. Note: This code has already been written for you. Populate the array. Note: The first element should be the integer input by the user. The second through tenth elements...
For this assignment, you will apply what you learned in analyzing Java™ code so far in...
For this assignment, you will apply what you learned in analyzing Java™ code so far in this course by writing your own Java™ program. The Java™ program you write should do the following: Accept user input that represents the number of sides in a polygon. Note: The code to do this is already written for you. If input value is not between 3 and 5, display an informative error message If input value is between 3 and 5, use a...
For this assignment, you will apply what you learned in analyzing Java™ code so far in...
For this assignment, you will apply what you learned in analyzing Java™ code so far in this course by writing your own Java™ program. The Java™ program you write should do the following: Accept user input that represents the number of sides in a polygon. Note: The code to do this is already written for you. If input value is not between 3 and 5, display an informative error message If input value is between 3 and 5, use a...
For this assignment, you will be analyzing the Java™ code in the linked Week 3 Analyze...
For this assignment, you will be analyzing the Java™ code in the linked Week 3 Analyze Assignment Zip File, and predicting the results. You will also examine both the code and the output for inconsistencies and clarity. This Java™ code includes examples of for, while, and do-while loops. Carefully read through the code line by line, then answer the following questions in a Microsoft® Word document: What is the output of the program as it is written? What improvement(s) could...
For this assignment, you will modify existing code to create a single Java™ program named BicycleDemo.java...
For this assignment, you will modify existing code to create a single Java™ program named BicycleDemo.java that incorporates the following: An abstract Bicycle class that contains private data relevant to all types of bicycles (cadence, speed, and gear) in addition to one new static variable: bicycleCount. The private data must be made visible via public getter and setter methods; the static variable must be set/manipulated in the Bicycle constructor and made visible via a public getter method. Two concrete classes...
Assignment Content Resource: ****************************CODE PASTED BELOW******************************* For this assignment, you will develop Java™ code that relies...
Assignment Content Resource: ****************************CODE PASTED BELOW******************************* 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...
Overview In this assignment you will apply what you have learned about standard deviation and normal...
Overview In this assignment you will apply what you have learned about standard deviation and normal curves to solve a number of problems, including finding the area under selected sections of a normal curve using z-scores, then solve a problem using normal distributions. Part 1: The weights of all one hundred (100) 9th graders at a school are measured, and it is found that the mean of all the measurements is 100 lbs., with a standard deviation of 15 lbs.  ...
In this assignment, you will be practicing the Java OOP skills we've learned this week in...
In this assignment, you will be practicing the Java OOP skills we've learned this week in implementing a customized mutable Array class. Please read the following requirements carefully and then implement your own customized Array class: === 1) Basic (100' pts total) === Your Array class must provide the following features: --1.1) (20' pts) Two constructors. One takes the input of the initialized capacity (int) and create the underlying array with that capacity; the other takes no input and creates...
In this assignment, you will be practicing the Java OOP skills we've learned this week in...
In this assignment, you will be practicing the Java OOP skills we've learned this week in implementing a customized mutable Array class. Please read the following requirements carefully and then implement your own customized Array class: === 1) Basic (100' pts total) === Your Array class must provide the following features: --1.1) (20' pts) Two constructors. One takes the input of the initialized capacity (int) and create the underlying array with that capacity; the other takes no input and creates...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT