Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(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

  1. (use java code)
  2. 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;

Declare a double variable, call it mydouble and give it the value 2.5 and then print it.

Remember: You can always apply mathematical operations in java (addition, subtraction multiplication and division, mod, ++, --, etc)

  1. 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();

Create a small program that asks the user to enter name and age and the program will display them back

  1. 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.

Create a string array of size 2 and name it myName and in index 0 write your name and index 1 write your family name, and then print on the screen:

My name is [index zero of the array] [index one of the array]

  1. 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!

Remember: You can use && and || operations in the conditions!

How about you brush on your skills and try a little if-else statement?

Write a Java program that takes from the user an integer number and checks if the number is positive or negative and display that on the screen.

  1. 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;

.

.

.

}

  • Print out numbers from 1 to 10 using for loop and then do the opposite (10 to 1) using while loop
  • Write a Java program that creates an array of random numbers and finds the smallest number in that array and displays it
  • Write a Java program that calculates the average of numbers in the array: 2, 9, 4, 5, 15, 20

Always make sure you make an update after/before every iteration, and make sure that the loop will exit at some point, if its difficult, trace your code line by line!

  1. 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)

{

}

}

Please take a note that java only supports pass by value. Do you know the difference between pass by value and pass by reference?

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.

A function can return void as well! What is a void value?

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

  • Write a function in Java that takes in two integers as parameters and returns the power.

Ex: 2,3 are parameters then the result would be 8 (23)

  • Write a function in Java that takes as parameters the number of hours a student registered, the price of an hour, additional fees and then calculates the total amount that that student needs to pay for that particular semester.
  • Write a Java program that calculates the total amount of money you will have in your account after 5 years if the original amount of money is 3000 and the interest rate is 3% per year.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions