0% found this document useful (0 votes)
52 views5 pages

Method Description: Study of Simple Java Programs

This document discusses three Java programs: 1) A program to find the roots of a quadratic equation by taking input from the user and using the Math class. 2) A program to check if a user-input number is positive, negative, or zero using conditional statements like if-else. 3) A program to print all prime numbers between two user-input intervals using looping statements like while. It also provides theory on relevant Java concepts like data types, conditional statements, looping statements, the Scanner and Math classes.

Uploaded by

abhishek shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views5 pages

Method Description: Study of Simple Java Programs

This document discusses three Java programs: 1) A program to find the roots of a quadratic equation by taking input from the user and using the Math class. 2) A program to check if a user-input number is positive, negative, or zero using conditional statements like if-else. 3) A program to print all prime numbers between two user-input intervals using looping statements like while. It also provides theory on relevant Java concepts like data types, conditional statements, looping statements, the Scanner and Math classes.

Uploaded by

abhishek shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

B.

STUDY OF SIMPLE JAVA PROGRAMS

1. Aim: WAP to find the roots of a quadratic equation


Problem Statement: Accept three integer numbers from user and display all possible
roots of a quadratic equation

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.

Methods of 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

Math.toDegrees It is used to convert the specified Radians angle to equivalent angle


measured in Degrees.
Math.toRadians It is used to convert the specified Degrees angle to equivalent angle
measured in Radians.
Example:
double ceil = Math.ceil(7.343); // ceil = 8.0
double floor = Math.floor(7.343); // floor = 7.0
int min = Math.min(10, 20);
double random = Math.random(); ----
The Math.random() method returns a random floating point number between 0 and 1.
2. Aim: WAP to check if the entered number is positive, negative or zero
Problem Statement: Accept an integer from user and using conditional statement check
if the number is positive, negative or zero.
Theory:
Conditional Statements: The following are the Conditional statements in java;
a) if statement: The if statement is the most basic of all the control flow
statements. The if statement tells our program to execute a certain section of
code only if a particular test evaluates to true.
Syntax:
if(condition)
{
Statement(s);
}

b) Nested if statement: An if statement inside another the statement. If the outer if


condition is true then the section of code under outer if condition would
execute and it goes to the inner if condition. If inner if condition is true then the
section of code under inner if condition would execute.
Syntax:
if(condition_1)
{
Statement1(s);
if(condition_2)
{
Statement2(s);
}
}
c) if-else statement: If a condition is true then the section of code under if would
execute else the section of code under else would execute.
Syntax:
if(condition)
{
Statement(s);
}
else
{
Statement(s);
}
d) if-else-if statement: Here, a user can decide among multiple options.The if
statements are executed from the top down. As soon as one of the conditions
controlling the if is true, the statement associated with that if is executed, and
the rest of the ladder is bypassed. If none of the conditions is true, then the
final else statement will be executed.

e) Switch Case Statement: The switch statement is a multiway branch statement.


It provides an easy way to dispatch execution to different parts of code based
on the value of the expression.
Example:
class ifelseifDemo
{
public static void main(String args[])
{
int i = 20;

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?

You might also like