Hello I need help with this Java code, and please could you do some changes on t
ID: 3816839 • Letter: H
Question
Hello I need help with this Java code, and please could you do some changes on the code to make the output clear as the home work details for the mune :
( homework )
- Warmup Composition and File Handling
In this assignment, you’ll read in dates from a .txt file, load an array of type Date objects, sort the array and write the Date values out to another text file.
Design a program that will read in dates from a file. The filename will be user-entered. The dates read from the file will be in the following format: month/day/year where month and day are 1 or 2 digits and year is 4 digits (note: the file will also contain the /'s - e.g. 3/24/2017) Proper date format is guaranteed, so no error checking is required.
Store each date in a Date object created from a Date class that you create (no java API classes like Calendar may be used.) Your Date class should contain a Day object, a Month object, and a Year object. This means you will also need to create Day, Month, and Year classes.
The Day class should have the following behaviors:
• set the day (day number)
• get the day number
• return information on the day via toString (your toString should override the one inherited from Object)
• compare days for equality (by overriding the equals method inherited from Object)
• construct a day
The Month and Year classes should have similar methods to the Day class. You are welcome to provide other behaviors for each class, but make sure they make sense in the context of that class.
The Date class should the following behaviors:
• set the date
• get the date
• display information on the Date via toString (override the toString inherited from Object): In the following format: Month as a word, the day and the year. Example: September 27 2013
• If the toString includes the day of the week, the month of the year, the year, and the day number of the year (e.g. Thursday, September 27, 2017, day 270 of the year)
• compare dates for equality (by overriding the equals method inherited from Object)
• sort dates by year, then month, then day (overrides the compareTo method from the Comparable interface)
•An EVC (the parameters to the constructor should be three integers)
• A DVC (defaults to 1 1 1970 as its date)
Provide error checking to ensure any data assigned to any objects of the three classes falls in a reasonable range.
Create a tester file called DateTester that contains the main method as well as other methods to test the program as specified above. Provide other methods to perform specific tasks (display the menu, get input from user, etc.). The program flow should be implemented as follows:
• obtaining the input filename from the user
• read the file (use your FileUtil class to open the file)
• count the dates
• create an array of Date references that matches the count
• fill the array with the Date objects
• sort the array (use your SortSearchUtil class for sorting – type Comparable)
• display a menu that allows the following choices:
Print the array of dates
• To the screen
• To a file if the user wants. You will need to prompt the user for the file name.
2. Allow the user to search for a date – Date will be user entered
3. Allow the user to add a date – will need to make a new array and copy over the dates and then re-sort
4. Allow the user to delete a date (by value) – will need to make a new array and copy over the dates
5. Quit
This menu should be displayed repetitively until the user chooses to quit
To turn in :
Be sure to contains all source files (.java files) necessary to compile and run your program (including SortSearchUtil.java and FileUtil.java.)
Here is the code :
DateTester.java
import java.util.Scanner;
import java.io.*;
public class DateTester {
public static void main (String args[]){
System.out.print("Enter the file name -> ");
String fileName = UserUtil.getUserString();
System.out.println();
Scanner fin = FileUtil.openInputFile(fileName);
int[][] ara = translateFile(fin, fileName);
fin.close();
Date[] fileDates = buildDateArray(ara);
SortSearchUtil.quickSort(fileDates, 0 , fileDates.length - 1);
String[] options = {"Print the array of dates.","Search for a date.","Add a date to the array.","Delete a date from the array", "Quit"};
int option = UserUtil.dynamicMenu(options);
while(true){
switch(option){
case 1: String[] printOptions = {"Print to screen.", "Print to file."};
int printOption = UserUtil.dynamicMenu(printOptions);
switch(printOption){
case 1: printDateArray(fileDates);
break;
case 2: FileUtil.printToFile(fileDates);
}
break;
case 2: printSearchedDate(searchDates(fileDates));
break;
case 3: fileDates = addDate(fileDates);
SortSearchUtil.quickSort(fileDates,0,fileDates.length - 1);
break;
case 4: fileDates = deleteDate(fileDates);
break;
case 5: System.exit(0);
}
option = UserUtil.dynamicMenu(options);
}
}
public static int[][] translateFile (Scanner fin, String fileName){ //takes a file, and the location/name of the file and returns a int array of dates
int count = FileUtil.countFile(FileUtil.openInputFile(fileName));
int countTemp = 0;
int[][] temp = new int[count][3];
while (fin.hasNextLine()){
String tempString = fin.nextLine();
String[] tempSplit = tempString.split("/");
for (int i = 0; i < 3; i++){
temp[countTemp][i] = Integer.parseInt(tempSplit[i]); //array goes {month,day,year}
}
countTemp++;
}
return temp;
}
public static Date[] buildDateArray (int[][] input){//builds the Date[] object from the ints previously translated.
int badDates = 0;
for (int i = 0; i < input.length; i++){
if (Date.valiDate(input[i][0],input[i][1],input[i][2]) != 1){
badDates++;
}
}
Date[] temp = new Date[input.length - badDates];
int counter = 0;
int placement = 0;
while(counter < input.length){
if (Date.valiDate(input[counter][0],input[counter][1],input[counter][2]) == 1){
temp[placement] = new Date(input[counter][0],input[counter][1],input[counter][2]);
placement++;
}
counter++;
}
System.out.println("There were " + badDates + " bad date lines in the file. ");
return temp;
}
public static void printDateArray (Date[] array){
System.out.println("The dates in the array are: ");
for (int i = 0; i < array.length; i++){
System.out.println(array[i].toStringLongForm());
}
System.out.println();
}
public static int searchDates(Date[] input){
int dayVal, monthVal, yearVal;
Date temp;
while(true){
System.out.print("Enter the day -> ");
dayVal = UserUtil.getUserInt();
System.out.print("Enter the month -> ");
monthVal = UserUtil.getUserInt();
System.out.print("Enter the year -> ");
yearVal = UserUtil.getUserInt();
if (Date.valiDate(monthVal, dayVal, yearVal) == 1){
temp = new Date(monthVal, dayVal, yearVal);
break;
}else{
System.out.println("Enter a valid date!");
}
}
return SortSearchUtil.binarySearch(input, temp, 0, input.length);
}
public static void printSearchedDate(int searchedDate){
if (searchedDate < 0){
System.out.println("Date not found!");
}else{
System.out.println("Date is at index: " + searchedDate);
}
}
public static Date[] addDate(Date[] input){
Date[] tempAra = new Date[input.length + 1];
int dayVal, monthVal, yearVal;
Date temp;
while(true){
System.out.print("Enter the day -> ");
dayVal = UserUtil.getUserInt();
System.out.print("Enter the month -> ");
monthVal = UserUtil.getUserInt();
System.out.print("Enter the year -> ");
yearVal = UserUtil.getUserInt();
if (Date.valiDate(monthVal, dayVal, yearVal) == 1){
temp = new Date(monthVal, dayVal, yearVal);
break;
}else{
System.out.println("Enter a valid date!");
}
}
for (int i = 0; i < input.length; i ++){
tempAra[i] = input[i];
}
tempAra[input.length] = temp;
return tempAra;
}
public static Date[] deleteDate(Date[] input){
int temp = searchDates(input);
Date[] tempAra = new Date[input.length - 1];
int count = 0;
int adder = 0;
while (count < (input.length - 1)){
if (( count + adder )!= temp){
tempAra[count]= input[count + adder];
count++;
}else{
adder++;
}
}
return tempAra;
}
}
Date.java
public class Date implements Comparable<Date>{
private Day theDay;
private Month theMonth;
private Year theYear;
public Date(){
this.theYear = new Year(1970);
this.theMonth = new Month(1, this.theYear.getLeapYear());
this.theDay = new Day(1, this.calcDayofWeek(1));
}
public Date(int monthVal, int dayVal , int yearVal){
theYear = new Year(yearVal);
theMonth = new Month(monthVal, this.theYear.getLeapYear());
theDay = new Day(dayVal, this.calcDayofWeek(dayVal));
}
public int getDay(){
return this.theDay.getDay();
}
public int getYear(){
return this.theYear.getYear();
}
public int getMonth(){
return this.theMonth.getMonth();
}
public void setYear(int yearVal){
this.theYear.setYear(yearVal);
}
public void setMonth(int monthVal){
Month temp = new Month(monthVal, this.theYear.getLeapYear());
this.theMonth = temp;
}
public void setDay(int dayVal){
Day temp = new Day(dayVal, this.calcDayofWeek(dayVal));
this.theDay = temp;
}
private int calcNumLeapDays(){
int count = 0;
for (int i = 1970; i < this.theYear.getYear(); i++){
if (Year.calcLeapYear(i)){
count++;
}
}
return count;
}
private int calcNumMonthDays(){ //returns the day
int count = 0;
for (int i = 1; i < this.theMonth.getMonth(); i++){
count += Month.DAY_LIMITS[Boolean.compare(this.theYear.getLeapYear(), false)][i - 1];
}
return count;
}
public int compareTo(Date that){
Year temp = new Year(1970);
int rawDaysThis = (this.theYear.compareTo(temp) * 365) * this.calcNumLeapDays() + this.calcNumMonthDays() + this.theDay.getDay();
int rawDaysThat = (that.theYear.compareTo(temp) * 365) * that.calcNumLeapDays() + that.calcNumMonthDays() + that.theDay.getDay();
return rawDaysThis - rawDaysThat;
}
@Override
public boolean equals(Object obj){
if (obj.getClass().getSimpleName().equals(this.getClass().getSimpleName())){
Date temp = (Date)obj;
if (this.theDay.equals(temp.theDay) && this.theMonth.equals(temp.theMonth) && this.theYear.equals(temp.theYear)){
return true;
}
}
return false;
}
@Override
public String toString(){
return "" + this.theMonth.getMonth() + "/" + this.theDay.getDay() +"/" + this.theYear.getYear();
}
public String toStringLongForm(){
Date temp = new Date (1,1,this.theYear.getYear());
return this.theDay.getDayOfWeek() + " " + this.theMonth.getMonthString() + " "+this.theDay.getDay()+" " + this.theYear.getYear() + ", day " + (this.compareTo(temp) + 1) + " of the year";
}
private int calcDayofWeek(int dayVal){ // formula source: http://mathforum.org/library/drmath/view/55837.html
int monthTemp = this.theMonth.getMonth();
int yearTemp = this.theYear.getYear();
if (monthTemp < 3){
if (monthTemp == 2){
monthTemp = 14;
yearTemp--;
}else{
monthTemp = 13;
yearTemp--;
}
}
int temp = dayVal + (2 * monthTemp) + ((3 * (monthTemp + 1) ) / 5) + yearTemp + (yearTemp/4) - (yearTemp/100) + (yearTemp/400) + 2;
return temp % 7;
}
public static int valiDate(int monthVal, int dayVal, int yearVal){ //returns 0 if date is bad, 1 if good.
int dayLimitTemp = 0;
try{
dayLimitTemp = Month.DAY_LIMITS[Boolean.compare(Year.calcLeapYear(yearVal),false)][monthVal - 1];
}catch (Exception e){
dayLimitTemp = 0;
}
if (dayLimitTemp == 0){
return 0;
}
if ( (yearVal >= 1970) && (yearVal <= 2400) && (monthVal > 0) && (monthVal < 13) && (dayVal > 0) && (dayVal <= dayLimitTemp)){
return 1;
}
else return 0;
}
}
Month.java
public class Month {
public static final String[] MONTHS_OF_YEAR = {"January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
public static final int[][] DAY_LIMITS = {{31, 28, 31, 30, 31,30, 31, 31, 30, 31, 30, 31},{31, 29, 31, 30, 31,30, 31, 31, 30, 31, 30, 31}};
private int monthVal;
private String monthString;
private int dayLimit;
public Month(int month,boolean isLeapYear){
this.monthVal = month;
monthSwitch(isLeapYear);
}
public int getMonth(){
return this.monthVal;
}
public String getMonthString(){
return this.monthString;
}
public int getDayLimit(){
return this.dayLimit;
}
public void setMonth(int month, boolean isLeapYear){
this.monthVal = month;
monthSwitch(isLeapYear);
}
@Override
public boolean equals(Object obj){
if (obj.getClass().getSimpleName().equals(this.getClass().getSimpleName())){
Month temp = (Month)obj;
if (this.monthVal == temp.monthVal){
return true;
}
}
return false;
}
private void monthSwitch(boolean isLeapYear){
this.monthString = Month.MONTHS_OF_YEAR[monthVal - 1];
this.dayLimit = this.DAY_LIMITS[Boolean.compare(isLeapYear, false)][monthVal - 1];
}
@Override
public String toString(){
return this.monthString;
}
}
UserUtil.java
import java.util.Scanner;
public class UserUtil{
public static int getUserPositiveInt(){ //psuedo try catch for getting a positive integer from the user
int temp = 0;
while (temp <= 0){
temp = getUserInt();
if (temp > 0){
return temp;
}
System.out.println("This integer is not positive!");
}
return temp;
}
public static int getUserInt(){ //try catch for getting an integer from the user
Scanner kb = new Scanner(System.in);
int temp = 0;
while (true){
try{
temp = kb.nextInt();
System.out.println();
break;
}
catch (Exception e){
System.out.println("Input an integer! Enter an integer -> ");
kb.nextLine();
temp = 0;
}
}
kb.nextLine();
return temp;
}
public static String getUserString(){
Scanner kb = new Scanner(System.in);
String temp;
while(true){
try{
temp = kb.nextLine();
System.out.println();
break;
}catch(Exception e){
System.out.println("Input a string! Enter the string -> ");
temp = null;
}
}
return temp;
}
public static int getUserIndex(int size){
System.out.print("Please enter the index of the rational you want to use -> ");
int temp = -1;
while (temp < 0 || temp > size - 1){
temp = getUserInt();
if (temp >= 0 && temp < size){
return temp;
}
System.out.print("Array index out of bounds! Pick a smaller index -> ");
}
return temp;
}
public static int dynamicMenu(String[] options){
int choice = 0;
while ( choice <= 0 || choice > options.length){
for (int i = 0; i < options.length; i++){
System.out.println(" "+ (i+1)+". " + options[i]);
}
System.out.print("Choice -> ");
choice = getUserInt();
if ( choice <= 0 || choice > options.length){
System.out.println("Invalid choice");
}
}
return choice;
}
}
SortSearchUtil.java
public class SortSearchUtil {
public static void selectionSort(int[] ara){
int temp = 0;
int indexSmallest = 0;
for (int posFill = 0; posFill < (ara.length - 1); posFill++){
indexSmallest = posFill;
for (int current = posFill + 1; current < ara.length; current++){
if (ara[current] < ara[indexSmallest]){
indexSmallest = current;
}
}
temp = ara[posFill];
ara[posFill] = ara[indexSmallest];
ara[indexSmallest] = temp;
}
}
public static void selectionSort(Comparable[] ara){
Comparable temp = null;
int indexSmallest = 0;
for (int posFill = 0; posFill < (ara.length - 1); posFill++){
indexSmallest = posFill;
for (int current = posFill + 1; current < ara.length; current++){
if (ara[current].compareTo(ara[indexSmallest]) < 0){
indexSmallest = current;
}
}
temp = ara[posFill];
ara[posFill] = ara[indexSmallest];
ara[indexSmallest] = temp;
}
}
public static void selectionSort(double[] ara){
double temp = 0;
int indexSmallest = 0;
for (int posFill = 0; posFill < (ara.length - 1); posFill++){
indexSmallest = posFill;
for (int current = posFill + 1; current < ara.length; current++){
if (ara[current] < ara[indexSmallest]){
indexSmallest = current;
}
}
temp = ara[posFill];
ara[posFill] = ara[indexSmallest];
ara[indexSmallest] = temp;
}
}
public static void selectionSort(String[] ara){
String temp = "";
int indexSmallest = 0;
for (int posFill = 0; posFill < (ara.length - 1); posFill++){
indexSmallest = posFill;
for (int current = posFill + 1; current < ara.length; current++){
if (ara[current].compareTo(ara[indexSmallest]) < 0){
indexSmallest = current;
}
}
temp = ara[posFill];
ara[posFill] = ara[indexSmallest];
ara[indexSmallest] = temp;
}
}
public static int linearSearch( int[] ara, int target){ //searches through a 1d int array and reports the index
for (int i = 0; i < ara.length; i++){ //if a given value is there, otherwise reports -1
if (ara[i] == target){
return i;
}
}
return -1;
}
public static void revArray(int[] input){ //Reverses the order of an array
int temp;
for(int i = 0; i <= input.length/2; i++){
temp = input[i];
input[i] = input[input.length - i - 1];
input[input.length - i - 1] = temp;
}
}
public static void quickSort(int[] input, int start, int end){
int index = partition(input, start, end);
if (start < index - 1){
quickSort(input, start, index - 1);
}
if (index < end){
quickSort(input, index, end);
}
}
private static int partition(int[] input, int start, int end){
int leftIndex = start, rightIndex = end;
int temp = 0;
int pivot = input[(leftIndex+rightIndex) / 2];
while (leftIndex <= rightIndex){
while (input[leftIndex] < pivot){
leftIndex++;
}while (input[rightIndex] > pivot){
rightIndex--;
}
if (leftIndex <= rightIndex){
temp = input[leftIndex];
input[leftIndex] = input[rightIndex];
input[rightIndex] = temp;
leftIndex++;
rightIndex--;
}
}
return leftIndex;
}
public static void quickSort(Comparable[] input, int start, int end){
int index = partition(input, start, end);
if (start < index - 1){
quickSort(input, start, index - 1);
}
if (index < end){
quickSort(input, index, end);
}
}
private static int partition(Comparable[] input, int start, int end){
int leftIndex = start, rightIndex = end;
Comparable temp = null;
Comparable pivot = input[(leftIndex+rightIndex) / 2];
while (leftIndex <= rightIndex){
while (input[leftIndex].compareTo(pivot) < 0){
leftIndex++;
}while (input[rightIndex].compareTo(pivot) > 0){
rightIndex--;
}
if (leftIndex <= rightIndex){
temp = input[leftIndex];
input[leftIndex] = input[rightIndex];
input[rightIndex] = temp;
leftIndex++;
rightIndex--;
}
}
return leftIndex;
}
public static void insertionSort (int[] input){
for (int i = 1; i < input.length; i++){
int temp = input[i];
int posTemp = i;
while (posTemp > 0 && temp < input[posTemp - 1]){
input[posTemp] = input[posTemp - 1];
posTemp--;
}
input[posTemp] = temp;
}
}
public static void insertionSort (String[] input){
for (int i = 1; i < input.length; i++){
String temp = input[i];
int posTemp = i;
while ( (posTemp > 0) && (temp.compareTo(input[posTemp - 1]) < 0) ){
input[posTemp] = input[posTemp - 1];
posTemp--;
}
input[posTemp] = temp;
}
}
public static int binarySearch (int[] inputAra, int inputTarget, int start, int end){
int mid = 0;
while (start <= end){
mid = (start + ((end-start)/2));
if (inputTarget < inputAra[mid]){
end = mid - 1;
}else if (inputTarget > inputAra[mid]){
start = mid + 1;
}else{
return mid;
}
}
return -1;
}
public static int binarySearch (Comparable[] inputAra, Comparable inputTarget, int start, int end){
int mid = 0;
while (start <= end){
mid = (start + ((end-start)/2));
if (inputTarget.compareTo(inputAra[mid]) < 0){
end = mid - 1;
}else if (inputTarget.compareTo(inputAra[mid]) > 0){
start = mid + 1;
}else{
return mid;
}
}
return -1;
}
}
FileUtil.java
import java.util.Scanner;
import java.io.*;
public class FileUtil {
public static Scanner openInputFile (String file){
String fileName = file;
Scanner fileScanner = null;
File fileHandle;
boolean failure = false;
while(true){
try{
if (failure == true){
Scanner kb = new Scanner(System.in);
fileName = kb.nextLine();
System.out.println();
failure = false;
}
fileHandle = new File(fileName);
fileScanner = new Scanner(fileHandle);
break;
}catch (FileNotFoundException e){
System.out.print("file "+fileName+" was not found! Enter a good file name-> ");
failure = true;
}
}
return fileScanner;
}
public static Scanner openInputFile() throws IOException{
Scanner kb = new Scanner(System.in);
String fileName;
System.out.print("Enter file name -> ");
fileName = kb.nextLine();
System.out.println();
return openInputFile(fileName);
}
public static int countFile(Scanner fin){
int count = 0;
while (fin.hasNextLine()){
count++;
fin.nextLine();
}
fin.close();
return count;
}
public static void printToFile(Object[] input){
PrintWriter fout = openOutputFile();
for (int i = 0; i < input.length; i++){
fout.println(input[i].toString());
}
fout.close();
}
public static PrintWriter openOutputFile(String fileName){
File temp;
PrintWriter fout;
temp = new File(fileName);
while (true){
try{
fout = new PrintWriter(temp);
break;
}catch(Exception e){
System.out.println("File Not Found");
System.exit(1);
}
}
return fout;
}
public static PrintWriter openOutputFile(){
Scanner kb = new Scanner(System.in);
String fileName;
System.out.print("Enter file -> ");
while (true){
try{
fileName = kb.nextLine();
System.out.println();
break;
}catch(Exception e){
System.out.println(" Input Error.");
}
}
return openOutputFile(fileName);
}
}
Year.java
public class Year {
private int yearVal;
private boolean isLeapYear;
public Year(int yearVal){
this.yearVal = yearVal;
this.isLeapYear = calcLeapYear();
}
private boolean calcLeapYear(){
if (this.yearVal % 4 != 0){
return false;
}else if (this.yearVal % 100 != 0){
return true;
}else if (this.yearVal % 400 != 0){
return false;
}else{
return true;
}
}
public static boolean calcLeapYear(int yearTemp){
if (yearTemp % 4 != 0){
return false;
}else if (yearTemp % 100 != 0){
return true;
}else if (yearTemp % 400 != 0){
return false;
}else{
return true;
}
}
public int getYear(){
return this.yearVal;
}
public boolean getLeapYear(){
return this.isLeapYear;
}
public void setYear(int yearVal){
this.yearVal = yearVal;
this.isLeapYear = calcLeapYear(yearVal);
}
public int compareTo(Year that){
return this.yearVal - that.yearVal;
}
@Override
public boolean equals(Object obj){
if (obj.getClass().getSimpleName().equals(this.getClass().getSimpleName())){
Year temp = (Year)obj;
if (this.yearVal == temp.yearVal){
return true;
}
}
return false;
}
@Override
public String toString(){
return "" + yearVal;
}
}
Day.java
public class Day {
private static final String[] DAYS_OF_WEEK = { "Saturday","Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private int dayVal;
private int dayOfWeekInt;
private String dayOfWeek;
public Day(int day, int dayOfWeek){
this.dayVal = day;
this.dayOfWeekInt = dayOfWeek;
this.dayOfWeek = DAYS_OF_WEEK[dayOfWeek];
}
public int getDay(){
return this.dayVal;
}
public int getDayOfWeekInt(){
return this.dayOfWeekInt;
}
public String getDayOfWeek(){
return this.dayOfWeek;
}
public void setDay(int day){
dayOfWeekInt = (dayOfWeekInt + (this.dayVal - day) % 7) + 1;
this.setDayOfWeek(this.dayOfWeekInt);
this.dayVal = day;
}
public void setDayOfWeek(int dayOfWeekInt){
this.dayOfWeek = DAYS_OF_WEEK[dayOfWeekInt - 1];
}
@Override
public boolean equals(Object input){
if (input.getClass().getSimpleName().equals(this.getClass().getSimpleName())){
Day that = (Day)input;
if (this.dayOfWeek.equals(that.getDayOfWeek()) && this.dayVal == that.getDay()){
return true;
}
}
return false;
}
}
dateList.txt
5/21/2012
2/21/2012
1/21/2012
6/21/2012
7/21/2012
8/21/2012
2/29/2012
2/30/2012
2/28/2012
Thank you
Explanation / Answer
Made changes in only one file mentioned below.
PROGRAM CODE:
Date.java
package date;
public class Date implements Comparable<Date>{
private Day theDay;
private Month theMonth;
private Year theYear;
public Date(){
this.theYear = new Year(1970);
this.theMonth = new Month(1, this.theYear.getLeapYear());
this.theDay = new Day(1, this.calcDayofWeek(1));
}
public Date(int monthVal, int dayVal , int yearVal){
theYear = new Year(yearVal);
theMonth = new Month(monthVal, this.theYear.getLeapYear());
theDay = new Day(dayVal, this.calcDayofWeek(dayVal));
}
public int getDay(){
return this.theDay.getDay();
}
public int getYear(){
return this.theYear.getYear();
}
public int getMonth(){
return this.theMonth.getMonth();
}
public void setYear(int yearVal){
this.theYear.setYear(yearVal);
}
public void setMonth(int monthVal){
Month temp = new Month(monthVal, this.theYear.getLeapYear());
this.theMonth = temp;
}
public void setDay(int dayVal){
Day temp = new Day(dayVal, this.calcDayofWeek(dayVal));
this.theDay = temp;
}
private int calcNumLeapDays(){
int count = 0;
for (int i = 1970; i < this.theYear.getYear(); i++){
if (Year.calcLeapYear(i)){
count++;
}
}
return count;
}
private int calcNumMonthDays(){ //returns the day
int count = 0;
for (int i = 1; i < this.theMonth.getMonth(); i++){
count += Month.DAY_LIMITS[Boolean.compare(this.theYear.getLeapYear(), false)][i - 1];
}
return count;
}
public int compareTo(Date that){
Year temp = new Year(1970);
int rawDaysThis = (this.theYear.compareTo(temp) * 365) * this.calcNumLeapDays() + this.calcNumMonthDays() + this.theDay.getDay();
int rawDaysThat = (that.theYear.compareTo(temp) * 365) * that.calcNumLeapDays() + that.calcNumMonthDays() + that.theDay.getDay();
return rawDaysThis - rawDaysThat;
}
@Override
public boolean equals(Object obj){
if (obj.getClass().getSimpleName().equals(this.getClass().getSimpleName())){
Date temp = (Date)obj;
if (this.theDay.equals(temp.theDay) && this.theMonth.equals(temp.theMonth) && this.theYear.equals(temp.theYear)){
return true;
}
}
return false;
}
@Override
public String toString(){
return "" + this.theMonth.getMonth() + "/" + this.theDay.getDay() +"/" + this.theYear.getYear();
}
public String toStringLongForm(){
Date temp = new Date (1,1,this.theYear.getYear());
return this.theDay.getDayOfWeek() + ", " + this.theMonth.getMonthString() + " "+this.theDay.getDay()+", " + this.theYear.getYear() + ", day " + (this.compareTo(temp) + 1) + " of the year";
}
private int calcDayofWeek(int dayVal){ // formula source: http://mathforum.org/library/drmath/view/55837.html
int monthTemp = this.theMonth.getMonth();
int yearTemp = this.theYear.getYear();
if (monthTemp < 3){
if (monthTemp == 2){
monthTemp = 14;
yearTemp--;
}else{
monthTemp = 13;
yearTemp--;
}
}
int temp = dayVal + (2 * monthTemp) + ((3 * (monthTemp + 1) ) / 5) + yearTemp + (yearTemp/4) - (yearTemp/100) + (yearTemp/400) + 2;
return temp % 7;
}
public static int valiDate(int monthVal, int dayVal, int yearVal){ //returns 0 if date is bad, 1 if good.
int dayLimitTemp = 0;
try{
dayLimitTemp = Month.DAY_LIMITS[Boolean.compare(Year.calcLeapYear(yearVal),false)][monthVal - 1];
}catch (Exception e){
dayLimitTemp = 0;
}
if (dayLimitTemp == 0){
return 0;
}
if ( (yearVal >= 1970) && (yearVal <= 2400) && (monthVal > 0) && (monthVal < 13) && (dayVal > 0) && (dayVal <= dayLimitTemp)){
return 1;
}
else return 0;
}
}
OUTPUT:
Enter the file name -> dateList.txt
There were 1 bad date lines in the file.
1. Print the array of dates.
2. Search for a date.
3. Add a date to the array.
4. Delete a date from the array
5. Quit
Choice -> 1
1. Print to screen.
2. Print to file.
Choice -> 1
The dates in the array are:
Saturday, January 21, 2012, day 21 of the year
Tuesday, Feburary 21, 2012, day 52 of the year
Tuesday, Feburary 28, 2012, day 59 of the year
Wednesday, Feburary 29, 2012, day 60 of the year
Monday, May 21, 2012, day 142 of the year
Thursday, June 21, 2012, day 173 of the year
Saturday, July 21, 2012, day 203 of the year
Tuesday, August 21, 2012, day 234 of the year
1. Print the array of dates.
2. Search for a date.
3. Add a date to the array.
4. Delete a date from the array
5. Quit
Choice -> 2
Enter the day -> 21
Enter the month -> 1
Enter the year -> 2012
Date is at index: 0
1. Print the array of dates.
2. Search for a date.
3. Add a date to the array.
4. Delete a date from the array
5. Quit
Choice -> 3
Enter the day -> 22
Enter the month -> 1
Enter the year -> 2012
1. Print the array of dates.
2. Search for a date.
3. Add a date to the array.
4. Delete a date from the array
5. Quit
Choice -> 1
1. Print to screen.
2. Print to file.
Choice -> 1
The dates in the array are:
Saturday, January 21, 2012, day 21 of the year
Sunday, January 22, 2012, day 22 of the year
Tuesday, Feburary 21, 2012, day 52 of the year
Tuesday, Feburary 28, 2012, day 59 of the year
Wednesday, Feburary 29, 2012, day 60 of the year
Monday, May 21, 2012, day 142 of the year
Thursday, June 21, 2012, day 173 of the year
Saturday, July 21, 2012, day 203 of the year
Tuesday, August 21, 2012, day 234 of the year
1. Print the array of dates.
2. Search for a date.
3. Add a date to the array.
4. Delete a date from the array
5. Quit
Choice -> 4
Enter the day -> 21
Enter the month -> 1
Enter the year -> 2012
1. Print the array of dates.
2. Search for a date.
3. Add a date to the array.
4. Delete a date from the array
5. Quit
Choice -> 1
1. Print to screen.
2. Print to file.
Choice -> 1
The dates in the array are:
Sunday, January 22, 2012, day 22 of the year
Tuesday, Feburary 21, 2012, day 52 of the year
Tuesday, Feburary 28, 2012, day 59 of the year
Wednesday, Feburary 29, 2012, day 60 of the year
Monday, May 21, 2012, day 142 of the year
Thursday, June 21, 2012, day 173 of the year
Saturday, July 21, 2012, day 203 of the year
Tuesday, August 21, 2012, day 234 of the year
1. Print the array of dates.
2. Search for a date.
3. Add a date to the array.
4. Delete a date from the array
5. Quit
Choice -> 1
1. Print to screen.
2. Print to file.
Choice -> 1
The dates in the array are:
Sunday, January 22, 2012, day 22 of the year
Tuesday, Feburary 21, 2012, day 52 of the year
Tuesday, Feburary 28, 2012, day 59 of the year
Wednesday, Feburary 29, 2012, day 60 of the year
Monday, May 21, 2012, day 142 of the year
Thursday, June 21, 2012, day 173 of the year
Saturday, July 21, 2012, day 203 of the year
Tuesday, August 21, 2012, day 234 of the year
1. Print the array of dates.
2. Search for a date.
3. Add a date to the array.
4. Delete a date from the array
5. Quit
Choice -> 1
1. Print to screen.
2. Print to file.
Choice -> 2
Enter file -> dateOutput.txt
1. Print the array of dates.
2. Search for a date.
3. Add a date to the array.
4. Delete a date from the array
5. Quit
Choice -> 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.