Wednesday, December 5, 2012

Java code for first codingbat problem

(Rough) Java code to make a java program out of the first warmup problem on codingbat.com

import java.io.*;
import java.util.Random;


public class sleepin
{
    public static void main(String[] args) throws IOException
    {
      //  prompt the user to enter their name
      System.out.print("Is it a weekday (0 or 1): ");

      //  open up standard input
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      boolean bWeekday = false;

      //  readLine() method
      try {
         bWeekday = br.readLine().charAt(0) != '0';
      } catch (IOException ioe) {
         System.out.println("IO error trying to read weekday!");
         System.exit(1);
      }


      System.out.print("Is it a vacation (0 or 1): ");

      //  open up standard input
      br = new BufferedReader(new InputStreamReader(System.in));

      boolean bVacation = false;

      //  readLine() method
      try {
         bVacation = br.readLine().charAt(0) != '0';
      } catch (IOException ioe) {
         System.out.println("IO error trying to read weekday!");
         System.exit(1);
      }


        if( sleepIn( bWeekday, bVacation ) )
        {
                System.out.println( "true" );
        }
        else
        {
                System.out.println( "false" );
        }
    }

    public static boolean sleepIn(boolean weekday, boolean vacation) {
      if (!weekday || vacation) {
        return true;
      }

      return false;

      // Solution notes: better to write "vacation" than "vacation == true"
      // though they mean exactly the same thing.
      // Likewise "!weekday" is better than "weekday == false".
      // This all can be shortened to: return (!weekday || vacation);
      // Here we just put the return-false last, or could use an if/else.
    }

}
~
~

Thursday, November 15, 2012

Great site for practicing simple Java problem solving: http://codingbat.com/

Wednesday, November 14, 2012

A website for compiling and running simple java programs:  http://www.compileonline.com/compile_java_online.php
A great free book on solving problems on algorithms:  http://larc.unt.edu/ian/books/free/poa.pdf