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 date (their age in days). Example: You were born on 1990/11/30, which was a Friday. You are 9952 days old.
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 date (their age in days).
Example: You were born on 1990/11/30, which was a Friday. You are 9952 days old.
To do this you will need to design, implement and test a Java class file, Date.java:
In order to this I've created a Java class but there's an error with the last three methods in the class:
public class Date {
private int year, month, day;
public Date(int yr, int mnth, int dy) {
this.year = yr;
this.month = mnth;
this.day = dy;
}
public Date() {
this.year = 1753;
this.month = 1;
this.day = 1;
}
public int getYear() {
return this.year;
}
public int getMonth() {
return this.month;
}
public int getDay() {
return this.day;
}
public String toString() {
return "" + this.year + "/" +
this.month + "/" + this.day;
}
public boolean equals(Date otherDate) {
if ((otherDate.getDay() ==
this.day) && (otherDate.getMonth() == this.month)
&& (otherDate.getYear() == this.year))
{
return
true;
} else {
return
false;
}
}
public boolean isLeapYear(int year) {
return ((year % 4) == 0) &&
!((year % 100 == 0) || (year % 400 != 0));
}
public void nextDay() {
//mutator method that advances this Date object’s fields to represent the next date
//nextDay() advances this Date object’s day, month and/or year, as needed
//Months that have 31 days
if(this.month==10 || this.month==8 || this.month==7 || this.month==5
|| this.month==3 || this.month==1) {
if(this.day==31) {
this.month++;
this.day=1;
} else {
this.day++;
}
}
//Months that have 30 days
if(this.month==11 || this.month==9 || this.month==6 || this.month==4) {
if(this.day==30) {
this.month++;
this.day=1;
}else {
this.day++;
}
}
//Only for month of December
if(this.month==12) {
if(this.day==31) {
this.year++;
this.month=1;
this.day=1;
} else {
this.day++;
}
}
//Only for month of Feb.
if(this.month==2) {
if(isLeapYear(year)==true) {
if(this.day==29) {
this.month++;
this.day=1;
}else {
this.day++;
}
}else if(isLeapYear(year)==false) {
if(this.day==28) {
this.month++;
this.day=1;
}else {
this.day++;
}
}
}
}
public int advanceTo(Date endDate){
//Initializes the increment to 0
int counterDate = 0;
//Increments the counter and uses nextDay() until the
counterdate equals the end date.
while (!this.equals(endDate)) {
nextDay();
counterDate++;
}
//Returns the Counter amount
return counterDate;
}
public String getDayOfWeek(){
//Creates an array with the weekdays in it.
String[] weekDays = { "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
// returns whichever day that the day is advanced to,
modulo 7 because
// that'll give us an appropriate index in the
array
Date refernceDate = new Date(1753,1,1);
return weekDays[refernceDate.advanceTo(this) %
7];
}
}
Can you help me fix these last three methods so that I get the correct input?
Please find correct days of week function: I have tested it is working.
String getDayOfWeek(){
//Creates an array with the weekdays in it.
String[] weekDays = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
int[] t = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
int y = this.year;
int m = this.month;
y -= (m < 3) ? 1 : 0;
int dayCode = ( y + y/4 - y/100 + y/400 + t[m-1] + this.day) % 7; //0 for Sunday, 1 for Monday, etc.
return weekDays[dayCode];
}
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...
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...
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...
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; }...
I'm having trouble with the daysSince() Method in bold at the end of the program public class Date { static final int [] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; private int day, month, year; public Date() { day = 1; month = 1; year = 2000; } public Date(int d, int m, int y) { day = d; month = m; year = y; } public Date (String s) { setDate(s);...
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...
Need help figuring out what would be the all 12 days output public class Date { private int day; private int month; private int year; private static int tracker; public Date() { this(1, 1, 2000); System.out.println("4"); } public Date(int year) { this(1, 1, year); System.out.println("3"); } public Date(int month, int year) { this(1, month,...
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,...
(IN JAVA ) Given the class Date that can prints the date in numerical form. Some applications might require the date to be printed in another form, such as March 24, 2005. Please do the following: Derive the class ExtDate so that the date can be printed either form. Add a data member to the class ExtDate so that the month can also be stored in string form. Add a method to output the month in the string format followed...
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...
Problem Description: Expand on 2 by implementing Insertion Sort and displaying the dates sorted after adding one to all dates. Run your code using the attached input files outputlong.txtoutputlab3.txt. Please note that your the dates that are sorted will be the ones after 1 day has been added to them. Sample Output: (green is user input) Run #2 using sample file outputlab3.txt Enter name of file to import or the word null to bypass: outputlab3.txt How many assessments in this...