Learn yo java - Java Tutorials
This site is designed to spread the java loooove!!! If you want to get started on java coding then you've come to the right place. Just go to my tutorials page and it'll get you going right away. Thanks!!!
Sunday, December 19, 2010
Sorry, again!
Well I've kinda forgotten about my blog for a while. I'm gonna try to keep on posting and maybe introduce new concepts with some games!
Tuesday, October 12, 2010
11. Explanation on GuessingRandomNumber class
Okay so I've just posted the class code here in case you didn't want to download it over at the source code page. Copy and paste it then look at the comments, they will explain what is going on!!!
- import java.util.Scanner;
- import java.util.Random;
- public class GuessRandomNumber
- {
- {
- //Create a new scanner object called input
- //Create a new random object called generator
- //Here we will count how many guesses the user has
- int guessCounter = 0;
- //This boolean is true if the user hasn't won
- boolean userHasNotWon = true;
- //This sets the number that the user is trying to guess to a number from 0-20
- int answer = generator.nextInt(20);
- //This while loop will run while the user hasn't won
- while(userHasNotWon == true)
- {
- //Here, we ask the user from between 0-20 and assign it to an int called guess
- int guess = input.nextInt();
- //If the guess is over twenty then we tell the user
- if(guess > 20) {
- }
- //If the guess is less than 0 then we tell the user
- if(guess < 0) {
- }
- //If the guess is in the right range then we check whether they were right or not
- if(guess >= 0 && guess <= 20) {
- //If the guess is too high then we tell the user and increment the guess counter by one
- if (guess < answer) {
- guessCounter += 1;
- }
- //If the guess is too low then we tell the user and increment the guess counter by 1
- if (guess > answer) {
- guessCounter +=1;
- }
- /**If the user guesses the right number then we tell them they won and
- tell them how many times they had to guess**/
- if (guess == answer) {
- userHasNotWon = false;
- }
- }
- }
- }
- }
Monday, October 11, 2010
10. Random numbers
So our super-cool user input program is, well, super-cool but we want to make a game. I mentioned making a where the user has to guess the number and you tell the user if the number is too high or too low. We will also count the number of times they guess and print that out at the end. However, if we want this game to be truly fun then we can't have the user guessing the same number over and over. To solve this problem we are going to use random numbers!
To get the code for the random numbers (much like getting the code for the Scanner) you have to import it like so:
import java.util.Random;
So that's that and now to use the code. In order to create a Random object you do this:
Random generator = new Random();
This creates a new Random object called generator where you can get random numbers from. To get a random number from it you use this function:
generator.nextInt(10);
This will get a random number between 0 and 10. So it gets an integer so to be able to use the number you have to assign it to an integer like so:
int r = generator.nextInt(10);
Now the integer r has a random number between 0 and 10! Now, I have a challenge for you. Try making the guessing number game I described above! Don't worry, I'll have the code in the source code section and you can download it from there but try making your own and sending it to me at drenguinp@gmail.com or you can post it in the comments.
Hint: Use a while loop that goes while the user hasn't guessed correctly. Also, use if loops to check if the user has guessed correctly or not.
What do you want tutorials on?
I will continue with my regular basic java tutorials and hope to keep that going for a while but I was wondering what you guys would like a tutorial on? I will try to make a tutorial for anything you want just leave a comment telling me what you want!
Sunday, October 10, 2010
9. Comments
So here's a super quick tutorial about comments, this is just because it is important and super useful and I have a little bit of time so anyways. Comments are a way for programmers to keep track of what they are programming. The computer doesn't pay attention to them so they are really useful for keeping track of stuff.
To do a single-line comment you do this:
//this is a single line comment
To do a multi-line comment you do this:
/** This is a multiple line comment
I can write on more than one line
and the computer will still not
care what I write!!!!!!**/
You can use comments to label certain parts of your program such as:
//this for loop runs from 1 to 10
See how this makes it easier for the programmer to understand what if going on in the program? It is good programming practice to leave comments so that you actually understand what you are writing!
That was a super-quick tutorial, I will post more about the random numbers and such tomorrow (hopefully!).
See you then!
To do a single-line comment you do this:
//this is a single line comment
To do a multi-line comment you do this:
/** This is a multiple line comment
I can write on more than one line
and the computer will still not
care what I write!!!!!!**/
You can use comments to label certain parts of your program such as:
//this for loop runs from 1 to 10
See how this makes it easier for the programmer to understand what if going on in the program? It is good programming practice to leave comments so that you actually understand what you are writing!
That was a super-quick tutorial, I will post more about the random numbers and such tomorrow (hopefully!).
See you then!
8. The if and else statement
Okay so now that you can read input from the user and do loops and you know about variables and all sorts of stuff it's time to learn about if statements and these things are awesome. So here a quick example of an if statment:
if(I am hungry) {
eat food;
}
Basically you just check if something is true and if it is then you do whatever follows. Fairly simple. So lets use the user input program that we had, if you don't have it you can follow tutorial #7 and that should catch you up or you can download the code from the source code page on my site. Anyways, open up the Userstuff java program and change it to this:
import java.util.Scanner;
public class Userstuff
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Please input your name and then click enter: ");
String name = input.nextLine();
System.out.print("Please input you age and then click enter: ");
int age = input.nextInt();
System.out.println("Hello " + name + " you are " + age + " years old");
if (age < 16) {
System.out.print(" and you're still a kid!");
}
}
}
So here we are just asking for the name and age, printing it out, and then checking if the person is still a kid. If they are then you print out "and you're still a kid!" This is pretty straightforward and pretty simple, maybe too simple. What about if they are older than 16, you could do a second if statement but that just seems too complicated. Luckily java already has an answer and is the else statement, lets use my earlier example to explain it better!
if(I am hungry) {
eat food;
}
else {
don't eat food;
}
So here we check if I am hungry, if I am I eat food but if I'm not I don't eat food. So lets use this else statement for the Userstuff program. After the if statement add this:
else {
System.out.print(" and you now have responsibilities!");
}
A little insulting but that's not the point. Now our program will print out " and you now have responsibilities!" if the person is 16 or older.
So now that you know about the if statement try to make a simple game. Something where people try to guess a number and you could tell them whether it's too high or too low using if statements!
By the way, I have not made a tutorial about using > or < signs and checking if things are true or not. I will make a tutorial about these next and maybe one about random numbers so we can make a sweet number guessing game! See you next time!
public class Userstuff
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Please input your name and then click enter: ");
String name = input.nextLine();
System.out.print("Please input you age and then click enter: ");
int age = input.nextInt();
System.out.println("Hello " + name + " you are " + age + " years old");
if (age < 16) {
System.out.print(" and you're still a kid!");
}
}
}
So here we are just asking for the name and age, printing it out, and then checking if the person is still a kid. If they are then you print out "and you're still a kid!" This is pretty straightforward and pretty simple, maybe too simple. What about if they are older than 16, you could do a second if statement but that just seems too complicated. Luckily java already has an answer and is the else statement, lets use my earlier example to explain it better!
if(I am hungry) {
eat food;
}
else {
don't eat food;
}
So here we check if I am hungry, if I am I eat food but if I'm not I don't eat food. So lets use this else statement for the Userstuff program. After the if statement add this:
else {
System.out.print(" and you now have responsibilities!");
}
A little insulting but that's not the point. Now our program will print out " and you now have responsibilities!" if the person is 16 or older.
So now that you know about the if statement try to make a simple game. Something where people try to guess a number and you could tell them whether it's too high or too low using if statements!
By the way, I have not made a tutorial about using > or < signs and checking if things are true or not. I will make a tutorial about these next and maybe one about random numbers so we can make a sweet number guessing game! See you next time!
Friday, October 8, 2010
7. Reading Input from the user!!!!!!
Okay so now that you know a couple things about java it's time to interact with your user!
In order to do this you need to use a Scanner which "scans" in what the user enters! In order to use this scanner you need to have the code for it, don't worry you don't have to download some random code from some site you don't know, it's all on your computer already you just need to access it. For regular java stuff (like what I've already told you) you don't need any special classes or code, this Scanner is something not included with the regular java stuff.
So enough of that, the code that you use to access the Scanner code is this:
import java.util.Scanner;This will let you import the Scanner stuff you need. Now you have to tell the computer you need to import before you actually do anything so this code HAS TO BE AT THE VERY TOP OF YOUR CLASS!!!!!! Before you write anything put this at the very tippity-top.
In order to set up your scanner you need to do something like this.
Scanner input= new Scanner(System.in);This creates a new scanner object which you can read data or input from. (You don't have to call your scanner "input", you could name it "myScanner" or something and it wouldn't make a difference) So now that you have your scanner here's how you read in some code.
To read in the next line of input you do:
String myStringNameGoesHere = input.nextLine();To read in the next integer you do this:
Int myIntNameGoesHere = input.nextInt();To read whatever is inputted next (basically the next word) you do this:
String myStringNameGoesHere = input.next();
Okay so lets put it into action, here is the code for a simple java program that reads in you name and your age and then prints it out again:
import java.util.Scanner;
public class Userstuff
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Please input your name and then click enter: ");
String name = input.nextLine();
System.out.print("Please input you age and then click enter: ");
int age = input.nextInt();
System.out.println("Hello " + name + " you are " + age + " years old!");
}
}
public class Userstuff
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Please input your name and then click enter: ");
String name = input.nextLine();
System.out.print("Please input you age and then click enter: ");
int age = input.nextInt();
System.out.println("Hello " + name + " you are " + age + " years old!");
}
}
Create a new class called Userstuff and paste this code into it, hopefully it should work, if not post a comment and I'll try to help you out. Next I'll show you how to use the if statement so you can tell the user different things depending on what they enter! Stay tuned.
EDIT: Now unfortunately this doesn't seem to accommodate long lines of stuff so the code looks really weird. If you copy and paste it it should look fine but if not then look at my source code section and download the source code for this.
Or, if your feeling really brave, try writing it in!!!!!
EDIT: Now unfortunately this doesn't seem to accommodate long lines of stuff so the code looks really weird. If you copy and paste it it should look fine but if not then look at my source code section and download the source code for this.
Or, if your feeling really brave, try writing it in!!!!!
Subscribe to:
Posts (Atom)