Question

In: Computer Science

Create a simple Java class for a Month object with the following requirements:  This program...

Create a simple Java class for a Month object with the following requirements:


 This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.
 All methods will have comments concerning their purpose, their inputs, and their outputs
 One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February, etc.)
 A constructor that takes no arguments, and sets the monthNumber to 1.
 Add a second constructor that takes in an integer argument to set the initial monthNumber for the new Month object. Use data protection to prevent the user from entering a number less than 1 or greater than 12. When a non-valid input is entered, throw a new IllegalArgumentException.
 A setMonth() method that takes an integer and uses data protection to prevent the user from entering a number less than 1 or greater than 12. Also throw an IllegalArgumentException if an illegal value is entered.
 A getMonth() method that returns the monthNumber as an integer.
 Add a String array property that holds the values of the month names (e.g. monthNames[3] would hold the value “March”). Remember, you can leave the 0th index blank/null
 Add a toString() method to use the monthNumber property to return the name of the month as a String. Use the private global String array with the names of the months in it to return the proper String based on the monthNumber.
 Add an equals() method that takes in a month object and returns a boolean based on the values of each object’s monthNumber
 Add a compareTo() method that takes in a month object and returns a negative number if the called object is smaller than the passed in object, a positive number if the called object is bigger than the passed in object, and zero (0) if the two objects are equivalent.

Create a simple program using Java that demonstrates the month object with the following requirements:


 That creates a month object using the no argument constructor.
 A second month object is created using the constructor that takes in an integer argument.
 Additionally, use either a do or while loop to get the user to enter a number between 1 and 12 using the setMonth() method on the 1st month object. The loop will continue until they enter a valid number.
 The program will display the month number for both of the objects using the getMonth() method.
 Display the month names using toString() for the months created, and see whether they are the same or not.
 Additionally, use the equals() method created above to show whether the two months are equivalent to each other or not.
 Use the compareTo() method created above to show which object is the biggest.
 Use appropriate try and catch statements to properly handle erroneous input by the user.

Solutions

Expert Solution

/* The java program that demonstrates the methods
* of Month class. Then print the results of the program
* on console output.
* */

//MonthTester.java
import java.util.Scanner;
public class MonthTester
{
   public static void main(String[] args)
   {
       Scanner console=new Scanner(System.in);
       //default constructor object
       Month one=new Month();
       //second Month object with month number =1
       Month second=new Month(1);
       //declare month variable
       int month = 0;
       //do-while loop
       do
       {      
           //try-catch block to handle invalid input
           try
           {
               System.out.printf("Enter month number : ");
               month=Integer.parseInt(console.nextLine());
              
               if(month<1 || month>12)
                   System.out.println("Invalid month number");
               else
                   second.setMonth(month);          
           }
           catch (Exception e)
           {
               System.out.println("invalid input");
           }

       }while(month<1 ||month>12);

       //print one and second objects
       System.out.println("Default month object");
       System.out.println(one.toString());
       System.out.println("Second month object");
       System.out.println(second.toString());

       //checking if equal method one two objects
       if(one.equals(second))
           System.out.println(one+" and "+second+" are same objects");
       else
           System.out.println(one+" and "+second+" are not same objects");
      
       //calling compareTo method on two month objects
       if(second.compareTo(one)>0)
           System.out.println(second+" is more than "+one);
       else if(second.compareTo(one)<0)
           System.out.println(second+" is less than "+one);
       else
           System.out.println(second+" and "+one+" are same objects");

   }
}
---------------------------------------------------------------------------------------------------------

/** The month class has an instance variable monthNumber
* The constructor and parameterized constructor is to set
* the default and parameter values to the month class.
* The class has setter and getter method for instance variable,
* monthNumber.
* */

//Month.java
public class Month implements Comparable
{
   //declare an instance variable
   private int monthNumber;
   //declare an array of month names
   private String months[]= {"", "January","February","March","April","May","June","July",
           "August","September","October","November","December"};
  
