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!
No comments:
Post a Comment