![]() |
School of Engineering and
Applied
Science Department of Computer Science CSci 53 -- Introduction to Software Development http://www.seas.gwu.edu/~csci53/fall03 Prof. Michael B. Feldman mfeldman@gwu.edu |
Basic Algorithm
if ((day == 1) || (day == 5)) //
Monday, Friday
{
System.out.println ("Work
at home today");
}
if ((day == 2) || (day == 4)) //
Tuesday, Thursday
{
System.out.println ("CSci
53, 11 AM - 12:15 PM");
System.out.println ("CSci
133, 2 - 3:15 PM");
System.out.println
("Office hours, 4 - 5:30 PM");
}
if (day == 3) // Wednesday
{
System.out.println
("Office hours, 2 - 5:30 PM");
}
if ((day == 6) || (day == 7)) //
Saturday, Sunday
{
System.out.println ("Have
a nice weekend");
}
if ((day == 1) || (day = 5))
// Monday, Friday
{
System.out.println ("Work
at home today");
}
else
{
if ((day == 2) || (day ==
4)) // Tuesday, Thursday
{
System.out.println ("CSci 53, 11 AM - 12:15 PM");
System.out.println ("CSci 133, 2 - 3:15 PM");
System.out.println ("Office hours, 4 - 5:30 PM");
}
else
{
if (day == 3)
// Wednesday
{
System.out.println ("Office hours, 2 - 5:30 PM");
}
else
{
if
((day == 6) || (day == 7)) // Saturday, Sunday
{
System.out.println ("Have a nice weekend");
}
else // none of the above
{
System.out.println ("Out of range input; try again")
}
}
}
}
if ((day == 1) || (day == 5)) //
Monday, Friday
{
System.out.println ("Work
at home today");
}
else if ((day == 2) || (day ==
4)) // Tuesday, Thursday
{
System.out.println ("CSci
53, 11 AM - 12:15 PM");
System.out.println ("CSci
133, 2 - 3:15 PM");
System.out.println
("Office hours, 4 - 5:30 PM");
}
else if (day == 3) // Wednesday
{
System.out.println
("Office hours, 2 - 5:30 PM");
}
else if ((day == 6) || (day ==
7)) // Saturday, Sunday
{
System.out.println ("Have
a nice weekend");
}
else // none of the above
{
System.out.println ("Out
of range input; try again")
}
switch(day)
{
case 1:
case 5: // Monday, Friday
System.out.println ("Work at home today");
break; // jump
to bottom of switch statement
case 2:
case 4: // Tuesday,
Thursday
System.out.println ("CSci 53, 11 AM - 12:15 PM");
System.out.println ("CSci 133, 2 - 3:15 PM");
System.out.println ("Office hours, 4 - 5:30 PM");
break;
case 3: // Wednesday
System.out.println ("Office hours, 2 - 5:30 PM");
break;
case 6:
case 7: // Saturday, Sunday
System.out.println ("Have a nice weekend");
break;
default: // anything else
System.out.println("Bad input");
}
final int MON = 1, TUE = 2, WED = 3,
THU = 4;
final int FRI = 5, SAT = 6, SUN = 7;
...
switch(day)
{
case MON:
case FRI: // Monday, Friday
System.out.println ("Work at home today");
break; // jump to bottom of switch statement
case TUE:
case THU: // Tuesday, Thursday
System.out.println ("CSci 53, 11 AM - 12:15 PM");
System.out.println ("CSci 133, 2 - 3:15 PM");
System.out.println ("Office hours, 4 - 5:30 PM");
break;
case WED: // Wednesday
System.out.println ("Office hours, 2 - 5:30 PM");
break;
case SAT:
case SUN: // Saturday, Sunday
System.out.println ("Have a nice weekend");
break;
default: // anything else
System.out.println("Bad input");
}
Sept. 9-11, 2003
Example: Average Age of Professors
Problem Specification
You're a very curious person. One thing you're curious about is the
ages of three of your professors. But how can you find out how old they
are? Some people are offended if you ask their age.
Luckily, the GW Undergraduate Bulletin has a list of (most of) the
professors in the back of the book. Each professor's listing gives
his/her degrees, showing the college or university and year of each
degree.
If we assume the typical student is 21 when (s)he earns a bachelors
degree, we can estimate a professor's age just by knowing the current
calendar year and that professor's graduation year.
You're also curious about the mean (average) age of the profs.
Algorithm Refinements
Step 2 can be refined:
Improved Algorithm
We can improve the algorithm a bit by realizing that we don't need 3
separate age variables. We can use one variable over and over, as long
as we accumulate the sum of the ages as we go along. We need a sum
variable; let's call it ageSum.
This gives a new refinement of Step 2:
2.1 Prompt the user and read in 1st professor's graduation year, then
calculate and display that professor's estimated age, and add it into
the running age sum.
2.2 Prompt the user and read in 2nd professor's graduation year, then
calculate and display that professor's estimated age, and add it into
the running age sum.
2.3 Prompt the user and read in 3rd professor's graduation year, then
calculate and display that professor's estimated age, and add it into
the running age sum.
See MeanAge2.java in the program distribution.
Sept. 4, 2003
Next week, you're driving to Long Island to visit your family. When
you
drive, you'd like to know what your average end-to-end speed, in miles
per hour, was for the trip.
You're not very good at arithmetic. Luckily, you'll have your trusty
laptop with you, so you can develop a little program that will compute
your average speed from the distance you've driven and the number of
hours the trip took.
Unfortunately, your laptop is a bit lame-brained, so it's able to
caculate only with "whole numbers" (integers).
To solve this problem, we must be given the number of miles (to the
nearest mile) and the number of hours (to the nearest hour). The
average speed (to the nearest MPH) will be the quotient of the number
of miles divided by the number of hours.
miles - the number of miles, an integer
hours - the number of hours, an integer
mph - the average speed, to the nearest integer value
Relevant Formulas
mph = miles ÷ hours
This is a very simple algorithm and needs no refinements.
Let's develop one right here! (Here is what we developed in class.)
| Test # | Miles | Hours | Reason | Expected Result | Actual Result |
| 1 | 15 | 3 | normal | 5 | |
| 2 | 10 | 0 | bad hours | error | |
| 3 | 10 | 15 | hours > miles | 0 | |
| 4 | A | 5 | nonnumeric value | error | |
| 5 | 0 | 5 | miles = 0 | 0 | |
| 6 | -10 | 5 | miles < 0 | -2 |
Start with the template - it's unnecessary to start with a blank page
mfeldman@hobbes.seas.gwu.edu:15:
mfeldman@hobbes.seas.gwu.edu:16:
cat ProjectTemplate.java
//--------------------------------------------------------------
//| ProjectTemplate.java <--
change to your project filename
//| Use this as a basis for your
own programs
//| Author: <your name> The
George Washington University
//| Last Modified: <date>
//--------------------------------------------------------------
import cs1.Keyboard;
public class ProjectTemplate
<-- change to your project name
{
public static void main
(String[] args)
{
// put your
code here
}
}
Declare the variables and copy the main algorithm steps as comments
//--------------------------------------------------------------
//| AverageSpeed.java
//| Finds average speed on a
trip.java
//| Author: M.B. Feldman The
George Washington University
//| Last Modified: September 5
2002
//--------------------------------------------------------------
import cs1.Keyboard;
public class AverageSpeed
{
public static void main
(String[] args)
{
int
miles; //
input - number of miles driven
int
hours; //
input - number of hours driven
int
mph;
// output - miles per hour
// Prompt the
user and read in the number of miles
// Prompt the
user and read in the number of hours
// Compute the
average speed
// Display the
average speed
}
}
Compile the framework; fix any errors. Then fill in the rest of the
program statements.
//--------------------------------------------------------------
//| AverageSpeed.java
//| Finds average speed on a
trip.java
//| Author: M.B. Feldman The
George Washington University
//| Last Modified: September 5
2002
//--------------------------------------------------------------
import cs1.Keyboard;
public class AverageSpeed
{
public static void main
(String[] args)
{
int
miles; //
input - number of miles driven
int
hours; //
input - number of hours driven
int
mph;
// output - miles per hour
// Prompt the
user and read in the number of miles
System.out.print
("How many miles did you drive (to the
nearest mile)? ");
miles =
Keyboard.readInt();
// Prompt the
user and read in the number of hours
System.out.print
("How many hours did you drive (to the
nearest hour)? ");
hours =
Keyboard.readInt();
// Compute the
average speed
mph = miles /
hours;
// Display the
average speed
System.out.print ("Your average speed
was ");
System.out.println (mph + " miles per hour.");
}
}
(end of document)