The while and for loops are the most important types of loops, and the most commonly used, there is also an advanced for loop called the for each loop but arrays are needed for it.
The while loop is a loop that keeps doing the code inside it until it's conditional is false.
boolean end = false;
int point = 0;
while(!end){
point ++;
if(point > 10){
end;
}
}
//this will run the loop until points is 10
As well as there being a for loop which runs the loop until the conditional say x < 10 reaches false, but is written differently than the while loop and is more controllable.
int point = 0;
for(int i = 0; i < 10; i++){
point++;
System.out.println(point);
}
//this program will run the loop ten times and will print on the console numbers from 1 to 11
// this is because the value of point is increased before the print statement
Loops are allowed to be inside of each other and you can use multiple combinations of the code to achieve different outcomes.
while(!end){
for(int i = 0; i < 10; i++){
point++;
System.out.print(point);
}
System.out.println();
if(point > 30){
end = true;
}
}
//this will run the loops until point is 30, the for loop will run 30 times and the while loop 3 times
For the next loop you will need to know about arrays, which is another post in this blog. After knowing about arrays you can use the for each loop, which loops for every array object or value.
int[] array = new int[4];
for(int arr : array){
arr = 3;
}
//this sets every int of the array to the number 3
//for-each loops are useful for scrolling around arrays quickly
Friday, February 12, 2016
String[] array = {"This", "is", "an", "array"}
Arrays and matrices are essential in Java programming, and programming in general. they are groups of variables or objects that hold different data in them.
a single array holds as many variables of the same data requirements that you need.
int[] array = new int[7];
int x = 1;
for(int i : array){
i = x;
x++;
}
//this program will create an array of integers
There are different ways to instantiating an array
String[] names = {"Edward", "Joseph", "Smigla"};
for(String name : names){
System.out.print(name + " ");
}
//this code will print: Edward Joseph Smigla on the console
//you can also pull any variable from an array using the index
names[1] = "E.";
names[1] = "J.";
names[1] = "Shmigs";
for(String name : names){
System.out.print(name + " ");
}//this code will print: E .J. Shmigs on the console
//the index of the arrays is in programmer numbers 0 - 2 for arrays with 3 indexes
//always decreasing the index number by one from the amount of indexes
you can also create arrays of objects such as this:
public Integer[] array = new Integer[32];
for(int i = 0; i < array.length(); i++){
array[i] = (int)(Math.random()*32);
}//Remember to cast because Math.random() give a double //variable
// you do not need to use the for-each loop to cycle through arrays, you can also use the length()
//method, which finds the length of the array given and returns it
You can also create arrays of arrays which are called matrices or 2D arrays (you can create as many arrays of arrays as needed)
int[][] numbers = new int[5][5];//this will create a 2D array containing 25 int variables
for(int i = 0; i < array.length(); i++){
for(int j = 0; j < array[i].length(); j++){
array[i][j] = (int)(Math.random() * 25);
System.out.print(array[i][j] + " ");
}
System.out.println();
}//to give values to multiple index arrays you need multiple loops
//this will print random values from 0 - 24, 25 times (with a space in between and a new line every //five variables)
Matrices can be instantiated in different ways like the simple array.
int[][] numbers = {{1,2,3},{4,5,6},{7,8,9}};
//this instantiates a 2D array with the variables 1,2,3,4,5,6,7,8, and 9. in different levels of the array
a single array holds as many variables of the same data requirements that you need.
int[] array = new int[7];
int x = 1;
for(int i : array){
i = x;
x++;
}
//this program will create an array of integers
There are different ways to instantiating an array
String[] names = {"Edward", "Joseph", "Smigla"};
for(String name : names){
System.out.print(name + " ");
}
//this code will print: Edward Joseph Smigla on the console
//you can also pull any variable from an array using the index
names[1] = "E.";
names[1] = "J.";
names[1] = "Shmigs";
for(String name : names){
System.out.print(name + " ");
}//this code will print: E .J. Shmigs on the console
//the index of the arrays is in programmer numbers 0 - 2 for arrays with 3 indexes
//always decreasing the index number by one from the amount of indexes
you can also create arrays of objects such as this:
public Integer[] array = new Integer[32];
for(int i = 0; i < array.length(); i++){
array[i] = (int)(Math.random()*32);
}//Remember to cast because Math.random() give a double //variable
// you do not need to use the for-each loop to cycle through arrays, you can also use the length()
//method, which finds the length of the array given and returns it
You can also create arrays of arrays which are called matrices or 2D arrays (you can create as many arrays of arrays as needed)
int[][] numbers = new int[5][5];//this will create a 2D array containing 25 int variables
for(int i = 0; i < array.length(); i++){
for(int j = 0; j < array[i].length(); j++){
array[i][j] = (int)(Math.random() * 25);
System.out.print(array[i][j] + " ");
}
System.out.println();
}//to give values to multiple index arrays you need multiple loops
//this will print random values from 0 - 24, 25 times (with a space in between and a new line every //five variables)
Matrices can be instantiated in different ways like the simple array.
int[][] numbers = {{1,2,3},{4,5,6},{7,8,9}};
//this instantiates a 2D array with the variables 1,2,3,4,5,6,7,8, and 9. in different levels of the array
Monday, February 8, 2016
If, Else Statements, and Switch.
If are ways to chose conditionals this can relate directly into real life, for example the problem about milk and eggs, when you go to the store and want to buy milk and eggs you first have to know whether there are milk and eggs. This problem in code would be solved with conditional statements:int milk = 0;
boolean eggs = true;
if (eggs){
milk = 6;
}
//That indicates what the programmer in the picture thought of //when going to the store
else{
milk = 1;
}
After this you have to make up an alternative to your condition just to make sure that if the condition is false then you are able to get a controlled result this is achieved with an else statement, for the else statement you do not need a condition because one was already stated, however you can use another if statement to add a condition.
int milk = 0;
int eggs = 20;
if (eggs > 0){
milk = 6;
}else if (eggs < 0){
milk = 1;
}
//This will identify that if there is more than 0 eggs there will be 6 bottles of milk, however if there is //no eggs the you'll only buy 1 bottle of milk.
Switch statements help identify different conditionals.
int milk = (int)(Math.random() * 6);//Will randomize the amount of milk from 0-6, remember casting
int eggs;
switch(milk){
case 1: eggs = 1;
break;
case 2: eggs = 2;
break;
case 3: eggs = 3;
default : eggs = 4;
break;
}
This conditional will check the value of milk and will compare it to the different cases that it's given, which will help indicate what block of code to activate, the "break" statement helps finishing the statement, without it the program would do the cases under the one without break until finding a break or a default statement, for example in the case 3 it would assign eggs to the value of 3, but then it would do the default case which assign eggs to 4.
Friday, January 29, 2016
public class JavaClass { public static void main(String[]args){ } }
Classes and Methods:
A class is a set of rules that will define an object in a later program, they are made to hold methods which are consequently the rules that formulate every object and these methods are written with different parameters and manipulated to benefit the code.
Ex. of a class:
public class Blog{
}
this will be what contains the rules and settings to create an object called Blog. They are usually started with a capital letter and are written with brackets which are what holds the code inside of the class.
Ex. of methods:
public class Blog{
int amountOfPosts
public static void main(String[]args){
Blog myBlog = new Blog(2);
Blog myBlog2 = new Blog();
//Will do the same as myBlog
myBlog.setAmountOfPosts(3);
}
public Blog(){
}
public Blog(int a){
amountOfPosts = a;
}
}
the main method is the most important method in Java programming, it helps define where a program starts and inside of it's brackets is where the program should be defined for the JRE(Java Run-time Environment) to read and run the program. The parameter of String[]args inside the parenthesis next to main is in itself quite complicated, it is an array of Strings that are assigned to variable args, it helps the program know that it is running.
public Blog() is a method that is defined as a Constructor, it defines what an object of class Blog must have when it is defined. In this case myBlog is an object of class Blog and has an amountOfPosts of two, this is assigned in the parenthesis next to Blog when creating a new object
and is ultimately assigned on the constructor, if the parameter of an object is empty such as myBlog2 it will use the constructor that has the same parameters.
There are different ways of writing a method that have to do with encapsulation (private, public, protected) but there is different types of methods as well depending on whether you want a return or not.
public void setAmountOfPosts(int a){
amountOfPosts = a;
}
this method will not give you a return because is a void method, this means that it does not by itself hold to any data but it will modify the program in any way you want
public static String getATitle(String t){
int title = t;
return title;
}
this is a method that requires a return because it holds on to data, in this case is a String data. It is not required to use the static modifier but for this type of method it is recommended because of recursion (Will probably write another post about this theme.)
A class is a set of rules that will define an object in a later program, they are made to hold methods which are consequently the rules that formulate every object and these methods are written with different parameters and manipulated to benefit the code.
Ex. of a class:
public class Blog{
}
this will be what contains the rules and settings to create an object called Blog. They are usually started with a capital letter and are written with brackets which are what holds the code inside of the class.
Ex. of methods:
public class Blog{
int amountOfPosts
public static void main(String[]args){
Blog myBlog = new Blog(2);
Blog myBlog2 = new Blog();
//Will do the same as myBlog
myBlog.setAmountOfPosts(3);
}
public Blog(){
}
public Blog(int a){
amountOfPosts = a;
}
}
the main method is the most important method in Java programming, it helps define where a program starts and inside of it's brackets is where the program should be defined for the JRE(Java Run-time Environment) to read and run the program. The parameter of String[]args inside the parenthesis next to main is in itself quite complicated, it is an array of Strings that are assigned to variable args, it helps the program know that it is running.
public Blog() is a method that is defined as a Constructor, it defines what an object of class Blog must have when it is defined. In this case myBlog is an object of class Blog and has an amountOfPosts of two, this is assigned in the parenthesis next to Blog when creating a new object
and is ultimately assigned on the constructor, if the parameter of an object is empty such as myBlog2 it will use the constructor that has the same parameters.
There are different ways of writing a method that have to do with encapsulation (private, public, protected) but there is different types of methods as well depending on whether you want a return or not.
public void setAmountOfPosts(int a){
amountOfPosts = a;
}
this method will not give you a return because is a void method, this means that it does not by itself hold to any data but it will modify the program in any way you want
public static String getATitle(String t){
int title = t;
return title;
}
this is a method that requires a return because it holds on to data, in this case is a String data. It is not required to use the static modifier but for this type of method it is recommended because of recursion (Will probably write another post about this theme.)
Friday, January 22, 2016
int primitiveData = 0;
In Java programming, Primitive Data is the most basic storage of data. Java is an Object oriented programming language, which means that most of the programming will be aimed towards and shaped around creating and modifying objects. The first step for that is Primitive Data.
There are several different ways to identify primitive data with variables. Java like every other programming language requires an IDE(Integrated Development Environment) which is any text editor and a Compiler such as Eclipse (http://www.eclipse.org/downloads/) to build and test programs, every IDE will need a copy of the Java Development Kit (JDK) (http://www.oracle.com/technetwork/java/javase/downloads) to access it download it and the IDE should automatically find it. I personally enjoy using Eclipse because as well as being a compiler it is also an IDE as well. Every line of code should be finished with a semicolon and any comment that you want to make shall be written with two forward slashes Ex: //This is a comment.
Different ways to identify variables in Java:
int (This identifies a variable to an integer or whole number. Ex: int x = 10; this will set variable x as an integer to the number 10, creating primitive data.)
boolean (This identifier will set a variable to either a true or false, the use of this is varied but it is mostly used for when a loop or a conditional requires a boolean, but is also not necessary at that point either. Ex: boolean end = false; <= Will set variable end to false)
For the next Identifiers you'll need to understand a basic concept of bit data, which means that a bit is a number in binary system (a 0 or 1), this makes up everything in programming, but it is unnoticeable. a byte is a gathering of 8 bits and everything is saved on bytes.
double (This identifier will set a variable to any rational number and can reach up to 64 bits or 8 bytes. Ex: double x1 = 10.32; It is also not necessary to always set an identifier with a number value as a number but you are also allowed to utilize operations such as x1 = 2.3 * 14; which will output 32.2, if an int is used for this same operation it will round to the nearest whole number it will set x1 to 32.)
float (This identifier will set a variable just like a double but is then used for different purposes, and is saved as 32 bits or 4 bytes.)
The next identifier deals with characters and it consequently called a char, a char identifier is used for single characters that can be typed on your keyboard. (Ex. char a = 'A'; to identify a char variable you have to type whichever character you want to assign the variable to inside single quotes.)
Strings: a string identifier is a special one because it is a combination of a seemly unlimited amount of char identifiers. It allows for an extensive use of characters. (Ex. String b = "This is String b :)";)
there are multiple special characters that you cannot use within strings but that will be talked about at a later time.
There are several different ways to identify primitive data with variables. Java like every other programming language requires an IDE(Integrated Development Environment) which is any text editor and a Compiler such as Eclipse (http://www.eclipse.org/downloads/) to build and test programs, every IDE will need a copy of the Java Development Kit (JDK) (http://www.oracle.com/technetwork/java/javase/downloads) to access it download it and the IDE should automatically find it. I personally enjoy using Eclipse because as well as being a compiler it is also an IDE as well. Every line of code should be finished with a semicolon and any comment that you want to make shall be written with two forward slashes Ex: //This is a comment.
Different ways to identify variables in Java:
int (This identifies a variable to an integer or whole number. Ex: int x = 10; this will set variable x as an integer to the number 10, creating primitive data.)
boolean (This identifier will set a variable to either a true or false, the use of this is varied but it is mostly used for when a loop or a conditional requires a boolean, but is also not necessary at that point either. Ex: boolean end = false; <= Will set variable end to false)
For the next Identifiers you'll need to understand a basic concept of bit data, which means that a bit is a number in binary system (a 0 or 1), this makes up everything in programming, but it is unnoticeable. a byte is a gathering of 8 bits and everything is saved on bytes.
double (This identifier will set a variable to any rational number and can reach up to 64 bits or 8 bytes. Ex: double x1 = 10.32; It is also not necessary to always set an identifier with a number value as a number but you are also allowed to utilize operations such as x1 = 2.3 * 14; which will output 32.2, if an int is used for this same operation it will round to the nearest whole number it will set x1 to 32.)
float (This identifier will set a variable just like a double but is then used for different purposes, and is saved as 32 bits or 4 bytes.)
The next identifier deals with characters and it consequently called a char, a char identifier is used for single characters that can be typed on your keyboard. (Ex. char a = 'A'; to identify a char variable you have to type whichever character you want to assign the variable to inside single quotes.)
Strings: a string identifier is a special one because it is a combination of a seemly unlimited amount of char identifiers. It allows for an extensive use of characters. (Ex. String b = "This is String b :)";)
there are multiple special characters that you cannot use within strings but that will be talked about at a later time.
Subscribe to:
Posts (Atom)