Question

Java Programming: Hi, I need help Modifying my code that will throw an IllegalArgumentException. for the...

Java Programming:

Hi, I need help Modifying my code that will throw an IllegalArgumentException.

for the following data entry error conditions:

  • A number less than 1 or greater than 12 has been entered for the month
  • A negative integer has been entered for the year

utilizes a try and catch clause to display an appropriate message when either of these data entry errors exceptions occur. Thank you let me know if you need more info

import java.util.*;

//Class definition
public class MonthDays
{
private int month;
private int year;
  
//Constructor
public MonthDays(int month, int year)
{
this.month = month;
this.year = year;
}
  
//Method that returns number of days in a month
public int getNumberOfDays()
{
//Variable to hold status of leap year
boolean isLeapYear;
  
//Checking for leap year
//Checking Divisible by 100
if (year % 100 == 0)
{
//Checking Divisible by 400
if(year % 400 == 0)
{
isLeapYear = true;
}
else
{
isLeapYear = false;
}
}
else
{
//Checking Divisible by 4
if(year % 4 == 0)
{
isLeapYear = true;
}
else
{
isLeapYear = false;
}
}
  
//Fetching number of months
switch(month)
{
//For month 2
case 2:
//If leap year, days are 29
if(isLeapYear)
return 29;
//If not a leap year, days are 28
else
return 28;
  
//For the following months days are 31
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: return 31;
  
//For the following months days are 30
case 4:
case 6:
case 9:
case 11: return 30;
}
  
return -1;
}
}

import java.util.*;

