11
11
Use
the following member methods for the given purposes:
Class name — Calculate
Member methods:
void inputdata() — to input both the values
void calculate() — to find sum and difference
void outputdata() — to print sum and difference of both the numbers
Use a main method to call the functions.
Ans:
import java.util.*;
public class Calculate
{
int a, b;
public void inputdata()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
a = sc.nextInt();
System.out.print("Enter second number: ");
b = sc.nextInt();
}
int sum, diff;
public void calculate()
{
sum = a + b;
diff = a - b;
}
public void outputdata()
{
System.out.println("Sum = " + sum);
System.out.println("Difference = " + diff);
}
public static void main(String args[])
{
Calculate obj = new Calculate();
obj.inputdata();
obj.calculate();
obj.outputdata();
}
}
Member Methods:
void getdata() — to accept three numbers
void findprint() — to check and display whether the numbers are Pythagorean Triplets or not.
import java.util.Scanner;
Calculate tax based on the given conditions and display the output as
Up to ₹2,50,000 No tax
Total Annual Taxable Income Tax Rate
import java.util.Scanner;
void input() Stores the cost of the article and name of the customer
void display() Displays the name of the customer, cost, discount and amount to be paid
Write a program to compute the discount according to the given conditions and display the output as
per the given format.
Up to ₹5,000 No discount
Output:
import java.util.Scanner;
Output
Question 5
int prv, pre to store the previous and present meter readings
void input() Stores the previous reading, present reading and name of the consumer
void display() Displays the name of the consumer, calls made, amount and total amount to be paid
Write a program to compute the monthly bill to be paid according to the given conditions and display
the output as per the given format.
However, every consumer has to pay ₹180 per month as monthly rent for availing the service.
Output:
import java.util.Scanner;
Output
Question 6
Write a program to compute the interest according to the given conditions and display the output.
import java.util.Scanner;
interest = (p * r * t) / 100.0;
amt = p + interest;
}
Output
Question 7
int day to store the number of days for which fine is to be paid
void input() To accept the name of the book and printed price of the book
void display() Displays the name of the book and fine to be paid
Write a program to compute the fine according to the given conditions and display the fine to be paid.
Days Fine
import java.util.Scanner;
Output
Question 8
Up to 5 years 15%
import java.util.Scanner;
Output
Question 9
Hero Honda has increased the cost of its vehicles as per the type of the engine using the following
criteria:
Write a program by using a class to find the new cost as per the given specifications:
void find() To find the new cost as per the criteria given above
void printcost() To print the type and new cost of the vehicle
import java.util.Scanner;
case 4:
newCost = cost + (cost * 0.12);
break;
default:
System.out.println("Incorrect type");
break;
}
}
Output
Question 10
int days To store the number of days the bike is taken on rent
Days Charge
Output:
import java.util.Scanner;
Output
Question 11
void convert() to obtain a string after converting each upper case letter into lower case and vice versa
import java.util.Scanner;
Output
Question 12
import java.util.Scanner;
Output
Question 13
A bookseller maintains record of books belonging to the various publishers. He uses a class with the
specifications given below:
Data Members:
Member Methods:
1. void getdata() — To accept title, author, publisher's name and the number of copies.
2. void purchase(int t, String a, String p, int n) — To check the existence of the book in the stock
by comparing total, author's and publisher's name. Also check whether noc >n or not. If yes,
maintain the balance as noc-n, otherwise display book is not available or stock is under
flowing.
import java.util.Scanner;
Output
Question 14
Data Members:
Member Methods:
import java.util.Scanner;
Output
Question 15
void display() To show all the details viz. name, marks, total and average
import java.util.Scanner;
Output
Question 16
int hours To store the number of hours the vehicle is parked in the parking lot
Data Members Purpose
Member
Purpose
Methods
void To compute the parking charge at the rate ₹3 for the first hour or the part thereof and ₹1.50
calculate( ) for each additional hour or part thereof.
Write a main method to create an object of the class and call the above methods.
import java.util.Scanner;
Output
Question 17
int totalamt To store the amount to be paid after updating the original amount
Member
Purpose
Methods
void accept() To take input for name, coach, mobile number and amount
void update() To update the amount as per the coach selected (extra amount to be added in the amount as
Member
Purpose
Methods
void display() To display all details of a customer such as name, coach, total amount and mobile number
First_AC ₹700
Second_AC ₹500
Third_AC ₹250
Sleeper None
Write a main method to create an object of the class and call the above member methods.
import java.util.Scanner;
int acc_num;
String title;
String author;
public void input() throws IOException {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter accession number: ");
acc_num = Integer.parseInt(br.readLine());
System.out.print("Enter title: ");
title = br.readLine();
System.out.print("Enter author: ");
author = br.readLine();
}
Program 2- Given below is a hypothetical table showing rates of Income Tax for male citizens below the age
of 65 years:
Taxable Income (TI) in Income Tax in
Is greater than 1,60,000 and less than or equal to 5,00,000 ( TI – 1,60,000 ) * 10%
Is greater than 5,00,000 and less than or equal to 8,00,000 [ (TI - 5,00,000 ) *20% ] + 34,000
Write a program to input the age, gender (male or female) and Taxable Income of a person.If the age is more
than 65 years or the gender is female, display “wrong category*.
If the age is less than or equal to 65 years and the gender is male, compute and display the Income Tax payable
as per the table given above.
Solution.
import java.util.Scanner;
Program 3- Write a program to accept a string. Convert the string to uppercase. Count and output the number
of double letter sequences that exist in the string.
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE
Sample Output: 4
Solution.
import java.util.Scanner;
Example:
(i) Input value of n=2, ch=’O’
Output:
OO
OO
(ii) Input value of x=2, y=5
Output:
@@@@@
@@@@@
(iii) Output:
*
**
***
Solution.
System.out.print(ch);
}
System.out.println();
System.out.print("@");
System.out.println();
System.out.print("*");
System.out.println();
}
Program 5- Using the switch statement, writw a menu driven program to:
(i) Generate and display the first 10 terms of the Fibonacci
series 0,1,1,2,3,5….The first two Fibonacci numbers are 0 and 1, and each
subsequent number is the sum of the previous two.
(ii)Find the sum of the digits of an integer that is input.
Sample Input: 15390
Sample Output: Sum of the digits=18
For an incorrect choice, an appropriate error message should be displayed
Solution.
import java.util.Scanner;
System.out.println("Menu");
switch (choice) {
case 1:
int a = 0;
int b = 1;
System.out.print("0 1 ");
break;
default:
System.out.println("Invalid Choice");
Program6- Write a program to accept the names of 10 cities in a single dimension string array and their STD
(Subscribers Trunk Dialing) codes in another single dimension integer array. Search for a name of
a city input by the user in the list. If found, display “Search Successful” and print the name of the city along
with its STD code, or else display the message “Search Unsuccessful, No such city in the list’.
Solution.
import java.util.Scanner;
import java.util.Scanner;
cities[i] = scanner.next();
std[i] = scanner.nextInt();
if (cities[i].equals(target)) {
System.out.println("Search successful");
searchSuccessful = true;
break;
}
}
if (!searchSuccessful) {