--------------BEFOREHAND--------------Tutorial 1: The very first steps
At
http://www.opticraft.net/index.php/topic,9505.0.htmlWe get the JDK, Eclipse, and start coding the 'Hello World' program!Tutorial 2: Learning about integers!
At
http://www.opticraft.net/index.php/topic,9697.0.htmlWe learn about integers, doubles, and even scanners!Tutorial 3: Making the calculator!
At
http://www.opticraft.net/index.php/topic,9729.0.htmlWe use what we have learnt and summarize it to make a simple, yet good program!Changelog:
0.1: Made the first template for my Java tutorials
0.2: Improved on the template
0.3: Made a 'BEFOREHAND' section at the top of the template.
CURRENT: 0.3.1: Making all my posts have the same ending, beforehand, and question score guide.
-------------------JAVA TUTORIAL #4 - Formatting and Java Math-------------------Hey people! bigbeno37 is back and it is time to learn some more Java!
-------------------THE CODE-------------------Sorry about my not posting a tutorial for a time, but I have been busy with other topics. Anyway, let's get started. To begin, we will start with formatting the outputs. So, up to this stage you may have only been using the 'System.out.println();' statement. However, this isn't the
ONLY way to output something to the screen. If you were looking through the answers to the last tutorials' questions, then you would have seen 'print'. This basically means that instead of printing to a new line, you print next to whatever you put next.
For example:
class Main{
public static void main(String[] args){
System.out.print("Hello World!");
//This will make whatever is printed below this line printed next to it.
System.out.println("Hurro");
//If you run this program, it will output 'Hello World!Hurro'
}
}
In that code, we declared the class, made the method, etc. but at the start, we have:
System.out.print("Hello World!");
This means that if anything is outputted to the screen
AFTER this, it will be printed next to whatever is put in the parameters. That means that if we do this:
System.out.print("HelloWorld!");
System.out.println("Derp");
It will print off 'HelloWorld!Derp'.
Now, what if we wanted a space between the words to make 'HelloWorld!Derp' 'HelloWorld! Derp'? Well, that is easy. All you do is add a space:
System.out.print("HelloWorld! ");
System.out.println("Derp");
This will output 'HelloWorld! Derp' to the screen. Why does it do this? Well, if you have a look, instead of 'HelloWorld!', it is 'HelloWorld! '. So that is how you format the outputting.
Now we shall start learning basic Java Math. In the previous lesson, we had briefly covered this subject, but not entirely. In this section, we shall cover some more of Java Math. So, we know '+', '-', '/', and '*'. So what more is there to learn? Well, there is actually quite a bit more to learn.
So what happens if we want to plus two integers together AND a number? Well, we could do that two ways. The first way is:
class Main{
public static void main(String[] args){
int fnum = 52;
int snum = 43;
int answer = fnum + snum + 21;
System.out.println(answer);
}
}
Here we are defining 3 integers: fnum, snum, and answer. Answer is equal to fnum plus snum plus 21, and then we output 'answer' to the screen. The second way is:
class Main{
public static void main(String[] args){
int fnum = 42;
int snum = 21;
int answer = 0;
System.out.println(answer+=fnum+=snum+=31);
}
}
Here we are once again defining 3 integers; fnum, snum, and answer. Below these is a println statement. Inside the parameters is what we must look at:
System.out.println(answer+=fnum+=snum+=31);
Inside the parameters it has 'answer+=fnum+=snum+=31'. This basically is saying that we want to add something to 'answer' equaling the value of 'fnum' and 'snum', and also 31. Think of it like this; We are plus-ing something to 'answer'. It equals the same value as 'fnum'.
Inside the println statement can contain a great deal of information, but it cannot hold extremely comples parameters. For those, there are other commands which we will learn down the road.
To make sure that you've learnt this, try defining 5 values and adding them to a 'total' double. Do this all inside the println parameters.
--------------(Not necessary but advisable) PRACTISING YOUR LEARNT JAVA--------------To practise your Java so far, please answer these following question either in your head, on a piece of paper, or on a new word document:
1) What does the 'print' command do?
2) How can you print something like this with two different print statements: 'Hello there! How are you?"?
3) How can you divide a double to a number?
4) How would you define an integer by the name of 'firsty' and add it to a double inside the parameters of a println statement?
5) Take an educated guess at how you would output something like this: 'This is your answer: (answer)'.
ANSWERS:
1) What does the 'print' command do?
A) It makes whatever is outputted BELOW this statement appear next to it.
2) How can you print something like this with two different print statements: 'Hello there! How are you?'?
A) You would do 'System.out.print("Hello There!");' and underneath it 'System.out.print("How are you?");'
3) How can you divide a double to a number?
A) You would do 'fnum / 5'
4) How would you define an integer by the name of 'firsty' and add it to a double inside the parameters of a println
statement?
A) You would do:
int fnum = 24;
double snum = 52;
System.out.println(fnum+=snum);
5) Take an educated guess at how you would output something like this: 'This is your answer: (answer)'.
A) You would first do 'System.out.print("This is your answer: ");' then do 'System.out.print(answer)'.
SCORES:
< 3: Try to re-read this tutorial and keep more notes in your brain.
3: Okay, but perhaps try looking briefly over the tutorial and re-writing the code.
4: Good job! You are ready to move onto the next tutorial.
5: Outstanding! You are more than ready to move onto the next tutorial.
--------------END SECTION--------------This has been tutorial #4 of my Java for beginners series. Please leave a reply containing constructive criticism and if you need any help, don't fret to either PM me or leave a message on this forum. I will get back to you ASAP!
Next tutorial:
(Under development)