0% found this document useful (0 votes)
2K views92 pages

Aissce 2021-22: Information Technology (802) Practical File Class Xii

Here are programs to demonstrate switch case statements: a) To accept radius of a circle and choice from user to calculate area or circumference: INPUT import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double radius, area, circumference; System.out.print("Enter radius of circle: "); radius = sc.nextDouble(); System.out.print("Enter your choice (1-Area, 2-Circumference): "); int choice = sc.nextInt(); switch(choice) { case 1: area = Math.PI * radius * radius;

Uploaded by

tushar
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)
2K views92 pages

Aissce 2021-22: Information Technology (802) Practical File Class Xii

Here are programs to demonstrate switch case statements: a) To accept radius of a circle and choice from user to calculate area or circumference: INPUT import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double radius, area, circumference; System.out.print("Enter radius of circle: "); radius = sc.nextDouble(); System.out.print("Enter your choice (1-Area, 2-Circumference): "); int choice = sc.nextInt(); switch(choice) { case 1: area = Math.PI * radius * radius;

Uploaded by

tushar
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/ 92

AISSCE

2021-22

INFORMATION TECHNOLOGY (802)


PRACTICAL FILE
CLASS XII

BOARD ROLL NO:


SRI VENKATESHWAR INTERNATIONAL
SCHOOL

(2020-2021)

INFORMATION TECHNOLOGY
SUB. CODE – 802

PRACTICAL FILE
MY SQL/JAVA

SUBMITTED BY-
Tushar Dhiman
XII-D
CERTIFICATE
This is to certify that Tushar dhiman of Class XII-D has worked
and completed his Information Technology Practical File under
my Guidance and supervision.

This is for ALL INDIA SENIOR SECONDARY SCHOOL CERTIFICATE


EXAMINATION (AISSCE).

(SESSION : 2020-21)

PLACE: Delhi
DATE:

MS. BARNALI BANERJE


Java
INDEX
➢ Operators
➢ Input by User
➢ Decide If or Else
➢ Switch Case
➢ While Statement
➢ Do While Statement
➢ For Loop Statement
➢ Arrays
➢ String Function
➢ Exception Handling
➢ Database Connectivity
OPERATORS
a) To find the result of the following expressions. (Assume a=80and b=40)
1) a%b 2) a/=b 3) (a+b*100)/10 4) a*=b 5) a+=b

Answer ->
INPUT
package com.me;
public class Main {
public static void main(String[] args) {
int a = 80, b = 40;
int A = a%b;
int B = a/=b;
int C = (a+b*100)/10;
int D = a*=b;
int E = a+=b;
System.out.println("1)" + A);
System.out.println("2)" + B);
System.out.println("3)" + C);
System.out.println("4)" + D);
System.out.println("5)" + E); }}
OUTPUT
b) To calculate area and perimeter of a circle.

Answer ->
INPUT
package com.me;

public class Main {


public static void main(String[] args) {
double π = 3.14;
int r = 15;
double area = π*(r*r);
System.out.println("Area of Circle: " + area);
double perimeter = 2*π*r;
System.out.println("Perimeter of Circle: " + perimeter); }}
OUTPUT
c) To calculate and display simple interest.

Answer ->
INPUT
package com.me;

public class Main {


public static void main(String[] args) {
int P = 12000;
int R = 60;
int T = 2;
int si = (P*R*T)/100;
System.out.println("Simple Interest: " + si);}}
OUTPUT
d) Print addition, subtraction, multiplication and division of any two given
numbers.

Answer ->
INPUT
package com.me;

public class Main {


public static void main(String[] args) {
int x = 120, y = 40;
int add = x + y;
int sub = x - y;
int mul = x * y;
int div = x / y;
System.out.println("Sum: " + add);
System.out.println("Difference: " + sub);
System.out.println("Product: " + mul);
System.out.println("Quotient: " + div); }}
OUTPUT
e) WAP to implement the formula - area = length * width * height
Answer ->
INPUT
package com.me;

public class Main {


public static void main(String[] args) {
int length = 500;
int width = 15;
int height = 25;
int area = length * width * height;
System.out.println ("Area: " + area);}}
OUTPUT
INPUT BY USER
Input by User

2. Write programs for the following:

