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 course - must be greater than 0? Enter due dates: 1: Enter month - between 1 and 12: Enter day - between 1 and 31: Enter year: 2: Enter month - between 1 and 12: Enter day - between 1 and 31: Enter year: 3: Enter month - between 1 and 12: Enter day - between 1 and 31: Enter year: The due dates are: 1: 2017/2/14 2: 2017/2/17 3: 2018/2/16 Due Dates with one day added are The due dates are: 1: 2017/2/15 2: 2017/2/18 3: 2018/2/17 Due Dates sorted are The due dates are: 1: 2017/2/15 2: 2017/2/18 3: 2018/2/17 Do another (y/n)? n
import java.util.Scanner;
public class DueDates {
private MyDate[] dueDates;
public DueDates() {
dueDates = new MyDate[10];}
public DueDates(int max) {
dueDates = new MyDate[max]; }
public boolean inputDueDates(Scanner in) {
System.out.println("Enter due dates:");
for (int i = 0; i < dueDates.length; i++) {
dueDates[i] = new MyDate();
System.out.println((i + 1) + ":");
dueDates[i].inputDate(in); }
return true;}
public void addOne() {
for (int i = 0; i < dueDates.length; i++)
dueDates[i].addOne();}
public String toString() {
String str = " ";
for (int i = 0; i < dueDates.length; i++) {
str += (i + 1) + ": " + dueDates[i].toString() + "\n";
} return str; }}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Lab2 {
public static void main(String[] args) {
String inputFile,outFile;
int size;
Scanner keyboard = new Scanner(System.in);
while(true) {
System.out.print("Enter name of file to import or the word null to
bypass :");
inputFile=keyboard.next();
if(inputFile.equalsIgnoreCase("NULL")) {
System.out.print("How many assessments in this course - must be
greater than 0?");
int assesments = keyboard.nextInt();
DueDates dates = new DueDates(assesments);
dates.inputDueDates(keyboard);
System.out.println("The due dates are:\n" +
dates.toString());
dates.addOne();
System.out.println("\nDue Dates with one date added are:");
System.out.println("The due dates are:\n" +
dates.toString());
System.out.print("Enter name of file to write to null to bypass
:");
outFile=keyboard.next();
if(!outFile.equalsIgnoreCase(null)) {
try {
FileWriter fw=new FileWriter(new File(outFile));
fw.write("Due Dates are:\n");
fw.write(dates.toString());
fw.close();
} catch (IOException e) {
e.printStackTrace();
} } }
else {
int day,month,year;
try {
Scanner fileRead=new Scanner(new File(inputFile));
size=fileRead.nextInt();
MyDate dates[]=new MyDate[size];
for(int i=0;i<size;i++) {
month=fileRead.nextInt();
day=fileRead.nextInt();
year=fileRead.nextInt();
dates[i]=new MyDate(month,day,year); }
fileRead.close();
System.out.println("The due dates are:\n");
for(int i=0;i<size;i++) {
System.out.println(dates[i].toString()); }
for(int i=0;i<size;i++) {
dates[i].addOne(); }
System.out.println("\nDue Dates with one date added are:");
System.out.println("The due dates are:\n");
for(int i=0;i<size;i++) {
System.out.println(dates[i].toString()); }
System.out.print("Enter name of file to write to null to bypass
:");
outFile=keyboard.next();
if(!outFile.equalsIgnoreCase(null)) {
try {
FileWriter fw=new FileWriter(new File(outFile));
fw.write("Due Dates are:\n");
for(int i=0;i<size;i++) {
fw.write(dates[i].toString()+"\n"); }
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}}
System.out.print("\nDo another (Y/N) ::");
char ch = keyboard.next(".").charAt(0);
if(ch=='Y'||ch=='y') {
continue; }
else {
break;
} } }}
import java.util.Scanner;
public class MyDate {
private int day = 1;
private int month = 1;
private int year = 2018;
public MyDate() { }
public MyDate( int month,int day, int year) {
this.day = day; this.month = month; this.year = year; }
public String toString() {
return new String("" + year + "/" + month + "/" + day); }
public boolean inputDate(Scanner in) {
month = 0; day = 0; year = 0;
do {
System.out.print("Enter month - between 1 and 12: ");
if (in.hasNextInt())
this.month = in.nextInt();
else {
System.out.println("Invalid month input");
in.next(); }
} while (this.month <= 0 || this.month > 12);
do {
System.out.print("Enter day - between 1 and 31: ");
if (in.hasNextInt())
this.day = in.nextInt();
else {
System.out.println("Invalid day input");
in.next(); }
} while (this.day <= 0
|| this.day > 31
|| (this.month == 2 && this.day > 29)
|| (this.day > 30 && (this.month == 9 || this.month == 4
|| this.month == 6 || this.month == 11)));
do {
System.out.print("Enter year: ");
if (in.hasNextInt())
this.year = in.nextInt();
else {
System.out.println("Invalid day input");
in.next();
} } while (this.year <= 0);
return true; }
public void addOne() {
int monDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if(month==12 && day==monDays[month-1]){
year++;
month=1;
day=1;}
else if(day==monDays[month-1]){
month++;
day=1;}
else if(day<monDays[month-1]){
day++;}
else if(day==monDays[month-1]){
day=1;
month++;
} } }
// MyDate.java
import java.util.Scanner;
public class MyDate implements Comparable<MyDate>{
private int day = 1;
private int month = 1;
private int year = 2018;
public MyDate() { }
public MyDate( int month,int day, int year) {
this.day = day; this.month = month; this.year = year;
}
public String toString() {
return new String("" + year + "/" + month + "/" + day);
}
public boolean inputDate(Scanner in) {
month = 0; day = 0; year = 0;
do {
System.out.print("Enter month - between 1 and 12: ");
if (in.hasNextInt())
this.month = in.nextInt();
else {
System.out.println("Invalid month input");
in.next();
}
} while (this.month <= 0 || this.month > 12);
do {
System.out.print("Enter day - between 1 and 31: ");
if (in.hasNextInt())
this.day = in.nextInt();
else {
System.out.println("Invalid day input");
in.next(); }
} while (this.day <= 0
|| this.day > 31
|| (this.month == 2 && this.day > 29)
|| (this.day > 30 && (this.month == 9 || this.month == 4
|| this.month == 6 || this.month == 11)));
do {
System.out.print("Enter year: ");
if (in.hasNextInt())
this.year = in.nextInt();
else {
System.out.println("Invalid day input");
in.next();
} } while (this.year <= 0);
return true;
}
public void addOne() {
int monDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if(month==12 && day==monDays[month-1]){
year++;
month=1;
day=1;
}
else if(day==monDays[month-1]){
month++;
day=1;
}
else if(day<monDays[month-1]){
day++;
}
else if(day==monDays[month-1]){
day=1;
month++;
}
}
// function to compare two dates
// returns -1 if this date occurs before other
// returns 1 if this date occurs after other
// returns 0 if this date is same as other
public int compareTo(MyDate other)
{
if(year < other.year)
{
return -1;
}else if(year > other.year)
{
return 1;
}else
{
if(month < other.month)
return -1;
else if(month > other.month)
return 1;
else
{
if(day < other.day)
return -1;
else if(day > other.day)
return 1;
else
return 0;
}
}
}
}
//end of MyDate.java
//DueDates.java
import java.util.Scanner;
public class DueDates {
private MyDate[] dueDates;
public DueDates() {
dueDates = new MyDate[10];
}
public DueDates(int max) {
dueDates = new MyDate[max];
}
public boolean inputDueDates(Scanner in) {
System.out.println("Enter due dates:");
for (int i = 0; i < dueDates.length; i++) {
dueDates[i] = new MyDate();
System.out.println((i + 1) + ":");
dueDates[i].inputDate(in);
}
return true;
}
public void addOne() {
for (int i = 0; i < dueDates.length; i++)
dueDates[i].addOne();
}
public String toString() {
String str = " ";
for (int i = 0; i < dueDates.length; i++) {
str += (i + 1) + ": " + dueDates[i].toString() + "\n";
}
return str;
}
// method to sort the dueDates array using insertion sort
public void sort()
{
MyDate key;
int i,j;
for(i=1;i<dueDates.length;i++)
{
j = i-1;
key = dueDates[i];
while(j >=0 && dueDates[j].compareTo(key) > 0)
{
dueDates[j+1] = dueDates[j];
j--;
}
dueDates[j+1] = key;
}
}
}
//end of DueDates.java
//Lab2.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Lab2 {
// method to sort dates array using insertion sort
public static void sort(MyDate dates[])
{
MyDate key;
int i,j;
for(i=1;i<dates.length;i++)
{
j = i-1;
key = dates[i];
while(j >=0 && dates[j].compareTo(key) > 0)
{
dates[j+1] = dates[j];
j--;
}
dates[j+1] = key;
}
}
public static void main(String[] args) {
String inputFile,outFile;
int size;
Scanner keyboard = new Scanner(System.in);
while(true) {
System.out.print("Enter name of file to import or the word null to bypass :");
inputFile=keyboard.next();
if(inputFile.equalsIgnoreCase("NULL")) {
System.out.print("How many assessments in this course - must be greater than 0?");
int assesments = keyboard.nextInt();
DueDates dates = new DueDates(assesments);
dates.inputDueDates(keyboard);
System.out.println("The due dates are:\n" + dates.toString());
dates.addOne();
System.out.println("\nDue Dates with one date added are:");
System.out.println("The due dates are:\n" + dates.toString());
System.out.println("Due Dates sorted are :");
dates.sort();
System.out.println("The due dates are:\n" + dates.toString());
System.out.print("Enter name of file to write to null to bypass :");
outFile=keyboard.next();
if(!outFile.equalsIgnoreCase(null)) {
try {
FileWriter fw=new FileWriter(new File(outFile));
fw.write("Due Dates are:\n");
fw.write(dates.toString());
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}else
{
int day,month,year;
try {
Scanner fileRead=new Scanner(new File(inputFile));
size=fileRead.nextInt();
MyDate dates[]=new MyDate[size];
for(int i=0;i<size;i++) {
month=fileRead.nextInt();
day=fileRead.nextInt();
year=fileRead.nextInt();
dates[i]=new MyDate(month,day,year);
}
fileRead.close();
System.out.println("The due dates are:\n");
for(int i=0;i<size;i++) {
System.out.println(dates[i].toString());
}
for(int i=0;i<size;i++) {
dates[i].addOne();
}
System.out.println("\nDue Dates with one date added are:");
System.out.println("The due dates are:\n");
for(int i=0;i<size;i++) {
System.out.println(dates[i].toString());
}
System.out.println("Due Dates sorted are :");
sort(dates);
System.out.println("The due dates are:\n");
for(int i=0;i<size;i++) {
System.out.println(dates[i].toString());
}
System.out.print("Enter name of file to write to null to bypass :");
outFile=keyboard.next();
if(!outFile.equalsIgnoreCase(null)) {
try {
FileWriter fw=new FileWriter(new File(outFile));
fw.write("Due Dates are:\n");
for(int i=0;i<size;i++) {
fw.write(dates[i].toString()+"\n"); }
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}catch (FileNotFoundException e) {
e.printStackTrace();
}
}
System.out.print("\nDo another (Y/N) ::");
char ch = keyboard.next(".").charAt(0);
if(ch=='Y'||ch=='y') {
continue;
}else
break;
}
keyboard.close();
}
}
//end of Lab2.java
Output:
Input file:
Output:
Problem Description: Expand on 2 by implementing Insertion Sort and displaying the dates sorted after adding...
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,...
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 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...
Why isnt my MyCalender.java working? class MyCalender.java : package calenderapp; import java.util.Scanner; public class MyCalender { MyDate myDate; Day day; enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } public static void main(String[] args) { Scanner sc= new Scanner(System.in); System.out.println("Enter date below :"); System.out.println("Enter day :"); int day = sc.nextInt(); System.out.println("Enter Month :"); int month = sc.nextInt(); System.out.println("Enter year :"); int year = sc.nextInt(); MyDate md = new MyDate(day, month, year); if(!md.isDateValid()){ System.out.println("\n Invalid date . Try...
(How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...
HINT 1)-Write a method to accept n integer number between 0 and 100 and return the average. 2)-Write a method to convert an the letter grade as follow. integer number between 0 and 100 to latter grade A to F and return. 100-90->A 89-80->B 79-70->C 69-60->D 00-59->F 3)-Write a method to compute GPA for following letter grade and return the GPA value. A =>4.0 B ->3.0 C->20 D->1.0 Hint for option 1 1 import java.util.Scanner 2 public class ProjPart2 3...
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...
Zeller's congruence is an algorithm developed to calculate the day of the week. Write a program that prompts the user to enter a year, month, day of month and it displays the name of the week. Here is my source code. I need it broke down into a flowchart. public static void main(String[] args) { //all variables are initialized to 0, because local variables require at least an initial value before use Scanner input = new...
I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...
The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...