Question

Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then...

Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then generate their documentation. Examine the documentation to see the logic used in creating each class.

(Code given below)

import java.util.ArrayList;
import java.util.Iterator;

class Calendar {
  private Year year;

  public Calendar() {
    year = new Year();
  }
  public void printCalendar() {
    year.printCalendar();
  }
}

class Year {
  private ArrayList<Month> months;
  private int number;

  public Year() {
    this(2013);
  }
  public Year(int number) {
    this.number = number;
    initializeMonths();
  }
  public Year(ArrayList<Month> months, int number) {
    this.months = months;
    this.number = number;
  }
  public void initializeMonths() {
    months = new ArrayList<Month>(12);

    months.add(new Month("January",   number));
    months.add(new Month("February",  number));
    months.add(new Month("March",     number));
    months.add(new Month("April",     number));
    months.add(new Month("May",       number));
    months.add(new Month("June",      number));
    months.add(new Month("July",      number));
    months.add(new Month("August",    number));
    months.add(new Month("September", number));
    months.add(new Month("October",   number));
    months.add(new Month("November",  number));
    months.add(new Month("December",  number));
  }
  public static boolean isLeapYear(int year) {
    if ( ((year % 4 == 0)  && (year % 100 != 0)) ||
         (year % 400 == 0) ) {
      return true;
    }
    return false;
  }
  public void printCalendar() {
    Month month;
    Iterator<Month> it = months.iterator();

    while (it.hasNext()) {
      month = it.next();
      month.printCalendar();
    }
  }
}

class Month {
  private ArrayList<Day> days;
  private String name;
  private int year;

  public Month() {
    days = new ArrayList<Day>();
    name = new String();
    year = 0;
  }
  public Month(String name, int year) {
    this.name = name;
    this.year = year;

    initializeDays();
  }
  public Month(ArrayList<Day> days, String name, int year) {
    this.days = days;
    this.name = name;
    this.year = year;
  }
  public int convertNameToNumber() {
    int nameToNumber = 1;

    if (name.equals("February")) {
      nameToNumber = 2;
    }
    else if (name.equals("March")) {
      nameToNumber = 3;
    }
    else if (name.equals("April")) {
      nameToNumber = 4;
    }
    else if (name.equals("May")) {
      nameToNumber = 5;
    }
    else if (name.equals("June")) {
      nameToNumber = 6;
    }
    else if (name.equals("July")) {
      nameToNumber = 7;
    }
    else if (name.equals("August")) {
      nameToNumber = 8;
    }
    else if (name.equals("September")) {
      nameToNumber = 9;
    }
    else if (name.equals("October")) {
      nameToNumber = 10;
    }
    else if (name.equals("November")) {
      nameToNumber = 11;
    }
    else if (name.equals("December")) {
      nameToNumber = 12;
    }

    return nameToNumber;
  }
  public int determineNumberOfDays() {
    int total;

    if ( name.equals("January") || name.equals("March")   ||
         name.equals("May")     || name.equals("July")    ||
         name.equals("August")  || name.equals("October") ||
         name.equals("December") ) {
      total = 31;
    }
    else if ( name.equals("April")     || name.equals("June") ||
              name.equals("September") || name.equals("November") ) {
      total = 30;
    }
    else {
      if (Year.isLeapYear(year)) {
        total = 29;
      }
      else {
        total = 28;
      }
    }
    return total;
  }
  public int determineNumberOfDays(int monthNumber) {
    if (monthNumber == 2) {
      if (Year.isLeapYear(year)) {
        return 29;
      }
      else {
        return 28;
      }
    }
    else if ( monthNumber == 4 || monthNumber == 6 || 
              monthNumber == 9 || monthNumber == 11 ) {
      return 30;
    }
    else {
      return 31;
    }
  }
  public int determineStartDayOffset() { 
 
    //get total number of days since 1/1/1800
    int i, monthNumber, startDayOffset, startDayOffset1800 = 3;
    long totalNumberOfDays = startDayOffset1800;

    for (i = 1800; i < year; i++) {
      if (Year.isLeapYear(i)) {
        totalNumberOfDays = totalNumberOfDays + 366;
      }
      else {
        totalNumberOfDays = totalNumberOfDays + 365;
      }
    }

    monthNumber = convertNameToNumber();
    for (i = 0; i < monthNumber; i++) {
      totalNumberOfDays = totalNumberOfDays + determineNumberOfDays(i + 1);
    }

    startDayOffset = (int) (totalNumberOfDays % 7);
    return startDayOffset;
  }
  public ArrayList<Day> getDays() {
    return days;
  }
  public String getName() {
    return name;
  }
  public void initializeDays() {
    int i, startDayOffset;
    int numberOfDays = determineNumberOfDays();
    String dayName = new String();

    days = new ArrayList<Day>(numberOfDays);
    startDayOffset = determineStartDayOffset();

    for (i = 0; i < numberOfDays; i++) {
      switch ((i + startDayOffset) % 7) {
        case 0:  dayName = "Sunday";
                 break;
        case 1:  dayName = "Monday";
                 break;
        case 2:  dayName = "Tuesday";
                 break;
        case 3:  dayName = "Wednesday";
                 break;
        case 4:  dayName = "Thursday";
                 break;
        case 5:  dayName = "Friday";
                 break;
        case 6:  dayName = "Saturday";
                 break;
        default: dayName = "Week Day";
      }

      days.add(new Day(dayName, (i + 1)));
    }
  }
  public void printCalendar() {
    int i, numberOfDays, startDayOffset;

    System.out.println();
    printTitle();

    numberOfDays = determineNumberOfDays();
    startDayOffset = determineStartDayOffset();

    for (i = 0; i < startDayOffset; i++) {
      System.out.print("    ");
    } 

    for (i = 0; i < numberOfDays; i++) {
      if ((i > 0) && (i + startDayOffset) % 7 == 0) {
        System.out.println();
      }

      days.get(i).printDay();
    }

    System.out.println();
  }
  public void printInfo() {
    System.out.println(toString());
  }
  public void printTitle() { 
    System.out.println("         " + name + ", " + year);
    System.out.println("-----------------------------");
    System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
  }
  public void setDays(ArrayList<Day> days) {
    this.days = days;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String toString() {
    String data = "";

    for (Day day : days) {
      data = data + day.toString() + "\n";
    }
    return data;
  }
}

class Day {
  private String name;
  private int number;

