March 19, 2024, 06:42:39 am

Author Topic: Java for beginners (2) - Learning about integers!  (Read 2862 times)

0 Members and 1 Guest are viewing this topic.

bigbeno37

  • Sr. Member
  • **
  • Posts: 318
  • We are all born with a purpose in life
    • View Profile
Java for beginners (2) - Learning about integers!
« on: May 10, 2012, 04:00:03 am »
--------------BEFOREHAND--------------

Tutorial 1: The very first steps
At http://www.opticraft.net/index.php/topic,9505.0.html
We get the JDK, Eclipse, and start coding the 'Hello World' program!

Tutorial 3: Making the calculator!
At http://www.opticraft.net/index.php/topic,9729.0.html
We 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 #2 - Learning about integers!

Hello and welcome back to my Java tutorial series! This is the second tutorial of my series, and today I will be teaching you what and how to implement integers, scanners, and get you ready for the next tutorial where we will be making a simple calculator..

Let's get started!

----------------------CODE----------------------

So, in your Eclipse application, please remove the code from last tutorial by selecting it and pressing the 'backspace' key on your keyboard. Now that we have an empty file, let's start coding.

First thing's first; what is an integer? Well, the answer to that question is that an integer is a WHOLE number. It can be 4, 1, 44, 999, 13513, even 34820938, so long as it is a whole number and does not have any decimals after it. A number WITH a decimal is called a 'Double' in Java.

So how do we implement integers and doubles into Java? Well, what we do is simple:

Code: [Select]
class Main{
     public static void main(String[] args){
          int number = 10;
     }
}

Notice how there is 'int number = 10' in the code? This means that we are making a new integer by the name of 'number', and its value is 10. If you want a more precise explanation, here it is:

int : this is telling Java that we are declaring a new integer

number : this is telling Java that we are declaring a new integer by the name of 'number'. An integer can be named anything at all, such as 'potato', 'fruit', 'optical', or even 'bigbeno37' :P.

=  : this is telling Java that we are about to tell it what 'number' will be equal to.

10 : this is telling Java that the integer 'number' is equal to 10.

So that's all good for whole numbers, but what about those numbers that have a decimal? Well, it's quite simple actually; simply replace 'int' with 'double'. Here is what it should look like now:

Code: [Select]
class Main{
     public static void main(String[] args){
          double number = 10.7;
     }
}

Here we defined a non-whole number, or double, named as 'number' and it value was set to 10.7. At this time, you may notice that the statement is underlined yellow. This is Eclipse simply telling you that the int / double is not being used. Do not worry about this right now.

At this stage, to make sure that you remember this, I would like you to try and type this into Eclipse (No copy + paste) about 2 or 3 times, to make sure you fully understand it. Also try renaming the int or double to something else, and changing the value of these ints or doubles.

Once you have done that, it is time to move on to another subject; Scanners. What are scanners? Scanners are a way for us to be able to detect what a person writes in Java. For example, with a Scanner, you can make Java detect that the person put 'OPTICAL RULEZ' and output it to the screen. But for now, let's start with the basics of Scanners.

To be able to use a Scanner, we must first do two things; we must import the Scanner in and then make an object for us to use. To do this, let's have a look at the code:

Code: [Select]
import java.util.Scanner;

class Main{
     public static void main(String[] args){
          Scanner input = new Scanner(System.in);
     }
}

If you noticed, above the declaration of the 'Main' class, we have:

Code: [Select]
import java.util.Scanner;

This is to firstly import the Scanner so that we may use it. Without importing the Scanner, we cannot use it in our code. The next thing we do is create an object. If you have a look in the code, you will see this:

Code: [Select]
Scanner input = new Scanner(System.in);

This basically means that we are declaring a new Scanner by the name of 'input'. Without doing these two steps, we will be unable to use Scanners at all.

So, we have the Scanner imported and setup, the integers / doubles set up, but what do we do with them? Well, before we move onto the calculator, let me firstly tell you how to let the user input something and then Eclipse outputting it to the screen. What we do is firstly declare a Scanner and name it, then tell Eclipse to output whatever we type in. To do this, we use the following code:

Code: [Select]
import java.util.Scanner;

class Main{
     public static void main(String[] args){
          Scanner input = new Scanner(System.in);
          System.out.println(input.nextLine);
     }
}

As you can see there, we first imported the Scanner, then made 'input' a scanner, and then told Eclipse to print off 'input.nextLine'. However, if you look closely, there are no speech marks in the println statement. The reason for this is because we are not printing off some text, but instead printing off a command which is run. What the 'input.nextLine' does is that it asks the user to type something in, then simply prints whatever you typed back at you. How it does this is by the command. We do 'input(Which is the name of the Scanner declared).nextLine'. nextLine basically means that it will record WHATEVER you type in, lest it be a number, a co-ordinate, or even a name.

