This game is called snakes and ladders and I hope you enjoy it! Please comment and tell me what you think. This game was made in Java purely programmed by me and it took less than 6 hours, so it's not that great. But I think it's a half-decent game for the time it took to make.
And remember, if you like this game, please be sure to click an ad to show some support! Thank you.
http://www.mediafire.com/?6fm88eq7t8s6tr7
NOTE: This game is specifically designed for screen resolutions that are 1024x768, so it might be a little messed up if your screen resolution is not that.
Monday, 19 March 2012
Monday, 5 March 2012
Game Requests..
Alright so I need your opinions on some games that you guys wanna see that could be made with the standard graphics class. So games like monopoly, one of those really old galaxy space shooter games, and games like that. So if you guys have any requests, it would be very helpful. Thank you. :)
Wednesday, 29 February 2012
Looping - Practice Answers
Question 1 Solution:
import java.util.Scanner;
class FirstLooping{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int sum = 0;
for(int i = 0; i < 100; i++){
int number = input.nextInt();
sum = sum + number;
}
System.out.println("total sum is: " + sum);
}
}
Question 2 Solution:
import java.util.Scanner;
class SecondLooping{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int sum = 0;
int average;
for(int i = 0; i < 50; i++){
int number = input.nextInt();
sum = sum + number;
}
average = sum / 50;
System.out.println("The sum is: " + average);
}
}
Question 3 Solution:
import java.util.Scanner;
class ThirdLooping{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int sum = 0;
for(int i = 0; i < 20; i++){
int number = input.nextInt();
sum = sum + number;
}
for(int i = 0; i < 10; i++){
int number = input.nextInt();
sum = sum - number;
}
System.out.println("Your total is: " + sum);
}
}
I hope you found these problem sets fairly easy because I'm sure it's going to get much more difficult later on. If you have any questions or concerns, please leave a comment and I'll be sure to respond to it as quickly as possible. If you have any java questions, even if it doesn't relate to this current material, please leave a comment and I'll reply to you as fast as possible.
import java.util.Scanner;
class FirstLooping{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int sum = 0;
for(int i = 0; i < 100; i++){
int number = input.nextInt();
sum = sum + number;
}
System.out.println("total sum is: " + sum);
}
}
Question 2 Solution:
import java.util.Scanner;
class SecondLooping{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int sum = 0;
int average;
for(int i = 0; i < 50; i++){
int number = input.nextInt();
sum = sum + number;
}
average = sum / 50;
System.out.println("The sum is: " + average);
}
}
Question 3 Solution:
import java.util.Scanner;
class ThirdLooping{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int sum = 0;
for(int i = 0; i < 20; i++){
int number = input.nextInt();
sum = sum + number;
}
for(int i = 0; i < 10; i++){
int number = input.nextInt();
sum = sum - number;
}
System.out.println("Your total is: " + sum);
}
}
I hope you found these problem sets fairly easy because I'm sure it's going to get much more difficult later on. If you have any questions or concerns, please leave a comment and I'll be sure to respond to it as quickly as possible. If you have any java questions, even if it doesn't relate to this current material, please leave a comment and I'll reply to you as fast as possible.
Looping - Practice Questions
Question 1:
Write a program that reads in 100 numbers and finds the sum of all those numbers.
Question 2:
Write a program that reads in 50 numbers and then finds the average out of those numbers.
Question 3:
Write a program that reads in 20 numbers and finds the sum of those 20 numbers. Inside the same program, make another loop that reads in 10 numbers but subtracts them from the total sum of the first 20 numbers.
Write a program that reads in 100 numbers and finds the sum of all those numbers.
Question 2:
Write a program that reads in 50 numbers and then finds the average out of those numbers.
Question 3:
Write a program that reads in 20 numbers and finds the sum of those 20 numbers. Inside the same program, make another loop that reads in 10 numbers but subtracts them from the total sum of the first 20 numbers.
Tuesday, 28 February 2012
Looping (Short and Brief)
Looping is a very useful tool inside java programming, and it's a way of accomplishing more tasks in less time. A scenerio where looping would be the most appropriate to use looping would be if you were to design a program that will read in 100 numbers, and are required to keep a running total (adding all of those numbers together). Without looping, this would take very long. But, with looping, you can accomplish this in minimal lines. It would look something like this:
import java.util.Scanner;
class RunningTotal{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int numberOfNumbers = 100;
int total = 0;
for(int i = 0; i < numberOfNumbers; i++){
int number = input.nextInt();
total = total + number;
}
System.out.println("your total is: " + total);
}
}
And that would be it. The loop shown above is known as a simple for loop. The type of loop that a for loop is is a counter-controlled loop because it only loops based on numbers. So, in the code above, we constructed a for loop to loop through 100 times. Each time it loops through, it will read in a number from the user and add it on to the total. When it's done looping, it will then break out of the loop and print out your total. If you were to write this program without looping, it would take very long and it wouldn't be very code conservative. Code conservation refers to the fact that you should use minimal lines in order to accomplish the most tasks. Looping is one way you can be code conservative.
There are 3 types of looping: counter-controlled looping, timer-controlled looping, and condition-controlled looping.
Timer-controlled looping means that it will execute every so often. An example: add a number after n seconds. I will be teaching timer-controlled looping later on inside these tutorials/tips, because at this very moment, it's not considered to be basic.
Condition-controlled looping means that you will loop through until the condition is NOT met. This means that while the condition is being met, it will keep looping. An example of this type of looping would be the while loop. Let's say you had a boolean (for those of you that don't know what a boolean is, a boolean is a statement which holds true or false, those are the only possible values for a boolean), and you set it equal to true. So it would look something like this:
boolean flag = true;
while(flag == true){
//body
}
There's only one problem with the code above, it's known as an infinite loop. This means that it will keep looping on forever because flag is always equal to true. In order to stop this, while you're performing your tasks, you would have to add in a "flag = false" inside your loop in order for the loop to break out of itself. An example of this would be something like this:
boolean flag = true;
int number = 0;
while(flag == true){
number = number + 1;
if(number == 10){
flag = false;
}
}
So, in the program above, it will keep looping through adding one to the value of number. When the number equals 10, it sets flag equal to false, meaning that the loop will not execute anymore because the condition is not being met. The only way that loop executes is only if the boolean flag is equal to true. When the number equals 10, you are setting the flag equal to false, causing this loop not to execute anymore. When a loop breaks out of itself, it goes on to the code after the last body bracket, if there is any. (It basically just continues the rest of your program).
import java.util.Scanner;
class RunningTotal{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int numberOfNumbers = 100;
int total = 0;
for(int i = 0; i < numberOfNumbers; i++){
int number = input.nextInt();
total = total + number;
}
System.out.println("your total is: " + total);
}
}
And that would be it. The loop shown above is known as a simple for loop. The type of loop that a for loop is is a counter-controlled loop because it only loops based on numbers. So, in the code above, we constructed a for loop to loop through 100 times. Each time it loops through, it will read in a number from the user and add it on to the total. When it's done looping, it will then break out of the loop and print out your total. If you were to write this program without looping, it would take very long and it wouldn't be very code conservative. Code conservation refers to the fact that you should use minimal lines in order to accomplish the most tasks. Looping is one way you can be code conservative.
There are 3 types of looping: counter-controlled looping, timer-controlled looping, and condition-controlled looping.
Timer-controlled looping means that it will execute every so often. An example: add a number after n seconds. I will be teaching timer-controlled looping later on inside these tutorials/tips, because at this very moment, it's not considered to be basic.
Condition-controlled looping means that you will loop through until the condition is NOT met. This means that while the condition is being met, it will keep looping. An example of this type of looping would be the while loop. Let's say you had a boolean (for those of you that don't know what a boolean is, a boolean is a statement which holds true or false, those are the only possible values for a boolean), and you set it equal to true. So it would look something like this:
boolean flag = true;
while(flag == true){
//body
}
There's only one problem with the code above, it's known as an infinite loop. This means that it will keep looping on forever because flag is always equal to true. In order to stop this, while you're performing your tasks, you would have to add in a "flag = false" inside your loop in order for the loop to break out of itself. An example of this would be something like this:
boolean flag = true;
int number = 0;
while(flag == true){
number = number + 1;
if(number == 10){
flag = false;
}
}
So, in the program above, it will keep looping through adding one to the value of number. When the number equals 10, it sets flag equal to false, meaning that the loop will not execute anymore because the condition is not being met. The only way that loop executes is only if the boolean flag is equal to true. When the number equals 10, you are setting the flag equal to false, causing this loop not to execute anymore. When a loop breaks out of itself, it goes on to the code after the last body bracket, if there is any. (It basically just continues the rest of your program).
Proper Naming and Semantics - Practice Answers
Question 1 Solution:
import java.util.Scanner;
class AverageProgram{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int firstNumber;
int secondNumber;
int thirdNumber;
int forthNumber;
int fifthNumber;
int sum;
int average;
System.out.println("Enter the first number");
firstNumber = input.nextInt();
System.out.println("Enter the second number");
secondNumber = input.nextInt();
System.out.println("Enter the third number");
thirdNumber = input.nextInt();
System.out.println("Enter the forth number");
forthNumber = input.nextInt();
System.out.println("Enter the fifth number");
fifthNumber = input.nextInt();
sum = firstNumber+secondNumber+thirdNumber+forthNumber+fifthNumber;
average = sum/5;
System.out.println("The average is " + average);
}
}
Question 2 Solution:
import java.util.Scanner;
class ProductOfThreeNumbers{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int firstNumber;
int secondNumber;
int thirdNumber;
int product;
System.out.println("Enter the first number");
firstNumber = input.nextInt();
System.out.println("Enter the second number");
secondNumber = input.nextInt();
System.out.println("Enter the third number");
thirdNumber = input.nextInt();
product = firstNumber*secondNumber*thirdNumber;
System.out.println("The product is: " + product);
}
}
Question 3 Solution:
import java.util.Scanner;
class MathArithmetic{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int firstNumber;
int secondNumber;
int thirdNumber;
int forthNumber;
int fifthNumber;
int sum;
int total;
System.out.println("Enter the first number");
firstNumber = input.nextInt();
System.out.println("Enter the second number");
secondNumber = input.nextInt();
System.out.println("Enter the third number");
thirdNumber = input.nextInt();
System.out.println("Enter the forth number");
forthNumber = input.nextInt();
System.out.println("Enter the fifth number");
fifthNumber = input.nextInt();
sum= firstNumber+secondNumber+thirdNumber+forthNumber;
total = sum/fifthNumber;
System.out.println("The total is: " + total);
}
}
Question 4 Solution:
import java.util.Scanner;
class SecondMathArithmetic{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int firstNumber;
int secondNumber;
int thirdNumber;
int forthNumber;
int sum;
int product;
int total;
System.out.println("Enter the first number");
firstNumber = input.nextInt();
System.out.println("Enter the second number");
secondNumber = input.nextInt();
System.out.println("Enter the third number");
thirdNumber = input.nextInt();
System.out.println("Enter the forth number");
forthNumber = input.nextInt();
sum = firstNumber-secondNumber;
product = thirdNumber*forthNumber;
total = sum / product;
System.out.println("Your total is: " + total);
}
}
So, the solutions are posted above! I hope you found these fairly simple, because I'm pretty sure it only gets harder from this point on.
import java.util.Scanner;
class AverageProgram{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int firstNumber;
int secondNumber;
int thirdNumber;
int forthNumber;
int fifthNumber;
int sum;
int average;
System.out.println("Enter the first number");
firstNumber = input.nextInt();
System.out.println("Enter the second number");
secondNumber = input.nextInt();
System.out.println("Enter the third number");
thirdNumber = input.nextInt();
System.out.println("Enter the forth number");
forthNumber = input.nextInt();
System.out.println("Enter the fifth number");
fifthNumber = input.nextInt();
sum = firstNumber+secondNumber+thirdNumber+forthNumber+fifthNumber;
average = sum/5;
System.out.println("The average is " + average);
}
}
Question 2 Solution:
import java.util.Scanner;
class ProductOfThreeNumbers{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int firstNumber;
int secondNumber;
int thirdNumber;
int product;
System.out.println("Enter the first number");
firstNumber = input.nextInt();
System.out.println("Enter the second number");
secondNumber = input.nextInt();
System.out.println("Enter the third number");
thirdNumber = input.nextInt();
product = firstNumber*secondNumber*thirdNumber;
System.out.println("The product is: " + product);
}
}
Question 3 Solution:
import java.util.Scanner;
class MathArithmetic{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int firstNumber;
int secondNumber;
int thirdNumber;
int forthNumber;
int fifthNumber;
int sum;
int total;
System.out.println("Enter the first number");
firstNumber = input.nextInt();
System.out.println("Enter the second number");
secondNumber = input.nextInt();
System.out.println("Enter the third number");
thirdNumber = input.nextInt();
System.out.println("Enter the forth number");
forthNumber = input.nextInt();
System.out.println("Enter the fifth number");
fifthNumber = input.nextInt();
sum= firstNumber+secondNumber+thirdNumber+forthNumber;
total = sum/fifthNumber;
System.out.println("The total is: " + total);
}
}
Question 4 Solution:
import java.util.Scanner;
class SecondMathArithmetic{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int firstNumber;
int secondNumber;
int thirdNumber;
int forthNumber;
int sum;
int product;
int total;
System.out.println("Enter the first number");
firstNumber = input.nextInt();
System.out.println("Enter the second number");
secondNumber = input.nextInt();
System.out.println("Enter the third number");
thirdNumber = input.nextInt();
System.out.println("Enter the forth number");
forthNumber = input.nextInt();
sum = firstNumber-secondNumber;
product = thirdNumber*forthNumber;
total = sum / product;
System.out.println("Your total is: " + total);
}
}
So, the solutions are posted above! I hope you found these fairly simple, because I'm pretty sure it only gets harder from this point on.
Proper Naming and Semantics - Practice Questions
Question 1:
Write a program that will find the average of 5 numbers, which are inputted by the user.
Question 2:
Write a program that will find the product of 3 numbers.
Question 3:
Write a program that reads in 5 numbers. It adds up the first 4 numbers and then divides that number by the 5th number.
Question 4:
Write a program that reads in 4 numbers. You subtract the first number by the second number, and multiply the third and the forth number. Now you will do: (first number subtracted by secondNumber) and divide that by (third number multiplied by forth number)
The answers will be posted inside a different post to avoid distractions.
Write a program that will find the average of 5 numbers, which are inputted by the user.
Question 2:
Write a program that will find the product of 3 numbers.
Question 3:
Write a program that reads in 5 numbers. It adds up the first 4 numbers and then divides that number by the 5th number.
Question 4:
Write a program that reads in 4 numbers. You subtract the first number by the second number, and multiply the third and the forth number. Now you will do: (first number subtracted by secondNumber) and divide that by (third number multiplied by forth number)
The answers will be posted inside a different post to avoid distractions.
Proper Naming And Semantics
When programming in Java, you want to use proper naming conventions for all of the variables you initialize and declare. You want to use a method called 'camel-casing' which is a proper naming convention inside Java, and most other programming languages. Let's say you were making a program to add 2 numbers together, the proper way of declaring these variables would be:
int firstNumber;
int secondNumber;
The reason why the example above is proper because the variable name has a direct correlation to the data that is going to be stored inside those variables. It also follows the proper naming convention for variables. The improper way of declaring these variables would be:
int firstnumber;
int secondnumber;
The reason why the example above is IMPROPER because it DOES NOT follow the proper naming conventions. Due to the fact that these variables ARE NOT instance variables (instance variables are variables which are static), you have to use camel casing. Camel casing is a naming convention where the first letter of the first word is not capitalized, and the first letter of every other word is capitalized.
So, if we were still going to make that program that adds up 2 numbers, we need to somehow read the user's input. The easiest and most basic way of doing this is using the "Scanner" class. The scanner class is a very useful class and it used for many different purposes. The location of the scanner class is inside the java.util.* package. So, at the very top of your java editor, you want to have the code:
import java.util.Scanner;
Remember, do not forget your semi-colons! So now, after having that line of code, you wanna make your class. A reasonable class name would be something around the lines of "AddingTwoNumbers". It would look something like this:
class AddingTwoNumbers{
//body
}
So, having that code, where is says "body" (which is commented out), you want to add your main method. Main method is essential to all java programs because a java program cannot run without the main method, with an exception to applets. The way the main method looks like is like this:
public static void main(String args[]){
}
the "public" & "static" are known as access modifiers. Example, another access modifier is 'private', and this means that other classes cannot use any of the variables of methods from this class, because the class is private. The "void" represents the return type. You can have just about any class as the return type, but if your return type is set to "void", this means that you are not returning anything. the "main" represents the method name. And the "String args[]" represents your parameters. Some methods can contain have a lot of parameters, there isn't really limit. So, inside the body of your main method, you want to initialize your Scanner so you can read in the input. The way you would do that is by copying the following code:
Scanner input = new Scanner(System.in);
So, when declaring/initializing variables, the very first word is usually the type of variable. So, in this case, it's a Scanner. The "input" is the variable name. the "new Scanner(System.in)", represents a constructor inside the Scanner class. The "System.in" inside the parameters mean that it's going to read in input through the basic console of your java editor. If you are using Eclipse, IntelliJ, NetBeans, Dr. Java, etc, it uses the default console in those programs. So, now you want to give the user a prompt telling them to enter the first number. It would look something like this:
System.out.println("Enter the first number");
This is now prompting the user to enter the first number, the text found within the parameters will be displayed inside the default console of your java editor. Now, we want to actually assign a value to the variable, firstNumber. This would look like this:
firstNumber = input.nextInt();
We declared firstNumber earlier on in the program so now, we have to initalize it. "nextInt()" is a method found within the Scanner class and it means that it is going to read inside a number. Now we want to prompt the user to enter the second number and then assign a value to the variable 'secondNumber'. This would look like this:
System.out.println("Enter the second number");
secondNumber = input.nextInt();
Now that both of our variables have a value, which would be a number entered by the user, we can now add them up. There are a few ways of doing this, one way would be:
System.out.println("Your total is: " + (firstNumber+secondNumber));
Or, if you feel more comfortable storing it inside another variable first and then outputting it to the screen, you can also do:
int total = firstNumber+secondNumber;
System.out.println("Your total is: " + total);
Both ways have the same effect. So now, your full program should look something like this:
import java.util.Scanner;
class AddingTwoNumbers{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int firstNumber;
int secondNumber;
int total;
System.out.println("Enter the first number");
firstNumber = input.nextInt();
System.out.println("Enter the second number");
secondNumber = input.nextInt();
total = firstNumber+secondNumber;
System.out.println("Your total is: " + total);
}
}
And that's it, you now have a program that can add 2 numbers which are inputted by the user. If you are looking for a challenge, or for some extra practice, why don't you try to create a program which finds the product of 2 numbers. (Finding the product means multiplying the 2 numbers together)
Hint: the " x " character does not mean multiply in Java!
int firstNumber;
int secondNumber;
The reason why the example above is proper because the variable name has a direct correlation to the data that is going to be stored inside those variables. It also follows the proper naming convention for variables. The improper way of declaring these variables would be:
int firstnumber;
int secondnumber;
The reason why the example above is IMPROPER because it DOES NOT follow the proper naming conventions. Due to the fact that these variables ARE NOT instance variables (instance variables are variables which are static), you have to use camel casing. Camel casing is a naming convention where the first letter of the first word is not capitalized, and the first letter of every other word is capitalized.
So, if we were still going to make that program that adds up 2 numbers, we need to somehow read the user's input. The easiest and most basic way of doing this is using the "Scanner" class. The scanner class is a very useful class and it used for many different purposes. The location of the scanner class is inside the java.util.* package. So, at the very top of your java editor, you want to have the code:
import java.util.Scanner;
Remember, do not forget your semi-colons! So now, after having that line of code, you wanna make your class. A reasonable class name would be something around the lines of "AddingTwoNumbers". It would look something like this:
class AddingTwoNumbers{
//body
}
So, having that code, where is says "body" (which is commented out), you want to add your main method. Main method is essential to all java programs because a java program cannot run without the main method, with an exception to applets. The way the main method looks like is like this:
public static void main(String args[]){
}
the "public" & "static" are known as access modifiers. Example, another access modifier is 'private', and this means that other classes cannot use any of the variables of methods from this class, because the class is private. The "void" represents the return type. You can have just about any class as the return type, but if your return type is set to "void", this means that you are not returning anything. the "main" represents the method name. And the "String args[]" represents your parameters. Some methods can contain have a lot of parameters, there isn't really limit. So, inside the body of your main method, you want to initialize your Scanner so you can read in the input. The way you would do that is by copying the following code:
Scanner input = new Scanner(System.in);
So, when declaring/initializing variables, the very first word is usually the type of variable. So, in this case, it's a Scanner. The "input" is the variable name. the "new Scanner(System.in)", represents a constructor inside the Scanner class. The "System.in" inside the parameters mean that it's going to read in input through the basic console of your java editor. If you are using Eclipse, IntelliJ, NetBeans, Dr. Java, etc, it uses the default console in those programs. So, now you want to give the user a prompt telling them to enter the first number. It would look something like this:
System.out.println("Enter the first number");
This is now prompting the user to enter the first number, the text found within the parameters will be displayed inside the default console of your java editor. Now, we want to actually assign a value to the variable, firstNumber. This would look like this:
firstNumber = input.nextInt();
We declared firstNumber earlier on in the program so now, we have to initalize it. "nextInt()" is a method found within the Scanner class and it means that it is going to read inside a number. Now we want to prompt the user to enter the second number and then assign a value to the variable 'secondNumber'. This would look like this:
System.out.println("Enter the second number");
secondNumber = input.nextInt();
Now that both of our variables have a value, which would be a number entered by the user, we can now add them up. There are a few ways of doing this, one way would be:
System.out.println("Your total is: " + (firstNumber+secondNumber));
Or, if you feel more comfortable storing it inside another variable first and then outputting it to the screen, you can also do:
int total = firstNumber+secondNumber;
System.out.println("Your total is: " + total);
Both ways have the same effect. So now, your full program should look something like this:
import java.util.Scanner;
class AddingTwoNumbers{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int firstNumber;
int secondNumber;
int total;
System.out.println("Enter the first number");
firstNumber = input.nextInt();
System.out.println("Enter the second number");
secondNumber = input.nextInt();
total = firstNumber+secondNumber;
System.out.println("Your total is: " + total);
}
}
And that's it, you now have a program that can add 2 numbers which are inputted by the user. If you are looking for a challenge, or for some extra practice, why don't you try to create a program which finds the product of 2 numbers. (Finding the product means multiplying the 2 numbers together)
Hint: the " x " character does not mean multiply in Java!
Subscribe to:
Posts (Atom)