Custom Search

Monday, May 23, 2011

Java V.S. JavaScript

In this post we will be covering the difference between Java and JavaScript.  If you want the simple answer here it is, Java and JavaScript are two completely different things!  Java is a computer programming language to build computer applications.  JavaScript is also a computer programming language but that's where the similarities end.  JavaScript is used inside of web site coding to add additional features such as surveys while Java is used to build applications.

Both languages are OOP (object oriented programming) but JavaScript was developed by Netscape and was originally called LiveScript.  On the other hand Java was invented by James Gosling at Sun micro systems.  Another difference is that while Java can stand on its own, JavaScript(or LiveScript) needs to be placed within and HTML document to function at all.  LiveScript was renamed JavaScript because at the time of creation Java was(and still is) one of the most popular coding languages.   So JavaScript replaced the name of LiveScript for marketing reasons.

So for those of you who have heard of Java and JavaScript I hope this post clears up the confusion.  JavaScript is a web site code script and is dependant on HTML(hyper link mark-up language) to run.  Java can stand alone and is used to build applications.  Java and JavaScript are completely different!

If you have any questions on Java V.S. JavaScript feel free to leave a comment.  Also if you have an idea for a future post feel free to leave a comment.  Well, that wraps it up for this post, see you in the next!

Sunday, May 22, 2011

Intro to the switch statement

In this post we will be covering the switch statement.  A switch statement is a more efficient way to write large amounts of  if statements.  This is useful when you have a program that asks the user for age, weight, height or any other variable that differs for every person and then does something different depending on that variable.  Without switch statements you would have to write if statements for any possible weight which would take forever. That’s where the power of switch statements comes in.  Let’s begin!

import java.util.Scanner;

public class Advice_Generator {



public static void main(String[] args){

Scanner a = new Scanner(System.in);

      int weight;

      weight = a.nextInt();

     

     

      if(weight == 60){System.out.print(weight);}

      if(weight == 61){System.out.print(weight);}

      if(weight == 62){System.out.print(weight);}





}





}

As seen above, we would have to write an if statement for every possible weight.  The switch statement makes this a little easier.

import java.util.Scanner;

public class Advice_Generator {



public static void main(String[] args){

Scanner a = new Scanner(System.in);

      int weight;

      System.out.println("Enter weight");

      weight = a.nextInt();

     

     

switch(weight){

case 60:System.out.println("Eat more, you are to light!");

case 61:System.out.println("Eat more, you are to light!");

case 62:System.out.println("Eat more, you are to light!");

case 63:System.out.println("Eat more, you are to light!");

case 64:System.out.println("Eat more, you are to light!");

case 65:System.out.println("Eat more, you are to light!");

case 66:System.out.println("Eat more, you are to light!");

case 67:System.out.println("Eat more, you are to light!");

case 68:System.out.println("Eat more, you are to light!");

case 69:System.out.println("Eat more, you are to light!");

case 70:System.out.println("Eat more, you are to light!");

case 71:System.out.println("Eat more, you are to light!");

case 72:System.out.println("Eat more, you are to light!");

case 73:System.out.println("Eat more, you are to light!");

case 74:System.out.println("Eat more, you are to light!");

case 75:System.out.println("If you are a child, you are ok.");

case 76:System.out.println("If you are a child, you are ok.");

case 77:System.out.println("If you are a child, you are ok.");

case 78:System.out.println("If you are a child, you are ok.");

case 79:System.out.println("If you are a child, you are ok.");

case 80:System.out.println("If you are a child, you are ok.");

     



}





}





}

We are creating a switch statement for the variable weight and then creating different cases for whatever the variable might be.  So if you are 74 pounds or under it would say Eat more.  This could be done more efficiently with an if statement(if(weight >74){

      System.out.println("If you are a child, you are ok.");

}  )

If statements are better if you have a range you want to do something for, but if you have an application that needs to do something different each time a switch statement is better.  For example a child likes and does different things at each age so a switch statement would be better, but if you were doing a application deciding if someone had to go to the gym, and if statement would be better because at a certain range you should go to the gym.  You would go to the gym if you 5000 pounds and anywhere above so an if statement is better.  Deciding whether to use an if statement or a switch statement simply depends on the goal of the application you are trying to build.

If you have any questions on switch statements or any other topic feel free to leave a comment.  Also if you have any ideas for future posts or things that you feel I can improve on, feel free to leave a comment.  Well, that wraps it up for this post, see you in the next!

Intro to logic operators

In this post we will be covering logic operators, what they are, how to use them, and what they are most commonly used for. A logic operator is a statement that helps the computer decide what to do and when to do it. Logic operators can be used in a variety of things but are commonly used in if else statements.



When using an if else statements you have to put a condition in the parameters of the statement like so:



 public class Main {



public static void main(String[] args){

            int x=1;

            if(x==1){ System.out.println("x is 1");

                       

            }else{

                        System.out.println("x is not 1");

            }

}

}

Here you are asking the computer what x is and depending on what x is do something, but what if you have two conditions that need to be true? We can write two if statements to make something happen for both as seen below, but what if we only want it to print out “Hello World x + y” if x=1 and y=2?







public class Main {



public static void main(String[] args){

            int x=1;

            int y=2;

            if(x==1){

                        System.out.println("Hello World,x");

            }else{

                        System.out.println("Conditions are not true");

            }

           

            if(y==2){

                        System.out.println("Hello World ,y");

                       

            }else{

                        System.out.println("Conditions are not true");

            }

}

}

Well, that’s where logic operators come into play! With the power of a logic operator we can type something like this:





public class Main {



public static void main(String[] args){

            int x=1;

            int y=2;

            if(x==1 && y==2){

                        System.out.println("Hello World x + y");

            }else{

                        System.out.println("Conditions are not true");

            }

           

           

}

}



We can use a double & sign to only do something if both sides of the condition are true(x is 1 and y is 2).  Another great part is that we can add in as many double & signs as we want so if we wanted to make an example like this we could:



public class Party {



public static void main(String[] args){

            boolean tim = true;

            boolean bob = true;

            boolean jim = false;

            boolean john = true;

            boolean James = true;

            boolean Parker = false;

            if(tim == true && bob == true && jim == true && john == true && James == true && Parker == true){

                        System.out.println("Full Party!!");

            }else{

                        System.out.println("Some could not make it to the best party ever.....");

            }

           

           

}

}



Here we make a simple party application to test if everybody showed up.  We declare a bunch of boolean(true of false) names.  True if they showed up, false if they didn’t.  Some of the names are set to true some to false.  We don’t need to declare the ones that are false because if we don’t declare them true booleans have a default value of false, but we set some to false just because.  The end result will be “Some could not make it to the best party ever…….” Because some didn’t come.  How do we use logic operators some only some have to come? Well lets say that you only want your best buds to come over, but they all invited people also so really its fine if only James and Tim come over(because they are your best buds!), but its ok with you if some other friends to come over. To make a logic operator that only needs James and Tim to come over, but will accept other friends would look like this:







public class Party {



public static void main(String[] args){

            boolean tim = true;

            boolean bob = true;

            boolean jim = false;

            boolean john = true;

            boolean James = true;

            boolean Parker = false;

            if(tim == true && James == true || bob == true && jim == true && john == true){

                        System.out.println("Great party");

            }else{

                        System.out.println("Man.... hardly anyone showed up....    ): ");

            }

           

           

}





The computer will print out great party if only tim and James comes over OR if bob, jim , and john come over.  Otherwise hardly anyone came to your party.  The double pipe keys ( | ) are right below your backspace key on most keyboards and indicate an or statement.  So if this side of the argument is true do it, if the other side is true do it,  if only one side is true do it, if both are true do it.  If both sides are false don’t do it, but with the double & signs both sides must be true to continue the action.  Remember, one = sign is assigning a value two = signs are checking the value!

If you have any questions or ideas for future posts feel free to leave a comment below!  Well, that wraps it up for this post, see you in the next!

Friday, May 20, 2011

Intro to GUI

In this post we will cover basics of GUI's(graphical user interface).  In this post we will build a frame with a text area.  Well, lets begin!


import javax.swing.*;
public class Main {
public static void main(String[]args){
JFrame frame = new JFrame("The title bar");
JTextArea text = new JTextArea();
frame.setSize(300,300);
frame.setVisible(true);
frame.getContentPane().add(text);
frame.setdefaultcloseoperation(JFrame.EXIT_ON_CLOSE);
}
}
In this code we are importing the javax.swing package( the * means import everything in the package), declaring a new JFrame variable named frame(a JFrame is a frame or window), Creating a text area named text, setting the size to 300 300(300 pixels in width, 300 pixels in height), and finally getting the content pane of the frame(think of the frame as a window pane and we are painting(adding) test to fill the window pane.  When you build and compile this program no matter how to alter the dimensions of the frame the text area will always fit the size of the frame.  This is because we added text to the whole frame(in later posts we will learn how to control placement and size of things such as text areas) so the size of the frame is the size of text.

If you have any questions on GUI's or ideas for future posts feel free to leave a comment below!  Well, that wraps it up for this post, see you in the next!