0% found this document useful (0 votes)
12 views9 pages

Java Programs for Class XII IT Practical

The document outlines a series of Java programming exercises for Class XII IT practicals, including programs for calculating percentages, storing and displaying names, using loops, arrays, and database connections. Each exercise specifies the required hardware and software, along with example code and expected output. The exercises cover fundamental programming concepts such as control structures, data types, and object-oriented programming principles.

Uploaded by

hfdasfd
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)
12 views9 pages

Java Programs for Class XII IT Practical

The document outlines a series of Java programming exercises for Class XII IT practicals, including programs for calculating percentages, storing and displaying names, using loops, arrays, and database connections. Each exercise specifies the required hardware and software, along with example code and expected output. The exercises cover fundamental programming concepts such as control structures, data types, and object-oriented programming principles.

Uploaded by

hfdasfd
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 – XII (IT) Practical (JAVA)

AIM: Write a program to calculate percentage calculator program, using three


variables named ,marks_obtained, total_marks and percentage.
Hardware Required: i3 processor and above, 4GB RAM, 500GB HD/SSD
Software Required: NetBeans 8.0 or higher

4. Write a Java program to calculate percentage calculator program, using three


variables named marks_obtained, total_marks and percentage.
Hardware Required: i3 processor and above, 4GB RAM, 500GB HD/SSD
Software Required: NetBeans 8.0 or higher
Code:
public static void main(String[] args) {
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);
}

Output

5. Write a Java program to store textual data, for example, the name of a student
Chandra S Azad.

Hardware Required: i3 processor and above, 4GB RAM, 500GB HD/SSD


Software Required: NetBeans 8.0 or higher
Code:
public static void main(String[] args) {
char middle_name = 'S';
String first_name = "Chandra";
String last_name = "Azad";
[Link](first_name+" "+ middle_name+" "+last_name); }
}

Output
6. Write a Java program to demonstrates usage of the switch statement for finding
week day using class method.

Hardware Required: i3 processor and above, 4GB RAM, 500GB HD/SSD


Software Required: NetBeans 8.0 or higher
Code:
import [Link];
public static void main (String[ ] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter the Weeday No. (1-7): ");
int day = [Link]();
switch (day) {
case 1: [Link]("Today is Monday");
break;
case 2: [Link]("Today is Tuesday");
break;
case 3: [Link]("Today is Wednesday");
break;
case 4: [Link]("Today is Thursday");
break;
case 5: [Link]("Today is Friday");
break;
case 6: [Link]("Today is Saturday");
break;
case 7: [Link]("Today is Sunday");
break;
default: [Link]("Invalid Weekday No");
break; }
}

Output

7. Write a Java program to print the squares of numbers from 1 to 5 using FOR loop

Hardware Required: i3 processor and above, 4GB RAM, 500GB HD/SSD


Software Required: NetBeans 8.0 or higher
Code:
public static void main (String[ ] args) {
for (int number = 1; number<= 5; ++number){
[Link]("Square of "+ number);
[Link](" = "+ number*number);}
}

Output
8. Write a Java program to print the squares of numbers from 1 to 5using WHILE
loop

Hardware Required: i3 processor and above, 4GB RAM, 500GB HD/SSD


Software Required: NetBeans 8.0 or higher
Code:
public static void main (String[ ] args) {
int number = 1;
while (number <= 5) {
[Link] ("Square of " + number);
[Link] (" = " + number*number);
++number; }
}

Output

9. Write a Java program to print the squares of numbers from 1 to 5using do while
loop

Hardware Required: i3 processor and above, 4GB RAM, 500GB HD/SSD


Software Required: NetBeans 8.0 or higher
Code:
public static void main (String[ ] args) {
int number = 1;
do {
[Link] ("Square of " + number);
[Link] (" = " + number*number);
++number; }
while (number <= 5);
}
Output

10. Write a Java program to enter your fname and Lname using Class method.

Hardware Required: i3 processor and above, 4GB RAM, 500GB HD/SSD


Software Required: NetBeans 8.0 or higher
Code:
import [Link];
public static void main(String[] args) {
Scanner user_input = new Scanner([Link]);
[Link]("Enter Your First Name : ");
String Fname = user_input.next();
[Link]("Enter Your Last Name : ");
String Lname = user_input.next();
[Link](Fname+" "+ Lname);
}

Output

11. Write a program to find out how many elements an array has, use the length
property:

Hardware Required: i3 processor and above, 4GB RAM, 500GB HD/SSD


Software Required: NetBeans 8.0 or higher
Code:
import [Link];
public static void main(String[] args) {
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
[Link]([Link]); }

