/*** Counseling Appointments
*********************** Connie is a counselor at OHS (Our High School). Connie meets students individually throughout the day. Each morning students ask her secretary for an appointment, and the secretary writes the name on a sheet of paper at the appropriate time. They want to use a computer program instead of paper, so that the data can appear on both the secretary's and the counselor's computer screen. So they need a way to record names in "boxes", like they do on the paper. The following program uses an ARRAY to simulate these boxes. It contains a cell (box) for each minute in the day, from 0 to 2399. It includes cells for non-real times, like 1199. ****************************************************/ public class Appointments { String[] names = new String[2400]; // from 0:00 until 24:00 public Appointments() { String name = ""; do { name = input("Name"); if(name.equals("ABORT")){ System.exit(0); } // end the program int time = Integer.parseInt(input("Time code (e.g. 840 or 1200)")); names[time] = name; showAllNames(); } while(true); // loop never ends } void showAllNames() // shows all the times where there is a name { for(int t = 0; t < 2400; t=t+1) { String name = names[t]; if(name!=null) { System.out.println(t + "\t" + name); } } System.out.println("================"); } public static void main(String[] args) { new Appointments(); } public String input(String prompt) { return javax.swing.JOptionPane.showInputDialog(null,prompt); } } /*** Improvements Needed ************************************ This needs lots of improvements: - it's easy to make a mistake and overwrite one name with a different one - incorrect time numbers, like 1080, should be rejected - the output only appears on the secretary's screen, not the counselors - if the computer stops, all data will be lost - it needs to be saved *************************************************************/ |
830
Carla 900 Adam 1030 Ellen 1200 Bill 1210 Fred 1500 Denise ================ |
An array is a list where many
pieces of data can be stored. This is similar to a sign-up sheet
that has boxes where you
can write a name. In this case, there are 2400 boxes - one box for
each minute of the day. Actually, there are also
extra boxes that do not correspond to a real time. Box #830 is for
the time 8:30 in the morning, but box 899 does not
represent a real time. If the secretary makes a mistake and types
866 instead of 855, the computer will put the name
in the box, but it doesn't make any sense in real terms.
The array (list) is created by the command:
String[] names = new String[2400];
The command to write a name into a box is:
names[time] = name;
Creating the array (list) does not actually create any boxes. Each
box is created when the name is saved in the list.
It's like have a blank sheet of paper, then drawing a box every time you
want to write a name. That's a bit mysterious,
but it means that most of the times contain NOTHING - that is
actually called null. So
the showAllNames method
must check each position and check that it is NOT equal to null:
String name = names[t];
if(name != null)
...
Notice that we do NOT use the .equals
method, because null is not a
value, but rather represents a missing box.