[go: up one dir, main page]

0% found this document useful (0 votes)
25 views20 pages

Chapter 5 of class-9

The document provides a comprehensive overview of Java programming concepts, specifically focusing on input handling using Scanner and Stream classes, as well as mathematical library methods. It includes questions and answers on importing packages, methods for accepting different data types, error types, and various mathematical functions. Additionally, it distinguishes between different functions like Math.ceil, Math.floor, Math.rint, and Math.round, explaining their behavior and providing examples.

Uploaded by

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

Chapter 5 of class-9

The document provides a comprehensive overview of Java programming concepts, specifically focusing on input handling using Scanner and Stream classes, as well as mathematical library methods. It includes questions and answers on importing packages, methods for accepting different data types, error types, and various mathematical functions. Additionally, it distinguishes between different functions like Math.ceil, Math.floor, Math.rint, and Math.round, explaining their behavior and providing examples.

Uploaded by

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

Chapter 5

I. Name the following:

1. a package needed to import Scanner class

2. a method that accepts a character through Scanner object

3. a package needed to import StreamReader class

4. a method to accept an exponential value through Scanner object

5. a method that accepts an integer token through Scanner object

Ans.

1. java.util

2. next( ).charAt( )

3. java.io

4. nextDouble();

5. nextInt( )

II. Write down the syntax with reference to Java programming:

1. to accept an integral value ‘p’ through Stream class

2. to accept a fractional value (float) ‘m’ through Scanner class

3. to accept the character ‘d’ through Stream class

4. to accept a fraction value ‘n’ in double data type through Stream class

5. to accept a word ‘wd’ through Stream class

6. to create a Scanner object.

Ans.

Note: BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


1. int p = Integer.parseInt( br.readLine( ));

2. float m=ob.nextFloat( );

3. char ch = (char) (br.read( ));

4. double n = Double.parseDouble(br.readLine( ));

5. String str = br.readLine( );

6. Scanner ob=new Scanner(System.in);

III. Differentiate between the following:

1. nextInt( ) and nextFloat( )methods

Ans. nextInt( ) receives and returns the token as a int data type whereas nextFloat( ) receives
and returns the token as a float data type.

2. Syntax and Logical errors

Syntax error

 These errors occur when the rules or the grammar of the programming language is not
followed.
 The output is not obtained since the program does not execute due to compiler errors.
 For example, missing semicolon, undefined variables, misspelt keywords, missing brackets
etc

Logical error

 It is the error in planning the program’s logic. The computer does not know that an error has
been made.
 The system simply follows the instructions and compiles without errors, but the execution
does not yield the desired output.
 For example, to find the product instead of using ‘*’ sign, the programmer might have used
‘+’ sign which yields the incorrect result.

IV Answer the following:

1. What do you mean by Scanner class?

Ans. Scanner class is available in the system package java.util which must be imported. After
importing this package and creating a Scanner object, the user can avail various methods
provided in the Scanner class to manipulate input data. String manipulation is easier in
Scanner class as each word can be obtained as a token and handled separately.
2. What are the different ways to give inputs in Java programming?

Ans. Input in Java can be obtained by three different methods:

i) By Using Command Line Arguments

Here, the data values are passed through arguments at the time of execution. The values
passed through the console are received by a String type args[ ]array (subscript wise) as
arguments to the parse function. These values are further converted to the required data type
for storage in the corresponding variables.

Example 1

int a = Integer.parseInt( args[0] );

float b= Float.parseFloat( args[1]);

ii) By Using Input Stream

InputStream requires Buffer ( a temporary storage) for the data values to be stored through an
object. However, it requires the importing of java.io package to perform all input output
operations.

Example

InputStreamReader ir=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(ir);

[or]

BufferedReader br=new BufferedReader (new InputStreamReader(System.in));

This object br can be used to invoke the various input methods of the BufferedReader class as
follows:

String s = br. readLine( );

int n = Integer.parseInt( br.readLine( ));

float f = Float.parseFloat( br.readLine( ));

double d = Double.parseDouble(br.readLine( ));

iii) By Using Scanner class


To accept the input using this method, first import the java.util package and then create the
object of the Scanner class. Then invoke any of the methods of the Scanner class through the
Scanner object.

Example

Scanner sn=new Scanner (System.in);

double d = sn.nextDouble( );