a) To accepts radius of a sphere and displays its volume and surface area.
(Hint: V = 4/3 π r2, S = 4π r3)
Answer ->
INPUT
package com.me;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.print("Enter the Radius of the Sphere: ");
double radius = sc.nextDouble();
System.out.println();
double π = 3.14;
double volume = (4/3)*π*radius*radius*radius;
double sa = 4*π*radius*radius;
System.out.println("Volume of the Sphere: " + volume);
System.out.println("Surface Area of the Sphere: " + sa);}}
OUTPUT
b) To accept marks in five subjects and display the average marks.
Answer ->
INPUT
package com.me;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int i;
System.out.println("Enter number of subjects");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
double avg=0;
System.out.println("Enter marks");
for( i=0;i<n;i++)
{a[i]=sc.nextInt();
}for( i=0;i<n;i++)
{avg=avg+a[i];
}System.out.print("Average of (");
for(i=0;i<n-1;i++)
{System.out.print(a[i]+",");
}System.out.println(a[i]+") ="+avg/n); }}
OUTPUT
c) To accept a number from the user. Then add 3 to that number, and then
multiply the result by 2, subtract twice the original number and display the
result.
Answer ->
INPUT
package com.me;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.print("Enter a Number: "); int num = sc.nextInt();
System.out.println();
int x = num + 3;
int y = x * 2 ;
int z = y - 2 * num;
System.out.println("Final Output: " + z); }}
OUTPUT
d) To accept the temperature in Fahrenheit and convert and display the
temperature in Celsius. (Hint: C = (F – 32) * 5/9)
Answer ->
INPUT
package com.me;
import java.util.Scanner;
public class Main { public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.print("Enter a Temperature in Fahrenheit: "); int num =
sc.nextInt();
System.out.println();
double tem = 0;
double F = tem;
double C = (F-32)*5/9;
System.out.println("Temperature in Celsius: " + C + "°C");}}
OUTPUT
e) To accept principle, rate of interest and time. Calculate and display simple
interest and compound interest.
Answer ->
INPUT
package com.me;
import java.util.Scanner;
public class Main { public static void main(String[] args) {
double pr, rate, t, sim,com;
Scanner sc=new Scanner (System. in);
System.out.println("Enter the amount:");
pr=sc.nextDouble();
System. out. println("Enter the No.of years:");
t=sc.nextDouble();
System. out. println("Enter the Rate of interest");
rate=sc.nextDouble();
sim=(pr * t * rate)/100;
com=pr * Math.pow(1.0+rate/100.0,t) - pr;
System.out.println("Simple Interest="+sim);
System.out. println("Compound Interest="+com);}}
OUTPUT
DECIDE IF OR
ELSE
Selection Structures
i. If or Else

Write programs for the following:

a) To check number entered by the user is odd or even.


Answer->
INPUT
package com.me;
import java.util.Scanner;
public class Main { public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.print("Enter a Number: "); int num = sc.nextInt();
System.out.println();
if(num%2==0)
System.out.println(num + " is a Even Number.");
else
System.out.println(num + " is a Odd Number.");}}
OUTPUT
b) To check age of a person and whether he is eligible to vote or not.
Answer->
INPUT
package com.me;
import java.util.Scanner;
public class Main { public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.print("Enter a Age: "); int age = sc.nextInt();
System.out.println();
if(age>=18)
System.out.println("The Person is Eligible to Vote.");
else
System.out.println("The Person is Not Eligible to Vote.");}}
OUTPUT
c) To accept any 3 numbers and display the greater number with appropriate
message.
Answer->
INPUT
package com.me;
import java.util.Scanner;
public class Main { public static void main(String[] args) {
int a, b, c, largest;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number:");
a = sc.nextInt();
System.out.println("Enter the second number:");
b = sc.nextInt();
System.out.println("Enter the third number:");
c = sc.nextInt();
largest = c > (a > b ? a : b) ? c : ((a > b) ? a : b);
System.out.println("The largest number is: "+largest);}}
OUTPUT
d) Program that accepts marks in 5 subjects. Then calculate the percentage
and print the remark based on it:

Answer->
INPUT
package com.me;
import java.util.Scanner;
public class Main { public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.print("Enter Sub 1 marks : "); int sub1 = sc.nextInt();
System.out.print("Enter Sub 2 marks : "); int sub2 = sc.nextInt();
System.out.print("Enter Sub 3 marks : "); int sub3 = sc.nextInt();
System.out.print("Enter Sub 4 marks : "); int sub4 = sc.nextInt();
System.out.print("Enter Sub 5 marks : "); int sub5 = sc.nextInt();
int total = sub1 + sub2 + sub3 + sub4 + sub5;
double perc = total / 5;
System.out.println();
System.out.println("Percentage: " + perc);
if(perc>=80)
System.out.println("Excellent !");
else if(perc>=60 && perc<80)
System.out.println("Very Good !");
else if (perc>=40 && perc<60)
System.out.println("Good !");
else
System.out.println("Work Hard !");}}
OUTPUT

