0% found this document useful (0 votes)
46 views25 pages

Computer 25

Uploaded by

aadhyasingh504
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)
46 views25 pages

Computer 25

Uploaded by

aadhyasingh504
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/ 25

1. Define a class to accept 10 characters from a user.

Using bubble sort


technique arrange them in ascending order. Display the sorted array and
original array.

import java.util.*;
public class Program
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
char ch[] = new char[10];
System.out.println("Enter 10 characters:");
for (int i = 0; i < ch.length; i++) {
ch[i] = in.nextLine().charAt(0);
}

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

//Bubble Sort
for (int i = 0; i < ch.length - 1; i++) {
for (int j = 0; j < ch.length - 1 - i; j++) {
if (ch[j] > (ch[j + 1])) {
char t = ch[j];
ch[j] = ch[j + 1];
ch[j + 1] = t;
}
}
}

System.out.println("\nSorted Array");
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i] + " ");
}
}
}
Output:
2. 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 Program
{
public static void main(String args[])
{
Scanner in = new Scanner(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:
3. Define a class to accept values into an array of double data type of size
20. Accept a double value from user and search in the array using linear
search method. If value is found display message "Found" with its
position where it is present in the array. Otherwise display message "not
found".

import java.util.*;
public class Program
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);

double arr[] = new double[20];


int l = arr.length;
int i = 0;

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


for (i = 0; i < l; i++)
{
arr[i] = in.nextDouble();
}

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


double n = in.nextDouble();

for (i = 0; i < l; i++)


{
if (arr[i] == n)
{
break;
}
}

if (i == l)
{
System.out.println("Not found");
}
else
{
System.out.println(n + " found at index " + i);
}
}
}

Output:
4. Define a class to accept values in integer array of size 10. Find sum of
one digit number and sum of two-digit numbers entered. Display them
separately.
Example:
Input: a[ ] = {2, 12, 4, 9, 18, 25, 3, 32, 20, 1}
Output:
Sum of one-digit numbers : 2 + 4 + 9 + 3 + 1 = 19
Sum of two-digit numbers : 12 + 18 + 25 + 32 + 20 = 107

import java.util.*;
public class Program
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int oneSum = 0, twoSum = 0, d = 0;
int arr[] = new int[10];
System.out.println("Enter 10 numbers");
int l = arr.length;

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


{
arr[i] = in.nextInt();
}

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


{
if(arr[i] >= 0 && arr[i] < 10 )
oneSum += arr[i];
else if(arr[i] >= 10 && arr[i] < 100 )
twoSum += arr[i];
}

System.out.println("Sum of 1 digit numbers = "+ oneSum);


System.out.println("Sum of 2 digit numbers = "+ twoSum);

}
}
Output:
5. Define a class to accept a number from user and check if it is an EvenPal
number or not.
(The number is said to be EvenPal number when number is palindrome
number (a number is palindrome if it is equal to its reverse) and sum of its digits
is an even number.)
Example: 121 – is a palindrome number
Sum of the digits – 1+2+1 = 4 which is an even number

import java.util.*;
public class Program
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

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


int num = in.nextInt();
int org = num;
int rev = 0;
int sum = 0;

while (num > 0) {


int d = num % 10;
rev = rev * 10 + d;
sum += d;
num /= 10;
}

if (org == rev && sum % 2 == 0) {


System.out.println(org + " is an EvenPal number.");
} else {
System.out.println(org + " is not an EvenPal number.");
}
}
}

Output:
6. Define a class to accept values into an integer array of order 4 x 4 and
check whether it is a DIAGONAL array or not. An array is DIAGONAL
if the sum of the left diagonal elements equals the sum of the right
diagonal elements. Print the appropriate message.
Example:
3425
2523
5327
1371
Sum of the left diagonal element = 3 + 5 + 2 + 1 = 11
Sum of the right diagonal element = 5 + 2 + 3 + 1 = 11
import java.util.*;

public class Program


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int[][] arr = new int[4][4];

System.out.println("Enter elements for 4x4 DDA:");


for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
arr[i][j] = in.nextInt();
}
}

int lSum = 0;
int rSum = 0;

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


lSum += arr[i][i];
rSum += arr[i][3 - i];
}

if (lSum == rSum) {
System.out.println("The array is a DIAGONAL array.");
} else {
System.out.println("The array is NOT a DIAGONAL array.");
}
}
}
Output:
7. Define a class pin code and store the given pin codes in a single
dimensional array. Sort these pin codes in ascending order using the
Selection Sort technique only. Display the sorted array.

110061, 110001, 110029, 110023, 110055, 110006, 110019, 110033

Import java.io.*;
public class Program
{
public static void main(String args[]) {
int[] arr = {110061, 110001,
110029, 110023,
110055, 110006,
110019, 110033};

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


{
int idx = i;
for (int j = i + 1; j < 8; j++)
{
if (arr[j] < arr[idx])
idx = j;
}

int t = arr[i];
arr[i] = arr[idx];
arr[idx] = t;
}

System.out.println("Sorted Array:");
for (int i = 0; i < 8; i++)
{
System.out.print(arr[i] + " ");
}

}
}
Output:
8. DTDC a courier company charges for the courier based on the weight of
the parcel. Define a class with the following specifications:
Class name: courier
Member variables:
name – name of the customer
weight – weight of the parcel in kilograms
address – address of the recipient
bill – amount to be paid
type – 'D'- domestic, 'I'- international
Member methods:
void accept ( ) — to accept the details using the methods of the Scanner
class only.
void calculate ( ) — to calculate the bill as per the following criteria:

