Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

deepa x-partb questions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30

PREVIOUS YEARS X BOARD EXAM SOLVED QUESTIONS

PART-B

Define a class named movieMagic with the following description:


Instance variables/data members:
int year – to store the year of release of a movie
String title – to store the title of the movie.
float rating – to store the popularity rating of the movie.
(minimum rating = 0.0 and maximum rating = 5.0)

Member Methods:
(i) movieMagic() Default constructor to initialize numeric data members to 0 and String
data member to “”.
(ii) void accept() To input and store year, title and rating.
(iii) void display() To display the title of a movie and a message based on the rating as
per the table below.

RATING MESSAGE TO BE DISPLAYED

0.0 to 2.0 Flop

2.1 to 3.4 Semi-hit

3.5 to 4.5 Hit

4.6 to 5.0 Super Hit

Write a main method to create an object of the class and call the above member
methods.

Ans.
Program

import java.util.Scanner;

public class movieMagic {

int year;
String title;
float rating;

public movieMagic() {
year = 0;
title = "";
rating = 0;
}

1
public void accept() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter title: ");
title = scanner.nextLine();
System.out.print("Enter year: ");
year = scanner.nextInt();
System.out.print("Enter rating: ");
rating = scanner.nextFloat();
}

public void display() {


System.out.println("Movie Title - " + title);
if (rating >= 0 && rating <= 2.0) {
System.out.println("Flop");
} else if (rating >= 2.1 && rating <= 3.4) {
System.out.println("Semi-hit");
} else if (rating >= 3.5 && rating <= 4.5) {
System.out.println("Hit");
} else if (rating >= 4.6 && rating <= 5.0) {
System.out.println("Super Hit");
}
}

public static void main(String[] args) {


movieMagic movie = new movieMagic();
movie.accept();
movie.display();
}

A special two-digit number is such that when the sum of its digits is added to the
product of its digits, the result is equal to the original two-digit number.

Example: Consider the number 59.


Sum of digits = 5 + 9 = 14
Product of its digits = 5 x 9 = 45
Sum of the sum of digits and product of digits= 14 + 45 = 59

Write a program to accept a two-digit number. Add the sum of its digits to the product
of its digits. If the value is equal to the number input, output the message “Special 2-
digit number” otherwise, output the message “Not a Special 2-digit number”.

Program

1 import java.util.Scanner;

3 public class SpecialNumber {


4

2
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter number: ");
8 int number = scanner.nextInt();
9 int rightDigit = number % 10;
10 int leftDigit = number / 10;
11 int sumOfDigits = rightDigit + leftDigit;
12 int productOfDigits = rightDigit * leftDigit;
13 int sumOfProductAndSum = sumOfDigits + productOfDigits;
14 if (sumOfProductAndSum == number) {
15 System.out.println("Special 2-digit number");
16 } else {
System.out.println("Not a Special 2-digit
17
number");
18 }
19 }
20 }

Sample Outputs

1 Enter number: 59
2 Special 2-digit number
1 Enter number: 34
2 Not a Special 2-digit number

Question 6.
Design a class to overload a function area() as follows:
(i) double area(double a. double b, double e) with three double arguments, returns the
area of a scalene triangle using the formula:

where

(ii) double area(int a, int b, int height) with three integer arguments, returns the area of

3
a trapezium using the formula:

(iii) double area(double diagonal1, double diagonal2) with two double arguments,
returns
the area of a rhombus using the formula:

Ans.
view source
print?

1 public class Area {


2
3 public double area(double a, double b, double c) {
4 double s = (a + b + c) / 2;

5 double area = Math.sqrt(s * (s - a) * (s - b) * (s -


c));
6 return area;
7 }
8
9 public double area(int a, int b, int height) {
10 double area = 0.5 * height * (a + b);
11 return area;
12 }

13
14 public double area(double diagonal1, double diagonal2) {
15 double area = 0.5 * (diagonal1 * diagonal2);
16 return area;
17 }
18 }

Question 9.
Write a program to accept the year of graduation from school as an integer value from
4
the user. Using the Binary Search technique on the sorted array of integers given below,
output the message ‘Record exists’ if the value input is located in the array. If not,
output the message Record does not exist”.
(1982, 1987, 1993. 1996, 1999, 2003, 2006, 2007, 2009, 2010)

Ans.