  public Day() {
    number = 0;
    name = new String();
  }
  public Day(String name, int number) {
    this.name = name;
    this.number = number;
  }
  public String getName() {
    return name;
  }
  public int getNumber() {
    return number;
  }
  public void printDay() {
    if (number < 10) {
      System.out.print(" ");
    }
    System.out.print("  " + number);
  }
  public void printInfo() {
    System.out.println(toString());
  }
  public void setName(String name) {
    this.name = name;
  }
  public void setNumber(int number) {
    this.number = number;
  }
  public String toString() {
    return "Name:   " + name + "\n" +
           "Number: " + number + "\n";
  }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

In class Calendar :

In this class , it defines a private type object of Year class and then in its default constructor

it assign default value of Year object. And after that it calls the printCalender() method of year class

using Year class object in its printCalendar() method.

In Class Year:

* First defines two variables one is ArrayList of Month object type and second for number of month.

* Then three constructors of year type

- one default constructor that sets the default value of year to 2013.

- one parameterized constructor with one parameter of number that initializes the number variable and calls

the initializeMonths() method.

- one with two parameters of Month type or number variable and it initializes the month and number variable.

* Next it declares initializeMonths() method. in this method first it assign month variable with 12 elements of month type

then adds every month name to Month ArrayList with number of month.

* Next method isLeapYear(int year) checks the given year is leap year or not.

* In printCalender() method it calls the printCalender() method of Month class.

In Class Month:

*In this first defines three variables one of ArrayList of Day class type , second is String type

for days name and last is int type for year.

*Then three constructor of Month class

-one is default constructor that initializes all variables .

-one with two arguments name of days and year value.

-one with three arguments Day object , name and year value.

*Next it defines a method convertNameToNumber() that return integer value. In this default value is one

and after it returns number of months according to their name.

*Next it defines a method determineNumberOfDays() that return integer value. In this it returns number of days according

to month name and leap year.

*Next it defines a method determineNumberOfDays() with one argument of month number that return integer value. In this it returns number of days according

to month number and leap year.

*Next it defines a method determineStartDayOffset() that return integer value to get total number of days .

* Getters for Days ArrayList and Name variable.

* Next defines a method initializeDays() that initializes days according to week and days of week.

* Next defines a method printCalendar() that prints the calendar in a specified manner.

* Next defines a method printInfo() that prints the toString method.

* Next defines a methodprintTitle() that prints the title for calendar.

* Next is toString() method that return Days object.

In Class Day:

*In this first defines two variables one of String type name and second number for days.

*Defines two constructors

-one is default constructor that initializes the number and name variable to default value.

-one with two arguments that initializes the number and name variable with arguments.

*Getters and Setters for Name and Number variables.

*Next defines a method printDay() that prints the number of day.

*Next defines a method printInfo() that prints the toString() method.

*Next toString method that returns name and number of day.

  

Add a comment
Know the answer?
Add Answer to:
Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then...
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
  • java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

