Search This Blog

Monday, February 28, 2011

Logical Programming Template, where to begin

I now have some of the basic and fundamental concepts behind me.  I also have some basic syntax, semantics and pragmatics behind me.  I believe my confusion is in not having a template "in my mind" of how and where to start after the problem domain has been defined.  I have come up with a basic template that I will try and use as a guide for the next little bit.  Cam, feel free to revise and add opinions.

1)  Create object classes
           
         --  Initialze and declare object properties
         --  Create methods that define properties of that object

2)  Create the main method

         --  Define objects and their arguments
         --  Create instance(s) of a method(multiple methods)
         --  Pass instance(s) to the method(s)

3)  Create method(s)

         --  Perform data conditioning
         --  return control back to calling method

Almost time for MySQL

Almost time to practice the small amount of training I have had with MySQL database. It also ties in a Java front end to call and manipulate the database from Java. Check it out . . . .
http://technicalfacilitation.com/learnwiki/index.php/Mysql

Saturday, February 26, 2011

Assignment

Cameron said... Here's an assignment for you. This is important to do, because it will really hit where you're getting frustrated - working with methods, both creating them and passing values to them. Give it a try!

I didn't compile the code in the examples, so you might want to make sure I haven't missed a semi-colon or anything. Regardless, the objective remains the same: create the methods that I've asked for, and write some code that invokes the methods by passing in the appropriate objects! This will be eye-opening for you.

****************************

Let's look at three core shapes: Circle, Point and Rectangle.

Here's how we create them:

Circle drum = new Circle(2);

Circle life = new Circle(10);

Point ed = new Point(0,0);

Point pelee = new Point(5,5);

Rectangle r1 = new Rectangle(2,3);

Rectangle r2 = new Rectangle(5,4);


If I wanted to just print out the area of a Circle, I could create a method like this:

public class PracticeMethods {
public void printCircleArea(Circle c) {
System.out.println("The area of the circle you passed me is: " + c.getArea();
}
}

To run this code, I could write a class like this:

public class PMRunner {

public static void main(String args[]) {
Circle drum = new Circle(2);
PracticeMethods pm = new PracticeMethods(); //create an instance
pm.printCircleArea(drum); //pass an instance of a Circle to the method.
}

}

When the PMRunner executes, it will print out: The area of the circle you passed me is: 6

So, here you see how to code a class that has a method that takes an argument. (The PracticeMethods class)

And you also see how to create an instance of a class, and call one of the instance methods, while also passing in an argument of the proper type.

Question: Why does the printCircleArea return void and not a String? Doesn't the method return a String?

In the PracticeMethods class, create the following methods:

compareCircleToCircle - it takes two circles as arguments. It returns true or false depending on whether the two circles have the same area.

comparePointToCircle - it is passed in a Point and a Circle. It prints out how much bigger the area of the circle is than the area of the point.

compareCircleToSquare - it is passed in a Circle and a Square. It returns a String that says "The circle is bigger than the square" or "The square is bigger than the circle" or "The circle and square are the same size."

compareSquareToSquare - it is passed in two Squares, and returns a boolean value indicating if the first square is bigger than the second. It also returns the difference in the sizes of the two squares.

Monday, February 21, 2011

Primitive Data Types

Did Chapter 6 of:


SCJA—Sun Certified Java Associate Certification Study Guide for Java 5, Java2EE and J2ME Technology form ExamScam.com

this morning.  I created an ExamField Java project in Eclipse to run ExamField. This way I could compile, run and debug scenarios in real time.  I learned a couple of things.  The primitive data type CHAR must go inside single quotes.
 
------------------------------------------------------------------------------------------
public class TestFieldChar {

    public static void main(String[] args) {
        char singleCharacter = 'a';
        System.out.println("Test Field");
        System.out.print(singleCharacter);

    }

}

OUTPUT:

Test Field
a

 ---------------------------------------------------------------------------------------

I thought that there may be an issue when compiling the output from the following example for BYTE:

CODE:

 public class TestFieldByte {