3. What is the use of the keyword ‘import’?

Ans. The keyword ‘import’ is used to import a package into the current program. When a
package is imported, all the classes and methods available in the package can be used in the
program.

4. What is a package? Give an example.

Ans. A package in Java is a collection of logically related classes. For example, all
mathematical functions are included in a class called ‘Math’ that is a part
of java.lang package which is a default package that is automatically imported into every
program.

5. What is the use of the keyword ‘import’ in Java programming?

Ans. The keyword import is used to import a complete package or a particular class of a
package into the program.

Example: import java.util.* [or] import java.util.Scanner

6. What is a compound statement? Give an example.

Ans.

A compound statement refers to a set of statements enclosed within curly brackets.

Example

if (a>b)

{sum = a+b;

Pdt = a*b;

}
7. Write down the syntax to input a character through Scanner class with an example.

Ans.

char ch = ob.next( ).charAt(0);

8. What do you understand by ‘Run Time’ error? Explain with an example.

Ans.

Sometimes, the program does not produce the desired results even after the compilation
errors are removed. This is due to incorrect mathematical tasks or due to input of incorrect
data. Since the compiler does not respond properly while executing a statement, it is called a
runtime error. For example, when a number is divided by 0, it results in a runtime error.

9. What are the different types of errors that take place during the execution of a
program?

Ans.

Generally, there are three types of errors that occur in a computer program. They are as
follows:

 Syntax errors
 Logical errors
 Runtime errors

10. Distinguish between:

a) Testing and Debugging

b) Syntax error and Logical error – repeated – Refer to III main 2nd question

Ans.

a)Testing and Debugging

Testing

i) Testing is a process in which a program is validated.

ii) Testing is complete when all desired verifications against specifications have been
performed.

Debugging
i) Debugging is a process in which the errors in the program are removed.

ii) Debugging is finished when there are no errors and the program executes producing the
desired result.

b) Syntax error and Logical error – repeated – Refer to III main 2nd question
Chapter 6
Mathematical Library Methods

I. Choose the correct answer:

1. Which of the following is false to find the square


of a number?

a) Math.pow(a,2) b) a * a

c) Math.sqrt(a,2) d) All of the above

2.What type of value is returned by Math.sqrt ( )?

a) int
b) float

c) double d) All

3. Which of the following syntax is true to find the


square root of a number?

a) sqrt(a) b) Math.sqrt(a)

c) Squareroot(a) d) None

4. Name the class that is used for different


Mathematical functions

a) Java.Math b) Java.Power

c) Java.Sqrt d) None

5. Give the output of the Math.abs(x) ; when x = –


9.99

a) -9.99 b) 9.99
c) 0.99
d) None

Ans.

1. c) Math.sqrt(a, 2)

2. c) double

3. b) Math.sqrt(a)

4. a) java.math (case sensitive)

5. b) 9.99

II. Predict the output:

1. System.out.println(Math.sqrt(10.24));
Ans. 3.2

2. System.out.println(Math.rint(-99.4));
Ans. – 99.0

3. System.out.println(Math.cbrt(42.875));
Ans. 3.5

4. System.out.println(Math.min(-25.5, -
12.5)); Ans. – 25.5

5. System.out.println(Math.ceil(0.95));
Ans. 1.0

6. System.out.println(Math.round(-18.51));
Ans. – 19

7. System.out.println(Math.max(-77.66, -
87.45)); Ans. – 77.66

8. System.out.println(Math.floor(-0.88));
Ans. – 1.0
9. System.out.println(Math.rint(98.5));
Ans. 98.0

10. System.out.println(Math.ceil(65.5));
Ans. 66.0

III. Write down the syntax of the following functions:

1. To find the smaller between two numbers p and


q Ans. Math.min(p,q)

2. To find the absolute value of a number


m. Ans. Math.abs(m)

3. To find the exponent of a number


k. Ans. Math.log(k)

4. To find the square root of a number


d. Ans. Math.sqrt(d)

5. To find the rounded-off of a number


b. Ans. Math.round(b)

IV. Predict the return data type of the following


functions:

1. Math.sqrt( ); Ans. double

2. Math.rint( ); Ans. double

3. Math.ceil( )
;
Ans. double

4. Math.round( ); Ans. integer

5. Math.floor( ); Ans. double

6. Math.log( ); Ans. double

V. Explain the following functions:


1. Math.random( )

Ans.

 This function returns a random number between 0 and 1. It


returns a double data type value. Normally it returns the
value as a fractional number. For example, double d =
Math. random( )
 To get an integer random number between 1 and n use as
follows:

int r = (int) (Math.random( )*n ) + 1

 To get an integer random number between m and n where


m and n are the lower and upper limits respectively, use as
follows:

int r = (int) ((Math.random( )* (n – m)) +m);

2. Math. max( )

Ans.

This function is used to find the maximum of two given


arguments. It returns a value depending upon the
arguments (i.e. if the two arguments are integer, then the
return type will be integer)

For example,

 int n = Math.max(23,38) ; This statement results in n=38,


return type being int since arguments are int.
 double n = Math.max(34.25 , 12.7); This statement
results in n=34.5, return type being double since
arguments are double.

3. Math.cbrt( )

Ans.
This function is used to find the cube root of a positive or
negative number. It always returns a value in a double data
type.

For example,

 double n = Math.cbrt(27.0) ; returns a double type value


for n as 3.0
 double n = Math.cbrt(15.625); returns a double type value
for n as 2.5
 double n = Math.cbrt(– 3.375); returns a value for n as –
1.5

4. Math.abs( )

Ans.

This function is used to return the absolute value of an


argument. (i.e. magnitude of the number without its sign
i.e. a positive value). It returns int/ long/ double data type
depending on the arguments supplied.

For example,

 int n = Math.abs(3); returns an integer value for n as 3


 int n = Math.abs(– 8); returns an integer value for n as 8
 double d = Math.abs(– 9.99) returns a double value for n as
9.99

5. Math.log( )

Ans.

This function returns the natural logarithmic value of a


given argument. It always returns a double type value.

For example,

double x = Math.log ( 6.25); returns a double type value for


x as 1.8325
VI. Distinguish between them with suitable
examples:

1. Math.ceil( ) and Math.floor( )

Ans.

Math.ceil( )

This function returns the smallest integer that is greater


than or equal to the argument. It always returns a double
data type whatever be the argument type ( int / float /
double)

Example

i) For positive numbers

 System.out.println( Math.ceil(8)); output is 8.0


 System.out.println(Math.ceil(8.5)); output is 9.0
 System.out.println(Math.ceil(8.912)); output is 9.0

ii) For negative numbers

 System.out.println( Math.ceil( – 0.45 )); output is – 0.0


 System.out.println( Math.ceil( – 8)); output is – 8.0
 System.out.println(Math.ceil(– 8.5)); output is – 8.0
 System.out.println(Math.ceil(– 8.912)); output is – 8.0

Math.floor( )

This function returns the largest integer that is less than or


equal to the argument. It always returns the value as a
double data type whether the argument is int/ float
/double.

Example

i) For positive numbers

 System.out.println( Math.floor (8)); output is 8.0


 System.out.println(Math.floor (8.912)); output is 8.0

ii) For negative numbers

 System.out.println( Math.ceil( – 0.48 )); output is – 1.0


 System.out.println( Math.ceil( – 8)); output is – 8.0
 System.out.println(Math.ceil(– 8.912)); output is – 9.0

2. Math.rint( ) and Math.round( )

Ans.

Math.rint( )

It returns the truncated value of a number. It always


returns an integer value in double data type. However, it
predicts different values for positive and negative numbers.

For positive numbers

 If the fractional part lies between 0 and 0.5 (both inclusive)


, then Math.rint( ) gives the integer part of the number.

For example ,

 Math.rint( 8.0) output is 8.0


 Math.rint(8.5) output is 8.0
 If the fractional part is more than 0.5,
then Math.rint( )returns the next higher integer of the
number.

For example,

 Math.rint(9.501) output is 10.0


 Math.rint(9.994) output is 10.0

For negative numbers

 If the fractional part lies between 0 (inclusive) and 0.5


(exclusive) , then Math.rint( ) gives the integer part of the
number.
For example ,

 Math.rint( –9.0 ) output is – 9.0


 Math.rint( –9.4) output is – 9.0
 If the fractional part is 0.5 or more than 0.5,
then Math.rint( )returns the next lower integer of the
number.

For example,

 Math.rint(–9.5) output is –10.0


 Math.rint( –9.9) output is –10.0

Math.round( )

