deepa x-partb questions
deepa x-partb questions
deepa x-partb questions
PART-B
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.
Write a main method to create an object of the class and call the above member
methods.
Ans.
Program
import java.util.Scanner;
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();
}
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.
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;
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?
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
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:
import java.util.Scanner;
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
}
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;
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;
8
vowelFound = true;
} else {
piglatin = piglatin + c;
}
}
piglatin = piglatin + "AY";
System.out.println("Piglatin word is " + piglatin);
}
}
import java.util.Scanner;
import java.util.Scanner;
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;
}
}
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]
int acc_num;
String title;
String author;
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:
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. [15]
import java.util.Scanner;
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;
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]
import java.util.Scanner;
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;
import java.util.Scanner;
import java.util.Scanner;
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;
}
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;
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.
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;
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;
String name;
int age;
int m1, m2, m3;
int maximum;
double average;
public Student() {
}
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 :
import java.util.Scanner;
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.
import java.util.Scanner;
23
String lastDigits =
squareNumber.substring(squareNumber.length() -
originalNumber.length(), squareNumber.length());
return lastDigits.equals(originalNumber);
}
Write a program to input a string in uppercase and print the frequency of each
character. [15]
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;
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;
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;
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.
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]
28
Ans.
import java.util.Scanner;
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");
}
}
30