Chapter 5-Input
Chapter 5-Input
Chapter 5-Input
Input In Java
Class 9 - APC Understanding Computer Applications with
BlueJ
Question 1
Question 2
Question 3
Question 4
Question 5
Question 1
Question 2
Question 3
Question 4
to accept a fraction value 'n' in double data type through Stream Class
Question 5
Question 6
Question 1
nextInt( ) and nextFloat( ) methods
nextInt( ) nextFloat( )
Scans the next token of input as an int Scans the next token of input as a float
Question 2
Syntax Errors occur when we violate the rules of writing the Logical Errors occur due to our mistakes in
statements of the programming language. programming logic.
Question 1
Question 2
Question 3
What is the use of the keyword import?
import keyword is used to import built-in and user-defined packages into our Java
program.
Question 4
Question 5
Question 6
if (a < b) {
/*
* All statements within this set of braces
* form the compound statement
*/
Question 7
Write down the syntax to input a character through scanner class with an example.
Syntax:
char <variable name> = <Scanner Object>.next().charAt(0);
Example:
Question 8
import java.util.Scanner;
class RunTimeError
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();
int result = 100 / n;
System.out.println("Result = " + result);
}
}
This program will work fine for all non-zero values of n entered by the user. When the user
enters zero, a run-time error will occur as the program is trying to perform an illegal
mathematical operation of division by 0. When we are compiling the program, we cannot
say if division by 0 error will occur or not. It entirely depends on the state of the program
at run-time.
Question 9
What are the different types of errors that take place during the execution of a program?
Logical errors and Run-Time errors occur during the execution of the program.
Question 10
Distinguish between:
(a) Testing and Debugging
Testing Debugging
In the process of Testing, we check if the program is working as In the process of Debugging, we correc
expected and find out the errors if it is not giving the expected
Testing Debugging
Syntax Errors occur when we violate the rules of writing the Logical Errors occur due to our mistakes in
statements of the programming language. programming logic.
Question 1
The time period of a Simple Pendulum is given by the formula:
T = 2π√(l/g)
Write a program to calculate the time period of a Simple Pendulum by
taking length and acceleration due to gravity (g) as inputs.
import java.util.Scanner;
Output
Question 2
Write a program by using class 'Employee' to accept Basic Pay of an
employee. Calculate the allowances/deductions as given below.
Question 3
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.
import java.util.Scanner;
Output
Question 4
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 an article as the input.
import java.util.Scanner;
Output
Question 5
Mr. Agarwal invests certain sum at 5% per annum compound interest
for three years. Write a program in Java to calculate:
(a) the interest for the first year
(b) the interest for the second year
(c) the amount after three years.
Take sum as an input from the user.
Sample Input: Principal = ₹5000, Rate =10%, Time = 3 yrs
Sample Output: Interest for the first year: ₹500
Interest for the second year: ₹550
Interest for the third year: ₹605
import java.util.Scanner;
public class KboatCompoundInterest
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter sum of money: ");
double p = in.nextDouble();
double interest = p * 5 * 1 / 100.0;
System.out.println("Interest for the first year = " +
interest);
p += interest;
interest = p * 5 * 1 / 100.0;
System.out.println("Interest for the second year = " +
interest);
p += interest;
interest = p * 5 * 1 / 100.0;
System.out.println("Interest for the third year = " +
interest);
}
}
Output
Question 6
A businessman wishes to accumulate 3000 shares of a company.
However, he already has some shares of that company valuing ₹10
(nominal value) which yield 10% dividend per annum and receive
₹2000 as dividend at the end of the year. Write a program in Java to
calculate the number of shares he has and how many more shares to
be purchased to make his target.
Hint: No. of share = (Annual dividend * 100) / (Nominal value * div%)
public class KboatShares
{
public static void main(String args[]) {
int sharesHeld = (2000 * 100)/(10 * 10);
System.out.println("No. of shares held currently = "
+ sharesHeld);
int sharesRequired = 3000 - sharesHeld;
System.out.println("No. of shares to purchase = "
+ sharesRequired);
}
}
Output
Question 7
Write a program to input the time in seconds. Display the time after
converting them into hours, minutes and seconds.
Sample Input: Time in seconds 5420
Sample Output: 1 Hour 30 Minutes 20 Seconds
import java.util.Scanner;
Output
Question 8
Write a program to input two unequal numbers. Display the numbers
after swapping their values in the variables without using a third
variable.
Sample Input: a = 23, b = 56
Sample Output: a = 56, b = 23
import java.util.Scanner;
Output
Question 9
A certain amount is invested at the rate 10% per annum for 3 years.
Find the difference between Compound Interest (CI) and Simple
Interest (SI). Write a program to take amount as an input.
Hint: SI = (P * R * T) / 100
A = P * (1 + (R/100)) T
CI = A - P
import java.util.Scanner;
Output
Question 10
A shopkeeper sells two calculators for the same price. He earns 20%
profit on one and suffers a loss of 20% on the other. Write a program to
find his total cost price of the calculators by taking selling price as input.
Hint: CP = (SP / (1 + (profit / 100))) (when profit)
CP = (SP / (1 - (loss / 100))) (when loss)
import java.util.Scanner;