    java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...

  • In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams...

    In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams and print it to the terminal via the driver class: import java.util.*; public class Racer implements Comparable { private final String name; private final int year; private final int topSpeed;    public Racer(String name, int year, int topSpeed){    this.name = name; this.year = year; this.topSpeed = topSpeed;    }    public String toString(){    return name + "-" + year + ", Top...

  • java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the...

    java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the below classes: public class Date {    private String month;    private String day;    private String year;    public Date(String month, String day, String year) {       this.month = month;       this.day = day;       this.year = year;    }    public String getMonth() {       return month;    }    public String getDay() {       return day;    }    public String...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...

    Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java //package simple; public class PartTest { public static void main(String[] args) { ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>();   // creating Part Object Part part1 = new ExpendablePart("Last, First"); part1.setNumber("AX-34R"); part1.setNcage("MN34R"); part1.setNiin("ABCD-RF-WDE-KLJM"); // printing information System.out.println(part1.toString());       //Create a part2 object of class Part Part part2=new ConsumablePart("Widget, purple"); part2.setNumber("12345"); part2.setNcage("OU812"); part2.setNiin("1234-12-123-1234");    // printing information System.out.println(part2.toString());    //checking equality of two Part class objects if(part1.equals(part2)) System.out.println("part1 and...

  • Below are the Car class and Dealership class that I created In the previous lab import...

    Below are the Car class and Dealership class that I created In the previous lab import java.util.ArrayList; class Car { private String make; private String model; private int year; private double transmission; private int seats; private int maxSpeed; private int wheels; private String type; public Car() { } public Car(String make, String model, int year, double transmission, int seats, int maxSpeed, int wheels, String type) { this.make = make; this.model = model; this.year = year; this.transmission = transmission; this.seats =...

  • JAVA: How do I output all the data included for each employee? I can only get...

    JAVA: How do I output all the data included for each employee? I can only get it to output the name, monthly salary and annual salary, but only from the Employee.java file, not Salesman.java or Executive.java. Employee.java package project1; public class Employee { private String name; private int monthlySalary; public Employee(String name, int monthlySalary) { this.name = name; this.monthlySalary = monthlySalary; } public int getAnnualSalary() { int totalPay = 0; totalPay = 12 * monthlySalary; return totalPay; } public String...

  • Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks...

    Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks for the help. Specifications: The class should have an int field named monthNumber that holds the number of the month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods. A no- arg constructor that sets the monthNumber field to 1. A constructor that accepts the number of the month as an argument. It should...

  • Below, you can find the description of your labwork for today. You can also find the...

    Below, you can find the description of your labwork for today. You can also find the expected output of this code in the Application Walkthrough section. You are going to improve your existing Money & Stock Trading Platform on previous week’s labwork by incorporating Collections. In previous labworks, you have used arrays for holding Customer and Item objects. For this labwork you need to use ArrayList for holding these objects. So, rather than defining Customer[] array, you need to define...

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