(Refer to the next page)


e) Program that accepts salary of an employee from the user. Deduct the
income tax from the salary on the following basis and finally display income

tax and net salary.


Answer->
INPUT
(Refer to the next page)
package com.me;
import java.util.Scanner;
public class Main { public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.print("Enter Employee's Salary: "); int num = sc.nextInt();
System.out.println();
if(num<7000)
System.out.println("The final salary of the employee is " +(num -
(num*10)/100 ) + ".");
else if(7000<num && num<15000)
System.out.println("The final salary of the employee is " +(num -
(num*20)/100 ) + ".");
else
System.out.println("The final salary of the employee is " +(num -
(num*30)/100) + ".");}}
OUTPUT
SWITCH CASE
Selection Structures

ii. Switch Case

Write programs for the following:


a) Menu driven program to accept radius of a circle and choice from the user.
Based on the choice 1 or 2 calculate and display
1. Area of Circle 2. Circumference of Circle
Answer->
INPUT
package com.me;
import java.util.*;
public class Main { public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
double π = 3.14;
System.out.print("Enter Radius : "); int r = sc.nextInt(); System.out.println();
System.out.println("Select your Choice:");
System.out.println("To Calculate Area of Circle, Press 1");
System.out.println("To Calculate Circumference of Circle, Press 2");
System.out.println();
System.out.println("Area of Circle: " + (π * (r * r)));}}
OUTPUT
b) Menu driven program to accept a number from the user. Based on the
choice ‘S’ or ‘C’ calculate and display Square of the number, Cube of the
number
Answer->
INPUT
package com.me;
import java.util.*;
public class Main { public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.print("Enter a Number : "); int num = sc.nextInt();
System.out.println();
System.out.println("Select your Choice:");
System.out.println(" 'S', To Calculate Square of the Number, Press 1");
System.out.println(" 'C', To Calculate Cube of the Number, Press 2");
System.out.println();
System.out.println("Square of the Number: " + (num*num));}}
OUTPUT
c) If the entered number is 0, the message “It is Sunday” should be displayed.
If the entered number is 1, the message “It is Monday” should be displayed
and so on.
Answer->
INPUT
package com.me;
import java.util.*;
public class Main { public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter weekday day number (1-7) : ");
int weekday = scanner.nextInt();
if(weekday == 1) {
System.out.println("Monday");
} else if(weekday == 2) {
System.out.println("Tuesday");
} else if(weekday == 3) {
System.out.println("Wednesday");
} else if(weekday == 4) {
System.out.println("Thursday");
} else if(weekday == 5) {
System.out.println("Friday");
} else if(weekday == 6) {
System.out.println("Saturday");
} else if(weekday == 7) {
System.out.println("Sunday");
} else {
System.out.println("Please enter weekday number between 1-7.");}}}
OUTPUT

(Refer to the next page)


d) Accept salary and grade from the user. Based on the grade add the bonus to

the salary and display net salary.

Answer->
INPUT

(Refer to the next page)


package com.me;
import java.util.Scanner;
public class Main { public static void main(String[] args) {
Scanner user_input=new Scanner(System.in);
char grade; double sal,bonus=0;
System.out.println("Enter salary");
sal=user_input.nextDouble();
System.out.println("Enter Grade");
grade=user_input.next().charAt(0);
switch (grade) {
case 'A' -> bonus = 0.20 * sal;
case 'B' -> bonus = 0.15 * sal;
case 'C' -> bonus = 0.10 * sal;
case 'D' -> bonus = 0.5 * sal;
default -> System.out.println("Invalid Entry");}
System.out.println("Bonus="+bonus);
System.out.println("Net salary="+(sal+bonus));}}
OUTPUT
e) Accepts an alphabet and check whether that alphabet is vowel or consonant.

Print with appropriate message.

Answer->
INPUT
package com.me;
import java.util.Scanner;
public class Main { public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.print("Enter a Alphabet: "); char choice = sc.nextLine().charAt(0);
switch (choice) {
case 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' -> System.out.println(choice + " is a
Vowel.");
default -> System.out.println(choice + " is a Consonant.");}}}
OUTPUT
WHILE
STATEMENT
Repetition Structures

