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!");
    }
  }
}

Wednesday, August 31, 2011

What are you implying?

Today we reviewed the class diagrams assignment from Monday.  Everyone did an excellent job and continues to show improvement.

We also starting our review of typecasting, also known as datatype conversions.  We went over the two different methods of datatype conversions:

Implicit casting
double quotient1 = 7 / 2;

Explicit casting
double quotient2 = (double) 7 / 2;
int quotient3 = (int)(9.99 / 1.11);


Homework: Explain why, in the examples given above, quotient2 returns 3.5, but quotient3 returns 9.

Monday, August 29, 2011

Busy, busy, busy...

I can't believe we're already two weeks into school. We've covered quite a bit of material. Since we were joined by a new student today, we took the opportunity to summarize and review what we've covered thus far.
  • Object Oriented Programming (vs. linear or process-oriented programming)
    • Encapsulation, inheritance and polymorphism
  • Classes and Objects
  • Reference types and Value types, and their respective relationships to the Heap and Stack
  • Various data types: int, long, char, decimal, Boolean, string, etc.
    • Hexadecimal number system (stemming from a question about why numeric data types have the maximum values that they do)
So, to link several of these concepts together we drew up a Class Diagram about a vacation, complete with all of the Objects (classes) that would be used or needed for a vacation, then providing the necessary properties and methods for those Objects.


Homework: Create a Class Diagram describing tonight's football game (you know, the one where the Bruin's are going to stomp CAC... that one).  Make sure to identify at least five Classes, with appropriate properties and methods for each.  This is due on Wednesday. 

Sunday, August 14, 2011

Welcome back to school!

Hello all! I'm very excited about the opportunity to fill-in and teach the first part of this class, and I'm sure we'll have a great time programming and exploring object-oriented concepts together. This year you will learn all about encapsulation, abstraction and polymorphism - and how to apply them in a program.

I'm sure I'll say this in class, but please remember to bring your thumb drive to class every time we meet.

Looking forward to meeting all of you,

Joshua Carroll
Interim Technology Teacher