1 import java.util.Scanner;
2
3 public class Search {
4

5 public static void main(String[] args) {


6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter year of graduation: ");
8 int graduationYear = scanner.nextInt();
int[] years =
9
{1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007,2009, 2010};
10 boolean found = false;

11 int left = 0;
12 int right = years.length - 1;
13 while (left <= right) {
14 int middle = (left + right) / 2;
15 if (years[middle] == graduationYear) {
16 found = true;

17 break;
18 } else if (years[middle] < graduationYear) {
19 left = middle + 1;
20 } else {
21 right = middle - 1;
22 }

23 }
24 if (found) {
25 System.out.println("Record exists");

5
26 } else {
27 System.out.println("Record does not exist");
28 }
29 }
30 }

Question 4:

Define a class called FruitJuice with the following description: [15]


Instance variables/data members:
int product_code – stores the product code number
String flavour – stores the flavor of the juice.(orange, apple, etc)
String pack_type – stores the type of packaging (tetra-pack, bottle etc)
int pack_size – stores package size (200ml, 400ml etc)
int product_price – stores the price of the product
Member Methods:
FriuitJuice() – default constructor to initialize integer data members
to zero and string data members to “”.
void input() – to input and store the product code, flavor, pack type,
pack size and product price.
void discount() – to reduce the product price by 10.
void display() – to display the product code, flavor, pack type,
pack size and product price.

import java.util.Scanner;

public class FruitJuice {

int product_code;
String flavour;
String pack_type;
int pack_size;
int product_price;

public FruitJuice() {
product_code = 0;
flavour = "";
pack_type = "";
pack_size = 0;
product_price = 0;
6
}

public void input() {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter product code: ");
product_code = scanner.nextInt();
System.out.print("Enter flavour: ");
flavour = scanner.next();
System.out.print("Enter pack type: ");
pack_type = scanner.next();
System.out.print("Enter pack size: ");
pack_size = scanner.nextInt();
System.out.print("Enter product price: ");
product_price = scanner.nextInt();
}

public void discount() {


product_price = (int) (0.9 * product_price);
}

public void display() {


System.out.println("Product Code: " + product_code);
System.out.println("Flavour: " + flavour);
System.out.println("Pack Type: " + pack_type);
System.out.println("Pack Size: " + pack_size);
System.out.println("Product Price: " + product_price);
}
}

The International Standard Book Number (ISBN) is a unique numeric book identifier
which is printed on every book. The ISBN is based upon a 10-digit code. The ISBN is
legal if:
1xdigit1 + 2xdigit2 + 3xdigit3 + 4xdigit4 + 5xdigit5 + 6xdigit6 + 7xdigit7 + 8xdigit8 +
9xdigit9 + 10xdigit10 is divisible by 11.
Example: For an ISBN 1401601499
Sum=1×1 + 2×4 + 3×0 + 4×1 + 5×6 + 6×0 + 7×1 + 8×4 + 9×9 + 10×9 = 253
which is divisible by 11.
Write a program to:
(i) input the ISBN code as a 10-digit integer.
(ii) If the ISBN is not a 10-digit integer, output the message “Illegal ISBN” and terminate
the program.
(iii) If the number is 10-digit, extract the digits of the number and compute the sum as
explained above.
If the sum is divisible by 11, output the message, “Legal ISBN”. If the sum is not
divisible by 11, output the message, “Illegal ISBN”. [15]

7
import java.util.Scanner;

public class ISBN {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter ISBN code: ");
int isbnInteger = scanner.nextInt();
String isbn = isbnInteger + "";
if (isbn.length() != 10) {
System.out.println("Ilegal ISBN");
} else {
int sum = 0;
for (int i = 0; i < 10; i++) {
int digit = Integer.parseInt(isbn.charAt(i) +
"");
sum = sum + (digit * (i + 1));
}
if (sum % 11 == 0) {
System.out.println("Legal ISBN");
} else {
System.out.println("Illegal ISBN");
}
}
}
}

Write a program that encodes a word into Piglatin. To translate word into Piglatin word,
convert the word into uppercase and then place the first vowel of the original word as
the start of the new word along with the remaining alphabets. The alphabets present
before the vowel being shifted towards the end followed by “AY”.
Sample Input(1): London Sample Output(1): ONDONLAY
Sample Input(2): Olympics Sample Output(2): OLYMPICSAY [15]

import java.util.Scanner;

public class Piglatin {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a String: ");
String input = scanner.next();
input = input.toUpperCase();
String piglatin = "";
boolean vowelFound = false;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if ((c == 'A' || c == 'E' || c == 'I' || c == 'O' ||
c == 'U') && !vowelFound) {
piglatin = c + piglatin;

8
vowelFound = true;
} else {
piglatin = piglatin + c;
}
}
piglatin = piglatin + "AY";
System.out.println("Piglatin word is " + piglatin);
}
}

Write a program to input 10 integer elements in an array and sort them


In descending order using bubble sort technique. [15]

import java.util.Scanner;

public class BubbleSort {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.println("Enter ten numbers:");
int[] numbers = new int[10];
for (int i = 0; i < 10; i++) {
numbers[i] = scanner.nextInt();
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10 - i - 1; j++) {
if (numbers[j] < numbers[j + 1]) {
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
System.out.println("Sorted Numbers:");
for (int i = 0; i < 10; i++) {
System.out.println(numbers[i]);
}
}

Design a class to overload a function series() as follows: [15]


(i) double series(double n) with one double argument and
returns the sum of the series.
sum = 1/1 + 1/2 + 1/3 + ….. 1/n
9
(ii) double series(double a, double n) with two double arguments
and returns the sum of the series.
sum = 1/a2 + 4/a5 + 7/a8 + 10/a11 ….. to n terms

public class Overload {

public double series(double n) {


double sum = 0;
for (int i = 1; i <= n; i++) {
sum = sum + (1.0 / i);
}
return sum;
}

public double series(double a, double n) {


double sum = 0;
for (int i = 0; i < n; i++) {
sum = sum + ((3 * i + 1.0) / Math.pow(a, 3 * i + 2));
}
return sum;
}
}

Using the switch statement, write a menu driven program: [15]


(i) To check and display whether a number input by the user is
a composite number or not (A number is said to be a composite, if it
has one or more then one factors excluding 1 and the number itself).
Example: 4, 6, 8, 9…
(ii) To find the smallest digit of an integer that is input:
Sample input: 6524
Sample output: Smallest digit is 2
For an incorrect choice, an appropriate error message should be displayed.

import java.util.Scanner;

public class Menu {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.println("Menu");
System.out.println("1. Check composite number");
System.out.println("2. Find smallest digit of a number");
System.out.print("Enter your choice: ");

10
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (isComposite(number)) {
System.out.println("It is a composite
number");
} else {
System.out.println("It is not a composite
number");
}
break;
case 2:
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int smallest = smallestDigit(num);
System.out.println("Smallest digit is " +
smallest);
break;
default:
System.out.println("Incorrect choice");
break;
}
}

public static boolean isComposite(int n) {


for (int i = 2; i < n; i++) {
if (n % i == 0) {
return true;
}
}
return false;
}

public static int smallestDigit(int number) {


int smallest = 9;
while (number > 0) {
int rem = number % 10;
if (rem < smallest) {
smallest = rem;
}
number = number / 10;
}
return smallest;
}
}

11
Define a class called Library with the following description:
Instance variables/data members:
Int acc_num – stores the accession number of the book
String title – stores the title of the book stores the name of the author
Member Methods:
(i) void input() – To input and store the accession number, title and author.
(ii)void compute – To accept the number of days late, calculate and display and fine
charged at the rate of Rs.2 per day.
(iii) void display() To display the details in the following format:
Accession Number Title Author
Write a main method to create an object of the class and call the above member
methods. [ 15]

public class Library {

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();
}

public void compute() throws IOException {


BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter number of days late: ");
int daysLate = Integer.parseInt(br.readLine());;
int fine = 2 * daysLate;
System.out.println("Fine is Rs " + fine);
}

public void display() {


System.out.println("Accession Number\tTitle\tAuthor");
System.out.println(acc_num + "\t" + title + "\t" +
author);
}

public static void main(String[] args) throws IOException {

12
Library library = new Library();
library.input();
library.compute();
library.display();
}
}

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

Does not exceed 1,60,000 Nil

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

Is greater than 8,00,000 [ (TI - 8,00,000 ) *30% ] + 94,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. [15]

import java.util.Scanner;

public class IncomeTax {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter age: ");
int age = scanner.nextInt();
System.out.print("Enter gender: ");
String gender = scanner.next();
System.out.print("Enter taxable income: ");
int income = scanner.nextInt();
if (age > 65 || gender.equals("female")) {
System.out.println("Wrong category");
} else {
double tax;
if (income <= 160000) { tax = 0;
} else if (income > 160000 && income <= 500000) {
tax = (income - 160000) * 10 / 100; } else if (income
>= 500000 && income <= 800000) {
tax = (income - 500000) * 20 / 100 + 34000;
} else {
13
tax = (income - 800000) * 30 / 100 + 94000;
}
System.out.println("Income tax is " + tax);
}
}
}

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 [ 15]

import java.util.Scanner;

public class StringOperations {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a String: ");
String input = scanner.nextLine();
input = input.toUpperCase();
int count = 0;
for (int i = 1; i < input.length(); i++) {
if (input.charAt(i) == input.charAt(i - 1)) {
count++;
}
}
System.out.println(count);
}
}

Design a class to overload a function polygon() as follows:


(i) void polygon(int n, char ch) : with one integer argument and one character type
argument that draws a filled square of side n using the character stored in ch.
(ii) void polygon(int x, int y) : with two integer arguments that draws a filled rectangle of
length x and breadth y, using the symbol ‘@’
(iii)void polygon( ) : with no argument that draws a filled triangle shown below.

Example:
(i) Input value of n=2, ch=’O’
Output:
OO
OO
(ii) Input value of x=2, y=5
Output:

14
@@@@@
@@@@@
(iii) Output:
*
**
*** [15]

public class Overloading {

public void polygon(int n, char ch) {


for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(ch);
}
System.out.println();
}
}

public void polygon(int x, int y) {


for (int i = 1; i <= x; i++) {
for (int j = 1; j <= y; j++) {
System.out.print("@");
}
System.out.println();
}
}

public void polygon() {


for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

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 [15]

import java.util.Scanner;

public class Menu {

15
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Menu");
System.out.println("1. Fibonacci Sequence");
System.out.println("2. Sum of Digits");
System.out.print("Enter choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
int a = 0;
int b = 1;
System.out.print("0 1 ");
for (int i = 3; i <= 10; i++) {
int c = a + b; System.out.print(c + " ");
a = b; b = c; }
break; case 2:
System.out.print("Enter a number: "); int num =
scanner.nextInt(); int sum = 0;
while (num > 0) {
int rem = num % 10;
sum = sum + rem;
num = num / 10;
}
System.out.println("Sum of digits is " + sum);
break;
default:
System.out.println("Invalid Choice");
}
}
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’. [15]

import java.util.Scanner;

public class Cities {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
String[] cities = new String[10];
int[] std = new int[10];
for (int i = 0; i < 10; i++) {
System.out.print("Enter city: ");
cities[i] = scanner.next();
System.out.print("Enter std code: ");
std[i] = scanner.nextInt();
}
System.out.print("Enter city name to search: ");
String target = scanner.next();
16
boolean searchSuccessful = false;
for (int i = 0; i < 10; i++) {
if (cities[i].equals(target)) {
System.out.println("Search successful");
System.out.println("City : " + cities[i]);
System.out.println("STD code : " + std[i]);
searchSuccessful = true;
break;
}
}
Write a program to input and sort the weight of ten people. Sort and display them in
descending order using the selection sort technique. [15]

import java.util.Scanner;

public class SelectionSort {

public void program() {


Scanner scanner = new Scanner(System.in);
int[] weights = new int[10];
System.out.println("Enter weights: ");
for (int i = 0; i < 10; i++) {
weights[i] = scanner.nextInt();
}
for (int i = 0; i < 10; i++) {
int highest = i;
for (int j = i + 1; j < 10; j++) { if
(weights[j] > weights[highest]) {
highest = j;
}
}
int temp = weights[highest];
weights[highest] = weights[i];
weights[i] = temp;
}
System.out.println("Sorted weights:");
for (int i = 0; i < 10; i++) {
System.out.println(weights[i]);
}
}
}
Write a program to input a number and print whether the number is a special number or
not. (A number is said to be a special number, if the sum of the factorial of the digits of
the number is same as the original number). [15]

import java.util.Scanner;

public class SpecialNumber {

public int factorial(int n) {

17
int fact = 1;
for (int i = 1; i <= n; i++) { fact = fact *
i; } return fact; } public int
sumOfDigita(int num) { int sum = 0; while (num >
0) {
int rem = num % 10;
sum = sum + rem;
num = sum / 10;
}
return sum;
}

public boolean isSpecial(int num) {


int fact = factorial(num);
int sum = sumOfDigita(fact);
if (sum == num) {
return true;
} else {
return false;
}
}

public void check() {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isSpecial(num)) {
System.out.println("It is a special number");
} else {
System.out.println("It is not a special number");
}
}

Write a program to accept a word and convert it into lowercase if it is in uppercase, and
display the new word by replacing only the vowels with the character following it. [15]

Example
Sample Input : computer
Sample Output : cpmpvtfr

import java.util.Scanner;

public class StringConversion {

public static void convert() {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a String: ");
String input = scanner.next();

18
input = input.toLowerCase();
String answer = "";
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c
== 'u') {
answer = answer + (char) (c + 1);
} else {
answer = answer + c;
}
}
System.out.println(answer);
}
}
Design a class to overload a function compare ( ) as follows: [15]
(a) void compare (int, int) – to compare two integer values and print the greater of the
two integers.
(b) void compare (char, char) – to compare the numeric value of two character with
higher numeric value
(c) void compare (String, String) – to compare the length of the two strings and print the
longer of the two.

public class Overloading {

public void compare(int a, int b) {


int max = Math.max(a, b);
System.out.println(max);
}

public void compare(char a, char b) {


char max = (char) Math.max(a, b);
System.out.println(max);
}

public void compare(String a, String b) {


if (a.length() > b.length()) {
System.out.println(a);
} else if (a.length() < b.length()) {
System.out.println(b);
} else {
System.out.println(a);
System.out.println(b);
}
}
}

Write a menu driven program to perform the following . (Use switch-case statement)
[15]
(a) To print the series 0, 3, 8, 15, 24 ……. n terms (value of ‘n’ is to be an input by the
user).
19
(b) To find the sum of the series given below:
S = 1/2+ 3/4 + 5/6 + 7/8 … 19/20

import java.util.Scanner;

public class Menu {

public void series1(int n) {


for (int i = 1; i <= n; i++) {
int term = i * i - 1;
System.out.print(term + " ");
}
}

public double series2(int n) {


double sum = 0;
for (int i = 1; i <= n; i++) {
sum = sum + (double) (2 * i - 1) / (2 * i);
}
return sum;
}

public void menu() {


Scanner scanner = new Scanner(System.in);
System.out.println("1. Print 0, 3, 8, 15, 24... n
tersm");
System.out.println("2. Sum of series 1/4 + 3/4 + 7/8
+ ... n terms");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
System.out.print("Enter n: ");
int n = scanner.nextInt();
switch (choice) {
case 1:
series1(n);
break;
case 2:
double sum = series2(n);
System.out.println(sum);
break;
default:
System.out.println("Invalid choice");
}
}

public static void main(String[] args) {


Menu menu = new Menu();
menu.menu();
}
}

20
Define a class ‘Student described as below: [15]
Data members/instance variables : name,age,m1,m2,m3 (marks in 3 subjects),
maximum, average
Member methods :
(i) A parameterized constructor to initialize the data members.
(ii) To accept the details of a student.
(iii) To compute the average and the maximum out of three marks.
(iv) To display the name, age, marks in three subjects, maximum and average.
Write a main method to create an object of a class and call the above member methods.

import java.util.Scanner;

public class Student {

String name;
int age;
int m1, m2, m3;
int maximum;
double average;

public Student() {
}

public Student(String n, int a, int marks1, int marks2, int


marks3, int max, double avg) {
name = n;
age = a;
m1 = marks1;
m2 = marks2;
m3 = marks3;
maximum = max;
average = avg;
}

public void acceptDetails() {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter name: ");
name = scanner.next();
System.out.print("Enter age: ");
age = scanner.nextInt();
System.out.print("Enter marks1: ");
m1 = scanner.nextInt();
System.out.print("Enter marks2: ");
m2 = scanner.nextInt();
System.out.print("Enter marks3: ");
m3 = scanner.nextInt();
}

public void compute() {


average = (m1 + m2 + m3) / 3.0;
maximum = Math.max(m1, (Math.max(m2, m3)));
}
21
public void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks1 " + m1);
System.out.println("Marks2 " + m2);
System.out.println("Marks3 " + m3);
System.out.println("Maximum: " + maximum);
System.out.println("Average: " + average);
}

public static void main(String[] args) {


Student student = new Student();
student.acceptDetails();
student.compute();
student.display();
}

Shasha Travels Pvt. Ltd. gives the following discount to its customers: [15]
Ticket amount Discount
Above Rs 70000 – 18%
Rs 55001 to Rs 70000 – 16%
Rs 35001 to Rs 55000 – 12%
Rs 25001 to Rs 35000 – 10%
less than Rs 25001 – 2%

Write a program to input the name and ticket amount for the customer and calculate
the discount amount and net amount to be paid. Display the output in the following
format for each customer :

1 SL. NO. Name Ticket charges Discount Net amount


21 - - - -

import java.util.Scanner;

public class Travels {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter sno: ");
int sno = scanner.nextInt();
System.out.print("Enter name: ");
String name = scanner.next();
System.out.print("Enter ticket charges: ");
int charges = scanner.nextInt();
int discountPercentage = 0;
if (charges > 70000) {
22
discountPercentage = 18;
} else if (charges >= 55001 && charges <= 70000) {
discountPercentage = 16; } else if (charges >= 35001 &&
charges <= 55000) { discountPercentage =
12; } else if (charges >= 25001 && charges <= 35000) {
discountPercentage = 10;
} else if (charges < 25001) {
discountPercentage = 2;
}
double discount = discountPercentage * charges / 100.0;
double netAmount = charges - discount;
System.out.println("Sl. No. \t Name \t Ticket Charges \t
Discount \t Net Amount");
System.out.println(sno + "\t" + charges + "\t" + discount
+ "\t" + netAmount);
}
}

Write a menu driven program to accept a number and check and display whether it is a
Prime Number or not OR an Automorphic Number or not. (Use switch-case statement).
[15]

(a) Prime number : A number is said to be a prime number if it is divisible only by 1 and
itself and not by any other number. Example : 3,5,7,11,13 etc.

(b) Automorphic number : An automorphic number is the number which is contained in


the last digit(s) of its square.
Example: 25 is an automorphic number as its square is 625 and 25 is present as the last
two digits.

import java.util.Scanner;

public class Menu {

public boolean isPrime(int n) {


for (int i = 1; i < n; i++) {
if (n % i == 0) {
return true;
}
}
return false;
}

public boolean isAutomorphic(int n) {


int square = n * n;
String originalNumber = n + "";
String squareNumber = n + "";

23
String lastDigits =
squareNumber.substring(squareNumber.length() -
originalNumber.length(), squareNumber.length());
return lastDigits.equals(originalNumber);
}

public void menu() {


Scanner scanner = new Scanner(System.in);
System.out.println("Enter 1 for prime number");
System.out.println("Enter 2 for automorphic number");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
System.out.print("Enter a number: ");
int num = scanner.nextInt();
switch (choice) {
case 1:
boolean prime = isPrime(num);
if (prime) {
System.out.println(num + " is a prime
number");
} else {
System.out.println(num + " is not a prime
number");
}
break;
case 2:
boolean automorphic = isAutomorphic(num);
if (automorphic) {
System.out.println(num + " is an automorphic
number");
} else {
System.out.println(num + " is not an
automorphic number");
}
break;
default:
System.out.println("Invalid choice");
}
}

public static void main(String[] args) {


Menu menu = new Menu();
menu.menu();
}
}

Write a program to input a string in uppercase and print the frequency of each
character. [15]

1 INPUT : COMPUTER HARDWARE

24
2 OUTPUT :
3 CHARACTERS FREQUENCY
4A 2
5 C 1
6 D 1
7 E 2
8 H 1
9M 1
10 O 1
11 P 1
12 R 3
13 T 1
14 U 1
15 W 1
import java.util.Scanner;

public class Frequency {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a String: ");
String input = scanner.nextLine();
int[] frequency = new int[26];
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (Character.isUpperCase(ch)) {
frequency[ch - 65]++;
}
}
System.out.println("Characters Frequency");
for (int i = 0; i < 26; i++) {
if (frequency[i] != 0) {
System.out.println((char) (i + 65) + "\t" +
frequency[i]);
}
}
}
}

Write a program to generate a triangle or an inverted triangle till n terms based upon
the user’s choice of triangle to be displayed. [15]

Example 1
Input: Type 1 for a triangle and
type 2 for an inverted triangle
1
Enter the number of terms

25
5
Output:
1
22
333
4444
55555

Example 2:
Input: Type 1 for a triangle and
type 2 for an inverted triangle
2
Enter the number of terms
6
Output:
666666
55555
4444
333
22
1

import java.util.Scanner;

public class Traingle {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Type 1 for a triangle and type 2 for an
inverted triangle: ");
int choice = scanner.nextInt();
System.out.print("Enter number of terms: ");
int n = scanner.nextInt();
if (choice == 1) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
} else if (choice == 2) {
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}

26
System.out.println();
}
}
}

Write a program to input a sentence and print the number of characters found in the
longest word of the given sentence.
For example is S = “India is my country” then the output should be 7. [15]

Ans.
import java.util.Scanner;

public class LongestWord {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = scanner.nextLine();
int longest = 0;
int currentWordLength = 0;
for (int i = 0; i < sentence.length(); i++) {
char ch = sentence.charAt(i);
if (ch == ' ') {
if (currentWordLength > longest) {
longest = currentWordLength;
}
currentWordLength = 0;
} else {
currentWordLength++;
}
}
if (currentWordLength > longest) {
longest = currentWordLength;
}
System.out.println("The longest word has " + longest + "
characters");
}
}

Write a class to overload a function num_calc() as follows: [15]

i) void num_calc(int num, char ch) with one integer argument and one character
argument, computes the square of integer argument if choice ch is ‘s’ otherwise finds
its cube.