i. While Statement

Write programs for the following:

a) Accept any 5 numbers from the user, Calculate square of the number and
print result.
Answer->
INPUT
package com.me;
import java.util.Scanner;
public class Main { public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.print("Enter the 1st Number: "); int num1 = sc.nextInt();
System.out.print("Enter the 2nd Number: "); int num2 = sc.nextInt();
System.out.print("Enter the 3rd Number: "); int num3 = sc.nextInt();
System.out.print("Enter the 4th Number: "); int num4 = sc.nextInt();
System.out.print("Enter the 5th Number: "); int num5 = sc.nextInt();
System.out.println();
int i = 0;
while (i<1) {
System.out.println("Square of " + num1 + ": " + (num1*num1));
i++;
System.out.println("Square of " + num2 + ": " + (num2*num2));
i++;
System.out.println("Square of " + num3 + ": " + (num3*num3));
i++;
System.out.println("Square of " + num4 + ": " + (num4*num4));
i++;
System.out.println("Square of " + num5 + ": " + (num5*num5));
i++;}}}
OUTPUT

(Refer to the next page)


b) Accept any three names from the keyboard and display “Your Name is”
name.
Answer->
INPUT

(Refer to the next page)


package com.me;
import java.util.Scanner;
public class Main { public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.print("Enter the 1st Name: "); String name1 = sc.next();
System.out.print("Enter the 2nd Name: "); String name2 = sc.next();
System.out.print("Enter the 3rd Name: "); String name3 = sc.next();
System.out.println();
int i = 0;
while (i<=1) {
System.out.println("Your Name is " + name1);
i++;
System.out.println("Your Name is " + name2);
i++;
System.out.println("Your Name is " + name3);
i++;}}}
OUTPUT
c) To print all alphabets from A to Z in the same line.
Answer->
INPUT
package com.me;

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


char a = 'A';
while (a <= 'Z') {
System.out.print(a + " " );
++a;
}}}
OUTPUT
DO WHILE
STATEMENT
ii. DO WHILE STATEMENT

Write programs for the following:

a) Print ASCII values and their equivalent characters. ASCII value varies from 0
to 255.
Answer->
INPUT
package com.me;
public class Main { public static void main(String[] args) {
int i = 0;
do {
System.out.println("The ASCII Value of " + (char)i + " = " + i);
i++;}
while(i<=255);}}
OUTPUT
b) Display Fibonacci Series using do while loop.
Answer->
INPUT
package com.me;
public class Main { public static void main(String[] args) {
int count = 12, num1 = 0, num2 = 1;
System.out.println("Fibonacci Series of " + count + " numbers: ");
int i = 1;
do {System.out.print(num1+ " ");
int x = num1 + num2;
num1 = num2;
num2 = x;
i++;}
while(i<=count);}}
OUTPUT
c) Print all the even numbers from 1 to 20
Answer->
INPUT
package com.me;
public class Main { public static void main(String[] args) {
int n = 20;
System.out.println("Even Numbers from 1 to 20 are:");
for ( int i=1; i<=20; i++ ) {
if(i % 2 == 0) {
System.out.print(i + " " );}}}}
OUTPUT
FOR LOOP
STATEMENT
iii. FOR LOOP STATEMENT

Write programs for the following:

a) To find factorial of any number entered by user.


Answer->
INPUT
package com.me;
import java.util.*;
public class Main { public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number: ");
int num=sc.nextInt();
int i=1,fact=1;
while(i<=num)
{fact=fact*i;
i++;}
System.out.println("Factorial of the number: "+fact);}}
OUTPUT
b) To generate multiplication table of any number entered by user.
Answer->
INPUT
package com.me;
import java.util.*;
public class Main { public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter number:");
int n=s.nextInt();
for(int i=1; i <= 10; i++)
{System.out.println(n+" * "+i+" = "+n*i);}}}
OUTPUT
c) To accept a number and display all even numbers till that number.
Answer->
INPUT
package com.me;
import java.util.*;
public class Main { public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Number: "); int num = sc.nextInt();
System.out.print("Even Numbers till " + num + ": ");
int i;
for (i = 0; i <= num; i = i + 2)
{System.out.print(i + " ");}}}
OUTPUT
ARRAYS
Arrays

a) Enter marks of 3 students and calculate percentage of each child using


