Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Lesson-5

Input In Java
I. Name the following

Question 1

a package needed to import scanner class


Ans. java.util

Question 2

a method that accepts a character through scanner object


Ans. next().charAt(0) or charAt()

Question 3

a package needed to import Stream Reader Class


Ans. java.io

Question 4

a method to accept an exponential value through scanner object


Ans. nextDouble()

Question 5

a method that accepts an integer token through scanner object


Ans. nextInt()

II. Write down the syntax with reference to Java


Programming

Question 1

to accept an integral value 'p' through Stream Class

Ans. InputStreamReader read = new InputStreamReader(System.in);


BufferedReader in = new BufferedReader(read);
int p = Integer.parseInt(in.readLine());

Question 2

to accept a fractional value (float) 'm' through Scanner Class

Ans. Scanner in = new Scanner(System.in);


float m = in.nextFloat();
Question 3

to accept a character 'd' through Stream Class

Ans. InputStreamReader read = new InputStreamReader(System.in);


BufferedReader in = new BufferedReader(read);
char d = (char)in.read();

Question 4

to accept a fraction value 'n' in double data type through Stream Class

Ans. InputStreamReader read = new InputStreamReader(System.in);


BufferedReader in = new BufferedReader(read);
double n = Double.parseDouble(in.readLine());

Question 5

to accept a word 'wd' through Stream Class

Ans. InputStreamReader read = new InputStreamReader(System.in);


BufferedReader in = new BufferedReader(read);
String wd = in.readLine();

Question 6

to create a scanner object

Ans. Scanner in = new Scanner(System.in);

III. Differentiate between the following

Question 1

nextInt( ) and nextFloat( ) methods

Ans.
nextInt( )
It is a Scanner class method which is used to input an integer type data from the
standard input device.

Example: int p=in.nextInt();


It means that variable the p will store an integer value through the Scanner object.

nextFloat( )
It is a Scanner class method which is used to input floating type data from the standard
input device.

Example: float p=in.nextFloat();

Question 2

Syntax and logical errors


Ans.
Syntax Errors Logical Errors

It occurs when we do not follow the


Logical Errors occur due to our
rules or the grammar of programming
mistakes in programming logic.
language.

Program compiles and executes but


Program fails to compile and execute.
doesn't give the desired output.

Logic errors need to be found and


Syntax Errors are caught by the
corrected by the people working on the
compiler.
program.

IV. Answer the following

Question 1

What do you mean by Scanner class?


Ans. Scanner class is the latest development in Java which is used to read (get) different
type of values by the user. It is present in java.util package.

Question 2

What are the different ways to give inputs in a Java Program?


Ans. Java provides the following ways to give input in a program:

1. Using Function Argument (Blue-j System)


2. Using InputStreamReader class.
3. Using Scanner class.
4. Using Command Line Arguments.

Question 3

What is the use of the keyword import?


Ans. The keyword import is used to include a package from Java Library into our Java
program.
Example: import java.util.Scanner;

Question 4

What is a package? Give an example.


Ans. Package is a set of various classes which can be included in our java program.
Example: java.util, java.lang.

Question 5

Give an example to input an integer number by using function argument.


Ans. Example:
class Sum
{
public static void main(int a, int b)
{
int s=0;
s=a+b;
System.out.println(“Sum=”+s);
}
}

Question 6

“Diagnostic messages detect only syntax errors”? Give comment.


Ans. Yes, Diagnostical messages detect only syntax error only because a java has built-
in keywords and delimiters, if we use them wrongly then compiler generates diagnostic
messages.

Question 7

Write down the syntax to input a character through scanner class with an example.

Ans. Syntax: char <variable name> = <Scanner Object>.next().charAt(0);


Example: Scanner in = new Scanner(System.in);
char ch = in.next().charAt(0);

Question 8

What do you understand by 'Run Time' error? Explain with an example


Ans. Errors that occur during the execution of the program due to incorrect
mathematical operations or by entering incorrect data values at the time of inputs.
Eample: Dividing a number by 0 or trying to find/calculate the square root of a
negative number.

Question 9

What are the different types of errors that take place during the execution of a program?
Ans. Logical errors and Run-Time errors occur during the execution of the program.

Question 10

Distinguish between:
(a) Testing and Debugging
Ans.

Testing Debugging

1. It is a process in which errors the


1. It is a process in which a program is errors are removed from the
validated. program.
2. It is complete when all desired 2. It is finished when there are no
verifications against specifications errors and the program is ready for
have been performed. execution.

(b) Syntax error and Logical error


Ans. Same as Q III. Differentiate 2.
Solutions to Unsolved Java Programs

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.
Ans.

import java.util.Scanner;
class Simple_Pendulum
{
public static void main(String args[])
{
double l, g, t=0.0;
Scanner in = new Scanner(System.in);
System.out.print("Enter length: ");
l = in.nextDouble();
System.out.print("Enter gravity: ");
g = in.nextDouble();
t = 2 * (22.0 / 7.0) * Math.sqrt(l/g);
System.out.println("Time period = " + t);
}
}

