Exercises 7.2

 

1.

   second
   third

2.

   if ((honors) && (awards))
      goodStudent = true;
   else
      goodStudent = false;

  

3.

   goodStudent = honors && awards;

 

 

4.

/** okToSpray determines if weather conditions will allow pesticide

 * spraying.

 *

 * Receive: temperature, a double

 *          relativeHumidity, a double

 *          windSpeed, a double

 * Return:  true if temperature >= 70 degrees F and

 *                  relativeHumidity is between 15% and 35% and

 *                  windSpeed <= 10 mph

 *          false otherwise

 */

 

public static boolean okToSpray(double temperature, double relativeHumidity,

                                double windSpeed)

{

  return (temperature >= 70.0) &&

         (0.15 <= relativeHumidity) && (relativeHumidity <= 0.35) &&

         (windSpeed <= 10);

}

 

 

5.

 /** creditApproved determines if a loan should be approved.

  *

  * Receive: income, a double

  *          assets, a double

  *          liabilities, a double

  * Return:  true if income >= $25,000 or assets >= $100,000

  *                  and

  *                  liabilities < $50,000

  *          false otherwise

  */

 

 


public static boolean creditApproved(double income, double assets,

                               double liabilities)

{

   return ( (income >= 70.0) || (assets >= 100000) )

          && (liabilities < 50000);

}

 

 

6.

/** isLeapYear determines if a year is a leap year.

 *

 * Receive: year, an int

 * Return:  true if year is a leap year, false otherwise

 */

 

public static boolean isLeapYear(int year)

{

   return (year % 4 == 0) &&

          (!(year % 100 == 0) || (year % 400 == 0));

}

 

 

7.        

 /** daysInMonth determines the number of days in a month.

  *

  * Receive: ints month and year

  * Return:  number of days in month of specified year

  */

 

 public static int daysInMonth(int month, int year)

 {

    if (month == 2)

       if (isLeapYear(year))

          return 29;

       else

          return 28;

    else if ( (month == 4) || (month == 6) ||

              (month == 9) || (month == 11) )

       return 30;

    else

       return 31;

 }

 

8.        

 /** daysInMonth determines the number of days in a month.

  *

  * Receive: month, a string

  *          year, an int

  * Return:  number of days in month of specified year

  */

 

 public static int daysInMonth(String month, int year)

 {

    month.toUpperCase();

 


    if (month.equalsIgnoreCase("FEBRUARY"))

       if (isLeapYear(year))

          return 29;

       else

          return 28;

    else if ( month.equalsIgnoreCase("APRIL")

           || month.equalsIgnoreCase("JUNE")

           || month.equalsIgnoreCase("SEPTEMBER")

           || month.equalsIgnoreCase("NOVEMBER") )

       return 30;

    else

       return 31;

 }

 

 

 

Exercises 7.3

 

1. 

   switch (transCode)

   {

         case 'D':

          balance += amount;

          break;

         case 'W':

          balance -= amount;

          break;

         case 'P':

          theScreen.println("Current balance: " + balance);

          break;

 

         default:       System.err.println("*** Invalid transaction code: "

                              + transCode + "\n");

   }

 

 

2.

   switch (operatorSymbol)   // NOTE:  operator  is a keyword

   {

      case '+':

                theScreen.println(a + b);

                break;

      case '-':

                theScreen.println(a - b);

                break;

      case '*':

                theScreen.println(a * b);

                break;

      case '/':

                theScreen.println(a / b);

                break;

      default:

                System.err.println("*** Invalid operator: "

                                           + operatorSymbol);

   }

 

 

3. 

/** callLetters finds the call letters for a TV channel.

 *

 * Receive: channel, an int

 * Return:  a string containing call letters for channel

 ********************************************************/

 

public static String callLetters(int channel)

{

   switch (channel)

   {

      case 2:   return "WCBS";

      case 4:   return "WNBC";

      case 5:   return "WNEW";

      case 7:   return "WABC";

      case 9:   return "WOR";

      case 11:  return "WPIX";

      case 13:  return "WNET";

      default:  System.err.println(channel + " not available");

                return "";

   }

}

 

 

4. 

/** shippingCost finds shipping cost for a given distance.

 *

 * Receive: distance, an int

 * Return:  cost to ship an item that distance

 ********************************************************/

 

public static double shippingCost (int distance)

{

   switch (distance / 100)

   {

      case 0:          return 5.00;

      case 1: case 2:  return 8.00;

      case 3: case 4:

      case 5:          return 10.00;

      case 6: case 7:

      case 8: case 9:  return 12.00;

      default:         System.err.println("*** Invalid Distance: "

                                                      + distance);

                       return 0.0;

   }

}

 

 


5. 

/** monthName finds the name of a month.

  *

  * Receive: month, an int

  * Return:  a string containing name of month

  **********************************************/

 

 public static String monthName(int month)

 {

    switch (month)

    {

       case 1:   return "January";

       case 2:   return "February";

       case 3:   return "March";

       case 4:   return "April";

       case 5:   return "May";

       case 6:   return "June";

       case 7:   return "July";

       case 8:   return "August";

       case 9:   return "September";

       case 10:  return "October";

       case 11:  return "November";

       case 12:  return "December";

       default:  System.err.println("*** Invalid Month: "

                                          + month);

                 return "";

    }

 }

 

6.

/** daysInMonth determines the number of days in a month.

 *

 * Receive: ints month and year

 * Return:  number of days in month of specified year

 ************************************************************/

 

public static int daysInMonth(int month, int year)

{

   switch (month)

   {

      case 2:           if (isLeapYear(year))

                           return 29;

                        else

                           return 28;

      case 1: case 3:

      case 5: case 7:

      case 8: case 10:

      case 12:          return 31;

 

      case 4: case 6:

      case 9: case 11:  return 30;

 

      default:          System.err.println("*** Invalid Month: "

                                          + month);

                        return 0;

   }

}

 

7.

 

 //--- Input/Output ---

 /** read(Keyboard) using the ann Keyboard class.

  *  Receive:       theKeyboard, a Keyboard object

  *  Input:         year, an int, from theKeyboard

  *  Postcondition: myName == corresponding year name if a valid

  *                  year has been input; otherwise the data members

  *                  are unchanged.

  */

  public void read(Keyboard theKeyboard)

  {

    int inYear = theKeyboard.readInt();

    myName = yearNameFor(inYear);

  }

 

 

8.

 

//--- Input/Output ---

 /** readName(Keyboard) using the ann Keyboard class.

  *  Receive:       theKeyboard, a Keyboard object

  *  Input:         yearName, a string, from theKeyboard

  *  Postcondition: myName == corresponding year name if a valid

  *                  year name has been input; otherwise the data members

  *                  are unchanged.

  */

  public void readName(Keyboard theKeyboard)

  {

   String yearName = theKeyboard.readLine();

   if ( yearName.equalsIgnoreCase("freshman") )

     myName = "Freshman";

   else if ( yearName.equalsIgnoreCase("sophomore") )

     myName = "Sophomore";

   else if ( yearName.equalsIgnoreCase("junior") )

     myName = "Junior";

   else if ( yearName.equalsIgnoreCase("senior") )

     myName = "Senior";

   else if ( yearName.equalsIgnoreCase("graduate") )

     myName = "Graduate";

   else

   {

     System.err.println("\nAcademicYear(String): invalid name "

                                   + yearName);

     System.exit(1);

   }

 

 }

 

 


9.

 

/** creditsNeeded()returns the minimum number of course credits needed

 * a particular academic-year classification.

 */

 

public int creditsNeeded()

{

 

   if ( myName.equals("Freshman") )

     return 0;

   else if ( myName.equals("Sophomore") )

     return 27;

   else if ( myName.equals("Junior") )

     return 58;

   else if ( myName.equals("Senior") )

     return 89;

   else          // myName.equals("Graduate") )

     return 124;

 

}

 

 

 

Exercises 7.4

 

1.  This function returns the absolute value of its argument.

 

2.   If the argument is an upper-case letter, the function returns its lowercase equivalent; otherwise, it simply returns the argument unchanged.

 

3.

    aScreen.println( (month < 10 ? "0" : "") + month + "/"

                 +  (day < 10 ? "0" : "" ) + day + "/"

                 +  (year % 100 < 10 ? "0" : "") + year % 100);

 

 

 

4.

    aScreen.println( ((number < 10) ? "00" : ( (number < 100) ? "0" : "" ))

                        + number );

 

5.

   public static int smallerOf(int x, int y)

   {

      return (x < y) ? x : y;

   }

 

 

6.   (a)

   public static int largestOf(int x, int y, int z)

   {

     return (x > y) ? ( (x > z) ? x : z ) : ( (y > z) ? y : z );

   }

 

 

      (b)

   public static int smallestOf(int x, int y, int z)

   {

      return (x < y) ? ( (x < z) ? x : z ) : ( (y < z) ? y : z ) ;

   }

 

 

7.

   public static int sum(int n)

   {

      return (n > 0) ? (n * (n + 1) / 2) : 0;

   }