Arrays.
Answer->
INPUT
package com.me;
import java.util.Scanner;
public class Main { public static void main(String[] args) {
final int TOTAL_STUDENTS = 3;
Scanner in = new Scanner(System.in);
int[] rollNo = new int[TOTAL_STUDENTS];
String[] name = new String[TOTAL_STUDENTS];
int[] s1 = new int[TOTAL_STUDENTS];
int[] s2 = new int[TOTAL_STUDENTS];
int[] s3 = new int[TOTAL_STUDENTS];
int[] s4 = new int[TOTAL_STUDENTS];
int[] s5 = new int[TOTAL_STUDENTS];
int[] s6 = new int[TOTAL_STUDENTS];
double[] p = new double[TOTAL_STUDENTS];
char[] g = new char[TOTAL_STUDENTS];
for (int i = 0; i < TOTAL_STUDENTS; 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();
System.out.print("Subject 4 Marks: ");
s4[i] = in.nextInt();
System.out.print("Subject 5 Marks: ");
s5[i] = in.nextInt();
System.out.print("Subject 6 Marks: ");
s6[i] = in.nextInt();
p[i] = (((s1[i] + s2[i] + s3[i] + s4[i]
+ s5[i] + s6[i]) / 600.0) * 100);
if (p[i] < 40)
g[i] = 'D';
else if (p[i] < 60)
g[i] = 'C';
else if (p[i] < 80)
g[i] = 'B';
else
g[i] = 'A';}
System.out.println();
for (int i = 0; i < TOTAL_STUDENTS; i++) {
System.out.println(rollNo[i] + "\t"
+ name[i] + "\t"
+ p[i] + "\t"
+ g[i]);}}}
OUTPUT
b) WAP to print the result of 5 students with passed or failed decision based
program.

Answer->
INPUT
package com.me;
public class Main { public static void main(String[] args) {
double[] marks = {420, 384, 470, 259.6, 397.5};
for (int i = 0; i < 5; i++)
{double percentage = (marks[i]/500)*100;
String result;
if (percentage >= 40) result = "Passed";
else result = "Failed";
System.out.print((i+1) + "\t");
System.out.print(marks[i] + "\t");
System.out.print(percentage + "\t\t");
System.out.println(result);}}}
OUTPUT
c) Write a program in Java to print the square of every alternate number in an
array.

Answer->
INPUT
package com.me;
public class Main { public static void main(String[] args) {
int [] num = {2, 4, 5, 4, 6};
for (int i = 0; i < 5; i = i+2)
{System.out.println("Alternate Elements: " + num[i]);
int sq = num[i]*num[i];
System.out.println("Square of Alternate Elements: " + sq);}}}
OUTPUT
d) WAP to print the sum, product and average of elements entered by the user.
Answer->
INPUT
package com.me;
import java.util.Scanner;
public class Main { public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
int[] num = new int[5];
System.out.print("Enter 5 Numbers: ");
for (int i=0;i<5;i++)
{num[i] = sc.nextInt();}
int sum =0;
System.out.println();
System.out.print("Sum: ");
for(int i=0;i<5;i++)
{sum = sum + num[i];}
System.out.println(sum);
int product=1;
for(int i=0; i<5;i++)
{product=product*num[i];}
System.out.println("Product: " + product);
int avg = sum / 5;
System.out.println("Average: " + avg);}}
OUTPUT
e) WAP to accept 5 names from user and store them in an array and print them
on screen in order.

Answer->
INPUT
package com.me;
import java.util.Scanner;
public class Main { public static void main(String[] args) {
String [] names = new String[5];
Scanner sc = new Scanner(System.in);
System.out.print("Enter 5 Names: ");
for (int i=0;i<5;i++)
{names[i] = sc.next();}
System.out.println();
System.out.println("Entered Names are: ");
for (int i=0;i<5;i++)
{System.out.println(names[i]);}}}
OUTPUT
f) WAP to print highest and lowest total marks of 10 students using Arrays.

