0% found this document useful (0 votes)
22 views

Project_Computer (1)

The document contains a series of programming questions and solutions in Java, covering topics such as array manipulation, matrix operations, searching algorithms, class definitions, and method overloading. Each question is followed by a Java code implementation that addresses the specified problem, demonstrating various programming concepts. The questions range from merging arrays to calculating discounts and identifying specific number properties.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Project_Computer (1)

The document contains a series of programming questions and solutions in Java, covering topics such as array manipulation, matrix operations, searching algorithms, class definitions, and method overloading. Each question is followed by a Java code implementation that addresses the specified problem, demonstrating various programming concepts. The questions range from merging arrays to calculating discounts and identifying specific number properties.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Question 1:

Suppose A, B, C are arrays of integers of sizes m, n, m + n


respectively. Give a program to produce a third array C, containing
all the data of array A and B.
import java.util. *;

public class ArrayMerge


{
public static void main(String args[]){
* in = new *(System.in);
System.out.print("Enter size m : ");
int m = in.nextInt();
int A[] = new int[m];

System.out.print("Enter size n : ");


int n = in.nextInt();
int B[] = new int[n];

int C[] = new int[m + n];

System.out.println("Enter elements of array A:");


for(int i = 0; i < m; i++) {
A[i] = in.nextInt();
}

System.out.println("Enter elements of array B:");


for(int i = 0; i < n; i++) {
B[i] = in.nextInt();
}

int idx = 0;
while(idx < A.length) {
C[idx] = A[idx];
idx++;
}

int j = 0;
while(j < B.length) {
C[idx++] = B[j++];
}

System.out.println("Elements of Array C :");


for(int i = 0; i < C.length; i++) {
System.out.print(C[i] + " ");
}
}
}

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 A[][] = {{1, 2, 3, 4} ,


{5, 6, 7, 8} ,
{9, 10, 11, 12} ,
{13, 14, 15, 16}};

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];
}
}
}

System.out.println("Sum of diagonal elements = " + sum);

}
}

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

the resultant array should be : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16


import java.util. *;

public class 2Dto1DArray


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

int A[][] = {{1, 2, 3, 4} ,


{5, 6, 7, 8} ,
{9, 10, 11, 12} ,
{13, 14, 15, 16}};

int B[] = new int[16];


int index = 0;

for(int i = 0; i < 4; i++)


{
for(int j = 0; j < 4; j++)
{
B[index++] = A[i][j];
}
}

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. *;

public class LinearSearch