This function returns the value in rounded-off form. If the


fractional part is less than 0.5 then it returns the same
value of the integer part of the argument. Otherwise, it
returns the next higher integer value. This function always
returns the integer value in long or int data type. However,
it predicts different outputs for positive and negative
numbers.

For positive numbers

 If the fractional part lies between 0 (inclusive) and 0.5


(exclusive) then the function , Math.round( ) returns the
integer part of that number.

For example,

 Math.round(8.0) output is 8.
 Math.round(8.49) output is 8.
 If the fractional part is 0.5 or more then the
function Math.round( ) returns the next higher integer.

For example,

 Math.round(8.5) output is 9.
 Math.round(8.99) output as 9.

For negative numbers


 If the fractional part lies between 0 and 0.5 (both inclusive)
then the function , Math.round( ) returns the integer part of
that number.

For example ,

 Math.round( –8.0) output as –8.


 Math.round(–8.5) output as –8.
 If the fractional part is 0.5 or more then the
function Math.round( ) returns the next higher integer.

For example ,

 Math.round( –8.51) output is –9.


 Math.round(–8.99) output is –9.

VII. Unsolved Programs:

1. Write a program in Java to input three numbers and


display the greatest and the smallest of two
numbers.

Hint : Use Math.min( ) and Math.max( )

Sample Input : 87, 65, 34

Sample Output :

Greatest number 87

Smallest number 34

Solution:

import java.util.*;

class Q1

static void main()


{Scanner sr=new Scanner(System.in);

int a,b,c,big,small;

System.out.println(“Enter the numbers”);

a=sr.nextInt();

b=sr.nextInt();

c=sr.nextInt();

big=Math.max(c,Math.max(a,b));

small=Math.min(c,Math.min(a,b));

System.out.println(“The greatest of the 3 values is “+big);

System.out.println(“The smallest of the 3 values is


“+small);

}}

 Write a program in Java to calculate and display the


hypotenuse of a Right Angled Triangle by taking
perpendicular and base as inputs.

Hint : h=

Solution:

import java.util.*;

class Q2

static void main()


{Scanner sr=new Scanner(System.in);

double p,b,h;

System.out.println(“Enter the perpendicular and then


base”);

p=sr.nextDouble();

b=sr.nextDouble();

h= Math.sqrt(p*p + b*b);

System.out.println(“The value of hypotenuse =”+h);

}}

 Write a program to input a number and evaluate the


results based on the number entered by the user:
 Natural logarithm of the number
 Absolute value of the number
 Square root of the number
 Cube of the number
 Random numbers between 0 (zero) and 1
(one) [ICSE 2006]

Solution:

import java.util.*;

class Q3

static void main()

{Scanner sr=new Scanner(System.in);

double n=0;

System.out.println(“Enter the number”);


n=sr.nextDouble();

System.out.println(“Logarithm=”+Math.log(n));

System.out.println(“Absolute value=”+Math.abs(n));

System.out.println(“Square root=”+Math.sqrt(n));

System.out.println(“Cube root=”+Math.cbrt(n));

System.out.println(“Random number between 0 and 1


is”+Math.random());

}}

 In an examination, you have appeared for three


subjects i.e. Physics, Chemistry and Biology. Write a
program in Java to calculate the average mark
obtained and finally display the marks in rounded-off
form.

Take Physics, Chemistry and Biology marks as


inputs.

Solution:

import java.util.*;

class Q4

static void main()

{Scanner sr=new Scanner(System.in);

double phy,chem,bio,avg;

System.out.println(“Enter the marks”);


phy=sr.nextDouble();

chem=sr.nextDouble();

bio=sr.nextDouble();

avg=(phy+chem+bio)/3;

avg=Math.round(avg);

System.out.println(“Average =”+avg);

}}

 You want to calculate the radius of a circle by using


the formula:

Area=(22/7)*r2 ; where r = radius of the circle.

Hence the radius can be calculated as:

r=

Write a program in Java to calculate and display the


radius of a circle by taking area as an input.

Solution:

import java.util.*;

class Q5{

static void main()

{Scanner sn=new Scanner(System.in);

double area,r;
System.out.println(“Enter the area of the circle”);

area=sn.nextDouble();

r=Math.sqrt(7*area/22.0);

System.out.println(“The radius of the circle=”+r);

}}

You might also like