Computer Project 2020

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

COMPUTER PROJECT 2020-2021

NAME- ANSH YADAV

CLASS- 10

SEC- E

TOPIC- ANY 15 PROGRAM


ASSIGNMENT
ACKNOWLEDGEMENT
I would like to express my special thanks of gratitude to
my teacher M.r.s Jaya Meghwani as well as our
principal M.r.s Manjit Batra who gave me the golden
opportunity to do this wonderful computer project the
topic any 15 programs assignment, which also helped
me in doing a lot of Research and i came to know about
so many new things obout java programming .
I am really thankfull of them.
Secondly I would also like to thanks my parents and my
friends who help me lot in finishing within the limited
time.

THANKS AGAIN TO ALL WHO HELPED ME

-MADE BY ANSH
YADAV
INDEX
Serials no. Date Title Teacher’s Sign
1 1/10/2020 Prg-1
2 1/10/2020 Prg-2
3 1/10/2020 Prg-3
4 1/10/2020 Prg-4
5 1/10/2020 Prg-5
6 1/10/2020 Prg-6

7 2/10/2020 Prg-7
8 2/10/2020 Prg-8
9 2/10/2020 Prg-9
10 2/10/2020 Prg-10
11 2/10/2020 Prg-11
12 2/10/2020 Prg-12
13 3/10/2020 Prg-13
14 3/10/2020 Prg-14

15 3/10/2020 Prg-15
PROGRAMS OF JAVA

Question 1.
Using the switch-case statement, write a menu driven program to do the following : [15]
(a) To generate and print Letters from A to Z and their Unicode Letters Unicode

(b) Display the following pattern using iteration (looping) statement: 1

