mitekstuf.files.wordpress.com  · web view- it's easy to make a mistake and overwrite one...

7
Appointments Download Source Code /*** 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. ****************************************************/ String[] names = new String[2400]; // from 0:00 until 24:00 void setup() { String name = ""; do { name = input("Name"); if(name.equals("ABORT")){ System.exit(0); } // end the program int time = int(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 nam

Upload: doanlien

Post on 01-Sep-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: mitekstuf.files.wordpress.com  · Web view- it's easy to make a mistake and overwrite one name with a different one

Appointments  Download Source Code

/*** 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 theappropriate time.  They want to use a computer program insteadof paper, so that the data can appear on both the secretary'sand the counselor's computer screen.  So they need a way torecord 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.****************************************************/

String[] names = new String[2400];  // from 0:00 until 24:00

void setup(){  String name = "";  do  {    name = input("Name");    if(name.equals("ABORT")){ System.exit(0); }  // end the program    int time = int(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)

Page 2: mitekstuf.files.wordpress.com  · Web view- it's easy to make a mistake and overwrite one name with a different one

     {       println(t + "\t" + name);       }   }     println("================");}

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     Carla900     Adam1030    Ellen1200    Bill1210    Fred1500    Denise================

Arrays

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 namein 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:

Page 3: mitekstuf.files.wordpress.com  · Web view- it's easy to make a mistake and overwrite one name with a different one

     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 methodmust 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.

Programming Practice

Download the program and run it, then type in a few names and times and check that they are saved correctly.

As it stands, it's possible to type in one name at 12:00, then type another name at 12:00 - the second names erases the first name and replaces it.  It would be nice if the program would warn the user that theyare about to erase a name, and ask "Are you sure?" before doing this.  Try to add an if… command to check that the position is null before writing - otherwise warn the user.

Add a second list for a second counselor.  Then each time a name is typed in, the program should ask "Which counselor do you wish to visit?".  Then the name should be recorded in the appropriate list.

Try to prevent using incorrect times, like 12:61.  To do this:o Extract the minutes with a command like this:

   int m = time % 100;That gets the remainder of division by 100, which is the minutes.

o Check that m is not greater than 59.  If it is, reject the entry.

Page 4: mitekstuf.files.wordpress.com  · Web view- it's easy to make a mistake and overwrite one name with a different one

Before any changes are made to the code:

Page 5: mitekstuf.files.wordpress.com  · Web view- it's easy to make a mistake and overwrite one name with a different one

Code change so that there are no double bookings:

Page 6: mitekstuf.files.wordpress.com  · Web view- it's easy to make a mistake and overwrite one name with a different one