This site requires JavaScript, please enable it in your browser!
Greenfoot back
sonali98
sonali98 wrote ...

2018/11/19

help with creating a weekly calendar using 2d arrays

sonali98 sonali98

2018/11/19

#
I am trying to create a weekly calendar using 2d arrays. the calendar is also supposed to convert military time to 12hour format and my methods are not working. I am also supposed to create a method that will create an output in the format of "hour(am/pm) : description". an example is 10am : doctor so far for my conversion method i have /** * Set the hour of this appointment, using a more human-friendly * string. * @param newHour The new hour for this appointment, using an * am/pm designation such as "9am" or "5pm". */ public void setTime(String newHour) { String str = ""; System.out.println(newHour.length()); if (newHour.length() == 3) { str = newHour.substring(1, newHour.length()); System.out.println(str); } if (str.equalsIgnoreCase("AM")) { this.hour = Integer.parseInt(newHour.substring(0, 1)); } else if (str.equalsIgnoreCase("PM")) { System.out.println(newHour.substring(0, 1)); this.hour = 12 + Integer.parseInt(newHour.substring(0, 1)); } else { str = newHour.substring(2, newHour.length()); } if (str.equalsIgnoreCase("PM")) { this.hour = 12 + (Integer.parseInt(newHour.substring(0))); } } and for the formatting method i have /** * Get a string representation of this appointment. * @return A human-readable representation of this appointment * that includes the time (in am/pm format) and the description, * such as "11am: CS 1114". */ @Override public String toString() { if (hour < 12) { return hour + ":" + description; } else if (hour == 12) { return hour + ":" + description; } else { return hour + (hour - 12) + ":" + description; } } any help is greatly appreciated
danpost danpost

2018/11/19

#
In the setTime method, what happens when newTime is "11am" or "12pm"? (the length is 4 in these cases) In the last line of the toString method, why do you double up on hour? (including it twice in the sum) Also, where is the method getting hour and description from? and where are "am" and "pm" applied? Keep in mind that 2400 military time is 12am in standard time.
sonali98 sonali98

2018/11/19

#
danpost wrote...
In the setTime method, what happens when newTime is "11am" or "12pm"? (the length is 4 in these cases) In the last line of the toString method, why do you double up on hour? (including it twice in the sum) Also, where is the method getting hour and description from? and where are "am" and "pm" applied? Keep in mind that 2400 military time is 12am in standard time.
Hour and description have been set as fields. Here is my entire Appointment Class public class Appointment extends Actor { //~ Fields ................................................................ private int hour; private String description; //~ Constructor ........................................................... /** * Creates a new Appointment object. * * @param hour The hour (time) of this appointment, in military time * (0-23). * @param description The description of this appointment. */ public Appointment(int hour, String description) { super(); if (hour >= 8 && hour <= 23) { this.hour = hour; this.description = description; } } public Appointment(String time, String description) { super(); setTime(time); this.description = description; } //~ Methods ............................................................... // ---------------------------------------------------------- /** * Get the description of this appointment. * @return This appointment's description. */ public String getDescription() { return description; } // ---------------------------------------------------------- /** * Get the hour of this appointment. * @return This appointment's hour, in military time. */ public int getHour() { return hour; } // ---------------------------------------------------------- /** * Set the description of this appointment. * @param newDescription The new description for this appointment. */ public void setDescription(String newDescription) { description = newDescription; } // ---------------------------------------------------------- /** * Set the hour of this appointment. * @param newHour The new hour for this appointment, in military * time. */ public void setHour(int newHour) { hour = newHour; } // ---------------------------------------------------------- /** * Set the hour of this appointment, using a more human-friendly * string. * @param newHour The new hour for this appointment, using an * am/pm designation such as "9am" or "5pm". */ public void setTime(String newHour) { String str = ""; System.out.println(newHour.length()); if (newHour.length() == 4) { str = newHour.substring(1, newHour.length()); System.out.println(str); } if (str.equalsIgnoreCase("AM")) { this.hour = Integer.parseInt(newHour.substring(0, 1)); } else if (str.equalsIgnoreCase("PM")) { System.out.println(newHour.substring(0, 1)); this.hour = 12 + Integer.parseInt(newHour.substring(0, 1)); } else { str = newHour.substring(2, newHour.length()); } if (str.equalsIgnoreCase("PM")) { this.hour = 12 + (Integer.parseInt(newHour.substring(0))); } } // ---------------------------------------------------------- /** * Get a string representation of this appointment. * @return A human-readable representation of this appointment * that includes the time (in am/pm format) and the description, * such as "11am: CS 1114". */ @Override public String toString() { if (hour < 12) { return hour + "am" + ":" + description; } else if (hour == 12) { return hour + "pm" + ":" + description; } else { return hour + "pm" + ":" + description; } } }
danpost danpost

2018/11/19

#
sonali98 wrote...
Hour and description have been set as fields. Here is my entire Appointment Class << Code Omitted >>
Now you are getting things really messed up. You changed the 3 to a 4, so now what about when the length is 3. Also, the block of code for that if was for a length of 3, so it no longer matches up with the changed condition. You have to deal with both conditions (or each one individually). On the other issue, you added the "am"s and "pm"s in, but you removed the adjustment (subtracting of 12) where it was needed.
sonali98 sonali98

2018/11/19

#
danpost wrote...
sonali98 wrote...
Hour and description have been set as fields. Here is my entire Appointment Class << Code Omitted >>
Now you are getting things really messed up. You changed the 3 to a 4, so now what about when the length is 3. Also, the block of code for that if was for a length of 3, so it no longer matches up with the changed condition. You have to deal with both conditions (or each one individually). On the other issue, you added the "am"s and "pm"s in, but you removed the adjustment (subtracting of 12) where it was needed.
i am very confused, do you think you could possibly write out what you are talking about for me?
danpost danpost

2018/11/19

#
sonali98 wrote...
i am very confused, do you think you could possibly write out what you are talking about for me?
Put the "-12" back in the last line of code; change the "4" back to a "3" in the setHour method; and add another if block in the setHour method to deal with those newTime strings whose length is 4.
You need to login to post a reply.