Java Input
Java Input
Input in Java
Section 3: Assignment Questions
1. Suppose you want to use the class MyClass of the package MyPackage.UtilityLibrary in a program
you are writing. What do you need to do to make the following?
i. class MyClass available to your program
Ans. import java.MyPackage.UtilityLibrary.MyClass;
3. What are delimiters? Which is the default delimiter used in the Scanner class?
Ans. A delimiter is a sequence of one or more characters that separates two tokens. The default
delimiter used in the Scanner class is a white space.
Ans. A logical error occurs when the program compiles and runs without errors, but produces an
incorrect result. For eg. ‘using wrong variable name’
6. If a student forgot to put a closing quotation mark on a string, what kind error would occur?
Ans. syntax error
7. A program has compiled successfully without any errors. Does this mean the program is error free?
Explain.
Ans. No. The program may have logical error and may give incorrect result.
8. A program needed to read an integer, but the user entered a string instead, and an error occurred
when the program was executed. What kind of error is this?
Ans. Runtime error
9. A student was asked to write a program for computing the area of a rectangle and he, mistakenly,
wrote the program to compute the perimeter of a rectangle. What kind of error is this?
Ans. Logical error
ii. Uses the object scanner to read a word from the keyboard and store it in the String variable named
stg.
Ans. String stg = scanner.next();
16. Write a statement to let the user enter an integer or a double value from the keyboard.
Ans. int input1 = keyboard.nextInt();
double input2 = keyboard.nextDouble();
17. Write a program in Java that takes input using the Scanner class to calculate the Simple Interest
and the Compound Interest with the given values:
i. Principle Amount = Rs.1,00,000
ii. Rate = 11.5%
iii. Time = 5 years
Display the following output:
i. Simple interest
ii. Compound interest
iii. Absolute value of the difference between the simple and compound interest.
Ans.
import java.util .*;
class sici
{
public static void main (String argu[ ])
{
double pr=100000,t=5, sim,com,rate=11.5;
Scanner sc=new Scanner (System. in);
www.bhuvantechs.com
sim=(pr * t * rate)/100;
com=pr * Math.pow(1.0+rate/100.0,t) - pr;
System.out.println("Simple Interest="+sim);
System.out. println("Compound Interest="+com);
System.out. println("Absolute value of the difference between the SI and CI"+(com-sim));
}
}
19. Write a program that takes the distance of the commute in kilometres, the car fuel consumption
rate in kilometre per gallon, and the price of a gallon of petrol as input. The program should then
display the cost of the commute.
Ans.
import java.util.*;
class Simple
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double dis,price,rate,cost;
System.out.println("Enter the distance to travel in kilometer");
dis=sc.nextDouble();
System.out.println("Enter the car fuel consumption rate in kilometre per gallon");
rate=sc.nextDouble();
System.out.println("Enter the price of a gallon of petrol");
price=sc.nextDouble();
cost=(dis*price)/rate;
www.bhuvantechs.com
20. Write a program in Java that accepts the seconds as input and converts them into the
corresponding number of hours, minutes and seconds. A sample output is shown below:
Enter Total Seconds:
5000
1 Hour(s) 23 Minute(s) 20 Second(s)
21. Write a program in Java, using the Scanner methods, to read and display the following details:
Name - as a String data type,
Roll Number - as an integer data type,
Marks in 5 subjects - as a float data type,
Compute and display the percentage of marks.
Ans. import java.util.Scanner;
public class seconds
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter Name: ");
String name = in.next();
System.out.print("Enter Roll No.: ");
int roll_no = in.nextInt();
System.out.print("Enter Science Marks: ");
float sci = in.nextFloat();
System.out.print("Enter Maths Marks: ");
float math = in.nextFloat();
System.out.print("Enter English Marks: ");
float eng = in.nextFloat();
System.out.print("Enter Hindi Marks: ");
www.bhuvantechs.com
www.bhuvantechs.com