    public static void main(String[] args) {
        byte b = 12;
        System.out.println("Test Field");
        System.out.print(b * b);

    }

}


OUTPUT:

Test Field
144

I presumed that the compiler would flag the output and go out of scope, as the maximum value for BYTE is 127.  12 * 12 is 144 and out of scope for BYTE data types.  In this case, all was well.  However, if you had a variable of type byte assigned to the output of b * b, you would go out of scope.

byte result = b * b, then result would be out of scope as it has been declared data type byte. 

-----------------------------------------------------------------------------------

BOOLEAN data types have no quotes, single quotes or bracketing when assigning.  Only possible values are true and false.

CODE:

public class TestFieldBoolean {

    public static void main(String[] args) {
        boolean condition = true;
        System.out.println("Test Field");
        System.out.print(condition);

    }

}

OUTPUT:

Test Field
true

------------------------------------------------------------------------------------

Saturday, February 19, 2011

New Project - Online Exam, Logic Diagram

Cam and I have taken some time out the last couple of days to chart the logic diagram for the online exam we are about to program and publish.  I have had some differences of opinion in the logic flow and, as Cam stated, there is more than one way to code the program.  I am trying to pay attention and grasp and understand his logic flow, which in itself is pretty easy.  Towards the end, Cam's experience became apparent with the addition of certain classes to help organize this flow for multiplicity reasons later on.  I will try and get a good copy, scan it and publish it here . . .

Wednesday, February 16, 2011

Java EE SDK, GlassFish Server and Eclipse IDE for EE

Just finished downloading the EE SDK (which contains the GlassFish Server) and Eclipse IDE for EE.  Cam showed me how to create an HTML and Java logic page, host it to a local server, create some tables in HTML and use Java to create buttons and a dialogue box. 

Awesome stuff, I feel I am one step further to grasping classes, methods, constructors, creating instances of such and passing data.  I believe something has clicked in my linear programmed head regarding objects NOT being variables or confusing objects with being a part of Java syntax.  An object is an object!  A tangible, manipulative thing which has methods that are used to describe that object, using variables and parameters.  Classes are ways to organize and sort these objects.

It took my awhile to get this, Cam had stated that I was doing well with the "hard" concepts, but missing out on this easy, essential and important concept of Java.
    

Sunday, February 13, 2011

Next assignment - space shuttle course alteration. JJ, Plot a line using two points

Master Jedi has asked I build a new class to plot a line using the classes we have been building regarding points, lines, rectangles, squares and triangles.  Hint - you need two sets of double parameters.  x1, x2 (starting point of line) and y1, y2 (ending point of line). 

Saturday, February 12, 2011

Specialization, abstraction, aggregation and inheritance

Tough night.

I believe my head is in the right place regarding concepts, but syntax and nomenclature have been huge pitfalls.  I am getting confused with when and where to initialize and declare primitive data types.  Let me stew on things for a day before I give the Larry Gutt special definitions of specialization, abstraction, aggregation and inheritance.  I have a 'feel' for the definitions, but need to visualize and piece them together with the concepts being taught.

Wednesday, February 9, 2011

Mock SCJA test

I took a mock SCJA cert test for fun this morning at Java Ranch.

http://www.coderanch.com/how-to/java/ScjpMockTests

I have literally no theory or reading behind me yet.  I scored 18 out of 36 over three sets of twelve.  Some of my corrects, I am sure, were guesses.  There are many questions on classes.  I have to concentrate my learning there in my own time. 

Tuesday, February 8, 2011

Constructor Methods

Tonight's training was a couple of hours of being introduced to Constructor Methods, in particular, pertaining to Squares, Rectangles and returning the area's of each (we were sending the variables for length and width).

Very clean.  Very cool.

Looping

Have some interesting things happening while looping through my RPS program to achieve a program run count of 100.  Trying to figure it out without asking for help or looking online.  I had also based an equation on total count but have rem'ed it out to concentrate on the loop.

Here's my code (excuse pragmatics, I am concentrating on concepts and debugging, not readability.)


import java.util.*;

public class AdvancedRPSLoopCounter {