Output
12. Write a program to create a two-dimensional array, add each array within its
own set of curly braces:

Hardware Required: i3 processor and above, 4GB RAM, 500GB HD/SSD


Software Required: NetBeans 8.0 or higher
Code:
import [Link];
public static void main(String[] args) {
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
int x = myNumbers[1][2];
[Link](x); }

Output

13. Write a program to use a for loop inside another for loop to get the elements
of a two-dimensional array

Hardware Required: i3 processor and above, 4GB RAM, 500GB HD/SSD


Software Required: NetBeans 8.0 or higher
Code:
import [Link];
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]);} }

Output
14. Write a program to store five marks in the array

Hardware Required: i3 processor and above, 4GB RAM, 500GB HD/SSD


Software Required: NetBeans 8.0 or higher
Code:
import [Link];
public static void main(String[] args) {
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);}

Output

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

Hardware Required: i3 processor and above, 4GB RAM, 500GB HD/SSD


Software Required: NetBeans 8.0 or higher
Code:
import [Link];
public static void main(String args[]) {
String[] names = {"Kajal", "Anisha", "Dipti", "Rashi", "Srishti", "Anisha", "Khusboo",
"Sweekriti","Archana","Angeline", "Chanchal", "Astha"};
[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]();}

Output

16. Write a program to check Age using call a method with an integer parameter.

Hardware Required: i3 processor and above, 4GB RAM, 500GB HD/SSD


Software Required: NetBeans 8.0 or higher
Code:
public static void main(String[] args) {
// 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
}

Output:

17. Write a program using Wrapper classes to provide a way to use primitive data
types (int, boolean, etc..) as objects.

Hardware Required: i3 processor and above, 4GB RAM, 500GB HD/SSD


Software Required: NetBeans 8.0 or higher
Code:
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);
}

Output

18. 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":

Hardware Required: i3 processor and above, 4GB RAM, 500GB HD/SSD


Software Required: NetBeans 8.0 or higher
Code:
public class Main {
public static void main(String[] args) {
Integer myInt = 100;
String myString = [Link]();
[Link]([Link]());
}

Output

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

Hardware Required: i3 processor and above, 4GB RAM, 500GB HD/SSD


Software Required: NetBeans 8.0 or higher
Code:
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){
}

Output:

Common questions

Powered by AI

Three Java programs demonstrate the use of different looping constructs: FOR, WHILE, and DO-WHILE. Each loop iterates from 1 to 5 and calculates the square of the current number. The FOR loop pre-defines the start, end, and increment in the syntax. The WHILE loop checks the condition before executing the loop body, and the DO-WHILE loop executes the loop body at least once before checking the condition .

The Java code employs nested FOR loops to iterate over the elements of a two-dimensional array. The outer loop navigates through the rows, while the inner loop accesses elements within each row. This approach efficiently traverses the entire array, enabling operations on each element, which is key for tasks like summing elements or performing matrix operations .

The Java program calculates percentage by dividing the marks obtained by the total marks and multiplying the result by 100. For example, if the total marks are 400 and the marks obtained are 346, the percentage calculation would be (346/400) * 100, resulting in 86.5% .

The Java program initializes an array to store five student marks. It iterates over the array to calculate the percentage and determine if each student passed or failed based on a threshold of 40%. The results are printed in a tabular format .

Converting an Integer to a String in Java allows for operations and methods applicable to strings, such as finding the length of the resulting string. This flexibility is advantageous for formatting, concatenating with other strings, and displaying numeric values in textual contexts .

The Java program defines a method named checkAge with an integer parameter for age. Inside the method, a conditional statement checks if the age is less than 18. If true, it prints 'Access denied - You are not old enough!' Otherwise, it prints 'Access granted - You are old enough!' This method is invoked with the age 20 in the main method .

The program connects to a MySQL database using JDBC. It first loads the MySQL JDBC driver, creates a connection with the database using the specified URL, username, and password, and executes an SQL query to select records from a table. The ResultSet object is used to retrieve data from the executed query, and the connection is closed after use .

The Java program uses a switch statement to identify the weekday. It prompts the user to enter a weekday number (1-7), then matches the input value to a corresponding case that prints the correct weekday name. If the input does not match any case, it defaults to printing 'Invalid Weekday No' .

The hardware required to run the Java programs includes an i3 processor and above, 4GB RAM, and either a 500GB HD or SSD. The software required is NetBeans 8.0 or higher .

The Java program uses wrapper classes to allow primitive data types to be used as objects. For example, an Integer object holds the value 5, a Double object holds 5.99, and a Character object holds 'A'. These wrapper objects are then printed, demonstrating their usage .

You might also like