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: