Project_Computer (1)
Project_Computer (1)
int idx = 0;
while(idx < A.length) {
C[idx] = A[idx];
idx++;
}
int j = 0;
while(j < B.length) {
C[idx++] = B[j++];
}
Output
Question 2:
Let A(n x n) that are not diagonal array. Write a program to find the
sum of all the elements which lie on either diagonal. For example, for
the matrix shown below, your program should output 68 = (1 + 6 + 11
+ 16 + 4 + 7 + 10 + 13):
import java.util. *;
public class DDADiagonalSum
{
public static void main(String args[]){
int sum = 0;
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
if (i == j || (i + j) == 3) {
sum += A[i][j];
}
}
}
}
}
Output
Question 3:
From a two-dimensional array A[4][4], write a program to prepare a
one-dimensional array B[16] that will have all the elements of A if
they are stored in row-major form. For example, for the following
array: int A[][] = {{1, 2, 3, 4} ,
{5, 6, 7, 8} ,
{9, 10, 11, 12} ,
{13, 14, 15, 16}};
System.out.println("Array B :");
for(int i = 0; i < 16; i++)
{
System.out.print(B[i] + " ");
}
}
}
Output
Question 4
Write a program to search for an ITEM linearly in array X[10].
Answer
import java.util. *;
if (i == 10) {
System.out.println("Search Unsuccessful");
}
else {
System.out.println("Search Successful");
System.out.println(item + " is present at index " + i);
}
}
}
Output
Question 5
Write a program to search for 66 and 71 in the array :
X[] = {3, 4, 7, 11, 18, 29, 45, 71, 87, 89, 93, 96, 99};
Make use of binary search technique. Also give the intermediate
results while executing this algorithm.
public class BinarySearch
{
public int binSearch(int arr[], int item) {
int l = 0, index = -1;
int h = arr.length - 1;
System.out.println();
int X[] = {3, 4, 7, 11, 18, 29, 45, 71, 87, 89, 93, 96,
99};
}
}
Output
Question 6:
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 * class.
void calculate(): To calculate the net amount to be paid by a
customer, based on the following criteria:
Price Discount
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.
import java.util. *;
public class Eshop
{
private String name;
private double price;
private double disc;
private double amount;
Output
Question 7:
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
import java.util. *;
Output
Question 8:
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.
Example 1:
Input: 2083
Output: Invalid
Example 2:
Input: 103
Output: Duck number
import java.util. *;
while (n != 0) {
count++;
n = n / 10;
}
if (count == 3)
{
n = num;
boolean isDuck = false;
while(n != 0)
{
if(n % 10 == 0)
{
isDuck = true;
break;
}
n = n / 10;
}
if (isDuck) {
System.out.println("Duck Number");
}
else {
System.out.println("Not a Duck Number");
}
}
else {
System.out.println("Invalid");
}
}
}
Output
Question 9:
Define a class to overload the method display as follows:
Explain
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
import java.util. *;
System.out.println("Pattern: ");
obj.display();
Output
Question 10
Design a class with the following specifications:
Class name: Student
Member variables:
name — name of student
age — age of student
mks — marks obtained
stream — stream allocated
(Declare the variables using appropriate data types)
Member methods:
void accept() — Accept name, age and marks using methods of *
class.
void allocation() — Allocate the stream as per following criteria:
mks stream
void print() – Display student name, age, mks and stream allocated.
Call all the above methods in main method using an object.
import java.util. *;
Output
Question 11:
Define a class to overload the function print as follows:
void print() - to print the following format
Explain
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
void print(int n) - To check whether the number is a lead number. A
lead number is the one whose sum of even digits are equal to sum of
odd digits.
e.g. 3669
odd digits sum = 3 + 9 = 12
even digits sum = 6 + 6 = 12
3669 is a lead number.
import java.util. *;
if(evenSum == oddSum)
System.out.println("Lead number");
else
System.out.println("Not a lead number");
}
System.out.println("Pattern: ");
obj.print();
Output
Question 12:
Define a class to accept a String and print the number of digits,
alphabets and special characters in the string.
Example:
S = "KAPILDEV@83"
Output:
Number of digits – 2
Number of Alphabets – 8
Number of Special characters – 1
import java.util. *;
int ac = 0;
int sc = 0;
int dc = 0;
char ch;
for (int i = 0; i < len; i++) {
ch = str.charAt(i);
if (Character.isLetter(ch))
ac++;
else if (Character.isDigit(ch))
dc++;
else if (!Character.isWhitespace(ch))
sc++;
}
}
}
Output
Question 13:
Define a class to declare a character array of size ten. Accept the
characters into the array and display the characters with highest and
lowest ASCII (American Standard Code for Information Interchange)
value.
EXAMPLE :
INPUT:
'R', 'z', 'q', 'A', 'N', 'p', 'm', 'U', 'Q', 'F'
OUTPUT :
Character with highest ASCII value = z
Character with lowest ASCII value = A
import java.util. *;
System.out.println("Enter 10 characters:");
for (int i = 0; i < len; i++)
{
ch[i] = in.nextLine().charAt(0);
}
char h = ch[0];
char l = ch[0];
if (ch[i] < l)
{
l = ch[i];
}
}
Output
Qus:14: Define a class to accept a string, and print the characters
with the uppercase and lowercase reversed, but all the other
characters should remain the same as before.
EXAMPLE:
INPUT : WelCoMe_2022
OUTPUT : wELcOmE_2022
import java.util. *;
System.out.println(rev);
}
}
Output
Question 15:
Define a class to accept two strings of same length and form a new word in such a way that,
the first character of the first word is followed by the first character of the second word and so
on.
Example :
Input string 1 – BALL
Input string 2 – WORD
OUTPUT : BWAOLRLD
import java.util. *;
if(s2.length() == len)
{
for (int i = 0; i < len; i++)
{
char ch1 = s1.charAt(i);
char ch2 = s2.charAt(i);
str = str + ch1 + ch2;
}
System.out.println(str);
}
else
{
System.out.println("Strings should be of same
length");
}
}
}
Output
Qus:16: Write a program to input and store roll numbers, names and
marks in 3 subjects of n number of students in five single
dimensional arrays and display the remark based on average marks
as given below:
85 — 100 Excellent
Average Marks Remark
75 — 84 Distinction
60 — 74 First Class
40 — 59 Pass
Answer
import java.util. *;
* in = new *(System.in);
System.out.print("Enter number of students: ");
int n = in.nextInt();
Output
Question 17:
Write a program to input a number and check and print whether it is
a Pronic number or not. (Pronic number is the number which is the
product of two consecutive integers)
Examples:
12 = 3 x 4
20 = 4 x 5
42 = 6 x 7
Answer
import java.util. *;
if (isPronic)
System.out.println(num + " is a pronic number");
else
System.out.println(num + " is not a pronic number");
}
}
Output
Question 18
Write a program in Java to accept a string in lower case and change
the first letter of every word to upper case. Display the new string.
Sample input: we are in cyber world
Sample output: We Are In Cyber World
Answer
import java.util. *;
System.out.println(word);
}
}
Output
Question 19:
Write a program to assign a full path and file name as given below.
Using library functions, extract and output the file path, file name and
file extension separately as shown.
Input
C:\Users\admin\Pictures\flower.jpg
Output
Path: C:\Users\admin\Pictures\
File name: flower
Extension: jpg
Answer
import java.util. *;
* in = new *(System.in);
System.out.print("Enter full path: ");
String filepath = in.next();
System.out.println("Extension:\t" +
filepath.substring(dotIdx + 1));
}
}
Output
Question 20:
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 Rs. 5,00,000 and less than [(TI - 5,00,000) x 20%]
or equal to Rs. 8,00,000 + 34,000
Output