Computer Science 15-100, Fall 2009
Class Notes:  Problem-Solving Practice


Note:  You may not use Java concepts we have not yet covered, including loops (do/while/for), conditionals ("if" statements or tertiary operators (?:)), arrays, or methods from any classes in java.util.* to solve these problems.  While they may be helpful, every problem here is solvable without them.


  1. letterGrade
  2. timeString
  3. dayOfWeek
  4. nearestBusStop

  1. letterGrade
    class MyCode {
      
      // This method (that does not use "if" statements or the conditional operator
      // ?:), takes an integer score for a grade and returns a char representing
      // the letter grade for that score, where scores of 90 or higher receive an A,
      // 80-89 receive a B, 70-79 receive a C, 60-69 receive a D, and below 60
      // receive an E.
    
      public static char letterGrade(int score) {
        return 'Z';
      }
      
      public static void testLetterGrade() {
        System.out.print("Testing letterGrade... ");
        assert(letterGrade(110) == 'A');
        assert(letterGrade(100) == 'A');
        assert(letterGrade(90) == 'A');
        assert(letterGrade(89) == 'B');
        assert(letterGrade(80) == 'B');
        assert(letterGrade(79) == 'C');
        assert(letterGrade(70) == 'C');
        assert(letterGrade(69) == 'D');
        assert(letterGrade(60) == 'D');
        assert(letterGrade(59) == 'E');
        assert(letterGrade(49) == 'E');
        assert(letterGrade( 0) == 'E');
        System.out.println("Passed all tests!");
      }
    
      public static void main(String[] args) {
        testLetterGrade();
      }   
    }
  2. timeString
    class MyCode {
      
      // This method (that does not use "if" statements or the conditional operator
      // ?:), takes one parameter, the number of minutes past midnight (which you
      // may assume is non-negative) and returns a String with the current time in
      // the usual HH:MM format, including leading zeroes when necessary for the
      // minutes but not the hours (so you would print out "four minutes past
      // 9 o’clock" as 9:04.  You should not include "am" or "pm", but you must
      // handle arbitrarily large values for the minutesPastMidnight parameter.
    
      public static String timeString(int minutesPastMidnight) {
        return "42";
      }
      
      public static void testTimeString() {
        System.out.print("Testing timeString... ");
        assert(timeString(3*60+5).equals("3:05"));
        assert(timeString(3*60+59).equals("3:59"));
        assert(timeString(11*60).equals("11:00"));
        assert(timeString(11*60+59).equals("11:59"));
        assert(timeString(0).equals("12:00"));
        assert(timeString(1).equals("12:01"));
        assert(timeString(59).equals("12:59"));
        assert(timeString(60).equals("1:00"));
        assert(timeString(59).equals("12:59"));
        assert(timeString(12*60).equals("12:00"));
        assert(timeString(100*12*60+8*60+3).equals("8:03"));
        System.out.println("Passed all tests!");
      }
    
      public static void main(String[] args) {
        testTimeString();
      }   
    }
  3. dayOfWeek
    class MyCode {
    
      // This method takes a date represented by three integers,
      // the month (1-12), the day (1-31), and the year, and returns an
      // integer representing the day-of-week for that date, where
      // Sunday is 1, Monday is 2, and so on, and Saturday is 7.
      // While there are several ways to do this, you must use
      // this formula (from the most-excellent web site mathforum.org):
      //   N = d + 2m + [3(m+1)/5] + y + [y/4] - [y/100] + [y/400] + 2
      // Then the remainder when you divide N by 7 is the day-of-week,
      // where Saturday is 0 and Friday is 6.  Note that these values for
      // the days are not quite the same as those returned by this method.
      // Here is mathforum's description of the formula:
      //    "d is the number or the day of the month, m is the number
      //     of the month, and y is the year. The brackets around the
      //     divisions mean to drop the remainder and just use the
      //     integer part that you get.
      //     Also, a VERY IMPORTANT RULE is the number to use for the
      //     months for January and February. The numbers of these months
      //     are 13 and 14 of the PREVIOUS YEAR. This means that to find
      //     the day of the week of New Year's Day [of 1998], 1/1/98,
      //     you must use the date 13/1/97."
      // Note: you must make the adjustment to the month and year when
      // appropriate.  So, for example, the date of New Year's Day for
      // 1998 would be obtained in the natural way:  dayOfWeek(1, 1, 1998).
      // You may ignore the cases where the month, day, or year are out of bounds.
    
      public static int dayOfWeek(int month, int day, int year) {
        return 42;
      }
    
      public static void testDayOfWeek() {
        System.out.print("Testing dayOfWeek... ");
        // On 2/5/2006, the Steelers won Super Bowl XL on a Sunday!
        assert(dayOfWeek(2, 5, 2006) == 1);
        // On 6/15/1215, the Magna Carta was signed on a Monday!
        assert(dayOfWeek(6, 15, 1215) == 2);
        // On 3/11/1952, the author Douglas Adams was born on a Tuesday!
        assert(dayOfWeek(3, 11, 1952) == 3);
        // on 4/12/1961, Yuri Gagarin became the first man in space, on a Wednesday!
        assert(dayOfWeek(4, 12, 1961) == 4);
        // On 7/4/1776, the Declaration of Independence was signed on a Thursday!
        assert(dayOfWeek(7, 4, 1776) == 5);
        // on 1/2/1920, Isaac Asimov was born on a Friday!
        assert(dayOfWeek(1, 2, 1920) == 6);
        // on 10/11/1975, Saturday Night Live debuted on a Saturday (of course)!
        assert(dayOfWeek(10, 11, 1975) == 7);
        System.out.println("Passed all tests!");
      }
    
      public static void main(String[] args) {
        testDayOfWeek();
      }
    }
    
  4. nearestBusStop
    class MyCode {
    
      // This method takes a street number (which you may assume is positive),
      // and returns the street number of the nearest bus stop, where buses
      // stop on streets that are multiples of 8 (8th, 16th, 24th, etc).
      // So it behaves as such:
      //   nearestBusStop(11) returns 8
      //   nearestBusStop(13) returns 16
      // But what about 12th street?  It is equally far from 8th and 16th
      // streets, but riders generally head towards town (0th street), and
      // so they prefer the lower bus stop.  Hence:
      //   nearestBusStop(12) returns 8
      // You can assume there is a bus stop on 0th street, and again you
      // can ignore the case where the street is negative).
    
      // Remember: do not use conditionals, loops, arrays, etc.
      // This can be done (in one line of code!) using only what we
      // have covered Week #1's notes (in fact, using just addition,
      // division and multiplication of integers).
    
      public static int nearestBusStop(int street) {
        return 42;
      }
    
      public static void testNearestBusStop() {
        System.out.print("Testing nearestBusStop... ");
        assert(nearestBusStop(0) == 0);
        assert(nearestBusStop(4) == 0);
        assert(nearestBusStop(5) == 8);
        assert(nearestBusStop(8) == 8);
        assert(nearestBusStop(11) == 8);
        assert(nearestBusStop(12) == 8);
        assert(nearestBusStop(13) == 16);
        System.out.println("Passed all tests!");
      }
    
      public static void main(String[] args) {
        testNearestBusStop();
      }
    }

Carpe diem!