Question

in java please Purpose: To practice designing a class that can be used in many applications....

in java please

Purpose: To practice designing a class that can be used in many applications. To write and implement a thorough test plan. You are NOT to use any of the Java API date classes (Gregorian calendar, Date…) except for the constructor method to set the date fields to the current data.

Write a class to hold data and perform operations on dates. Ie: January 2, 2019   or 3/15/2018

You must include a toString( ), an equals( ) and a compareTo( ) method.

Methods required in addition to the several constructor methods (including the copy constructor),the
toString( ), equals( ), compareTo( ), set( ) methods:

public Date add(int numDays)
public Date subtract(int numDays)

public int daysBetween(Date anotherDate)

public String getDate(char format)//allow representations in various fomats ie: January 5, 2019 or 1/5/2019

You will receive ten points extra credit if you consider leap years in your calculations.

Once you unit test each method using the BlueJ debugging tools, write a driver class with main( ) that tests each of the methods thoroughly. This is part of the thought processes/skills of programming that is essential. Test all possible scenarios and print statements to the screen to show what you are testing and the results of the test.

You will not get credit for any method that is not tested and test results printed from main( ).

Come up with your own ideas of what a user would want to do with a Date object. Test and document each.

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

ANSWER:

public class Date
{
private int day;
private int month;
private int year;

public Date(int day, int month, int year)
{
this.day = day;
this.month = month;
this.year = year;
}

public string toString()
{
return (this.day + "-" + this.month + "-" + this.year);
}

public bool equals(Date date)
{
return (this.day == date.day &&
this.month == date.month &&
this.year == date.year);
}

public bool equals(Date date, Date date1)
{
return (date.day == date1.day &&
date.month == date1.month &&
date.year == date1.year);
}

public Date add(int numDays)
{
var tempDays = this.GetDaysInCurrentMonth();
while (numDays > tempDays)
{
this.month = this.GetNextMonth();
if (this.month == 1)//Again got Jan, year shouls be incremeneted
{
this.year = this.year + 1;
}

tempDays = this.GetDaysInCurrentMonth();
numDays = numDays - tempDays;
}

this.day = numDays;
  
return this;
}

public int daysBetween(Date anotherDate)
{
if(this.year > anotherDate.year)
{
return (this.year - anotherDate.year) * 365;//in year 365 days are there
}
  
else if(this.day > anotherDate.day)
{
return this.day - anotherDate.day;
}

return this.day - anotherDate.day;
}

public string getDate(char format)
{
if(format == 's')
{
return this.day + "//" + this.month + "//" + this.year;
}
else if(format == 'l')
{
return this.day + "of " + this.month + ", " + this.year;
}

return this.day + "//" + this.month + "//" + this.year;
}

public Date subtract(int numDays)
{
var tempDays = this.GetDaysInCurrentMonth();
while (numDays > tempDays)
{
this.month = this.GetPrevMonth();
if (this.month == 12)//Again got Jan, year shouls be incremeneted
{
this.year = this.year - 1;
}

tempDays = this.GetDaysInCurrentMonth();
numDays = numDays - tempDays;
}

this.day = this.GetDaysInCurrentMonth() - numDays;

return this;
}

private int GetDaysInCurrentMonth()
{
int noOfDaysInMonth = -1;
switch(this.month)
{
case 1://Jan
noOfDaysInMonth = 31;
break;

case 2://Feb
if(this.year % 400 == 0)//If leapyear
{
noOfDaysInMonth = 29;
}
else
{
noOfDaysInMonth = 28;
}
break;

case 3://March
noOfDaysInMonth = 31;
break;

case 4://April
noOfDaysInMonth = 30;
break;

case 5://May
noOfDaysInMonth = 30;
break;

case 6://Jun
noOfDaysInMonth = 30;
break;

case 7://Jul
noOfDaysInMonth = 31;
break;

case 8://Aug
noOfDaysInMonth = 31;
break;

case 9://Sept
noOfDaysInMonth = 30;
break;

case 10://Oct
noOfDaysInMonth = 31;
break;

case 11://Nov
noOfDaysInMonth = 30;
break;

case 12://Dec
noOfDaysInMonth = 31;
break;
}

return noOfDaysInMonth;
}

private int GetNextMonth()
{
int nextMonth = -1;
switch(this.month)
{
case 1:
nextMonth = 2;
break;

case 2:
nextMonth = 3;
break;

case 3:
nextMonth = 4;
break;

case 4:
nextMonth = 5;
break;

case 5:
nextMonth = 6;
break;

case 6:
nextMonth = 7;
break;

case 7:
nextMonth = 8;
break;

case 8:
nextMonth = 9;
break;

case 9:
nextMonth = 10;
break;

case 11:
nextMonth = 12;
break;

case 12:
nextMonth = 1;
break;

}

return nextMonth;
}

private int GetPrevMonth()
{
int nextMonth = -1;
switch (this.month)
{
case 1:
nextMonth = 12;
break;

case 2:
nextMonth = 1;
break;

case 3:
nextMonth = 2;
break;

case 4:
nextMonth = 3;
break;

case 5:
nextMonth = 4;
break;

case 6:
nextMonth = 5;
break;

case 7:
nextMonth = 6;
break;

case 8:
nextMonth = 7;
break;

case 9:
nextMonth = 8;
break;

case 11:
nextMonth = 10;
break;

case 12:
nextMonth = 11;
break;

}

return nextMonth;
}
}