{
public static void main(String args[]){
* in = new *(System.in);

int X[] = new int[10];


int i = 0;

System.out.println("Enter array elements : ");


for(i = 0; i < 10; i++)
{
X[i] = in.nextInt();
}

System.out.println("Input Array is:");


for (i = 0; i < 10; i++) {
System.out.print(X[i] + " ");
}
System.out.println();

System.out.print("Enter ITEM to search : ");


int item = in.nextInt();
for (i = 0; i < 10; i++) {
if (X[i] == item) {
break;
}
}

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("Searching for " + item);


while (l <= h) {
int m = (l + h) / 2;
System.out.println("Searching in sub-array :");
for(int i = l; i < h; i++)
System.out.print(arr[i] + " ");

System.out.println();

System.out.println("Comparing " + item + " with "


+ arr[m] + " at index " + m);

if (arr[m] < item)


l = m + 1;
else if (arr[m] > item)
h = m - 1;
else {
index = m;
break;
}
}
return index;
}

public static void main(String args[]) {


BinarySearch obj = new BinarySearch();

int X[] = {3, 4, 7, 11, 18, 29, 45, 71, 87, 89, 93, 96,
99};

int index = obj.binSearch(X, 66);


if (index == -1) {
System.out.println("66 not found");
}
else {
System.out.println("66 found at index " + index);
}

index = obj.binSearch(X, 71);


if (index == -1) {
System.out.println("71 not found");
}
else {
System.out.println("71 found at index " + index);
}

}
}

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

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.
import java.util. *;
public class Eshop
{
private String name;
private double price;
private double disc;
private double amount;

public void accept() {


* in = new *(System.in);
System.out.print("Enter item name: ");
name = in.nextLine();
System.out.print("Enter price of item: ");
price = in.nextDouble();
}

public void calculate() {


double d = 0.0;
if (price < 1000)
d = 0.0;
else if (price <= 25000)
d = 5.0;
else if (price <= 57000)
d = 7.5;
else if (price <= 100000)
d = 10.0;
else
d = 15.0;

disc = price * d / 100.0;


amount = price - disc;

public void display() {


System.out.println("Item Name: " + name);
System.out.println("Net Amount: " + amount);
}

public static void main(String args[]) {


Eshop obj = new Eshop();
obj.accept();
obj.calculate();
obj.display();
}
}

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. *;

public class CountVowels


{
public static void main(String args[])
{
* in = new *(System.in);
System.out.print("Enter the string: ");
String str = in.nextLine();
str = str.toUpperCase();
str += " ";
int count = 0;
int len = str.length();

for (int i = 0; i < len - 1; i++)


{
char ch = str.charAt(i);
if(ch == 'A'
|| ch == 'E'
|| ch == 'I'
|| ch == 'O'
|| ch == 'U')
count++;
}

System.out.println("String : " + str);


System.out.println("Number of vowels : " + count);
}
}

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. *;

public class DuckNumber


{
public static void main(String args[])
{
* in = new *(System.in);
System.out.print("Enter a number: ");
int num = in.nextInt();
int n = num;
int count = 0;

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:

void display( ): To print the following format using nested loop

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. *;

public class MethodOverload


{
public void display()
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
}

public void display(int n)


{
while( n != 0)
{
int d = n % 10;
System.out.println(Math.sqrt(d));
n = n / 10;
}
}

public static void main(String args[])


{
MethodOverload obj = new MethodOverload();
* in = new *(System.in);

System.out.println("Pattern: ");
obj.display();

System.out.print("Enter a number: ");


int num = in.nextInt();
obj.display(num);
}
}

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

>= 300 Science and Computer


mks stream

>= 200 and < 300 Commerce and Computer

>= 75 and < 200 Arts and Animation

< 75 Try Again

void print() – Display student name, age, mks and stream allocated.
Call all the above methods in main method using an object.
import java.util. *;

public class Student


{
private String name;
private int age;
private double mks;
private String stream;

public void accept()


{
* in = new *(System.in);
System.out.print("Enter student name: ");
name = in.nextLine();
System.out.print("Enter age: ");
age = in.nextInt();
System.out.print("Enter marks: ");
mks = in.nextDouble();
}

public void allocation()


{
if (mks < 75)
stream = "Try again";
else if (mks < 200)
stream = "Arts and Animation";
else if (mks < 300)
stream = "Commerce and Computer";
else
stream = "Science and Computer";
}

public void print()


{
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks: " + mks);
System.out.println("Stream allocated: " + stream);
}

public static void main(String args[]) {


Student obj = new Student();
obj.accept();
obj.allocation();
obj.print();
}
}

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. *;

public class MethodOverload


{
public void print()
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= 4; j++)
{
System.out.print(i + " ");
}
System.out.println();
}
}

public void print(int n)


{
int d = 0;
int evenSum = 0;
int oddSum = 0;
while( n != 0)
{
d = n % 10;
if (d % 2 == 0)
evenSum += d;
else
oddSum += d;
n = n / 10;
}

if(evenSum == oddSum)
System.out.println("Lead number");
else
System.out.println("Not a lead number");
}

public static void main(String args[])


{
MethodOverload obj = new MethodOverload();
* in = new *(System.in);

System.out.println("Pattern: ");
obj.print();

System.out.print("Enter a number: ");


int num = in.nextInt();
obj.print(num);
}
}

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. *;

public class Count


