Saturday, September 29, 2012

9/29/12 Instance variables in objects

In my last post, I explained what an object is in java. Objects use instance variables, which are variables that can only be accessed and changed using the object itself. If you want to find out what value an object has for a certain variable, you need to program the object to give you the variable. If you want to edit a variable, you need to program the object to change the value of that variable from an outside source. How do you do this, you may ask? Well, in this post I will teach you how to build a object such that you can let another class both  find the value of and edit its variables.

To do this, I will write out the code, and write // followed by an explanation of what it does.

public class object{

 private int a;
 private int b;
 //This creates 2 instance variables named a and b

 public object(){
  a = 0;
  b = 0;
 }
 //This defines the default value of a and b for a new instance    
 //of the class object

 public object(int newA,int newB){
  a = newA;
  b = newB;
 }
 //This defines the values of a and b for a new instance of the 
 //class object

 public void setA(int newA){
  a = newA;
 }
 //This will allow for you to change the value of a from outside 
 //the class object

 public void setB(int newB){
    b = newB;
 }
//This will allow for you to change the value of b from outside 
//the class object

 public int getA(){
  return a;
 }
 //This will return the value of a where it is needed outside of 
 //the class object


 public int getB(){
  return b;
 }
 //This will return the value of a where it is needed outside of 
 //the class object

 public String toString(){
  return a + " " + b;
 }
 //This will return a summery of all the variables in the class 
 //object to where it is called in another class

}


Hopefully you have learned a thing or two about objects, classes, and instance variables. If you have any questions, just post them on the comment section, and I will get back to you ASAP.

Sunday, September 23, 2012

9/23/12 Object basics

Java is considered a object oriented programming language, so it it all about objects. Objects are substances that have "rules" that govern them, and you can create as many of the same object as you want in your program. All of these instances of the object can be manipulated differently, but they all have the same code.  In this post, I plan to teach you about objects.

Unlike integers and other primitive data types, objects do not have a limited space that they occupy on the computer. When I was teaching you about integers, I mentioned a bunch of calculations that you can do to those integers. Objects are not like a variable, but more like a physical object. This physical object can have multiple properties that describes how it looks and acts. These properties are like variables that describe the object, so an object could have a variable that describes how tall it is, or how fast it is traveling. Physical objects can also do things like move around and eat stuff. Guess what, you can also make objects in your program do things! To make your object do things, you will need to create and code a method for what you want your object to do.

If you are still not fully understanding how an object works in java, I will give you an example. Lets say that I wanted to make an object that is a sole, I would want to make variables for its height, weight, length, position, speed, how hungry it is, and much more. Some methods that the sole would have would include eating, swimming, sleeping, and hiding.

Hopefully, you now know a little bit more about java, objects, and soles. Thanks for reading!

Sunday, September 16, 2012

9/16/12 Calling a method

When programming in java, have you ever asked yourself the question "do I relay need to type this code over and over again?". If so, welcome to the world of methods. A method is a way to easily recall a piece of code in your program whenever you need it, so if I wanted to have the computer do a large calculation multiple times with different inputs, I would use a method. In this post, I will enplane how to create methods when programming in java.

So, you may be wondering "how do I create a method?", well you will type the following code:

public static [type] [name] ( [data type] [variable name], [data type] [variable name], ...){


}
You may be wondering "what dose this mean?". We will take this one step at a time, starting with public. When you say public, you are saying that this method is available for your program to use. Java is a object oriented language, meaning that all methods and classes are actually objects unless their name contains static.  When you say static in a method, you are saying that this method is not an object in the program.

Normally when you use methods, you are asking for the computer to spit out a piece of data for you. In my last post, I briefly discussed the ways you can store data. In place of the phrase [type], you would insert the data type that you want the computer to spit out. If you do not want to computer to return any data, you would just type void instead of a data type. Where the code says [name], you simply type what you want to call the method.

In parentheses, you would enter the inputs that the code needs to operate. You do this by first typing the type of the variable, and then what you want to call it. Your method can contain as many variables as you would like it to have. You would type the code that you want the computer to run when calling this method between the curly brackets.


Friday, September 7, 2012

9/7/12 Integers

In java, there are multiple ways you can store data on the computer. These are called data types.This week, we have learned about 4 different data types. These data types are integers, doubles, chars, and strings. All of these data types are used to store different kinds of data, like you can store numbers in doubles, but you can not store words in them. Today, I will explain how to create and edit integers in java.

An integer can hold an integer, or a number that has no fractional part. In java, to create an integer you type
"int A;" where A can be whatever you want to call the integer. This will make a new integer, and to give integer A a value, you would type "A = ?;" where ? represents what you want A to equal. As an example, lets say I wanted to create a new integer, call it N, and make N be equal to 5. I would type:
int N;
N = 5;
It is that easy to define an integer. If you want to, you could also type "int N = 5;", and it would create an integer N where N is equal to 5. Now that you know all about how to create an integer, you may be wondering "why is it useful to create an integer?". It is useful to create an integer because you can change what number the integer holds within your code.

Now that you know how to create an integer, you can add, subtract, multiply, or divide to change the value of that integer. I will show you how to change the value of the integer N that I said was 5 earlier in this post.
N = N + 5; (will increase the value of N by 5)
N = N * 5; (will multiply N by 5)
N = N - 5; (will  decrease  the value of N by 5)
N = N / 5; (will divide N by 5)
There is one error that could arise from editing the value of an integer. If you have N, and you divide it by 2, the answer is two and a half. Two and a half is not an integer, so your code will not work if you try to make an integer become a number that is not one. There is a way to fix this problem. If instead of typing "N = N/2;", you type "N = (int) N/2;", N will be truncated down to 2, which is an integer. This works because by typing "(int)", you are telling the computer to give you the answer as an integer. This is all I have to tell you about integers and java, so hopefully you now know how to create and edit integers in java.

*Note: you may have noticed that at the end of each line of code, I put a semicolon. In java, you do this to signify the end of a command.