/**************************************** * Name: Rahul N. * Date: Oct 16th, 2023 * Description: To write a program that makes appointment * and to see if the two appointments overlap each other ****************************************/ import java.util.Scanner; public class Appointment { private int day; private int month; private String description; public Appointment(int day, int month, String description) { this.day = day; this.month = month; this.description = description; } public boolean hasTheSameDate(Appointment other) { return this.day == other.day && this.month == other.month; } public String toString() { return month + "/ " + day + ": " + description; } // Main Method public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the details for the first appointment"); System.out.print("Day: "); int day1 = scanner.nextInt(); System.out.print("Month: "); int month1 = scanner.nextInt(); scanner.nextLine(); // consume the newline character System.out.print("Description: "); String description1 = scanner.nextLine(); Appointment appointment1 = new Appointment(day1, month1, description1); System.out.println("Enter the details for the second appointment"); System.out.print("Day: "); int day2 = scanner.nextInt(); System.out.print("Month: "); int month2 = scanner.nextInt(); scanner.nextLine(); // consume the newline character System.out.print("Description: "); String description2 = scanner.nextLine(); Appointment appointment2 = new Appointment(day2, month2, description2); System.out.println("Appointment 1: " + appointment1); System.out.println("Appointment 2: " + appointment2); if (appointment1.hasTheSameDate(appointment2)) { System.out.println("The appointments have the same date."); } else { System.out.println("The appointments do not have the same date."); } } } My results has been: Enter the details for the first appointment Day: 23 Month: 7 Description: hair Enter the details for the second appointment Day: 23 Month: 7 Description: car Appointment 1: 7/ 23: hair Appointment 2: 7/ 23: car The appointments have the same date. Process finished with exit code 0 My question: How can I change my code so that it converts 7/ 23 to 7/23?
* Name: Rahul N.
* Date: Oct 16th, 2023
* Description: To write a program that makes appointment
* and to see if the two appointments overlap each other
****************************************/
import java.util.Scanner;
public class Appointment {
private int day;
private int month;
private String description;
public Appointment(int day, int month, String description) {
this.day = day;
this.month = month;
this.description = description;
}
public boolean hasTheSameDate(Appointment other) {
return this.day == other.day && this.month == other.month;
}
public String toString() {
return month + "/ " + day + ": " + description;
}
// Main Method
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the details for the first appointment");
System.out.print("Day: ");
int day1 = scanner.nextInt();
System.out.print("Month: ");
int month1 = scanner.nextInt();
scanner.nextLine(); // consume the newline character
System.out.print("Description: ");
String description1 = scanner.nextLine();
Appointment appointment1 = new Appointment(day1, month1, description1);
System.out.println("Enter the details for the second appointment");
System.out.print("Day: ");
int day2 = scanner.nextInt();
System.out.print("Month: ");
int month2 = scanner.nextInt();
scanner.nextLine(); // consume the newline character
System.out.print("Description: ");
String description2 = scanner.nextLine();
Appointment appointment2 = new Appointment(day2, month2, description2);
System.out.println("Appointment 1: " + appointment1);
System.out.println("Appointment 2: " + appointment2);
if (appointment1.hasTheSameDate(appointment2)) {
System.out.println("The appointments have the same date.");
} else {
System.out.println("The appointments do not have the same date.");
}
}
}
Enter the details for the first appointment
Day: 23
Month: 7
Description: hair
Enter the details for the second appointment
Day: 23
Month: 7
Description: car
Appointment 1: 7/ 23: hair
Appointment 2: 7/ 23: car
The appointments have the same date.
Process finished with exit code 0
My question: How can I change my code so that it converts 7/ 23 to 7/23?
In the provided code, the objective is to create a Java program that allows users to make appointments and check whether two appointments overlap in terms of date. The program includes an Appointment
class with attributes for day, month, and description. It also provides functionality to compare and determine whether two appointments share the same date. The user interacts with the program by inputting the details of two appointments, including the day, month, and a description. The program then evaluates whether these appointments fall on the same date and provides an informative response. An important modification was made to ensure that the date is displayed without an extra space, resulting in a cleaner and more readable output. This code serves as a practical example for managing appointments and comparing dates within a Java program.
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 2 images