9
And we're back! Welcome to episode 10 of Java Text Tutorials!
Table of Contents:
10. So what it this calculator?
10.1 Creating the Scanner
10.2 Creating the Variables
10.3 Using Scanner and Variables to Make a Basic Calculator
10. So what is this calculator?
This 'basic calculator', can only be used by the person with it open in eclipse. Therefor it is not ideal to make a full program based on it. But it works for a tutorial. We might get to that point though ;). What this does, is it recieves input from the user (you), adds it together and prints the answer. The answer can only be a number, or it will throw an error. As we start making the calculator, it will begin to make sense.
10.1 Creating the Scanner
This is a fairly easy task even for beginners. A Scanner is like an object, and as we mve on you will begin to see that many times you want to use a Java class, that you will use an object. It is done like this:
Scanner s = new Scanner(System.in);
What does this mean? It is, technically, creating an object of class Scanner and calling s. Then it sets s equal to a new Scanner. This tells Java that this is new, not something already in your code. System.in is a bit different. It is telling the console to recieve an input from the user. The program will not continue until you do such. As for now, it will not be recieving an input, but that will come. Make sure to include this in your main method not anywhere else.
10.2 Creating the Variables
This is an easy task. You can do two things.
1. Create 3 integers
or
2. Create 3 doubles
If you choose to do three integers, keep in mind they cannot have decimals. Neither of them can contain any letters or symbols, only numbers. I will be using three doubles. So this is my setup so far:
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
double firstNumber;
double secondNumber;
double answer;
}
See the names of the double? There is firstNumber, secondNumber, and answer. These will be the numbers that we will be using. Neither of them should equal anything yet.
10.3 Using Scanner and Variables to Make a Basic Calculator
So after the variables, the rest is really just math and the system. After your variables type in this:
firstNumber = s.nextDouble();
secondNumber = s.nextDouble();
If you chose to use int instead of double, replace nextDouble() with nextInt(). What this is doing is setting firstNumber equal to an input from the scanner. It is requesting to use a double. If you use letters when you type in your numbers to the console you will have an error. This is not smart enough for Algebra, so just use numbers. The rest is easy, just do this:
answer = firstNumber + secondNumber;
System.out.println(answer);
What this is saying is that answer will be equal to whatever firstNumber and secondNumber is. Then it prints it. This is just doing basic math. After you have that done just run it. In the console enter a number, and hit enter. Then enter another number and hit enter. This will then print the combination of those two numbers. Try it out with a bunch of numbers! Also just ignore the error with the Scanner, it is saying that the scanner never closed. If it is really bugging you, type in:
s.close();
at the end and the error will go away.
Source code used in this tutorial:
Ignore the silly class name ;). I had a more advanced calculator for a possibly future tutorial that I didn't want to remove.
In the next tutorial we will talk about arrays.
Table of Contents:
10. So what it this calculator?
10.1 Creating the Scanner
10.2 Creating the Variables
10.3 Using Scanner and Variables to Make a Basic Calculator
10. So what is this calculator?
This 'basic calculator', can only be used by the person with it open in eclipse. Therefor it is not ideal to make a full program based on it. But it works for a tutorial. We might get to that point though ;). What this does, is it recieves input from the user (you), adds it together and prints the answer. The answer can only be a number, or it will throw an error. As we start making the calculator, it will begin to make sense.
10.1 Creating the Scanner
This is a fairly easy task even for beginners. A Scanner is like an object, and as we mve on you will begin to see that many times you want to use a Java class, that you will use an object. It is done like this:
Scanner s = new Scanner(System.in);
What does this mean? It is, technically, creating an object of class Scanner and calling s. Then it sets s equal to a new Scanner. This tells Java that this is new, not something already in your code. System.in is a bit different. It is telling the console to recieve an input from the user. The program will not continue until you do such. As for now, it will not be recieving an input, but that will come. Make sure to include this in your main method not anywhere else.
10.2 Creating the Variables
This is an easy task. You can do two things.
1. Create 3 integers
or
2. Create 3 doubles
If you choose to do three integers, keep in mind they cannot have decimals. Neither of them can contain any letters or symbols, only numbers. I will be using three doubles. So this is my setup so far:
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
double firstNumber;
double secondNumber;
double answer;
}
See the names of the double? There is firstNumber, secondNumber, and answer. These will be the numbers that we will be using. Neither of them should equal anything yet.
10.3 Using Scanner and Variables to Make a Basic Calculator
So after the variables, the rest is really just math and the system. After your variables type in this:
firstNumber = s.nextDouble();
secondNumber = s.nextDouble();
If you chose to use int instead of double, replace nextDouble() with nextInt(). What this is doing is setting firstNumber equal to an input from the scanner. It is requesting to use a double. If you use letters when you type in your numbers to the console you will have an error. This is not smart enough for Algebra, so just use numbers. The rest is easy, just do this:
answer = firstNumber + secondNumber;
System.out.println(answer);
What this is saying is that answer will be equal to whatever firstNumber and secondNumber is. Then it prints it. This is just doing basic math. After you have that done just run it. In the console enter a number, and hit enter. Then enter another number and hit enter. This will then print the combination of those two numbers. Try it out with a bunch of numbers! Also just ignore the error with the Scanner, it is saying that the scanner never closed. If it is really bugging you, type in:
s.close();
at the end and the error will go away.
Source code used in this tutorial:
Ignore the silly class name ;). I had a more advanced calculator for a possibly future tutorial that I didn't want to remove.
In the next tutorial we will talk about arrays.
Tags |
3102283
6
Create an account or sign in to comment.
double firstNumber, secondNumber, answer;
instead of
double firstNumber;
double secondNumber;
double answer;