Answer->
INPUT
package com.me;
import java.util.Scanner;
public class Main { public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int [] marks = new int[10];
int [] mylist = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
int [] occurrences = {0,0,0,0,0,0,0,0,0,0};
int sum=0,highest=-1,lowest=9999,mylistindex=0;
boolean found;
System.out.println("Enter 10 marks:");
for (int i=0;i<10;i++) {marks[i]=in.nextInt();sum=sum+marks[i];}
System.out.println("The numbers entered are:");
for (int tmp:marks){
System.out.print(tmp+" ");
if (tmp>highest) highest=tmp;
if (tmp<lowest) lowest=tmp;
sum+=tmp;
found=false;
for (int i=0;i<mylistindex;i++){
if (mylist[i]==tmp) {found=true;occurrences[i]++;break;}}
if (!found)
{mylist[mylistindex]=tmp;occurrences[mylistindex]=1;mylistindex++;}}
System.out.println();
System.out.println("The average mark is: "+ sum/10);
System.out.println("Highest mark is: "+ highest);
System.out.println("Lowest mark is: ="+lowest);
for (int i=0;i<mylistindex;i++){
System.out.println(mylist[i]+"\t"+occurrences[i]);}}}
OUTPUT
g) Use binary search to search for an element in a number array. The search
key must be accepted from the user.

Answer->
INPUT
package com.me;
import java.util.Arrays;
public class Main { public static void main(String[] args) {
int[] num = {23,7,56,90,45,77};
Arrays.sort(num);
int key = 56;
System.out.print("Sorted array: ");
for (int j : num) System.out.print(j + " ");
System.out.println();
System.out.println("Element found at index: " +
Arrays.binarySearch(num,key));}}
OUTPUT
STRING
FUNCTION
STRING FUNCTION

a) Consider the string str=“Global Warming”. Write statements to implement


the following:
i. To display the last 4 characters.
ii. To change the case of the given string.
iii. To display the length of the string.
iv. To check if the string contains the substring “Wa”.
v. To replace all occurrences of the letter ‘a’ in the string with ‘*’
Answer->
INPUT
package com.me;
public class Main { public static void main(String[] args) {
String str = "Global Warming";
int initial = str.length()-4;
for(int i = initial; i<str.length(); i++)
{System.out.print(str.charAt(i) + " ");}
System.out.println();
System.out.println(str.toUpperCase());
System.out.println(str.toLowerCase());
System.out.println(str.length());
System.out.println(true);
System.out.println(str.replace('a','*'));}}
OUTPUT
b) Write the code to enter the string and change it to lowercase, uppercase,
length and extract character from 2nd to 4th character.
Answer->
INPUT
package com.me;
class Test {
static void convertOpposite(StringBuffer str)
{int ln = str.length();
for (int i = 0; i < ln; i++) {
char c = str.charAt(i);
if (Character.isLowerCase(c))
str.replace(i, i + 1,
Character.toUpperCase(c) + "");
else
str.replace(i, i + 1,
Character.toLowerCase(c) + "");}}
public static void main(String[] args)
{StringBuffer str = new StringBuffer("uppercase LOWERCASE");
convertOpposite(str);
System.out.println(str);}}
OUTPUT
c) Write the code to accept a string and perform the following functions.
i. Find length of the string.
Answer->
INPUT
package com.me;
import java.util.Scanner;
public class Main { public static void main(String[] args) {
String str;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string");
str=sc.nextLine();
char[] ch =str.toCharArray();
int n2 = ch.length;
System.out.println("Length of the string(using length method) = "+n2);}}
OUTPUT
ii. Find no. of words of the string.
Answer->
INPUT
package com.me;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String text;
int countWords=0;
Scanner SC=new Scanner(System.in);
System.out.print("Enter string: ");
text=SC.nextLine();
for(int i=0; i<text.length()-1; i++)
{if(text.charAt(i)==' ' && text.charAt(i+1)!=' ')
countWords++;}
System.out.println("Total number of words in string are: "+
(countWords+1));}}
OUTPUT
iii. Find number of vowels of the string.
Answer->
INPUT
package com.me;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int count = 0;
System.out.println("Enter a sentence :");
Scanner sc = new Scanner(System.in);
String sentence = sc.nextLine();
for (int i=0 ; i<sentence.length(); i++){
char ch = sentence.charAt(i);
if(ch == 'a'|| ch == 'e'|| ch == 'i' ||ch == 'o' ||ch == 'u'){
count ++;}}
System.out.println("Number of vowels in the given sentence is "+count);}}
OUTPUT
Exception
handling
Exception Handling

a) A scenario where Arithmetic Exception occurs.

Answer->
INPUT
package com.me;
public class Main { public static void main(String[] args) {
try{int num1=30, num2=0;
int output=num1/num2;
System.out.println ("Result: "+output);}
catch(ArithmeticException e){
System.out.println ("You Shouldn't divide a number by zero");}}}
OUTPUT
b) A scenario where ArrayIndexOutOfBoundsException occurs.

