Monday, September 19, 2011

WHAT? I thought I had math on A days!

Today we started a practical exercise in class that we will continue to build on this week.  This one provides two solutions for a quadratic equation using the quadratic formula.

Note: Some of this may note look nice and neat in the blog window, but if you copy it into JCreator, it should be much easier to read.




import java.util.Scanner;


public class QuadraticCalc {


    public QuadraticCalc() {
    }


public static void main (String args[]) {
Scanner input = new Scanner(System.in);


System.out.println("This will find the possible answers for the Quadratic Formula given a, b and c.\n\n");


int a = 0; // These need to be initialize or the compiler will complain.
int b = 0;
int c = 0;


if (args.length == 3) { // If they gave values when they ran the program, use them. In Browxy this is done
// by entering them in the Arguments box. (Try "3 -10 5".)
// Otherwise, prompt for values.


a = Integer.parseInt(args[0]); // Keep in mind that 'args' is an array, and the first value in an array has an index of 0.
b = Integer.parseInt(args[1]);
c = Integer.parseInt(args[2]);
}
else {
try {
System.out.print("Enter value of A: ");
a = input.nextInt(); // If you enter a non-numeric value, it will throw an exception.


System.out.print("Enter value of B: ");
b = input.nextInt(); // Same as above


System.out.print("Enter value of C: ");
c = input.nextInt();
}
catch (Exception e) {
System.out.println("Exception thrown in try..catch block.");
}
}
System.out.println("\nPossible solutions are: ");
System.out.println("  x = " + QuadFormula(a, b, c, Operator.Addition));
System.out.println("  x = " + QuadFormula(a, b, c, Operator.Subtraction));
  }


public static double QuadFormula(int a, int b, int c, Operator op) { // Note the change I made to the signature.
// Operator is defined further down.
double answer = 0;


if (op == Operator.Addition) {
 answer = (b * -1) + Math.sqrt(Math.pow(b, 2) - (4 * a * c));
}
else if (op == Operator.Subtraction) {
 answer = (b * -1) - Math.sqrt(Math.pow(b, 2) - (4 * a * c));
}


answer = answer / (2 * a);
return answer;
}


public enum Operator { // Now, we're just having fun.  Are you familiar with enumerators?
Addition,
Subtraction
}
}

Friday, September 2, 2011

Cast down your integers where you are

Today we completed our review of casting numeric types.  We learned that when an explicit cast is involved, the cast operation performs first, then any mathmatical operations.  (This was the key to Wednesday's homework.)
We then started a review of code comments and escape character sequences, such as carriage return/line break (\n), double quotes (\"), single quotes (\'), tab (\t), and backslash (\\).
During class we discovered several online Java compilers - one of which we actually used in class.  I thought it was pretty neat.  The idea of being able to write and debug code on my iPad sounds pretty cool to me.  Among the tools that we found these stood out as being very promising:
One last note... During your X period today I wrote this little number.  Thought I'd share it with you all.  Feel free to compile it and see if it runs.  Have a good weekend.

import java.lang.*;
public class Program {

  public static class CompSciTeacher {
    public static String MoodDescription;
    public static int MinutesToCalmDown;
  }

  public static class CompSciClass {
    public static boolean wasChattingOnline = true;
    public static boolean isGoingToGetLotsOfHomeworkNextWeek;
 
}

  public static void main(String[] args) {
 
    if (CompSciClass.wasChattingOnline) {
        CompSciTeacher.MoodDescription = "poor";
        CompSciTeacher.MinutesToCalmDown = 5;
        CompSciClass.isGoingToGetLotsOfHomeworkNextWeek = true;
        System.out.println("The Computer Science class was chatting online.");
    }
    else {
    System.out.println("The Computer Science class was not chatting online.");
    }
   
    System.out.println("The Computer Science teacher is in a " + CompSciTeacher.MoodDescription + " mood.");
   
    if (CompSciTeacher.MinutesToCalmDown > 0) {
    System.out.println("It\'s going to take about " + CompSciTeacher.MinutesToCalmDown + " minutes for him to calm down.");
    }
   
    if (CompSciClass.isGoingToGetLotsOfHomeworkNextWeek) {
    System.out.println("Ouch.  Don't plan on seeing that new movie next week.");
    }
    else {
        System.out.println("Have a good weekend!");
    }
  }
}