  public static void main(String args[]) {

      
        /* Variable declarations */      


        String player1 = null;
        String player2 = null;
        int p1win = 0;
        int p1lose = 0;
        int p2win = 0;
        int p2lose = 0;
        int tie = 0;
        String result = null;

    /* Loop one hundred times */


    for (int i=0;i<101;i++) {
  

        /* Create player 1 absolute random 0 - 2 */


        Random r1 = new Random();
        int randomInt1 = Math.abs(r1.nextInt() % 3);
  

        /* Assign player 1 variable */


      
        if(randomInt1 == 0) {
            player1="rock";
        }
        if(randomInt1 == 1) {
            player1="paper";
        }
        if(randomInt1 == 2) {
            player1="scissors";
        }



        /* Create player 2 absolute random 0 - 2 */
      


        Random r2 = new Random();
        int randomInt2 = Math.abs(r2.nextInt() % 3);


      
        /* Assign player 2 variable */



        if(randomInt2 == 0) {
            player2="rock";
        }
        if(randomInt2 == 1) {
            player2="paper";
        }
        if(randomInt2 == 2) {
            player2="scissors";
        }



        /* Tie Condition */


      

        if (player1.equals(player2)) {
            result="Tie";
            tie = tie +1;

        }  

      

        /* Win/Lose Conditions */



        if (player1.equals("rock")) {
          
            if (player2.equals("paper")) {
                result="Player 2 Wins";
                p2win = p2win +1;
            }
            if (player2.equals("scissors")) {
                result="Player 2 Loses";
                p2lose = p2lose +1;
            }
        }

  
        if (player1.equals("paper")) {
            if (player2.equals("rock")) {
                result="Player 2 Loses";
                p2lose = p2lose +1;
            }
            if (player2.equals("scissors")) {
                result="Player 2 Wins";
                p2win = p2win +1;
            }
        }

  
        if (player1.equals("scissors")) {
            if (player2.equals("rock")) {
                result="Player 2 Wins";
                p2win = p2win +1;
            }
            if (player2.equals("paper")) {
                result="Player 2 Loses";
                p2lose = p2lose +1;
            }

        }


  
        /* Print to standard output per game result, print game counter */


   
        System.out.println(result);
        System.out.println("Counter: " + i);
          System.out.println(" ");
      
      
          }



        /* Print total ties, wins and loses */

        /* p1win = i - (p2lose + tie); */
        /* p1lose = i - (p2win + tie); */

      
      
        System.out.println("\n \t Number of ties: " + tie);
        System.out.println("\n \t Player 1 Wins: " + p1win);
        System.out.println("\n \t Player 1 Loses: " + p1lose);
        System.out.println("\n \t Player 2 Wins: " + p2win);
        System.out.println("\n \t Player 2 Loses: " +p2lose);
              

    }


}

The output from my program is as follows:

Counter: 100
Number of ties: 39
Player 2 Wins: 34
Player 2 Losses: 28

Some interesting things to note.  When I go to execution start, the first line count begins at 5.  The program is only looping 96 times.

Will get back later when I figure out the solution or break and ask for help . . . .

Monday, February 7, 2011

RPS Project

I have been assigned the task of coding the RPS (Rock/Paper/Scissors) game.  As Cam stated, it ties some of the most important elements, logics and concepts of programming together. 

I am brand new at the semantics and pragmatics of Java, JSP (Java Server Page) and OOP (Object Oriented Programming), but my past experience is turning out to be a real shining star.  Many of the the concepts are the same and the if and for syntax statements are exactly the same.  I already understand variable declaration, nesting, loops and passing data.

Trying to type out as much code as possible without blasting out to Google and cutting and pasting existing code.  I am finding the whole bracketing concept quite easy to logically segregate different functions, BUT THEY ARE EVERYWHERE in the code!!!  My most prevalent compiling error so far has been parsing errors.

So, as of right now, with the help of Cam supplying concepts and syntax where needed, the first copy of a standalone version of RPS is completed, along with a loop of 3 times and a counter.  I am about to tear up the code and try and loop for a greater number of runs, randomize the user input (player 1 = server, player 2 = user) to allow for non-user input and begin keeping track of the number of wins, the number of losses and the number of ties.

The Beginning

Well, this is the start of another journey.  Cameron McKenzie, an old friend of mine from highschool, who is well established in the Java programming community and the author of a multitude of Java certification and technical source books, has taken me under his wing and started my tutelage.

I previously had experience with Basic and VBasic programming languages years ago, but the whole OOP environment has soared since the days of linear code.  I am currently being taught Java and JSP (Java Server Page).

As it happened when I was younger, I AM HOOKED yet again.  Waking up after sleeping for two hours at 1 in the morning and being incapable of not loading up my JDK and working on new tasks being setup by Cameron.

It is a very exciting time and I believe my past computer knowledge and interest have set me up for what is to come.