Question
(use java code) Declaring, instantiating, and using local variables In Java, to declare a local variable (what is a local variable?), you write the data
- (use java code)
- Declaring, instantiating, and using local variables
In Java, to declare a local variable (what is a local variable?), you write the data type and then the name of the variable and then you type a semicolon to finish the statement like this: int number; Here you declared a variable of type integer and named it number
---But wait! Where do I write this?! Put your declaration line int number; inside your main method public static void main(String[] args)
instantiating your variables would follow the syntax in the following example: int number=5;
|
- Getting user input
To get input from user, you must first import a library to enable you to do this. To import a library, go to the very beginning of your java file, go to the line right after the package line and right the following: import java.util.Scanner; java.util.Scanner is the name of the library we need. After importing your library, write the following inside your main method Scanner input = new Scanner(System.in); -- Do not worry if you do not fully understand the structure of the previous statement, you will be able to understand throughout the course.
To take integer input from the user you will write the following inside your main method: int myNumber=input.nextInt(); to take double input you will do the following: double myNumber=input.nextDouble(); to take a string, String myName=input.next(); To take a full line, String myLine=input.nextLine();
|
- Declaring, instantiating, and using arrays
In java, indicating the size of the array during the declaration is a syntax error (what is a syntax error?). So to declare an array in java you will only need a name and a type for your array, example: int[] foo;
To actually create the array you will write the following: int[] foo=new int[5]; (you can replace int with the data type needed)
To create and instantiate your array in one step, do the following: int[] foo={1,2,3,4,5}; //this is an array named foo of type int and has value 1 in index 0, value 2 in index 1, etc.
|
- if else statements
In Java, if-else statements follow the following syntax if(condition) { Statement 1; Statement 2; } else { Statement 1; Statement 2; } Remember, you can always have an if without an else, but it is impossible to have an else without an if.
If you need to check for multiple conditions and do different actions based on the condition that is satisfied use else if!
How about you brush on your skills and try a little if-else statement?
|
- Loops
Java loops are very simple! They follow a very simple syntax! A while loop will have the structure: while(condition) { Statement 1; Statement 2; . . . }
A for loop has the structure: for(instantiation; condition; update) { Statement 1; Statement 2; . . . }
|
- Methods
Methods (or functions) help you to write a more organized code by hiding detailed steps from main and giving main the final result only (in case the function returns something!). The structure of a Java method would be: Public static returnType methodName(ParameterList) { Function body . . . Return statement }
Do not panic! You will learn the meaning of public static throughout this course. An example for this would be Public static int doubleIt(int myNumber) { return myNumber*2; }
This function definition should be placed inside the class that has your project name but outside your main method!
public class myProject {
//You can place your function here
public static void main(String[] args) {
} }
So how will this function be used? Simply call this function from the main method. See the following usage: public static void main(String[] args) { int a=5; int doubledA; doubledA=doubleIt(a); System.out.println(The double of: +a+ is +doubledA); }
The function in previous example returns an integer, remember a function can return a value of any type, you just need to indicate the type in the head of the function definition.
Lets try to do the same example but use void as a return type to see the difference.
Public static void doubleIt(int myNumber) { int doubleMyNumber=myNumber*2; System.out.println(The double of: +myNumber+ is + doubleMyNumber);
}
Notice that there is no need to put a return statement since the return type is void, however we needed to print the result from inside the function because whatever that is inside that function is available only within the scope of that function, no other function can know what is inside that function unless that function returns that value or store the value in a global variable (what are global variables?).
and our main would look like the following:
public static void main(String[] args) { int a=5; doubleIt(a); }
our main function look simpler now because everything is done inside the function doubleIt including printing the result
|
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started