CS 1713 Practice Problems #1

Problem 1: Tracking student information

The incomplete class `StudentInfo` is used to keep track of a student's records. It contains the name, hours taken, and grade points earned by the 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 {    // fields   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   }   public String toString( )  {                                               // code here  }    //  modifiers here ...} `
• Write the code for the constructor and the three accessor methods `getName`, `getCreditHours`, and `getGradePoints` .
• Complete the `computeGPA` method. The `computeGPA` method 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.
• Complete the `isSenior` method. The `isSenior` method returns `true` if the given student has at least 125 credit hours and has a GPA of at least 2.0; otherwise, ``` isSenior``` returns `false`.
• Write the client code necessary to instantiate a student named John Goodstudent with 90 hours and 310 grade points.
• Write the client code that will print the message "John Goodstudent is a senior" or "John Goodstudent is Not a senior" depending on his status.

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;    // fields       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     } `
• Complete the constructor for the `TicketCalculator` class.
• Write the `computeTicketCost` method 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 double MALELIMIT = 27.8;   public static final double FEMALELIMIT = 27.3;   // fields   private char myGender;   private double myWeight;   // in kilograms   private double myHeight;   // in meters   // constructor   public ObesityStats( char gender, double weight, double height)   {                                                    // code here   }   // accessors here   public double getMassIndex()   {                                                   // code here   }   public void weightResults()  {                                                   // code here   }   // modifiers here } `
• Write the code for the constructor.
• Write the `getMassIndex` method. The `getMassIndex` method 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 `
• An index is 27.8 or greater for men or 27.3 or greater for women is considered obese. Write the `weightResults` method 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.
• Write the client code necessary to instantiate the following:
• A male that weighs 81.8 kilos and is 1.7 meters tall
• 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 double IDEAL = 21;   public static final double BUSHELSPERACRE = 8.4;   public static final double RATEDECREASE = 0.05;   // fields  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 } `
• Write the code for the constructor and the two accessor methods ``` getRainFall ``` and `getNumAcres`.
• Given the rainfall and the number of acres, write the `bushelsProduced` method for a given farm.

Objective: Solidify ability to write simple classes
Last revision: August 19, 2002 at 4:00 pm