Question 2

Write a program by using class 'Employee' to accept Basic Pay of an employee. Calculate the
allowances/deductions as given below.

Allowance / Deduction Rate

Dearness Allowance (DA) 30% of Basic Pay

House Rent Allowance (HRA) 15% of Basic Pay

Provident Fund (PF) 12.5% of Basic Pay

Finally, find and print the Gross and Net pay.


Gross Pay = Basic Pay + Dearness Allowance + House Rent Allowance
Net Pay = Gross Pay - Provident Fund
Ans.

import java.util.Scanner;
class Employee
{
public static void main(String args[])
{
double bp, da=0.0, hra=0.0, pf=0.0, gp=0.0, np=0.0;
Scanner in = new Scanner(System.in);
System.out.print("Enter Basic Pay: ");
bp = in.nextDouble();
da = 0.3 * bp;
hra = 0.15 * bp;
pf = 0.125 * bp;
gp = bp + da + hra;
np = gp - pf;
System.out.println("Gross Pay = " + gp);
System.out.println("Net Pay = " + np);
}
}

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.
Ans.

import java.util.Scanner;
class Digital_Camera
{
public static void main(String args[])
{
double mrp, d=0.0, p=0.0, gst=0.0, netp=0.0;
Scanner in = new Scanner(System.in);
System.out.println("Enter printed price of Digital Camera:");
mrp = in.nextDouble();
d = mrp * 10 / 100.0;
p = mrp - d;
gst = p * 6 / 100.0;
netp=p+gst;
System.out.println("Amount to be paid: " + netp);
}
}

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.
Ans.

import java.util.Scanner;
class Discounts
{
public static void main(String args[])
{
double p, d1=0.0,amt1=0.0,d2=0.0,amt2=0.0,d3=0.0,amt3=0.0;
Scanner in = new Scanner(System.in);
System.out.print("Enter price of article: ");
p = in.nextDouble();
d1 = p * 30 / 100.0;
amt1 = p - d1;
System.out.println("30% discount = " + d1);
System.out.println("Amount after 30% discount = " + amt1);
d2 = p * 20 / 100.0;
amt2 = p - d2;
d3 = amt2 * 10 / 100.0;
amt2= amt2 - d3;
System.out.println("20% discount = " + d2);
System.out.println("10% discount = " + d3);
System.out.println("Amount after successive discounts = "+amt2);
}
}

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

Ans.

import java.util.Scanner;
class CompoundInterest
{
public static void main(String args[])
{
double p, ci=0.0;
Scanner in = new Scanner(System.in);
System.out.print("Enter principal amount: ");
p = in.nextDouble();
ci = p * 5 * 1 / 100.0;
System.out.println("Interest for the first year = " + ci);
p = p + ci;
ci = p * 5 * 1 / 100.0;
System.out.println("Interest for the second year = " + ci);
p = p + ci;
ci = p * 5 * 1 / 100.0;
System.out.println("Interest for the third year = " + ci);
}
}

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%)
Ans.

class Shares
{
public static void main(String args[])
{
int sh,sr;
sh = (2000 * 100)/(10 * 10);
System.out.println("No. of shares held currently="+ sh);
sr = 3000 - sh;
System.out.println("No. of shares to purchase="+ sr);
}
}

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
Ans.

import java.util.Scanner;
class Time
{
public static void main(String args[])
{
long s, h=0, m=0;
Scanner in = new Scanner(System.in);
System.out.print("Enter time in seconds: ");
s = in.nextLong();
h = s / 3600;
s = s % 3600;
m = s / 60;
s= s % 60;
System.out.println(h+" Hours "+m+" Minutes "+s+" Seconds");
}
}

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
Ans.

import java.util.Scanner;
class Swap
{
public static void main(String args[])
{
int a,b;
Scanner in = new Scanner(System.in);
System.out.println("Enter two unequal numbers");
System.out.print("Enter first number: ");
a = in.nextInt();
System.out.print("Enter second number: ");
b = in.nextInt();
a = a + b;
b = a - b;
a = a - b;
System.out.println(" a = " + a + " b= " + b);
}
}

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
Ans.

import java.util.Scanner;
class Compound
{
public static void main(String args[])
{
double P,SI=0.0, A=0.0, CI=0.0,;
Scanner in = new Scanner(System.in);
System.out.print("Enter principal Amount: ");
P = in.nextDouble();
SI = P * 10 * 3 / 100;
A = P * Math.pow(1 + (10/100.0), 3);
CI = A - P;
System.out.print("Difference between CI & SI: "+(CI - SI));
}
}

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)
Ans.

import java.util.Scanner;
class Shopkeeper
{
public static void main(String args[])
{
double sp, cp
Scanner in = new Scanner(System.in);
System.out.print("Enter the selling price: ");
sp = in.nextDouble();
cp1 = (sp / (1 + (20 / 100.0)));
cp2 = (sp / (1 - (20 / 100.0)));
tCP = cp1 + cp2;
System.out.println("Total Cost Price = " + tCP);
}
}

You might also like