   //default constructor
   public Month()
   {
       monthNumber=1;
   }
   //parameter constructor
   public Month(int month)
   {
       if(month<1 || month>12)
           monthNumber=1;
       else
           monthNumber=month;
   }
  
   //settter method for month
   public void setMonth(int month)
   {
       if(month<1 || month>12)
           monthNumber=1;
       else
           monthNumber=month;
   }
   //getter method for month
   public int getMonth()
   {
       return monthNumber;
   }
   /*Override the toString method*/
   public String toString()
   {
       return String.format("Month Name: %s", months[monthNumber]);
   }
   /*Override the equals method*/
   public boolean equals(Object obj)
   {
       Month other=(Month)obj;
       return monthNumber==other.getMonth();
   }
   /*Override the compareTo method*/
   public int compareTo(Object obj)
   {
       Month other=(Month)obj;
       return monthNumber-other.getMonth();
   }
}

---------------------------------------------------------------------------------------------------------

Sample Output:

RUn1:

Enter month number : 3
Default month object
Month Name: January
Second month object
Month Name: March
Month Name: January and Month Name: March are not same objects
Month Name: March is more than Month Name: January

Run2:

Enter month number : 1
Default month object
Month Name: January
Second month object
Month Name: January
Month Name: January and Month Name: January are same objects
Month Name: January and Month Name: January are same objects


Related Solutions

in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer...
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer array called nums that has 20 cells b. generate a random number between 5 and 30, and populate the array nums c. print the minimum and maximum number in the array nums d. print sum and average of numbers in the array nums Your output look like this: (Note: numbers shown below will be different in your program due to the random numbers) minimum...
Simple Java class. Create a class Sportscar that inherits from the Car class includes the following:...
Simple Java class. Create a class Sportscar that inherits from the Car class includes the following: a variable roof that holds type of roof (ex: convertible, hard-top,softtop) a variable doors that holds the car's number of doors(ex: 2,4) implement the changespeed method to add 20 to the speed each time its called add exception handling to the changespeed method to keep soeed under 65 implement the sound method to print "brooom" to the screen. create one constructor that accepts the...
Write a program that meets the following requirements: Cat Class Create a class called Cat which...
Write a program that meets the following requirements: Cat Class Create a class called Cat which has only the following instance variables: - name - breed - number of legs - year born Create the no-argument constructor Create the constructor which uses all fields as parameters Write the getter and setter methods for all instance variables Override the toString method using the example shown above There should be NO main method in the Cat class. CatTester Class Create a class...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static void main(String[] args) { String bottle1_label="Milk"; float bottle1_volume=250; float bottle1_capacity=500; bottle1_volume=addVolume(bottle1_label, bottle1_volume,bottle1_capacity,200); System.out.println("bottle label: " + bottle1_label + ", volume: " + bottle1_volume + ", capacity: " +bottle1_capacity); String bottle2_label="Water"; float bottle2_volume=100; float bottle2_capacity=250; bottle2_volume=addVolume(bottle2_label, bottle2_volume,bottle2_capacity,500); System.out.println("bottle label: " + bottle2_label + ", volume: " + bottle2_volume + ", capacity: " +bottle2_capacity); } public static float addVolume(String label, float bottleVolume, float capacity, float addVolume)...
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1....
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs to adjust the...
Write a program in java that does the following: Create a StudentRecord class that keeps the...
Write a program in java that does the following: Create a StudentRecord class that keeps the following information for a student: first name (String), last name (String), and balance (integer). Provide proper constructor, setter and getter methods. Read the student information (one student per line) from the input file “csc272input.txt”. The information in the file is listed below. You can use it to generate the input file yourself, or use the original input file that is available alone with this...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs...
Create a Java program. The class name for the program should be 'EncryptText'. In the main...
Create a Java program. The class name for the program should be 'EncryptText'. In the main method you should perform the following: You should read a string from the keyboard using the Scanner class object. You should then encrypt the text by reading each character from the string and adding 1 to the character resulting in a shift of the letter entered. You should output the string entered and the resulting encrypted string. Pseudo flowchart for additional code to be...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT