HOME | Overview | Syllabus | Instructor | Lectures | Laboratories | Readings | Assignments | Resources | Other Links
CS 1713 Lecture 18: Class Design Review Problems
October 11, 1999


Problem 1: Tracking student information

The incomplete class StudentInfo is used to keep track of a students records.  It
        contains the name, hours taken, and grade points earn by a student.  The class
        calculates the GPA of the student and determines if the student is a senior.  Other
        member functions are not shown.

     public class StudentInfo
     {
       // data
          private String myName;
          private int myCreditHours;
          private double myGradePoints;

       // constructor
          public StudentInfo(String name, int creditHours, double gradePoints)  {
           // code here
          }

       //  accessors

          public String GetName(){
           // code here
          }
          public int GetCreditHours(){
           // code here
          }
          public double GetGradePoints() {
           // code here
          }

          public double ComputeGPA() {
           // code here
          }

          public boolean IsSenior() {
           // code here
          }
       //  modifiers here
      }

a.      Write the code for the constructor and the three accessor functions GetName,
     GetCreditHours, and GetGradePoints.
b.     Complete the member function ComputeGPA. ComputeGPA computes the
        students grade point average by dividing grade points by the credit hours.  The
        GPA for a student with 0 credit hours should be set to 0.
c.     Complete the member function IsSenior.  IsSenior should return true if the
        given student has at least 125 credit hours and has a GPA of at least 2.0;
        otherwise, IsSenior should return false.
d.     Write the client code necessary to instantiate a student named John Goodstudent
         with 90 hours and 310 grade points.
 e.     Write the client code that will print the message "John Goodstudent is a senior" or
        John Goodstudent is Not a senior.


Problem 2: Computing ticket cost

The following incomplete class TicketCalculator is used (among other things)
        to calculate the cost of speeding tickets given by the officers of Speedtrap, Tx.  It
        contains the speed of the driver and the posted speed where the ticket was given.

    public class TicketCalculator {
    //    Class constants

          //    1-10 miles per hour over
                public static final double LOWCOST = 10.00;
          //    11-20 miles per hour over
                public static final double MIDCOST = 15.00;
          //    21 or over
                public static final double MAXCOST = 20.00;

    //    data
          private int mySpeed;
          private int myPostedSpeed;

    //    constructor
          public TicketCalculator(int speed, int postedSpeed) {
            // code here
          }

    //     accessors
           public double ComputeTicketCost() {
             // code here
           }

    // other accessors here
    // modifiers here
    }

    a. Complete the constructor for the class TicketCalculator.

    b. Write the member function ComputeTicketCost using the following criteria:

         Miles over speed limit                   Cost per mile over speed limit
         ================================================
              1-10                                           LOWCOST  (10.00)
              11-20                                         MIDCOST  (15.00)
              over 21                                       MAXCOST (20.00)
                (If the driver was not speeding return a value of 0.)


Problem 3: Analyzing body weight

The following incomplete class is used to determine if a person is obese.
         It records the gender, weight and height of a person.  The class calculates the
         body mass index and "other" statistics for a person requiring this information.

    public class ObesityStats {

    //  Class constants
        public static final MALELIMIT = 27.8;
        public static final FEMALELIMIT = 27.3;

    //  Data
        private char myGender;
        private double myWeight;  // in kilograms
        private double myHeight;   // in meters

    // constructor
        public CDC( char gender, double weight, double height) {
           // code here
        }

    // accessors here

        public double GetMassIndex() {
          // code here
        }

        public void WeightResults(){
         // code here
        }
    // modifiers here
    }

a.      Write the code for the constructor.
b.     Write the member function GetMassIndex that returns the body mass index
        given the height and weight in meters and kilograms, respectively. The U.S.
        CDC(Centers for Disease Control) determines obesity according to a
        "body mass index," computed by
                                                            Weight in kilograms
                                             index =   -----------------------
                                                              (Height in meters)2

c.     An index is 27.8 or greater for men or 27.3 or greater for women is
        considered obese. Write the member function WeightResults that prints the
        message "obese" or "Not obese".  The character gender will contain either an 'f' or
        'F' for female or a 'm' or 'M' for male.  You may call the function GetMassIndex
        and assume it works as intended.
d.     Write the client code necessary to instantiate the following:
         1.     A male that weighs 81.8 kilos and is 1.7 meters tall
         2.     A Female that weighs 59 kilos and is 1.57 meters tall.


Problem 4: Analyzing production based on rainfall

The following incomplete class ProductionIndicator is used to predict the
        seasonal wheat harvest for acreage based on the average rainfall.  Under ideal
        conditions of  21 inches of rain a single acre will produce 8.4 bushels of wheat.
        For every inch of rain above or below 21, production is decreased 5%.

    public class ProductionIndicator {

    //  Class Constants
        public static final IDEAL = 21;
        public static final BUSHELSPERACRE = 8.4;
        public static final RATEDECREASE = 0.05;

   //   data
        private double myRainFall;
        private double myNumAcres;

   //   constructor
        public ProductionIndicator(double rainFall, double numAcres) {
            // code here
        }

   //   accessors
        public double GetRainFall() {
          // code here
        }

        public double GetNumAcres {
          // code here
        }

        public double BushelsProduced () {
          // code here
        }
        //  modifiers here
    }

a.     Write the code for the constructor and the two accessor functions GetRainFall
        and GetNumAcres.

b.     Given the rainfall and the number of acres, write the member function
        BushelsProduced for a given farm.
 
 


Objective: Solidify ability to write simple classes
Last revision: October 3, 1999 at 10:30 am