Answer->
INPUT
package com.me;
public class Main { public static void main(String[] args) {
try{int a[]=new int[10];
a[11] = 9;}
catch(ArrayIndexOutOfBoundsException e){System.out.println
("ArrayIndexOutOfBounds");}}}
OUTPUT
DATABASE
CONNECTIVITY
Database Connectivity

a) Retrieving data from the MySQL table in a Java program.

Answer->
INPUT

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class sqldemo {

public static void main(String[] args) {

String dbURL = "jdbc:mysql://localhost:3306/student";

String username ="root";

String password = "admin";

try{Connection dbCon =DriverManager.getConnection(dbURL,username,

password);

Statement stmt = dbCon.createStatement();

String query ="select * from sdetails";

ResultSet rs = stmt.executeQuery(query);

while(rs.next()){

for (int i = 1; i <=5; i++) {System.out.print(rs.getString(i));System.out.print("|");}

System.out.println();}}
catch(SQLException ex)

{System.out.println(ex.getMessage());} } }
SQL
a) Consider the following tables EMP and SALGRADE, write the query for the
following:
ANSWER->
TABLE: EMPLOYEE

ECODE NAME DESIG SGRADE DOJ DOB


101 Vikrant Executive S03 2003-03-23 1980-01-13
102 Ravi Head-IT S02 2010-02-12 1987-07-22
103 John Cena Receptionist S03 2009-06-24 1983-02-24
105 Azhar Ansari GM S02 2009-08-11 1984-03-03
108 Priyam Sen CEO S01 2004-12-29 1982-01-19
TABLE: SALGRADE

SGRADE SALARY HRA


S01 56000 18000
S02 32000 12000
S03 24000 8000
i. To display details of all employee in descending order of their DOJ

INPUT

mysql> select * from emp order by doj desc ;

OUTPUT

ii. To display NAME AND DESIG of those employees whose sgrade is either
‘S02’ or‘S03’

INPUT

mysql> SELECT NAME,DESIG FROM emp WHERE sgrade ='S02' OR sgrade = 's03';

OUTPUT
iii. To display NAME, DESIG, SGRADE of those employee who joined in the
year 2009

INPUT

mysql> SELECT NAME, Desig, sgrade FROM emp WHERE YEAR(doj) = 2009 ;

OUTPUT

iv. To display all SGRADE, ANNUAL_SALARY from table SALGRADE [where


ANNUAL_SALARY = SALARY*12]

INPUT

mysql> SELECT sgrade, salary * 12 AS Annual_Salary FROM Salgrade ;

OUTPUT
v. To display number of employee working in each SALGRADE from table
EMPLOYEE

INPUT

mysql> SELECT sgrade, COUNT(sgrade) FROM emp GROUP BY SGRADE ;

OUTPUT

vi. To display NAME, DESIG, SALARY, HRA from tables EMPLOYEE and
SALGRADE where SALARY is less than 50000

INPUT

mysql> select NAME , DESIG , SALARY , HRA from emp , salgrade where
emp.sgrade = salgrade.sgrade and salary < 50000 ;

OUTPUT
b) Consider the table SHOPPE and ACCESSORIES, write the query for the
following:
ANSWER->
TABLE: SHOPPE

(Refer to the next page)


TABLE: ACCESSORIES
i. To display Name and Price of all the Accessories in descending order of
their Price

INPUT

mysql> select name , price from accessories order by price desc ;

OUTPUT

ii. To display Id and Sname of all the Shoppe location in „Nehru Place‟

INPUT

mysql> select id , sname from shoppe where area = "nehru place" ;

OUTPUT
iii. To display Name, Minimum and Maximum Price of each Name from
ACCESSORIES table

INPUT

mysql> select name , min(price)"minimum price" , max(price)"maximum price"


from accessories group by name ;

OUTPUT

iv. To display Name, Price of all Accessories and their respective SName
from table SHOPPE and ACCESSORIES where Price is 5000 or more.

INPUT

mysql> select name , price , sname from accessories , shoppe where


accessories.id = shoppe.id and price >=5000 ;

OUTPUT
v. To display all details of accessories where name contains word „Board‟;

INPUT

mysql> select * from accessories where name like '%board%' ;

OUTPUT

c) Consider the table given below. Write SQL queries for the following:
ANSWER->
TABLE: Gym

(Refer to the next page)


Columns REGID stores Registration Id, PREWEIGHT stores weight of the
person before joining Gym,

