Chapter 5 of class-9
Chapter 5 of class-9
Ans.
1. java.util
2. next( ).charAt( )
3. java.io
4. nextDouble();
5. nextInt( )
4. to accept a fraction value ‘n’ in double data type through Stream class
Ans.
2. float m=ob.nextFloat( );
Ans. nextInt( ) receives and returns the token as a int data type whereas nextFloat( ) receives
and returns the token as a float data type.
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.
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?
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
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
[or]
This object br can be used to invoke the various input methods of the BufferedReader class as
follows:
Example
double d = sn.nextDouble( );
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.
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.
Ans. The keyword import is used to import a complete package or a particular class of a
package into the program.
Ans.
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.
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
b) Syntax error and Logical error – repeated – Refer to III main 2nd question
Ans.
Testing
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
a) Math.pow(a,2) b) a * a
a) int
b) float
c) double d) All
a) sqrt(a) b) Math.sqrt(a)
c) Squareroot(a) d) None
a) Java.Math b) Java.Power
c) Java.Sqrt d) None
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)
5. b) 9.99
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
3. Math.ceil( )
;
Ans. double
Ans.
2. Math. max( )
Ans.
For example,
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,
4. Math.abs( )
Ans.
For example,
5. Math.log( )
Ans.
For example,
Ans.
Math.ceil( )
Example
Math.floor( )
Example
Ans.
Math.rint( )
For example ,
For example,
For example,
Math.round( )
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 example ,
For example ,
Sample Output :
Greatest number 87
Smallest number 34
Solution:
import java.util.*;
class Q1
int a,b,c,big,small;
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));
}}
Hint : h=
Solution:
import java.util.*;
class Q2
double p,b,h;
p=sr.nextDouble();
b=sr.nextDouble();
h= Math.sqrt(p*p + b*b);
}}
Solution:
import java.util.*;
class Q3
double n=0;
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));
}}
Solution:
import java.util.*;
class Q4
double phy,chem,bio,avg;
chem=sr.nextDouble();
bio=sr.nextDouble();
avg=(phy+chem+bio)/3;
avg=Math.round(avg);
System.out.println(“Average =”+avg);
}}
r=
Solution:
import java.util.*;
class Q5{
double area,r;
System.out.println(“Enter the area of the circle”);
area=sn.nextDouble();
r=Math.sqrt(7*area/22.0);
}}