Add a comment
Know the answer?
Add Answer to:
in java please Purpose: To practice designing a class that can be used in many applications....
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 programing Question 1 (Date Class):                                   &nbsp

    JAVA programing Question 1 (Date Class):                                                                                                     5 Points Create a class Date with the day, month and year fields (attributes). Provide constructors that: A constructor that takes three input arguments to initialize the day, month and year fields. Note 1: Make sure that the initialization values for the day, month and year fields are valid where the day must be between 1 and 31 inclusive, the month between 1 and 12 inclusive and the year a positive number. Note 2:...

  • Description: This project focuses on creating a Java class, Date.java, along with a client class, DateCleint.java,...

    Description: This project focuses on creating a Java class, Date.java, along with a client class, DateCleint.java, which will contain code that utilizes your Date.java class. You will submit both files (each with appropriate commenting). A very similar project is described in the Programming Projects section at the end of chapter 8, which you may find helpful as reference. Use (your own) Java class file Date.java, and a companion DateClient.java file to perform the following:  Ask user to enter Today’s...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

  • Please help me with this Java problem. Would greatly appreciate comments in the code as well:...

    Please help me with this Java problem. Would greatly appreciate comments in the code as well: Define a class called Counter whose internal "count" variable is a basic integer counter. This class track that count from its instantiation in the constructor (can be set to any positive integer, or 0). The count should never be allowed to be negative. Include methods that will set the counter to 0, increase the count by 1, and decrease the count by 1. Include...

  • In Java You are to write a program that determines the day of the week for...

    In Java You are to write a program that determines the day of the week for New Year's Day in the year 3000. To do this, you must create your own date class (MyDate) and use the following interface and main program: /** Interface for Date objects to be used by the Year3000 driver program. @author Jon Sorenson */ public interface DateInterface { /** @return the day of the month (1-31) */ public int getDay(); /** @return the day of...

  • DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to...

    DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to solve the problem using short methods that work together to solve the operations of add, subtract multiply and divide.   A constructor can call a method called setSignAndRemoveItIfItIsThere(). It receives the string that was sent to the constructor and sets a boolean variable positive to true or false and then returns a string without the sign that can then be processed by the constructor to...

  • Java project Project Complex number class. Tuesday April 17 Write a Complex number class. It will...

    Java project Project Complex number class. Tuesday April 17 Write a Complex number class. It will have a default constructor, explicit constructor, and the following methods: read O public Complex add(Complex), public Complex subtract(Complex), public Complex multiply(Complex), public Complex divide(Complex), public boolean equals(complex) and a toString) method. Include get and set methods as well. On my website is an explanation of complex numbers and examples with expected answers along with a main demo program. Study the explanation and use your...

  • IN JAVA Write a class Store which includes the attributes: store name, city. Write another class...

    IN JAVA Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method...

  • cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher,...

    cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher, price, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the book. Write another class called Bookshelf, which has name and array of Book objects. Bookself capacity is maximum of five books. Includes method for Bookself that adds, removes,...

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