InputInJava
InputInJava
BlueJ
Question 1
Question 2
Question 3
Question 4
Question 5
Question 1
Question 2
to accept a fractional value (float) 'm' through Scanner Class
Question 3
Question 4
to accept a fraction value 'n' in double data type through Stream Class
Question 5
Question 6
Question 1
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 Logical Errors occur due to our
of writing the statements of the programming mistakes in programming logic.
language.
Program fails to compile and execute. Program compiles and executes but
doesn't give the desired output.
Syntax Errors are caught by the compiler. Logic errors need to be found and
corrected by the people working on the
program.
Question 1
Question 2
Question 3
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:
Question 8
What do you understand about the 'Run Time' error? Explain with an example
Errors that occur during the execution of the program primarily due to the state of the program
which can only be resolved at runtime are called Run Time errors.
Consider the below example:
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
Syntax Errors occur when we violate the rules of Logical Errors occur due to our
writing the statements of the programming mistakes in programming logic.
language.
Program fails to compile and execute. Program compiles and executes but
doesn't give the desired output.
Syntax Errors are caught by the compiler. Logical errors need to be found and
corrected by people working on the
program.
Question 1
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;
public class KboatSimplePendulum
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter length: ");
double l = in.nextDouble();
System.out.print("Enter g: ");
double g = in.nextDouble();
double t = 2 * (22.0 / 7.0) * Math.sqrt(l/g);
System.out.println("T = " + t);
}
}
Output
Question 2
Write a program by using class 'Employee' to accept Basic Pay of an employee. Calculate the
allowances/deductions as given below.
import java.util.Scanner;
public class Employee
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Basic Pay: ");
double bp = in.nextDouble();
double da = 0.3 * bp;
double hra = 0.15 * bp;
double pf = 0.125 * bp;
double gp = bp + da + hra;
double np = gp - pf;
System.out.println("Gross Pay = " + gp);
System.out.println("Net Pay = " + np);
}
}
Output
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;
public class KboatCameraPrice
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter printed price of Digital
Camera:");
double mrp = in.nextDouble();
double disc = mrp * 10 / 100.0;
double price = mrp - disc;
double gst = price * 6 / 100.0;
price += gst;
System.out.println("Amount to be paid: " + price);
}
}
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;
public class KboatDiscounts
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter price of article: ");
double price = in.nextDouble();
Output
Question 5
Mr. Agarwal invests certain sum at 5% per annum compound interest for three years. Write a
program in Java to calculate:
import java.util.Scanner;
Output
Question 6
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;
public class KboatTimeConvert
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter time in seconds: ");
long secs = in.nextLong();
long hrs = secs / 3600;
secs %= 3600;
long mins = secs / 60;
secs %= 60;
System.out.println(hrs + " Hours " + mins
+ " Minutes " + secs + " Seconds");
}
}
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;
public class KboatShopkeeper
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the selling price: ");
double sp = in.nextDouble();
double cp1 = (sp / (1 + (20 / 100.0)));
double cp2 = (sp / (1 - (20 / 100.0)));
double totalCP = cp1 + cp2;
System.out.println("Total Cost Price = " + totalCP);
}
}