Question

In: Computer Science

Design and implement the class Day that implements the day of the week in a program....

Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type Day:

  1. Set the day.
  2. Print the day.
  3. Return the day.
  4. Return the next day.
  5. Return the previous day.
  6. Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add 4 days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.
  7. Add the appropriate constructors.
  8. Write the definitions of the methods to implement the operations for the class Day, as defined in A through G.
  9. Write a program to test various operations on the class Day.

Solutions

Expert Solution

Thanks for the question, here is the class Day and DayTest. Have implemented both the classes in Java as the programming language was not explicitly mentioned.

The DayTest contains the test of various operations.

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

public class Day {


    private String day;

// below are the only valid names for a day
    private static final String SHORT_NAMES[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};


    public Day(String day) {
        setDay(day);
    }

    // a.
    private void setDay(String day) {

        boolean validArgument = false;
        for (String name : SHORT_NAMES) {
            if (day.equals(name)) {
                this.day = day;
                validArgument = true;
                break;
            }
        }

// if the day name is not a valid name throw error
        if (!validArgument)
            throw new IllegalArgumentException("Invalid day name entered");
    }

    // b.
    public void printDay() {
        System.out.println(day + "day");
    }

    // c.
    public String getDay() {
        return day;
    }

    // d. Return the next day.

    public String nextDay() {
        int nextDay = 0;
        for (int i = 0; i < SHORT_NAMES.length; i++) {
            if (SHORT_NAMES[i].equals(day)) {
                nextDay = (i + 1) % 7;
                break;
            }
        }
        return SHORT_NAMES[nextDay] + "day";
    }

    // e.
    public String previousDay() {
        int previousDay = 0;
        for (int i = 0; i < SHORT_NAMES.length; i++) {
            if (SHORT_NAMES[i].equals(day)) {
                previousDay = i - 1 == -1 ? 6 : i - 1;
                break;
            }
        }
        return SHORT_NAMES[previousDay] + "day";
    }

    // f.
    public String dayAfterNDays(int n) {

        int index = 0;
        for (int i = 0; i < SHORT_NAMES.length; i++) {
            if (SHORT_NAMES[i].equals(day)) {
                index = i;
                break;
            }
        }
        int indexOfNthDay = (index + n) % 7;
        if (indexOfNthDay < 0) indexOfNthDay = 7 + indexOfNthDay;
        return SHORT_NAMES[indexOfNthDay] + "day";

    }

}

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


public class DayTest {


    public static void main(String[] args) {

        Day today = new Day("Mon");
        System.out.print("Today is : ");today.printDay();
        System.out.println("Next day: " + today.nextDay());
        System.out.println("Next day: " + today.previousDay());
        System.out.println("After 4 days : " + today.dayAfterNDays(4));
        System.out.println("After 13 days : " + today.dayAfterNDays(13));
        System.out.println("Previous -14 days : " + today.dayAfterNDays(-14));


    }
}

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

thanks a lot. Please do up vote please : )


Related Solutions

In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement the compareTo method to compare the triangles on the basis of similarity. Draw the UML diagram for your classes. Write a Driver class (class which instantiates the comparableTriangle objects) to determine if two instances of ComparableTriangle objects are similar (sample output below). It should prompt the user to enter the 3 sides of each triangle and then display whether or not the are similar...
Language: Java Design and implement a program that implements an Interpolation Search method. Interpolation search is...
Language: Java Design and implement a program that implements an Interpolation Search method. Interpolation search is similar to binary search, except it tries to begin the search nearer to the location of the item. Instead of the using the middle value of the sorted array, interpolation search estimates the location of the target with respect to the first & last values in the array. The implementation is the same as binary search except that you should calculate the mid value...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be thrown when a string is discovered that has too many characters in it. Create a driver that reads in strings from the user until the user enters DONE. If a string that has more then 5 characters is entered, throw the exception. Allow the thrown exception to terminate the program 2) Create a class called TestScore that has a constructor that accepts an array...
Write a program that will recognize and evaluate prefix expressions. First design and implement a class...
Write a program that will recognize and evaluate prefix expressions. First design and implement a class of prefix expressions. This class should contain methods to recognize and evaluate prefix expressions. This chapter discusses the algorithms you will need to implement these methods. Walls and Mirrors 3rd edition Chapter 6 Programming Practice 8. Programming Language: Java.
JAVA PROGRAM Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class...
JAVA PROGRAM Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class converts an infix expression to a postfix expression. The PostFixCalculator class evaluates a postfix expression. This means that the expressions will have already been converted into correct postfix form. Write a main method that prompts the user to enter an expression in the infix form, converts it into postfix, displays the postfix expression as well as it's evaluation. For simplicity, use only these operators,...
Program Specifications: Write a program that defines a class HumanBMI, implements it as required, and tests...
Program Specifications: Write a program that defines a class HumanBMI, implements it as required, and tests the class implementation. The class definition and implementation should be separated into HumanBMI.h and HumanBMI.cpp files. A. The class HumanBMI consists of three private member variables: name of type string, height of type int in inches, and weight of type int in pounds. The class HumanBMI also includes the following public member functions: 1) setName to set the name member variable with a string...
In Java class PassengerComparator implements Comparator for a Passenger Implement a comparator where a FirstClassPassenger precedes...
In Java class PassengerComparator implements Comparator for a Passenger Implement a comparator where a FirstClassPassenger precedes a CoachPassenger,. If both Passengers are of the same TicketClass then the Passenger with the lower TicketNumber precedes the Passenger with the higher TicketNumber.
Create a class called MovieReducerExtremes that implements MediaReducer. Implement a reducer that takes a movie list...
Create a class called MovieReducerExtremes that implements MediaReducer. Implement a reducer that takes a movie list and an option ("newest" or "oldest"), then return the newest or oldest movie as appropriate.Submit both the MovieReducerExtremes and the Movie class from the first question. /////Required Output://///// Newest\n 2014 AKA Jessica Jones Action \n Oldest\n 1936 Cabaret Music \n Given Files: Movie.java public class Movie extends Media { public Movie(String name, int year, String genre) { super(name, year, genre); } public String getEra()...
: Design and implement class Radio to represent a radio object. The class defines the following...
: Design and implement class Radio to represent a radio object. The class defines the following attributes (variables) and methods: Assume that the station and volume settings range from 1 to 10. A private variable of type int named station to represent a station number. Set to A private variable of type int named volume to represent the volume setting. Set to 1. A private variable of type boolean named on to represent the radio on or off. Set to...
CODE must using C++ language. Write the definition of the class dayTye that implements the day...
CODE must using C++ language. Write the definition of the class dayTye that implements the day of the week in a program. The class dayType should store the day of the week as integer. The program should perform the following operations on an object of type dayType 1. set the day 2. display the day as a string - Sunday, ... Saturday 3. return the day as an integer 4. return the next as an integer 5. return the previous...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT