Question

In: Computer Science

Can someone check to see if anything needs to be changed from this code? The instructions...

Can someone check to see if anything needs to be changed from this code? The instructions are confusing and I'm not sure if this fulfills everything because my compiler won't run it properly.

DIRECTIONS:

In this project we will develop classes to implement a Day Planner program. Be sure to develop the code in a step by step manner, finish phase 1 before moving on to phase 2.

Phase 1

The class Appointment is essentially a record; an object built from this class will represent an Appointment in a Day Planner . It will contain 5 fields: month (3 character String), day (int), hour (int), minute (int), and message (String no longer then 40 characters).

Write 5 get methods and 5 set methods, one for each data field. Make sure the set methods verify the data. (E.G. month is a valid 3 letter code). Simple error messages should be displayed when data is invalid, and the current value should NOT change.

Write 2 constructor methods for the class Appointment . One with NO parameters that assigns default values to each field and one with 5 parameters that assigns the values passed to each field. If you call the set methods in the constructor(s) you will NOT need to repeat the data checks.

Write a toString method for the class Appointment . It should create and return a nicely formatted string with ALL 5 fields. Pay attention to the time portion of the data, be sure to format it like the time should be formatted ( HH : MM ) , a simple if-else statement could add a leading zero, if needed.

Write a method inputAppointment () that will use the class UserInput from a previous project, ask the user to input the information and assign the data fields with the users input. Make sure you call the UserInput methods that CHECK the min/max of the input AND call the set methods to make sure the fields are valid.

Write a main() method, should be easy if you have created the methods above, it creates a Appointment object, calls the inputAppointment () method to input values and uses the method toString() print a nicely formatted Appointment object to the screen. As a test, use the constructor with 5 parameters to create a second object (you decide the values to pass) and print the second object to the screen. The primary purpose of this main() method is to test the methods you have created in the Appointment class.

Phase 2

Create a class Planner , in the data area of the class declare an array of 20 Appointment objects. Make sure the array is private (data abstraction).

In this project we are going to build a simple Day Planner program that allow the user to create various Appointment objects and will insert each into an array. Be sure to insert each Appointment object into the array in the proper position, according to the date and time of the Appointment . This means the earliest Appointment object should be at the start of the array, and the last Appointment object at the end of the array.

Please pre load your array with the following Appointment objects:

          Mar 4, 17:30 Quiz 1
          Apr 1, 17:30 Midterm
          May 6, 17:30 Quiz 2
          Jun 3, 17:30 Final

Notice how the objects are ordered, with the earliest date at the start of the array and the latest at the end of the array.

The program will display the following menu and implement these features:

A)dd Appointment , D)elete Appointment , L)ist Appointment , E)xit

Some methods you must implement in the Planner class for this project:

  • Planner () constructor that places the 4 default Appointment objects in the array
  • main() method the creates the Planner object, then calls a run method
  • run() method that displays the menu, gets input, acts on that input
  • compareAppointment (Appointment A1, Appointment A2) method that returns true if A1 < A2, false otherwise
  • insertAppointment (Appointment A1) places A1 in the proper (sorted) slot of the array
  • listAppointment () method lists all Appointment objects in the array (in order) with a number in front
  • deleteAppointment () delete an object from the array using the number listAppointment () outputs in front of the item
  • addAppointment () calls inputAppointment () from the Appointment class and places it in the proper position of the array. Use an algorithm that shifts objects in the array (if needed) to make room for the new object. DO NOT sort the entire array, just shift objects

You may add additional methods to the Planner and Appointment classes as long as you clearly document 'what' and 'why' you added the method at the top of the class. The Appointment class could use one or more constructor methods. DO NOT in any way modify the UserInput class. If it so much as refers to a day or month or anything else in the Planner or Appointment class there will be a major point deduction.

Appointment.java
import java.util.Scanner;

public class Appointment {
public static void main(String[] args) {
}

private String month;
private int day;
private int hour;
private int minute;
private String message;

public String getMonth() {
return month;
}
public void setMonth(String month) {
if(month.length()!=3) {
System.out.println("Month should be 3 letter string");
this.month = "Jan";
}
else {
this.month = month;
}
  
}
public int getDay() {
return day;
}
public void setDay(int day) {
if(month=="Feb") {
if(day<=28 && day>=1) {
this.day = day;
}
else {
System.out.println("Enter a day (1-28");
this.day =01;
}
}
else if(month=="Apr" || month=="Jun" || month=="Sep" || month=="Nov") {
if(day<=30 && day>=1) {
this.day = day;
}
else {
System.out.println("Enter a day 1-30");
this.day =01;
}
}
else {
if(day<=31 && day>=1) {
this.day = day;
}
else {
System.out.println("Enter a day 1-31");
this.day =01;
}
}

  
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
if(hour<=24 && hour>=0) {
this.hour = hour;
}
else {
System.out.println("Hour must be between 0 and 24.");
this.hour = 00;
}
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
if(minute<60 && minute>=0) {
this.minute= minute;
}
else {
System.out.println("Minute must be between 0 and 59.");
this.minute = 00;
}
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
if(message.length()<=40) {
this.message = message;
}
else {
System.out.println("Msg length max is 40");
this.message=message.substring(0, 40);
}
}
public Appointment() {
month="Jan";
day=01;
hour=00;
minute=00;
message="";
}
public Appointment(String month,int day,int hour,int minute,String message) {
setMonth(month);
setDay(day);
setHour(hour);
setMinute(minute);
setMessage(message);
}

public String toString() {
String str=month+" ";
str+=Integer.toString(day);
str+=", ";
if(hour<10) {
String padded ="0"+Integer.toString(hour);
str+=padded;
}
else {
str+=Integer.toString(hour);
}
str+=":";
if(minute<10) {
String padded ="0"+Integer.toString(minute);
str+=padded;
}
else {
str+=Integer.toString(minute);
}
str+=" "+message;
return str;
}

public int compareTo(Appointment app) {
String monthArray[]= {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
int ind1 = 0,ind2 = 0;
for(int i=0;i<12;i++) {
if(monthArray[i].equalsIgnoreCase(month)) {
ind1=i;
}
if(monthArray[i].equalsIgnoreCase(app.getMonth())) {
ind2=i;
}
}
if(ind1<ind2) {
return 1;
}
else if(ind1==ind2) {
if(this.day<app.getDay()) {
return 1;
}
else if(this.getDay()>app.getDay()) {
return -1;
}
else {
if(this.getHour()<app.getHour()) {
return 1;
}
else if(this.getHour()>app.getHour()) {
return -1;
}
else {
if(this.getMinute()<app.getMinute()) {
return 1;
}
else if(this.getMinute()>app.getMinute()) {
return -1;
}
else {
return 0;
}
}
}
}
else {
return -1;
}
}
}

Planner.java

public class Planner {
public static void main(String[] args) {
}
private Appointment[] appointments;
private int cnt;

public Planner() {
appointments=new Appointment[20]; cnt=0;
Appointment a1=new Appointment("Mar",4,17,30,"Quiz 1");
appointments[cnt]=a1; cnt++;
a1=new Appointment("Apr",1,17,30,"Midterm");
appointments[cnt]=a1; cnt++;
a1=new Appointment("May",6,17,30,"QUIZ 2");
appointments[cnt]=a1; cnt++;
a1=new Appointment("Jun",3,17,30,"Final");
appointments[cnt]=a1; cnt++;
}
public void printAppointments() {
for(int i=0;i<cnt;i++) {
System.out.println(appointments[i]);
}
}
public void inputAppointment(Appointment app) {
boolean flag=false;
if(cnt<19) {
for(int i=0;i<cnt;i++) {
if(appointments[i].compareTo(app)==-1) {
for(int j=cnt;j>i;j--) {
appointments[j]=appointments[j-1];   
}
appointments[i]=app;
cnt++;
flag=true;
break;
}
else if(appointments[i].compareTo(app)==0) {
for(int j=cnt;j>i;j--) {
appointments[j]=appointments[j-1];   
}
appointments[i]=app;
cnt++;
flag=true;
break;
}
if(flag==false) {
appointments[cnt]=app;
cnt++;
}   
}
}
else {
System.out.println("\nAll appointments are filled.\n");
}
}

public void deleteAppointment() {
if(cnt>=0) {
for(int i=0;i<cnt;i++) {
appointments[i]=appointments[i+1];
}
cnt--;
System.out.println("First appointment deleted.");
}
else {
System.out.println("There are no appointments in the schedule.");
}
}
}

Solutions

Expert Solution

Appointment.java

import java.util.Scanner;

public class Appointment {
public static void main(String[] args) {
}

private String month;
private int day;
private int hour;
private int minute;
private String message;

public String getMonth() {
return month;
}
public void setMonth(String month) {
if(month.length()!=3) {
System.out.println("Month should be 3 letter string");
this.month = "Jan";
}
else {
this.month = month;
}
  
}
public int getDay() {
return day;
}
public void setDay(int day) {
if(month=="Feb") {
if(day<=28 && day>=1) {
this.day = day;
}
else {
System.out.println("Enter a day (1-28");
this.day =01;
}
}
else if(month=="Apr" || month=="Jun" || month=="Sep" || month=="Nov") {
if(day<=30 && day>=1) {
this.day = day;
}
else {
System.out.println("Enter a day 1-30");
this.day =01;
}
}
else {
if(day<=31 && day>=1) {
this.day = day;
}
else {
System.out.println("Enter a day 1-31");
this.day =01;
}
}

  
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
if(hour<=24 && hour>=0) {
this.hour = hour;
}
else {
System.out.println("Hour must be between 0 and 24.");
this.hour = 00;
}
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
if(minute<60 && minute>=0) {
this.minute= minute;
}
else {
System.out.println("Minute must be between 0 and 59.");
this.minute = 00;
}
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
if(message.length()<=40) {
this.message = message;
}
else {
System.out.println("Msg length max is 40");
this.message=message.substring(0, 40);
}
}
public Appointment() {
month="Jan";
day=01;
hour=00;
minute=00;
message="";
}
public Appointment(String month,int day,int hour,int minute,String message) {
setMonth(month);
setDay(day);
setHour(hour);
setMinute(minute);
setMessage(message);
}

public String toString() {
String str=month+" ";
str+=Integer.toString(day);
str+=", ";
if(hour<10) {
String padded ="0"+Integer.toString(hour);
str+=padded;
}
else {
str+=Integer.toString(hour);
}
str+=":";
if(minute<10) {
String padded ="0"+Integer.toString(minute);
str+=padded;
}
else {
str+=Integer.toString(minute);
}
str+=" "+message;
return str;
}

public int compareTo(Appointment app) {
String monthArray[]= {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
int ind1 = 0,ind2 = 0;
for(int i=0;i<12;i++) {
if(monthArray[i].equalsIgnoreCase(month)) {
ind1=i;
}
if(monthArray[i].equalsIgnoreCase(app.getMonth())) {
ind2=i;
}
}
if(ind1<ind2) {
return 1;
}
else if(ind1==ind2) {
if(this.day<app.getDay()) {
return 1;
}
else if(this.getDay()>app.getDay()) {
return -1;
}
else {
if(this.getHour()<app.getHour()) {
return 1;
}
else if(this.getHour()>app.getHour()) {
return -1;
}
else {
if(this.getMinute()<app.getMinute()) {
return 1;
}
else if(this.getMinute()>app.getMinute()) {
return -1;
}
else {
return 0;
}
}
}
}
else {
return -1;
}
}
}

Planner.java

import java.util.*;
public class Planner {
public static void main(String[] args) {
//create the planner object
Planner obj = new Planner();
char ch;
Scanner sc = new Scanner(System.in);
while(true)
{
   //prompt the user to enter an option
   System.out.print("A)dd Appointment \n D)elete Appointment \n L)ist Appointment \n E)xit\nEnter your choice:\t");
   ch = sc.nextLine().charAt(0);
   //if the choice is A
   if(ch == 'A')
   {
       //prompt the user to enter all the fields
       System.out.print("Enter Month:\t");
       String month = sc.nextLine();
       System.out.print("Enter Day:\t");
       int day = sc.nextInt();
       System.out.print("Enter hours:\t");
       int hours = sc.nextInt();
       System.out.print("Enter minutes:\t");
       int minutes = sc.nextInt();
       sc.nextLine();
       System.out.print("Enter Message:\t");
       String message = sc.nextLine();
       //create the object for appointment
       Appointment app = new Appointment(month, day, hours, minutes, message);
       //insert the obect into the array
       obj.inputAppointment(app);
   }
   //if the choice is D
   else if(ch == 'D')
   {
       //delete the first object
       obj.deleteAppointment();
   }
   //if the choice is L
   else if(ch == 'L')
   {
       //list all the objects
       obj.printAppointments();
   }
   //if the choice is E, exit
   else if(ch == 'E')
   {
       break;
   }
   else
   {
       System.out.println("Invalid choice");
   }
}
}

private Appointment[] appointments;
private int cnt;

public Planner() {
appointments=new Appointment[20]; cnt=0;
Appointment a1=new Appointment("Mar",4,17,30,"Quiz 1");
appointments[cnt]=a1; cnt++;
a1=new Appointment("Apr",1,17,30,"Midterm");
appointments[cnt]=a1; cnt++;
a1=new Appointment("May",6,17,30,"QUIZ 2");
appointments[cnt]=a1; cnt++;
a1=new Appointment("Jun",3,17,30,"Final");
appointments[cnt]=a1; cnt++;
}
public void printAppointments() {
for(int i=0;i<cnt;i++) {
System.out.println(appointments[i]);
}
}
public void inputAppointment(Appointment app) {
boolean flag=false;
if(cnt<19) {
   for(int i=0;i<cnt;i++) {
       //if the current object is greater than or equal to the given object(app)
       //inset the app before the current object
       if(appointments[i].compareTo(app)==-1 || appointments[i].compareTo(app)==0) {
           for(int j=cnt;j>i;j--) {
           appointments[j]=appointments[j-1];   
           }
           appointments[i]=app;
           cnt++;
           flag=true;
           break;
       }
      
   }
   if(!flag) {
       appointments[cnt]=app;
       cnt++;
   }   
}

else {
System.out.println("\nAll appointments are filled.\n");
}
}

public void deleteAppointment() {
if(cnt>=0) {
for(int i=0;i<cnt;i++) {
appointments[i]=appointments[i+1];
}
cnt--;
System.out.println("First appointment deleted.");
}
else {
System.out.println("There are no appointments in the schedule.");
}
}
}

If you have any doubts please comment and please don't dislike.


Related Solutions

Can someone check to see if I answered them correctly? The answers are highlighted in Bold...
Can someone check to see if I answered them correctly? The answers are highlighted in Bold font. I also need help with Question 6 1) What criteria must sales transactions meet in order for the seller to recognize revenues before collecting cash? a. The revenues must be earned (the firm must have achieved substantial performance). b. The amount to be received must qualify as an asset (there must be a future economic benefit and the amount must be measured with...
can someone please check this code? – Enter Quantity of Numbers Design a program that allows...
can someone please check this code? – Enter Quantity of Numbers Design a program that allows a user to enter any quantity of numbers until a negative number is entered. Then display the highest number and the lowest number. For the programming problem, create the pseudocode and enter it below. Enter pseudocode here start     Declarations           num number           num high             num low              housekeeping()     while number >=0 detailLoop()      endwhile      finish() stop housekeeping( )           output...
can someone finish and check my code on main. cpp? Its not working for me even...
can someone finish and check my code on main. cpp? Its not working for me even though im sure my code make sense is it possible to output each function to show they work. this is supposed to be a vector class library made from allocated memory i have included templated functions in the class file to help create the rest of the functions. Thank you so much note: i did not include main.cpp because it  was empty- im hoping someone...
Can someone make an example problem from these instructions? This is Linear Algebra and this pertains...
Can someone make an example problem from these instructions? This is Linear Algebra and this pertains to matrices. Find the inverses and transposes of elementary and permutation matrices and their products. Use your own numbers to create a problem for this or post a similar problem that describes this. I need this knowledge for a quiz.
Can someone please provide the answers so I can check my work? Baldwin Corporation Excerpts from...
Can someone please provide the answers so I can check my work? Baldwin Corporation Excerpts from the Statement of Financial Position for Baldwin Corporation as of September 30, Year 5, are presented below. Cash $   950,000 Accounts receivable (net) 1,675,000 Inventories   2,806,000 Total current assets $5,431,000 Accounts payable $1,004,000 Accrued liabilities      785,000 Total current liabilities $1,789,000 The Board of directors of Baldwin Corporation met on October 4, Year 5, and declared regular quarterly cash dividends amounting to $750,000 ($0.60 per share)....
can someone translate this pseudo code to actual c++ code while (not the end of the...
can someone translate this pseudo code to actual c++ code while (not the end of the input) If the next input is a number read it and push it on the stack else If the next input is an operator, read it pop 2 operands off of the stack apply the operator push the result onto the stack When you reach the end of the input: if there is one number on the stack, print it else error
can someone change this code so that timestandard can be calculated at the end of the...
can someone change this code so that timestandard can be calculated at the end of the code using the inputs n,RF,PFD, and the measured cycles, instead of being called from the beginning of the code using namespace std; float timestandard(float time, float rf, float pfd) { return((time / 100) * rf * (1 + pfd)); /* calculating the time standard using the given formula*/ } int main() { int n, rf, pfd, x; /* inputs are taken*/ cout << "****...
Can you fix this code please. the removing methods id no doing anything. this is java...
Can you fix this code please. the removing methods id no doing anything. this is java code import java.util.NoSuchElementException; public class DoublyLinkedList<E> {    public int size;    public Node head;    public Node tail;             @Override    public boolean isEmpty() {               return size == 0;    }    @Override    public int getSize() {               return 0;    }    @Override    public void addAtFront(E element) {       ...
Give some examples of ways you can check to see if a solution is reasonable or...
Give some examples of ways you can check to see if a solution is reasonable or not. Why is it important that you as an engineer be confident in your solutions?.
Can someone create a Test bench for this 4 Bit USR code so that it can...
Can someone create a Test bench for this 4 Bit USR code so that it can shift left, shift right and Load. This is in VHDL. Please type out the code. library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity Uni_reg is port( LR,SP,clk,clear,shL,shR: in std_logic; -- shL = shift left shR= shift right Da,Db,Dc : in std_logic; --inputs for load Qa,Qb,Qc : out std_logic); --out puts from the flipflops end Uni_reg; architecture Structural of Uni_reg is signal lr1,lr2,sp1,sp2,R1,R2,R3 : std_logic; signal L1,L2,L3,LOAD1,LOAD2,LOAD3:std_logic; signal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT