Section - B
Question - 3
Define a class called with the following specifications:
Class name: Eshop
Member variables:
String name: name of the item purchased
double price: Price of the item purchased
Member methods:
void accept(): Accept the name and the price of the item using the methods of Scanner class.
void calculate(): To calculate the net amount to be paid by a customer, based on the following criteria:
| Price | Discount |
|---|---|
| 1000 - 25000 | 5.0% |
| 25001 - 57000 | 7.5% |
| 57001 - 100000 | 10.0% |
| More than 100000 | 15.0% |
void display(): To display the name of the item and the net amount to be paid.
Write the main method to create an object and call the above methods.
Answer
import java.util.Scanner;
class Eshop {
String name;
double price;
void accept() {
Scanner sc = new Scanner(System.in);
name = sc.nextLine();
price = sc.nextDouble();
}
double calculate() {
double discount = 0.0;
if (price > 100000) {
discount = 15.0;
} else if (price > 57000) {
discount = 10.0;
} else if (price > 25000) {
discount = 7.5;
} else if (price > 1000) {
discount = 5.0;
}
return ((100.0 - discount) / 100.0) * price;
}
void display() {
System.out.println("Name of item: " + name + "\nNet amount to be paid: Rs. " + calculate());
}
public static void main(String[] args) {
Item item = new Item();
item.accept();
item.display();
}
}
Question - 4
Define a class to accept values in integer array of size 10. Sort them in an ascending order using selection sort technique. Display the sorted array.
import java.util.Scanner;
public class ArraySort {
public static void main(String args[]) {
int[] nums = new int[10];
// Input
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
nums[i] = sc.nextInt();
}
// Sort
for (int step = 0; step < size - 1; step++) {
int min_idx = step;
for (int i = step + 1; i < size; i++) {
if (array[i] < array[min_idx]) {
min_idx = i;
}
}
int temp = array[step];
array[step] = array[min_idx];
array[min_idx] = temp;
}
// Display
for (int i = 0; i < 9; i++) {
System.out.print(nums[i] + ", ");
}
System.out.println(nums[9]);
}
}
Question - 5
Define a class to accept a string and convert it into uppercase. Count and display the number of vowels in it.
Input: robotics
Output: ROBOTICS
Number of vowels: 3
Answer
import java.util.Scanner;
class UpperCaseVowel {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
System.out.println("Upper Case: " + input.toUpperCase());
int count = 0;
for (int i = 0; i < input.length(); i++) {
switch (input.charAt(i)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
count++;
break;
}
}
System.out.println("Number of Vowels: " + count);
}
}
Question - 6
Define a class to accept values into a 3×3 array and check if it is a special array. An array is a special array if the sum of the even elements = sum of the odd elements.
Example:
A[][]={{ 4 ,5, 6}, { 5 ,3, 2}, { 4, 2, 5}};
Sum of even elements = 4+6+2+4+2 =18
Sum of odd elements= 5+5+3+5=18
Answer
import java.util.Scanner;
class SpecialArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] array = int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print("Value for item in row " + j + " and column " + i + " > ");
array[i][j] = sc.nextInt();
}
}
int evenSum = 0, oddSum = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (array[i][j] % 2 == 0) {
evenSum += array[i][j];
} else {
oddSum += array[i][j];
}
}
}
System.out.println("is special array? " + (oddSUm == evenSum));
}
}
Question - 7
Define a class to accept a 3 digit number and check whether it is a duck number or not. Note: A number is a duck number if it has zero in it.
Example1:
Input: 2083
Output: Invalid
Example 2:
Input: 103
Output: Duck number
Answer
import java.util.Scanner;
class {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if (num >= 100 && num <= 999) {
do {
if (n % 10 == 0) {
System.out.println("Duck Number");
return; // stop function execution.
}
n = n / 10;
} while (n > 0);
}
System.out.println("Invalid")
}
}
Question - 8
Define a class to overload the method display as follows:
void display(): To print the following format using nested loop
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
void display(int n): To print the square root of each digit of the given number
Example: n = 4329
output – 3.0
1.414213562
1.732050808
2.0
Answer
class TriangleAndSqrts {
void display() {
for (int i = 0; i < 5; i++) {
for (int j = 1; j <= i + 1; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
void display(int n) {
do {
System.out.println(Math.sqrt(n % 10));
n = n / 10;
} while (n > 0);
}
}