0% found this document useful (0 votes)
937 views4 pages

Class XII Java Programs Overview

The document contains class notes on the topic of practical files for an Information Technology class. It includes 16 examples of Java code covering topics like percentage calculators, string manipulation, arrays, sorting, methods, wrappers classes, database connectivity and more. The code snippets provide examples of programming concepts and techniques for students to practice.

Uploaded by

Beluga
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)
937 views4 pages

Class XII Java Programs Overview

The document contains class notes on the topic of practical files for an Information Technology class. It includes 16 examples of Java code covering topics like percentage calculators, string manipulation, arrays, sorting, methods, wrappers classes, database connectivity and more. The code snippets provide examples of programming concepts and techniques for students to practice.

Uploaded by

Beluga
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

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;
[Link]("Student 1's Percentage = "+percentage);
marks_obtained = 144;
percentage = (marks_obtained/total_marks)*100;
[Link]("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";
[Link](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;
}
[Link] (day);}
4. WAP to display the values from 1 to 5 using FOR loop
int i;
for (i=1; i <=5; i++){
[Link](“” + i);}

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


int i = 0;
while (i<=5){
[Link]("" + i);
i++;

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


int i = 1;
do{
[Link]("" + i);
i++;
}
while (i <=5);

7. Write a program to enter your name using String


String First_Name = [Link]();
String Last_Name = [Link]();
[Link](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"};
[Link]([Link]);
}

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];
[Link](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 < [Link]; ++i) {
for(int j = 0; j < myNumbers[i].length; ++j) {
[Link](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;
[Link]("\tClass Report");
[Link]("--------------------------------------");
[Link]("RollNo\tMarks\tPercentage\tResult");
[Link]("--------------------------------------");
for (int i = 0; i <[Link]; i++) {
double percentage = (marks[i]/total_marks)*100;
String result;
if (percentage >= 40)
result = "Passed";
else
result = "Failed";
[Link]((i+1)+"\t");
[Link](marks[i]+"\t");
[Link](percentage+"\t\t");
[Link](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"};
[Link]("Names Array before Sorting:");
for (int i = 0; i<[Link]; i++)
[Link](names[i] + ",");
[Link]();
[Link](names);
[Link]("Names after Sorting:");
for (int i=0; i < [Link]; i++)
[Link](names[i] + ",");
[Link]();}

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) {
[Link]("Access denied - You are not old enough!");

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


} else {
[Link]("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';
[Link](myInt);
[Link](myDouble);
[Link](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 = [Link]();
[Link]([Link]());}

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


import [Link].*;
public class Connecting {
public static void main(String[] args) {
try{
String Query = "Select * from stud where rollno = 2";
[Link]("[Link]");
Connection con = [Link]("jdbc:mysql://localhost/school","root","opjs");
Statement st = [Link]();
ResultSet rs = [Link](Query);
[Link]();
String sname = [Link](2);
[Link](sname);
[Link](); }
catch(Exception e){
}

(Note: Write it in your IT Practical Copy)


Note: ‘Content developed/prepared absolutely from home.

Common questions

Powered by AI

The program uses the Arrays.sort() method to sort an array of strings alphabetically. This method is beneficial because it provides a simple and efficient way to sort arrays, leveraging Java's built-in TimSort algorithm. This algorithm is optimized for real-world data and ensures stable, adaptive sorting with a time complexity of O(n log n), making it ideal for handling large datasets or frequent sorting operations .

The document proposes using wrapper classes such as Integer, Double, and Character to handle primitive data types as objects. This approach is advantageous because it allows for the use of object methods, extends functionality to primitives in collections (such as ArrayLists), and supports object-oriented features like generics and polymorphism. Additionally, it facilitates operations like serialization and synchronization .

The switch statement in the program sets the day variable based on the integer value of today. Each case in the switch corresponds to a day of the week, mapping numbers 1 through 7 to 'Monday' through 'Sunday', respectively. The default case handles any invalid input by assigning 'Incorrect Day!'. The program ensures correct output by using a break statement after each case, preventing fall-through and ensuring that only the correct weekday assignment is executed .

The document demonstrates converting an Integer to a String using the toString() method. The length of the resulting String is then determined using the length() method of the String class. This technique efficiently transforms numerical data into a character sequence for further processing or display measures .

The document presents a try-catch block for error handling while connecting to a database. The try block includes all database operations such as loading the database driver, establishing a connection, executing a query, and retrieving results. If any exceptions occur during these operations, the catch block handles them, preventing crashes and enabling graceful failure. This mechanism protects against SQL exceptions, connection failures, or driver issues, ensuring that the program can respond appropriately to errors without terminating unexpectedly .

The document provides a method, checkAge(int age), that uses an if-else statement to determine access based on age. If the provided age is less than 18, the method outputs 'Access denied - You are not old enough!'. Otherwise, it outputs 'Access granted - You are old enough!'. This method encapsulates the logic within one function, easily reusable for multiple age-checking scenarios .

A two-dimensional array is initialized by defining each row as an array within curly braces. The document provides an example where myNumbers is declared as {{1, 2, 3, 4}, {5, 6, 7}}. Accessing elements uses index notation [i][j] where 'i' is the row index and 'j' the column index. For instance, accessing myNumbers[1][2] retrieves the value '7', illustrating random access capability for retrieving specific elements .

The document uses a method that calculates the percentage by defining three variables: marks_obtained, total_marks, and percentage. The percentage is calculated using the formula: percentage = (marks_obtained/total_marks)*100. This method ensures accuracy by utilizing double data types for both marks_obtained and percentage, which allows for precise decimal arithmetic .

Nested for loops are used to iterate over the rows and columns of a two-dimensional array. The outer loop iterates through each sub-array (row), and the inner loop iterates through each element within the current sub-array (column). This approach is necessary to access each element individually and ensures that all elements within the multi-dimensional structure are processed, allowing the complete contents of the array to be printed .

The program calculates the percentage for each mark in a double array by dividing it by total_marks (400) and multiplying by 100. It determines pass or fail status by comparing each percentage with the cutoff of 40%. It uses an if-else construct to assign 'Passed' or 'Failed' to a result string, which is then printed along with the computed percentage, providing a clear class performance report .

You might also like