Weight in Kgs Rate per Kg

First 5 Kgs Rs.800

Next 5 Kgs Rs.700

Above 10 Kgs Rs.500

An additional amount of Rs.1500 is charged if the type of the courier is I


(International)
void print ( ) — To print the details
void main ( ) — to create an object of the class and invoke the methods

import java.util.*;
public class Program
{
private String name;
private double weight;
private String address;
private double bill;
private char type;

public void accept() {


Scanner in = new Scanner(System.in);

System.out.print("Enter customer's name: ");


name = in.nextLine();
System.out.print("Enter recipient's address: ");
address = in.nextLine();

System.out.print("Enter parcel weight (in Kgs): ");


weight = in.nextDouble();

System.out.println("Enter courier type");


System.out.print("D (Domestic), I (International): ");
type = in.next().charAt(0);
}

public void calculate() {


if (weight <= 5) {
bill = weight * 800;
} else if (weight <= 10) {
bill = (5 * 800) + ((weight - 5) * 700);
} else {
bill = (5 * 800) + (5 * 700) + ((weight - 10) * 500);
}

if (type == 'I') {
bill += 1500;
}
}

public void print() {


System.out.println("Customer's Name: " + name);
System.out.println("Parcel Weight: " + weight + " Kgs");
System.out.println("Recipient's Address: " + address);
System.out.println("Type of Courier: " + type);
System.out.println("Bill Amount: Rs." + bill);
}

public static void main(String[] args) {


courier obj = new courier();
obj.accept();
obj.calculate();
obj.print();
}
}

Output:
9. Define a class to accept a String and print if it is a Super String or not. A
String is Super if the number of uppercase letters are equal to the number
of lowercase letters. [Use Character and String methods only]
Example: “COmmITmeNt”
Number of uppercase letters = 5
Number of lowercase letters = 5
String is a Super String

import java.util.*;
class Program
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the string: ");
String s = in.nextLine();
int upper = 0;
int lower = 0;
for(int i = 0; i < s.length(); i++)
{
char ch = s.charAt(i);
if(Character.isUpperCase(ch))
upper++;
else if(Character.isLowerCase(ch))
lower++;
}
if(upper == lower)
System.out.println("String is a Super String");
else
System.out.println("String is not a Super String");
}
}

Output:
10. Define a class to initialize the following data in an array.
Search for a given character input by the user, using the Binary Search
technique.
Print “Search successful” if the character is found otherwise print “Search
is not successful”.
‘A’, ‘H’, ‘N’, ‘P’, ‘S’, ‘U’, ‘W’, ‘Y’, ‘Z’, ‘b’, ‘d’

import java.util.*;
class Program{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
char a[] = {'A', 'H', 'N', 'P', 'S', 'U', 'W', 'Y', 'Z', 'b', 'd'};
System.out.print("Character to be searched: ");
char key = in.nextLine().charAt(0);
int low = 0;
int high = a.length - 1;
int mid = 0;
while(low <= high){
mid = (low + high) / 2;
if(key == a[mid])
break;
else if(key < a[mid])
high = mid - 1;
else
low = mid + 1;
}
if(low > high)
System.out.println("Search is not successful");
else
System.out.println("Search is successful");
}
}
Output:
11. Define a class to accept values into a 4 × 4 integer array. Calculate and
print the NORM of the array. NORM is the square root of sum of squares of all
elements.

14.3
11.1 12.2 13.1
18.6
15.5 16.2 17.1
22.2
19.3 20.6 21.1
26.3
23.3 24.4 25.6
Sum of squares of elements = 1 + 4 + 1 + 9 + 25 + 4 + 1 + 36 + 9 + 36 + 1 + 4 +
9 + 16 + 36 + 9 = 201
NORM = Square root of 201 = 14.177446878757825

import java.util.*;
class Program{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int a[][] = new int[4][4];
int sum = 0;
System.out.println("Enter array elements:");
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
a[i][j] = Integer.parseInt(in.nextLine());
sum += a[i][j] * a[i][j];
}
}
double root = Math.sqrt(sum);
System.out.println("NORM = " + root);
}
}
Output:
12. Define a class named CloudStorage with the following specifications:
Member Variables:
int acno – stores the user’s account number
int space – stores the amount of storage space in GB purchased by the user
double bill – stores the total price to be paid by the user
Member Methods:
void accept() – prompts the user to input their account number and storage
space using Scanner class methods only.
void calculate() – calculates the bill total price based on the storage space
purchased using the pricing table provided:

Storage range Price per GB (Rs)

First 15 GB 15

Next 15 GB 13

Above 30 GB 11

void display() – displays the account number, storage space and bill to be paid.
Write a main method to create an object of the class and invoke the methods of
the class with respect to the object.

import java.util.Scanner;
class CloudStorage{
int acno;
int space;
double bill;
public void accept(){
Scanner in = new Scanner(System.in);
System.out.print("Account number: ");
acno = Integer.parseInt(in.nextLine());
System.out.print("Space: ");
space = Integer.parseInt(in.nextLine());
}
public void calculate(){
if(space <= 15)
bill = space * 15;
else if(space <= 30)
bill = 225 + (space - 15) * 13;
else
bill = 420 + (space - 30) * 11;
}
public void display(){
System.out.println("Account No. " + acno);
System.out.println("Storage space: " + space);
System.out.println("Bill: " + bill);
}
public static void main(String[] args){
CloudStorage obj = new CloudStorage();
obj.accept();
obj.calculate();
obj.display();
}
}
Output:

You might also like