//Driver class
public class MonthDaysDriver
{
//Main method
public static void main(String args[])
{
int month, year;
  
//Scanner class object
Scanner sc = new Scanner(System.in);
  
//Reading month
System.out.print("\n\n Enter a month (1-12): ");
month = sc.nextInt();
  
//Reading year
System.out.print("\n\n Enter a year: ");
year = sc.nextInt();
  
//Creating object
MonthDays obj = new MonthDays(month, year);
  
//Printing number of days
System.out.println("\n " + obj.getNumberOfDays() + " days \n");
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

//Class definition

public class MonthDays {

private int month;

private int year;

// Constructor

public MonthDays(int month, int year) {

if (month < 0 || month > 12)

throw new IllegalArgumentException("Month should be in the range from (1-12)");

if (year < 0)

throw new IllegalArgumentException("Number cannot be negative");

this.month = month;

this.year = year;

}

// Method that returns number of days in a month

public int getNumberOfDays() {

// Variable to hold status of leap year

boolean isLeapYear;

// Checking for leap year

// Checking Divisible by 100

if (year % 100 == 0) {

// Checking Divisible by 400

if (year % 400 == 0) {

isLeapYear = true;

} else {

isLeapYear = false;

}

} else {

// Checking Divisible by 4

if (year % 4 == 0) {

isLeapYear = true;

} else {

isLeapYear = false;

}

}

// Fetching number of months

switch (month) {

// For month 2

case 2:

// If leap year, days are 29

if (isLeapYear)

return 29;

// If not a leap year, days are 28

else

return 28;

// For the following months days are 31

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

return 31;

// For the following months days are 30

case 4:

case 6:

case 9:

case 11:

return 30;

}

return -1;

}

}

==========================================================


import java.util.*;

// Driver class

public class MonthDaysDriver {

// Main method

public static void main(String args[]) {

try {

int month, year;

// Scanner class object

Scanner sc = new Scanner(System.in);

// Reading month

System.out.print("\n\n Enter a month (1-12): ");

month = sc.nextInt();

// Reading year

System.out.print("\n\n Enter a year: ");

year = sc.nextInt();

// Creating object

MonthDays obj = new MonthDays(month, year);

// Printing number of days

System.out.println("\n " + obj.getNumberOfDays() + " days \n");

} catch (IllegalArgumentException e) {

System.err.println(e.getMessage());

}

}

}

====================================================================================

SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
Java Programming: Hi, I need help Modifying my code that will throw an IllegalArgumentException. for the...
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
  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the fo...

    I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the form (month day year): 2 28 2018  Ask user to enter Birthday in the form (month day year): 11 30 1990  Output the following: The date they were born in the form (year/month/day). The day of the week they were born (Sunday – Saturday). The number of days between their birthday and today’s...

  • Take your MonthDay class from Assignment #6 and modify it to throw an IllegalArgumentException when the user of the class attempts to pass an invalid value for month and year. Make sure you have your...

    Take your MonthDay class from Assignment #6 and modify it to throw an IllegalArgumentException when the user of the class attempts to pass an invalid value for month and year. Make sure you have your main method(function) in a separate source file demonstrating use of the try/catch block to catch the IllegalArgumentException that the MonthDay class may throw in the event of an invalid month, or year. class MonthDays {    private int month;    private int year;    public MonthDays(int aMonth, int...

  • this needs to be in Java: use a comparator class which is passed into the sort...

    this needs to be in Java: use a comparator class which is passed into the sort method that is available on the ArrayList. import java.util.Scanner; public class Assign1{ public static void main(String[] args){ Scanner reader = new Scanner (System.in); MyDate todayDate = new MyDate(); int choice = 0; Library library = new Library(); while (choice != 6){ displayMainMenu(); if (reader.hasNextInt()){ choice = reader.nextInt(); switch(choice){ case 1: library.inputResource(reader, todayDate); break; case 2: System.out.println(library.resourcesOverDue(todayDate)); break; case 3: System.out.println(library.toString()); break; case 4: library.deleteResource(reader,...

  • My values are not storing into my object and I keep returning zero. please explain my...

    My values are not storing into my object and I keep returning zero. please explain my mistake thanks public class CalanderDate { int year = 0 ; int day = 0; int month= 0;    public static void main(String[] args) { CalanderDate date; date = new CalanderDate( 1, 10 ,1999); System.out.print(date); }          public CalanderDate(int month, int day , int year ) { if (month < 1){ month = 1; } if (month> 12){ month = 12; }...

  • Provide comments for this code explaining what each line of code does import java.util.*; public class...

    Provide comments for this code explaining what each line of code does import java.util.*; public class Chpt8_Project {    public static void main(String[] args)       {        Scanner sc=new Scanner(System.in);        int [][]courses=new int [10][2];        for(int i=0;i<10;i++)        {            while(true)            {                System.out.print("Enter classes and graduation year for student "+(i+1)+":");                courses[i][0]=sc.nextInt();                courses[i][1]=sc.nextInt();                if(courses[i][0]>=1...

  • This java code won't run and I can't figure out the problem with it. Please help...

    This java code won't run and I can't figure out the problem with it. Please help import java.io.*; import java.util.*; import java.math.*; import java.util.Scanner; public class Fibonacci {    // Returns n-th Fibonacci number    static BigInteger fib(int n)    {        BigInteger[] Fibo = new BigInteger[n+2]; Fibo[0] = BigInteger.ZERO; Fibo[1] = BigInteger.ONE;           for (int j=2 ; j<=n ; j++)        {            Fibo[j] = Fibo[j-1].add(Fibo[j-2]);        }    return (Fibo[n]); }...

  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • Create a Java application named Problem14 using the two files Dr. Hanna provided to you—Date.java andProblem14.java—then change his code to add two non-trivial public, non-static methods to the Date class.

    Create a Java application named Problem14 using the two files Dr. Hanna provided to you—Date.java andProblem14.java—then change his code to add two non-trivial public, non-static methods to the Date class.1. Increment a Date to the next day (for example, 1-31-2011 increments to 2-1-2011; 12-31-2010 increments to 1-1-2011).2. Decrement a Date to the previous day (for example, 2-1-1953 decrements to 1-31-1953; 1-1-1954 decrements to 12-31-1953).. Extra Credit (up to 5 points) Re-write the Date member functions Input() and Output() to usedialog boxes to...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

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
ADVERTISEMENT