Solution.
import java.io.*;
import java.util.*;
class SwitchCase {
public static void main(String args[ ]) {
Scanner sc = new Scanner (System.in);
System.out.println(” 1. Enter 1 for Unicode:”);
System.out.prindn(” 2. Enter 2 for Pattern:”);
System.out.println(“Enter your choice:”);
int choice sc.nextlntO;
switch(choice){
case 1:
char ch;
System.out.println( “Letters \t Unicode”);
for (ch = ‘A’; ch < = ‘Z’; ch+ +) {
System.out.println(ch +”\t” + (int)ch);
}
break;
case 2:
int i, j;
for (i = 1; i < = 5; i+ +) {
for (j = 1; j < = i; j + +)
{
System.out.print(j + “”);.
}
System.out.printlnO;
}
break;
default:
System.out.println(“Wrong choice entered:”);
}

Question 2.

Write a program to input a sentence and convert it into uppercase and count and display the total
number of words starting with a letter ‘A’. [15]
Example:
Sample Input: ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY ARE EVER
CHANGING.
Sample Output : Total number of words starting with letter A’ = 4.
Solution.
import java.io.*;
import java.util.*;
class UpperCount {
public static void main(String args[ ]) {
int i, a;
Scanner sc = new Scanner(System.in);
String str, str1, str2;
System.out.prindn(“Enter sentence::”);
str = sc.nextLine();
strl = str.toUpperCaseO; ‘
str2 = “” + strl;
a = 0; ,
for (i = 0; i < = str2.1ength(); i+ +) {
if(str2.charAt(i) == ‘ ‘)
if(str2.charAt(i + 1) == ‘A’);
a+ +;
}
System.out.println(“Total number of words starting with letter ‘A’::” +a);
}

Question 3.

A tech number has even number of digits. If the number is split in two equal halves, then the square of
sum of these halves is equal to the number itself. Write a program to generate and print all four digit
tech numbers. [15]
Example :
Consider the number 3025
Square of sum of the halves of 3025 = (30+25)2
= (55)2
= 3025 is a tech number.
Solution.
import java.io.*;
import java.util.*;
class TechNumber {
public static void main(String args[ ]) {
int i, a, b, sum;
String n;
System.out.println(“Four Digits Tech Numbers are::”);
for(i = 1000; i < 1000; i+ +) {
n = i +””;
a = lnteger,parselnt(n.substring(0, 2));
b = Integer.parselnt(n.substring(2));
sum = (int)Math.pow((a + b), 2);
if (sum == i)
System.out.println(i);
}
}
}

Question 4.

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. [15]
Sample input: we are in cyber world
Sample output : We Are In Cyber World
Solution:
import java.io.*;
import java.util.Scanner;
class ChangeLetter {
public static void main(String args[ ]) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.print(“Enter String in lowercase:”);
String str 1 = sc.next( );
strl = “” +strl;
String str2 = ” “:
for (int i = 0; i<strl.length( ); i+ +) {
if(strl ,charAt(i) = = “) {
str2 = str2 + ” +Character. toUpperCase(strl.charAt(i+l));
i+ + ;
}.
else
str2= str2 + strl.charAt(i);
}
System.out.println(str2.trim( ));
}
}

Question 5.

Design a class to overload a function volume() as follows : [15]


(i) double volume (double R) — with radius (R) as an argument, returns the volume of sphere using the
formula.
V = 4/3 × 22/7 × R3
(ii) double volume (double H, double R) – with height(H) and radius(R) as the arguments, returns the
volume of a cylinder using the formula.
V = 22/7 × R2 × H
(iii) double volume (double L, double B, double H) – with length(L), breadth(B) and Height(H) as the
arguments, returns the volume of a cuboid using the formula.
Solution:
class ShapesVolume {
public static double volume (double R) {
double V = 4.0 / 3, * 22.0 / 7 * Math.pow(R, 3);
return V;
}
public static double volume(double H, double R) {
double V = 22.0 / 7 * R * R * H ;
return V;
}
public static double volume (double L, double B, double H) {
double V = L * B * H;
return V;

Question 6.

Write a program to accept name and total marks of N number of students in two single subscript
array name[] and totalmarks[ ].   [15]
Calculate and print:
(i) The average of the total marks obtained by N Number of students.
[average = (sum of total marks of all the students)/N]
(ii) Deviation of each student’s total marks with the average.
[deviation = total marks of a student – average] ‘
Solution:
import java.io.*;
import java. util. Scanner;
class NameMarks {
public static void main(String argsO) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.print(“Enter number of students:”);
int N = sc.nextlnt( );
String named = new String[N];
int totalmarksG = new int[N];
double deviation[ ] = new double[N];
double sum = 0;
for (int i = 0; i < N; i+ +) {
System.out.print(“Enter Name of the Student:”);
name[i] = sc.next( );
System.out.print(“Enter Marks:”);
totalmarks[i] = sc.nextlntO;
sum = sum + totalmarks [i];
}
double average = sum / N;
System.out.println(“The average of the total marks of ” +N+” number of students:” +average);
for (int i = 0; i < N; i+ +) {
deviadon[i] = total marks (i] – average;
System.out.println(“Deviation of” + name[i] + “s marks with the average:” +deviation[i]);
}
}
}

Question 7.

Write a program to accept a number and check and display whether it is a spy number or not. (A number
is spy if the sum of its digits equals the product of its digits.) [15]
Example : consider the number 1124,
Sum of the digits = l + l+ 2 + 4 = 8
Product of the digits = 1×1 x2x4 = 8
Answer:
class Spy {
public static void main(int n) {
int sum = 0;
int multiple = 1;
int a;
int p = n;
// a stores each digit extracted and p creates a backup of input.
while(n ! = 0) {
a = n % 10; ,
sum = sum + a;
multiple = multiple * a;
n = n/10;
}
System.out.println(“The sum of ” +p +” is ”+sum);
System.out.println(“The product of “+p +” is ” + multiple);
if(sum = = multiple) {
System.out.println(“Aha, ” + “It is a Spy Number Where Sum = Product”);
}
else {
System.out.println(” It is NOT a Spy Number Where Sum ft Product”);
}
}
}

Question 8.

Write a program to input integer elements into an array of size 20 and perform the following operations:
[15]
(i) Display largest number from the array.
(ii) Display smallest number’from the array.
(iii) Display sum of all the elements of the array.
Answer:
import java.util.Scanner;
class LargeSmallSum {
public static void main(String args[ ]) {
int n;
int max, min, sum = 0;
int i, j;
Scanner s = new Scanner(System.in);
System.out.print(“Enter no. of elements you want in array:”);
n = s.nextlnt();
int a[ ] = new int[n];
System.out.println(“Enter all the elements:”);
for (i = 0; i < n; i+ +) {
a[i] = s.nextlntO;
}
max = a[0];
min = a[0];
for(i = 0; i < n ; i + +) { if(a[i] > max) {
max = a[i];
}
if (a[i] < min) {
min = a[i];
}
}
System.out.println(“Maximum Number is:”+max);
System.out.println(“Smallest Number is:” +min);
for(i = 0; i < n; i+ +) {
sum = sum + a[i];
}
System.out.println(“Sum of the Numbers is:” +sum);
}
}

Question 9.

Write a program to input forty words in an array. Arrange these words in descending order of alphabets,
using selection sort technique. Print the sorted array. [15]
Answer:
import java.util.Scanner;
class Descending {
public static void main(String args[ ]) {
int n;
String temp;
Scanner s = new Scanner(System.in);
System.out.print(“Enter number of words you want to enter:”);
n = s.nextlnt( );
String names[ ] = new String[n];
Scanner s1 = new Scanner(System.in);
System.out.println(“Enter all words:”);
for(int i = 0; i < n; i+ +) {
names[i] = s1.nextLine[( );
}
for (int i = 0; i < n; i+ +){
for (int j = i + 1; j < n;j++){
if (names[i].compareTo(namesG]) < 0) {
temp = names [i];
names[i] = namesG];
names[j] = temp;
}
}
}
System.out.print(“Words in Descending Order:”);
for (int i = 0;i<n-l;i++){
System.out.print(names[i] + “,”);
}
System.out.print(names[n – 1]);
}
}
Question 10:

Write a program to initialize the given data in an array and find the minimum ; and maximum
values along with the sum of the given elements.
Numbers : 2 5 4 1 3
Output :
Minimum value : 1
Maximum value: 5
Sum of the elements : 15 

Answer:
class maxmin
{
public static void main()
{
int [ ]n = {2,5,4,1,3};
int h=n[0];
int l=n[0];
int s=0;
for(int i=0; i<n.length; i++)
{
if(h<n[i])
{
h=n[i]
}
if(l<n[i])
{
l=n[i];
}
s=s+n[i];
}
System.out.println(“Minimum Value :”+l);
System.out.println(“Maximum Value :”+h);
System.out.println(“The Sum of the elements :”+s);
}
}

Question 11:

Write a program to enter a sentence from the keyboard and count the number of times a
particular word occurs in it. Display the frequency of the search word.
Example:
INPUT:
Enter a sentence : the quick brown fox jumps over the lazy dog.
Enter a word to be searched : the
OUTPUT :
Searched word occurs : 2 times. 

Answer:
import java.io.*;
class abc
{
public static void main() throws IOException
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
String str, word, wrd=“ ”;
char ch;
int c=0;
System.out.println(“Enter A sentence”);
str=br.readLine( );
str=str.trim( );
System.out.println(“Enter a word to be searched”);
word=br.readLine( );
str=str + “ ”;
for(int i=0; i<str.length( ); i++)
{
ch=str.charAt(i);
if (ch! = ‘ ’)
{
wrd=wrd+ch;
}
else
{
if(word.equals(wrd))
{
c++;
}
wrd=“ ”;
}
}
System.out.println(“searched word occurs”+c);
}
}

Question 12:
Using a switch statement, write a menu driven program to convert a given temperature from
Fahrenheit to Celsius and vice versa. For an incorrect choice, an appropriate error message
should be displayed.

(HINT : C =   × (F – 32) and F = 1.8 × (C + 32) 

Answer:
import java.io.*;
class Temp
{
public static void main(String arg[ ])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int choice;
double f=0, c=0;
System.out.println(“Menu”);
System.out.printing(“Fahrenheit to Celsius”);
System.out.println(“2.Celsius to Fahrenheit”);
System.out.println(“3.Wrong choice.Re-enter”);
System.out.println(“Enter your choice(l-3):”);
choice=Integer.parseInt(br.readLine());
switch(choice)
{
case 1 : System.out.println(“Enter the value of Fahrenheit”);
f=Double.parseDouble(br.readLine());
c=5/9 * (f-32);
System.out.println(“Temperature Fahrenheit to Celsius =”+c);
break;
case 2 : System.out.println(“Enter the value of Celsius”);
c=Double.parseDouble(br.readLine());
f=1.8*(c+32);
System.out.println(“Celsius to Fahrenheit =”+f);
break;
default :
System.out.println(“Wrong choice! Re-enter”);
break;
}
}
}

Question 13:

Write a program using a method Palin(), to check whether a string is a Palindrome or not. A
Palindrome is a string that reads the same from left to right and vice versa.
E.g. MADAM, ARORA, ABBA, etc. 
Answer:
class palin
{
public static void palin(String s)
{
int l=s.length();
int i, j=l-1;
int x=0;
for(i=0; i<l/2; i++)
{
if(s.charAl(i)!=s.charAt(j))
{
x=l; break;
}
j––;
}
if(x==0)
{
System.out.println(“The word is Palindrome”);
}
else
{
System.out.println(“The word is not Palindrome”);
}
}

Question 14:

Write a program to input a string and print out the text with the uppercase and lowercase letters
reversed, but all other characters should remain the same as before.
Example:
INPUT : WelComE TO School
OUTPUT : wELcOMe to SCHOOL 

Answer:
class letter
{
public static void main( )throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter any string :”);
String s = br.readLine ( );
int len=s.length();
String s1=””;//second string
for(int i=0; i<len; i++)
{
char ch=s.charAt(i);
if(Character.isUpperCase(ch))
{
s1=s1+Character.toLowerCase(ch);
}
else if(Character.isLowerCase(s.charAt(i)))
{
s1=s1+Character.toUpperCase(ch);
}
else
{
s1=s1+s.charAt(i);
}
}
s=s1,
System.out.println(s);
}
}

Question 15:

Define a class and store the given city names in a single dimensional array. Sort these names in
alphabetical order using the Bubble Sort technique only.
INPUT : Delhi, Bangalore, Agra, Mumbai, Calcutta
OUTPUT : Agra, Bangalore, Calcutta, Delhi, Mumbai  

Answer:
import java.io.*;
class city
{
public static void main( )
{
String [ ] name = {“Delhi”, “Bangalore”, “Agra”, “Mumbai”, “Calcutta”};
int i, j;
String temp;
//bubble sort begins
for(i=0; i<4; i++)
{
for (j=0; j<4—i; j++)
{
if((name[j].compareTo(name[j+1]))>0)
{
temp=name[j];
name[j]=name[j+1];
name[j+1]=temp;
}
}
}
//printing
for(i=0; i<5; i++)
{
System.out.println(name [i]);
}
}
}

You might also like