27
ii) void num_calc(int a, int b, char ch) with two integer arguments and one character
argument. It computes the product of integer arguments if ch is ‘p’ else adds the
integers.

iii) void num_calc(String s1, String s2) with two string arguments, which prints whether
the strings are equal or not.

public class Overloading {

public void num_calc(int num, char ch) {


if (ch == 's') {
double square = Math.pow(num, 2);
System.out.println("Square is " + square);
} else {
double cube = Math.pow(num, 3);
System.out.println("Cube is " + cube);
}
}

public void num_calc(int a, int b, char ch) {


if (ch == 'p') {
int product = a * b;
System.out.println("Product is " + product);
} else {
int sum = a + b;
System.out.println("Sum is " + sum);
}
}

public void num_calc(String s1, String s2) {


if (s1.equals(s2)) {
System.out.println("Strings are same");
} else {
System.out.println("Strings are different");
}
}
}

Write a menu driven program to access a number from the user and check whether it is
a BUZZ number or to accept any two numbers and to print the GCD of them. [15]

a) A BUZZ number is the number which either ends with 7 is is divisible by 7.

b) GCD (Greatest Common Divisor) of two integers is calculated by continued division


method. Divide the larger number by the smaller; the remainder then divides the
previous divisor. The process is repeated till the remainder is zero. The divisor then
results the GCD.

28
Ans.
import java.util.Scanner;

public class Menu {

public boolean isBuzzNumber(int num) {


int lastDigit = num % 10;
int remainder = num % 7;
if (lastDigit == 7 || remainder == 0) {
return true;
} else {
return false;
}
}

public int gcd(int a, int b) {


int dividend, divisor;
if (a > b) {
dividend = a;
divisor = b;
} else {
dividend = b;
divisor = a;
}
int gcd;
while (true) {
int remainder = dividend % divisor;
if (remainder == 0) {
gcd = divisor;
break;
}
dividend = divisor;
divisor = remainder;
}
return gcd;
}

public void menu() {


Scanner scanner = new Scanner(System.in);
System.out.println("1. Buzz Number");
System.out.println("2. GCD");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
if (choice == 1) {
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isBuzzNumber(num)) {
System.out.println(num + " is a buzz number");
} else {
System.out.println(num + " is not a buzz
number");

29
}
} else if (choice == 2) {
System.out.print("Enter two numbers: ");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int gcd = gcd(num1, num2);
System.out.println("GCD: " + gcd);
} else {
System.out.println("Invalid Choice");
}
}

public static void main(String[] args) {


Menu menu = new Menu();
menu.menu();
}

30

You might also like