{
public static void main(String args[])
{
* in = new *(System.in);
System.out.println("Enter a string:");
String str = in.nextLine();

int len = str.length();

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++;
}

System.out.println("No. of Digits = " + dc);


System.out.println("No. of Alphabets = " + ac);
System.out.println("No. of Special Characters = " + 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. *;

public class ASCIIVal


{
public static void main(String[] args)
{
* in = new *(System.in);
char ch[] = new char[10];
int len = ch.length;

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];

for (int i = 1; i < len; i++)


{
if (ch[i] > h)
{
h = ch[i];
}

if (ch[i] < l)
{
l = ch[i];
}
}

System.out.println("Character with highest ASCII value: " +


h);
System.out.println("Character with lowest ASCII value: " +
l);
}

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. *;

public class ChangeCase


{
public static void main(String args[])
{
* in = new *(System.in);
System.out.println("Enter a string:");
String str = in.nextLine();
int len = str.length();
String rev = "";

for (int i = 0; i < len; i++)


{
char ch = str.charAt(i);
if (Character.isLetter(ch))
{
if(Character.isUpperCase(ch))
{
rev += Character.toLowerCase(ch);
}
else
{
rev += Character.toUpperCase(ch);
}
}
else
{
rev += ch;
}
}

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. *;

public class StringMerge


{
public static void main(String args[])
{
* in = new *(System.in);
System.out.println("Enter String 1: ");
String s1 = in.nextLine();
System.out.println("Enter String 2: ");
String s2 = in.nextLine();
String str = "";
int len = s1.length();

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:

Average Marks Remark

85 — 100 Excellent
Average Marks Remark

75 — 84 Distinction

60 — 74 First Class

40 — 59 Pass

Less than 40 Poor

Answer
import java.util. *;

public class AvgMarks


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

* in = new *(System.in);
System.out.print("Enter number of students: ");
int n = in.nextInt();

int rollNo[] = new int[n];


String name[] = new String[n];
int s1[] = new int[n];
int s2[] = new int[n];
int s3[] = new int[n];
double avg[] = new double[n];

for (int i = 0; i < n; i++) {


System.out.println("Enter student " + (i+1) + "
details:");
System.out.print("Roll No: ");
rollNo[i] = in.nextInt();
in.nextLine();
System.out.print("Name: ");
name[i] = in.nextLine();
System.out.print("Subject 1 Marks: ");
s1[i] = in.nextInt();
System.out.print("Subject 2 Marks: ");
s2[i] = in.nextInt();
System.out.print("Subject 3 Marks: ");
s3[i] = in.nextInt();
avg[i] = (s1[i] + s2[i] + s3[i]) / 3.0;
}
System.out.println("Roll No\tName\tRemark");
for (int i = 0; i < n; i++) {
String remark;
if (avg[i] < 40)
remark = "Poor";
else if (avg[i] < 60)
remark = "Pass";
else if (avg[i] < 75)
remark = "First Class";
else if (avg[i] < 85)
remark = "Distinction";
else
remark = "Excellent";
System.out.println(rollNo[i] + "\t"
+ name[i] + "\t"
+ remark);
}
}
}

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. *;

public class PronicNumber


{
public static void main(String args[]) {
* in = new *(System.in);
System.out.print("Enter the number to check: ");
int num = in.nextInt();
boolean isPronic = false;

for (int i = 1; i <= num - 1; i++) {


if (i * (i + 1) == num) {
isPronic = true;
break;
}
}

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. *;

public class String


{
public static void main(String args[]) {
* in = new *(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();
String word = "";

for (int i = 0; i < str.length(); i++) {


if (i == 0 || str.charAt(i - 1) == ' ') {
word += Character.toUpperCase(str.charAt(i));
}
else {
word += str.charAt(i);
}
}

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. *;

public class FilepathSplit


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

* in = new *(System.in);
System.out.print("Enter full path: ");
String filepath = in.next();

char pathSep = '\\';


char dotSep = '.';
int pathSepIdx = filepath.lastIndexOf(pathSep);
System.out.println("Path:\t\t" + filepath.substring(0,
pathSepIdx));

int dotIdx = filepath.lastIndexOf(dotSep);


System.out.println("File Name:\t" +
filepath.substring(pathSepIdx + 1, dotIdx));

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 ₹

Does not exceed Rs. 1,60,000 Nil

Is greater than Rs. 1,60,000 and less than


(TI - 1,60,000) x 10%
or equal to Rs. 5,00,000.

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

[(TI - 8,00,000) x 30%]


Is greater than Rs. 8,00,000
+ 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.
Answer
import java.util. *;

public class IncomeTax


{
public static void main(String args[]) {
* in = new *(System.in);
System.out.print("Enter Gender(male/female): ");
String gender = in.nextLine();
System.out.print("Enter Age: ");
int age = in.nextInt();
System.out.print("Enter Taxable Income: ");
double ti = in.nextDouble();
double tax = 0.0;

if (age > 65 || gender.equalsIgnoreCase("female")) {


System.out.print("Wrong Category");
}
else {
if (ti <= 160000)
tax = 0;
else if (ti <= 500000)
tax = (ti - 160000) * 10 / 100;
else if (ti <= 800000)
tax = 34000 + ((ti - 500000) * 20 / 100);
else
tax = 94000 + ((ti - 800000) * 30 / 100);

System.out.println("Tax Payable: " + tax);


}
}
}

Output

You might also like