SHARE
TWEET

APCS_CH8_EasyDemo.java

calcpage Mar 13th, 2013 43 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This program demonstrates the use of EasyReader, EasyWriter,
  2. // EasySound, and EasyDate.
  3.  
  4. public class EasyDemo
  5. {
  6.   public static void main(String[] args)
  7.   {
  8.     // Test EasyReader and EasyWriter:
  9.     // ==============================
  10.  
  11.     EasyReader kboard = new EasyReader();
  12.     System.out.print("Enter output file name: ");
  13.     String fileName = kboard.readWord();
  14.  
  15.     // Write a couple of lines to outputFile:
  16.     EasyWriter outputFile = new EasyWriter(fileName);
  17.     if (outputFile.bad())
  18.     {
  19.       System.out.println("*** Cannot create " + fileName + " ***");
  20.       System.exit(1);
  21.     }
  22.     outputFile.println("Metric measures:");
  23.     outputFile.printf("%2d kg = %5.3f lbs\n", 1, 2.2046226);
  24.     outputFile.close();
  25.  
  26.     // Append a line to outputFile:
  27.     outputFile = new EasyWriter(fileName, "app");
  28.     if (outputFile.bad())
  29.     {
  30.       System.out.println("*** Cannot open " + fileName + " ***");
  31.       System.exit(1);
  32.     }
  33.     outputFile.printf("%2d km = %5.3f miles\n", 1, 0.6213712);
  34.     outputFile.close();
  35.  
  36. /* outputFile contains:
  37. Metric measures:
  38.  1 kg = 2.205 lbs
  39.  1 km = 0.621 miles
  40. */
  41.  
  42.     // Read the data back from the file and display:
  43.     EasyReader inputFile = new EasyReader(fileName);
  44.     if (inputFile.bad())
  45.     {
  46.       System.out.println("*** Cannot open " + fileName + " ***");
  47.       System.exit(1);
  48.     }
  49.     String line = inputFile.readLine();  // read the first line
  50.     System.out.println(line);
  51.     int k = inputFile.readInt();  // read the second line piece by piece
  52.     System.out.print(" " + k + " ");
  53.     String word = inputFile.readWord();
  54.     System.out.print(word);
  55.     char ch = inputFile.readChar();
  56.     System.out.print(ch);
  57.     ch = inputFile.readChar();
  58.     System.out.print(ch);
  59.     ch = inputFile.readChar();
  60.     System.out.print(ch);
  61.     double x = inputFile.readDouble();
  62.     System.out.printf("%5.3f ", x);
  63.     word = inputFile.readWord();
  64.     System.out.println(word);
  65.     inputFile.readLine();         // skip the tail of the line
  66.     line = inputFile.readLine();  // read the third line
  67.     System.out.println(line);
  68.     inputFile.close();
  69.     System.out.println();
  70.  
  71. /* Screen output:
  72. Metric measures:
  73.  1 kg = 2.205 lbs
  74.  1 km = 0.621 miles
  75. */
  76.  
  77.     // Test EasySound:
  78.     // ==============
  79.  
  80.     EasySound bells = new EasySound("bells.wav");
  81.     bells.play();
  82.  
  83.     // Test EasyDate:
  84.     // =============
  85.  
  86.     int bDayMonth = 11;
  87.     int bDayDay = 3;
  88.  
  89.     EasyDate today = new EasyDate();
  90.     System.out.println("Today is " + today);
  91.  
  92.     EasyDate tomorrow = today.add(1);
  93.     System.out.println("Tomorrow will be " + tomorrow);
  94.  
  95.     EasyDate yesterday = today.add(-1);
  96.     System.out.println("yesterday was " + yesterday);
  97.  
  98.     int yr = today.getYear();
  99.     System.out.println(yr + " is a leap year: true/false? " +
  100.                                      EasyDate.isLeapYear(yr));
  101.  
  102.     EasyDate myBirthday = new EasyDate(bDayMonth, bDayDay, yr);
  103.  
  104.     if (today.equals(myBirthday))
  105.       System.out.println("Today is my birthday");
  106.     else if (yesterday.equals(myBirthday))
  107.       System.out.println("My birthday was yesterday");
  108.     else
  109.     {
  110.       if (myBirthday.compareTo(today) < 0)
  111.         myBirthday = new EasyDate(bDayMonth, bDayDay, yr + 1);
  112.       System.out.println(today.daysTo(myBirthday) +
  113.                      " days are left until my next birthday");
  114.     }
  115.  
  116.     System.out.println();
  117.   }
  118. }
RAW Paste Data
Top