Now that we have learnt about Scanners, I would like you to take a brief moment and try to copy this code out by yourself and try renaming the Scanner name. After about 2 - 3 tries of this, you are ready to end off this tutorial.

--------------(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. How do we import a Scanner

2. What is an int, and what does it stand for?

3. How do we make a 'double' that is named 'double' and has a value of 41.92?

4. What does '(Scanner name).nextLine' do?

5. Take an educated guess at how you would add two ints together

ANSWERS:

1. How do we import a Scanner
A: You do 'import java.util.Scanner;'

2. What is an int, and what does it stand for?
A: An int is a way for storing a value and stands for Integer

3. How do we make a 'double' that is named 'double' and has a value of 41.92?
A: You do 'double double = 41.92;'

4. What does '(Scanner name).nextLine' do?
A: When used in conjunction with the 'System.out.prinln();' statement, it asks the user to input something and then it outputs that variable.

5. Take an educated guess at how you would add two ints together
A: You do (int name) + (int name);

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 #2 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:
http://www.opticraft.net/index.php/topic,9729.html
« Last Edit: May 11, 2012, 11:47:09 pm by bigbeno37 »


"We are all born on this planet with a purpose in life," This quote keeps me going during the day, always trying to find that answer.

Unholy_Gibbon

  • The Nocturnal Op
  • Full Member
  • *
  • Posts: 210
    • View Profile
Re: Java for beginners (2) - Learning about integers!
« Reply #1 on: May 11, 2012, 12:45:27 am »
Ok so with the integers I'm getting this issue (sorry it's so big but when I resized it you couldn't read the text):

I've tried both "Number" and "number" just in case it was supposed to be capitalised but both of them get underlined red and give me the same problem.

bigbeno37

  • Sr. Member
  • **
  • Posts: 318
  • We are all born with a purpose in life
    • View Profile
Re: Java for beginners (2) - Learning about integers!
« Reply #2 on: May 11, 2012, 01:52:35 am »
Are you sure that they weren't underlined yellow? Because if it is then all that means is that it is not being used in the java code. By next tutorial, that should go away, but apart from that, I have no idea what could be wrong. Also, remove the 'public' from 'public class Main'


"We are all born on this planet with a purpose in life," This quote keeps me going during the day, always trying to find that answer.

bigbeno37

  • Sr. Member
  • **
  • Posts: 318
  • We are all born with a purpose in life
    • View Profile
Re: Java for beginners (2) - Learning about integers!
« Reply #3 on: May 11, 2012, 05:39:27 am »
Tutorial number 3 should be up in the following days. It will contain how to add / subtract / divide / multiply integers or doubles with each other and how to utilize the Scanner with the integers.


"We are all born on this planet with a purpose in life," This quote keeps me going during the day, always trying to find that answer.

Unholy_Gibbon

  • The Nocturnal Op
  • Full Member
  • *
  • Posts: 210
    • View Profile
Re: Java for beginners (2) - Learning about integers!
« Reply #4 on: May 11, 2012, 02:28:42 pm »
Are you sure that they weren't underlined yellow? Because if it is then all that means is that it is not being used in the java code. By next tutorial, that should go away, but apart from that, I have no idea what could be wrong. Also, remove the 'public' from 'public class Main'

Yeah I checked again, it's yellow not red. So for both the integers, doubles and scanner I'm supposed to get errors as they aren't being used in the code yet?

bigbeno37

  • Sr. Member
  • **
  • Posts: 318
  • We are all born with a purpose in life
    • View Profile
Re: Java for beginners (2) - Learning about integers!
« Reply #5 on: May 11, 2012, 10:02:36 pm »
Are you sure that they weren't underlined yellow? Because if it is then all that means is that it is not being used in the java code. By next tutorial, that should go away, but apart from that, I have no idea what could be wrong. Also, remove the 'public' from 'public class Main'

Yeah I checked again, it's yellow not red. So for both the integers, doubles and scanner I'm supposed to get errors as they aren't being used in the code yet?

In Eclipse, if something is WRONG with your code, it will be underlined red.
In Eclipse, if something is NOT USED with your code, it will ne underlined yellow.

So yes, don't worry about it. It's perfectly normal for you to get yellow underlines here.


"We are all born on this planet with a purpose in life," This quote keeps me going during the day, always trying to find that answer.