[go: up one dir, main page]

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

Class IX Lesson 5

The document discusses various programming concepts in Java, including the differences between nextInt() and nextFloat() methods, types of errors (syntax, logical, and runtime), and the distinction between testing and debugging. It also explains Java packages and provides examples of programs to calculate the time period of a simple pendulum, employee pay, and discounts on products. Additionally, it includes code snippets for practical implementations of these concepts.

Uploaded by

gswarupa12
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)
7 views5 pages

Class IX Lesson 5

The document discusses various programming concepts in Java, including the differences between nextInt() and nextFloat() methods, types of errors (syntax, logical, and runtime), and the distinction between testing and debugging. It also explains Java packages and provides examples of programs to calculate the time period of a simple pendulum, employee pay, and discounts on products. Additionally, it includes code snippets for practical implementations of these concepts.

Uploaded by

gswarupa12
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/ 5

Chapter 5

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:


(NOTE: Q1, Q2, & Q3 Write from the textbook which I have given from the textbook)

Question 4
What is a java package? Give an example.

Answer

A package is a named collection of Java classes that are grouped based on their functionality.
For example, java.util.

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.

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.

V Java Programming (variable description compulsory)


1. The time period of a Simple Pendulum is given by the formula:
T=

l
g
Write a program to calculate the time period of a Simple Pendulum by taking length (l)
22
and acceleration due to gravity(g) as inputs. [ π= ]
7
Solution:
import java.util.*;
class Q1
{
void main(double l, double g)
{
double T,pi=22/7.0;
T=2*pi*Math.sqrt(l/g);
System.out.println(“Time Period=”+T);
}
}
 Write a program by using class ‘Employee’ to accept Basic Pay of an employee. Calculate
the allowances/deductions as given below. Finally find and print the Gross and Net Pay.
Allowances /Deduction rate
Dearness Allowance (DA) : 30% of the Basic Pay
House Rent Allowance(HRA) : 15% of the Basic Pay
Provident Fund : 12.5% of the Basic Pay
Gross Pay=Basic Pay + Dearness Allowance + House Rent Allowance.
Net Pay=Gross Pay-Provident Fund
Solution:
import java.util.*;
class Employee
{
void main(double basic)
{
double da,hra,pf,gross,net;
da=30/100.0 * basic;
hra=15/100.0 * basic;
pf=12.5/100 * basic;
gross=basic +da+hra;
net=gross-pf;
System.out.println(“Gross Pay=”+gross);
System.out.println(“Net Pay=”+net);
}
}
 A shopkeeper offers 10% discount on the printed price of a Digital Camera. However, a
customer has to pay 6% GST on the remaining amount. Write a program in Java to
calculate the amount to be paid by the customer taking printed price as an input.
Solution:
import java.util.*;
class Q3
{
void main(double price)
{double dis,gst,amt1,amt2;
dis=10/100.0 * price;
amt1=price-dis;
gst=6/100.0 * amt1;
amt2=amt1+gst;
System.out.println(“The amount after discount and GST=”+amt2);
}
}
 A shopkeeper offers 30% discount on purchasing articles whereas the other shopkeeper
offers two successive discounts 20% and 10% for purchasing the same articles. Write a
program in Java to compute and display the discounts. Take the price of the article as the
input.
Solution:
import java.util.*;
public class Q4
{
static void main()
{
Scanner sr=new Scanner(System.in);
double price,xamt,yamt1,yamt2;
System.out.println(“Enter the price of the article”);
price=sr.nextDouble();
xamt=price – 30/100.0 * price;
yamt1=price – 20/100.0 *price;
yamt2= yamt1- 10/100.0* yamt1;
System.out.println(“Price in Shop1=”+xamt);
System.out.println(“Price in Shop2=”+yamt2);
}
}

You might also like