CURRWEIGHT stores current weight, DOJ stores Date of Joining, BRANCH


stores the branch of

Gym where the person has enrolled.

(Refer to the next page)


i. To display names of members along with their previous and current
weights who are in Model Town branch.

INPUT

mysql> select name,preweight,currweight from gym where branch =’model


town’;

OUTPUT

ii. To display BRANCH wise count of members in the Gym. (i.e. display the
BRANCH and number of members in each BRANCH)

INPUT

mysql> select branch,count(branch) as 'number people' from gym group by


branch ;

OUTPUT
iii. To display names and date of joining of all the members who joined in
the year 2018.

INPUT

mysql> select name,doj from gym where year(doj)=2018 ;

OUTPUT

iv. To display Names and Current weight of all the members in descending
order of Current Weight.

INPUT

mysql> select name,currweight from gym order by currweight desc ;

OUTPUT
v. To display Gender wise, maximum current weight (i.e. display the
gender and maximum current weight of each gender)

INPUT

mysql> select gender,max(currweight) as max_weight,count(gender) from gym


group by gender ;

OUTPUT

vi. To display names and date of joining of members who have their names
starting with ‘S’ and ending with ‘a’.

INPUT

mysql> select name , doj from gym where name like 'S%A';

OUTPUT
d) Consider the following table SHOP and write the following queries in SQL:

ANSWER->

i. To display all items of the given table.

INPUT

mysql> select * from SHOP ;

OUTPUT
ii. To display I_NAME and PRICE in descending order of quantity.

INPUT

mysql> select i_name,price,quantity from shop order by quantity desc ;

OUTPUT

iii. To count total number of items whose price is 10.

INPUT

mysql> select count(price=10) as numberofitems from shop ;

OUTPUT
iv. To add a new value in SHOP table.

INPUT

insert into shop values(106,’eraser’,5,30)

OUTPUT

v. To display item name and quantity whose price is more than 10.

INPUT

mysql> select i_name , price from SHOP where price > 10 ;

OUTPUT
e) Consider the following tables EMPLOYEES. Write SQL commands for the
statements:

ANSWER->

EMPID FIRSTNAME LASTNAME ADDRESS CITY


010 George Smith 83 First Street Howard
105 Mary Jones 842 Vine Ave. Losantiville
152 Sam Tones 33 Elm St. Paris
215 Sarah Ackerman 440 U.S. 110 Upton
i. To create a table employees table.

INPUT

mysql> create table employee ( EMPID INT , FIRSTNAME VARCHAR(20) ,


LASTNAME VARCHAR(20) , ADDRESS VARCHAR(20) , CITY VARCHAR(20) ) ;

OUTPUT

ii. To display all records of the table.

INPUT

mysql> select * from employee ;

OUTPUT
iii. To display the content of EMPLOYEES table in descending order of
FIRSTNAME.

INPUT

mysql> select firstname from employee order by firstname desc ;

OUTPUT

iv. To change the city name to “Delhi” where empid is 152.

INPUT

mysql> update employee set city = 'delhi' where empid = 152;

OUTPUT
v. To delete record of employee who’s EMPID IS 152.

INPUT

mysql> delete from employee where empid=152;

OUTPUT

f) Consider the following table named “GYM” with details about Fitness
products
Prcode Prname Unitprice Manufacturer
P101 Cross trainer 25000 Avon fitness
P102 Treadmill 32000 AG fitline
P103 Massage chair 20000 Fit Express
P104 Vibration trainer 22000 Avon fitness
P105 bike 13000 Fit express

(Refer to the next page)


Write SQL statements to do the following:

i. Display the names and unit price of all the products in the store.

INPUT

mysql> select prname,unitprice from gym ;

OUTPUT
ii. Display details of all the products with unit price in the range 20000 to
25000

INPUT

mysql> select prname,unitprice from gym where unitprice between 20000 and
25000 ;

OUTPUT

iii. Display all rows sorted in descending order of unit price.

INPUT
mysql> select * from gym order by unitprice desc ;
OUTPUT
iv. Display details of all products with manufacturer name starting with
“A”.

INPUT

mysql> select *from gym where manufacturer like'a%' ;

OUTPUT

v. Add a new row for product with the details: “P106”, “Vibro Exercise”,
23000, “Avon Fitness”

INPUT

mysql> insert into gym

-> values ('p106','vibro exercise',23000,'avon fitness') ;

OUTPUT

You might also like