Method Description: Study of Simple Java Programs
Method Description: Study of Simple Java Programs
Theory:
a) Scanner class: The Scanner class is used to get user input, and it is found in the
java.util package. So, we need to import this packet first before using the
methods of Scanner class. To use the Scanner class, create an object of the class
and use any of the available methods found in the Scanner class.
Method Description
nextBoolean() Reads a boolean value from the user
nextByte() Reads a byte value from the user
nextDouble() Reads a double value from the user
nextFloat() Reads a float value from the user
nextInt() Reads a int value from the user
nextLine() Reads a String value from the user
nextLong() Reads a long value from the user
nextShort() Reads a short value from the user
b) Data Types:
Data types classify the different values to be stored in the variable. In java,
there are two types of data types:
I. Primitive Data Types: These are predefined and available within the
Java language. There are 8 primitive types: byte (1 byte), short (2
bytes), int (4 byte), long (8 byte), char (2 byte), float (4 byte), double
(8 byte), and boolean (1 byte).
II. Non-primitive Data Types: These include array, class, interface etc.
c) Math class: The Math class contains various methods for performing basic
numeric operations such as the logarithm, cube root, and trigonometric functions
etc. This class is found in lang package. Since lang package is default imported,
we need not import that package. The various java math methods are as follows;
Method Description
Math.abs() It will return the Absolute value of the given value.
Math.max() It returns the Largest of two values.
Math.min() It is used to return the Smallest of two values.
Math.round() It is used to round of the decimal numbers to the nearest value.
Math.sqrt() It is used to return the square root of a number.
Math.cbrt() It is used to return the cube root of a number.
Math.pow() It returns the value of first argument raised to the power to second
argument.
Math.ceil() It is used to find the smallest integer value that is greater than or
equal to the argument or mathematical integer.
Math.floor() It is used to find the largest integer value which is less than or equal to
the argument and is equal to the mathematical integer of a double
value.
Math.random() It returns a double value with a positive sign, greater than or equal
to 0.0 and less than 1.0.
Trigonometric Math Methods
Method Description
Math.sin() It is used to return the trigonometric Sine value of a Given double value.
Math.cos() It is used to return the trigonometric Cosine value of a Given double value.
Math.tan() It is used to return the trigonometric Tangent value of a Given double
value.
Angular Math Methods
Method Description
if (i == 10)
System.out.println("i is 10");
else if (i == 15)
System.out.println("i is 15");
else if (i == 20)
System.out.println("i is 20");
else
System.out.println("i is not present"); } }
3. Aim: WAP to list all prime numbers between two given intervals
Problem Statement: Accept two numbers from user between which prime numbers are
to be generated, and print all prime numbers between the given intervals.
Theory:
Looping Statements: Java provides three ways for executing the loops. While all
the ways provide similar basic functionality, they differ in their syntax and
condition checking time.
a) while loop: A while loop is a control flow statement that allows code to be
executed repeatedly based on a given Boolean condition. The while loop
can be thought of as a repeating if statement.
Syntax :
while (boolean condition)
{
loop statements...
}
b) for loop: for loop provides a concise way of writing the loop structure.
Unlike a while loop, a for statement consumes the initialization, condition
and increment/decrement in one line thereby providing a shorter, easy to
debug structure of looping.
Syntax:
for (initialization condition; testing condition; increment/decrement)
{
statement(s)
}
c) do while: do while loop is similar to while loop with only difference that
it checks for condition after executing the statements, and therefore is an
example of Exit Control Loop.
Syntax:
do
{
statements..
} while (condition);
VIVA Questions:
1. How do we take input and display output in Java?
2. How do we use the Scanner class to take input?
3. Explain the use of + (concatenation operator) while displaying.
4. What are the different data types in java?
5. What are the different loops in java give syntax?
6. Explain the use of break and continue statements
7. What are the ways we can exit from a loop?
8. How are functions of Math class accessed?
9. Scanner class is in which package?
10. State various operators in java?