You are on page 1of 4

Class Notes

Class: XII
Topic: Practical File
Subject: Information Technology (802) Term-II

1. Write a program to calculate percentage calculator program, using three variables named
marks_obtained, total_marks and percentage.

int total_marks = 400;


double marks_obtained = 346;
double percentage = 0.0;
percentage = (marks_obtained/total_marks)*100;
System.out.println("Student 1's Percentage = "+percentage);
marks_obtained = 144;
percentage = (marks_obtained/total_marks)*100;
System.out.println("Student 2's Percentage = "+percentage);

2. Write a program to store textual data, for example, the name of a student Mayank M Saxena).

char middle_name = 'M';


String first_name = "Mayank";
String last_name = "Saxena";
System.out.println(first_name+" "+ middle_name+" "+last_name);

3. Write a program to demonstrates usage of the switch statement for finding week day.

public static void main (String[ ] args) {


int today = 5;
String day = "";
switch (today) {
case 1: day = "Monday";
break;
case 2: day = "Tuesday";
break;
case 3: day = "Wednesday";
break;
case 4: day = "Thursday";
break;
case 5: day = "Friday";
break;
case 6: day = "Saturday";
break;
case 7: day = "Sunday";
break;
default: day = "Incorrect Day!";
break;
}
System.out.println (day);}
4. WAP to display the values from 1 to 5 using FOR loop
int i;
for (i=1; i <=5; i++){
System.out.println(“” + i);}

5. WAP to print first 5 natural numbers using WHILE loop


int i = 0;
while (i<=5){
System.out.println("" + i);
i++;

6. WAP to print first 5 natural numbers using do while loop


int i = 1;
do{
System.out.println("" + i);
i++;
}
while (i <=5);

7. Write a program to enter your name using String


String First_Name = JTF1.getText();
String Last_Name = JTF2.getText();
JTF3.setText(First_Name+" "+ Last_Name);

8. Write a program to find out how many elements an array has, use the length property:
public static void main(String[] args) {
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
}

9. Write a program to create a two-dimensional array, add each array within its own set of curly
braces:
public static void main(String[] args) {
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
int x = myNumbers[1][2];
System.out.println(x); }

10. Write a program to use a for loop inside another for loop to get the elements of a two-
dimensional array
public static void main(String[] args) {
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
for (int i = 0; i < myNumbers.length; ++i) {
for(int j = 0; j < myNumbers[i].length; ++j) {
System.out.println(myNumbers[i][j]);} }
11. Write a program to store five marks in the array
double[]marks = {346, 144, 103, 256.5, 387.5};
double total_marks = 400;
System.out.println("\tClass Report");
System.out.println("--------------------------------------");
System.out.println("RollNo\tMarks\tPercentage\tResult");
System.out.println("--------------------------------------");
for (int i = 0; i <marks.length; i++) {
double percentage = (marks[i]/total_marks)*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);}

12. Write a program to sort an array of Strings in alphabetic order


public static void main(String args[]) {
String[] names = {"Ravi", "Kushi", "Ganga","Avani","Ramesh","Thomas","Purvi","Aman"};
System.out.println("Names Array before Sorting:");
for (int i = 0; i<names.length; i++)
System.out.print(names[i] + ",");
System.out.println();
Arrays.sort(names);
System.out.println("Names after Sorting:");
for (int i=0; i < names.length; i++)
System.out.print(names[i] + ",");
System.out.println();}

13. Write a program to check Age using call a method with an integer parameter.
// Create a checkAge() method with an integer parameter called age
static void checkAge(int age) {
// If age is less than 18, print "access denied"
if (age < 18) {
System.out.println("Access denied - You are not old enough!");

// If age is greater than, or equal to, 18, print "access granted"


} else {
System.out.println("Access granted - You are old enough!"); }
}
public static void main(String[] args) {
checkAge(20); // Call the checkAge method and pass along an age of 20
}

14. Write a program using Wrapper classes to provide a way to use primitive data types (int,
boolean, etc..) as objects.
public class Main {
public static void main(String[] args) {
Integer myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
System.out.println(myInt);
System.out.println(myDouble);
System.out.println(myChar); }

15. Write a JAVA program to convert an Integer to a String, and use the length() method of the
String class to output the length of the "string":
public class Main {
public static void main(String[] args) {

Integer myInt = 100;


String myString = myInt.toString();
System.out.println(myString.length());}

16. Write a Java program to connect to the database,


import java.sql.*;
public class Connecting {
public static void main(String[] args) {
try{
String Query = "Select * from stud where rollno = 2";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/school","root","opjs");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(Query);
rs.next();
String sname = rs.getString(2);
System.out.println(sname);
con.close(); }
catch(Exception e){
}

(Note: Write it in your IT Practical Copy)


Note: ‘Content developed/prepared absolutely from home.

You might also like