JPR Pratical Exam Code
JPR Pratical Exam Code
import java.util.Scanner;
2
// Display even numbers from 1 to 20 using for loop
public class PT1_2 {
public static void main(String[] args) {
for(int i = 0; i < 20;i += 2){
System.out.println("I : " + i);
}
}
}
3
// Display numbers divisible by 2 and 5 from 1 to 50
public class PT1_3 {
public static void main(String[] args) {
for(int i = 1; i <= 50; i++){
if(i % 2 == 0 && i % 5 == 0)
System.out.print(" I = " + i);
}
}
}
4
// Calculate the factorial of a given number using while loop
public class PT1_4 {
public static void main(String[] args) {
int number = 5; // Example number for factorial calculation
int i = 1;
int factorial = 1;
6
// Print a multiplication table of a given number from 1 to 10 using
nested for loops
public class PT1_6 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println("Start of Multiplication Table " + i);
for (int j = 1; j <= 10; j++) {
System.out.println(i + " X " + j + " = " + i * j);
}
System.out.println("End of Multiplication Table " + i + "\n");
}
}
}
7
// Display the following pattern
// 1
// 1 2
// 1 2 3
// 1 2 3 4
public class PT1_7 {
public static void main(String[] args) {
for(int i = 1; i < 5; i++){
for(int j = 1; j <= i; j++){
System.out.print(j + " ");
}
System.out.println();
}
}
}
1
// Write a program to show the use of any six methods of String
class
public class PT2_1 {
public static void main(String[] args) {
String Name = "Daksh";
String Specification = " Tech God";
// 1
System.out.println(Name.charAt(0));
// 2
System.out.println(Name.length());
// 3
System.out.println(Name.toUpperCase());
// 4
System.out.println(Name.toLowerCase());
// 5
System.out.println(Name.concat(Specification));
//6
System.out.println(Name.replace("Daksh","Smiley"));
//7
System.out.println(Name.compareTo(Specification));
//8
System.out.println(Name.compareToIgnoreCase(Specification));
//9
System.out.println(Name.equals("Smiley"));
//10
System.out.println(Name.indexOf("S"));
}
}
2
//Write a program to show the use of any six methods of
Vector class
import java.util.Vector;
//1
v.add(1);
//2
System.out.println(v.elementAt(0));
//3
System.out.println(v.firstElement());
//4
System.out.println(v.get(0));
//5
System.out.println(v.capacity());
//6
System.out.println(v.size());
//7
System.out.println(v.contains(1));
//8
System.out.println(v.remove(1));
//9
System.out.println(v.equals(1));
//10
System.out.println(v.isEmpty());
}
}
3
// Write a program to show the use of any six method of Integer
Wrapper class
public class PT2_3 {
public static void main(String[] args) {
Integer wrapper = new Integer(5);
//1
System.out.println(wrapper.compareTo(Integer.valueOf(1)));
//2
System.out.println(wrapper.equals(5));
//3
System.out.println(wrapper.shortValue());
//4
System.out.println(wrapper.longValue());
//5
System.out.println(wrapper.doubleValue());
//6
System.out.println(wrapper.floatValue());
//7
System.out.println(wrapper.intValue());
//8
System.out.println(wrapper.toString());
//9
System.out.println(wrapper.hashCode());
//10
System.out.println(wrapper.compare(5,5));
//11
System.out.println(wrapper.max(1,5));
//12
System.out.println(wrapper.min(1,5));
}
}
4
// Write a program to display the rate of interest of
banks by method overriding
public class PT2_4 {
public static void main(String[] args){
Book book = new Book();
book.SetRateOfInterest(0.2);
System.out.println(book.GetRateOfInterest());
}
}
class Bank{
protected double RateOfInterest = 0.3;// 3%
public double GetRateOfInterest(){
return RateOfInterest;
}
}
//Override
public double GetRateOfInterest(){
return RateOfInterest;
}
}
5
// Write a program to demonstrate the concept of
multilevel inheritance in Java
public class PT2_5 {
public static void main(String[] args) {
Boss boss = new Boss();// 1
Manager manager = new Manager();// 2
Employee employee = new Employee();// 3
}
}
class Boss {
Boss() {
System.out.println("Class : Boss : 1");
}
}
class Formula {
// Assume that room is a square shape
protected double Area;
protected double Perimeter;
protected double Side;
interface Area{
public double AreaOfRectangle(double length,double Width);
public double AreaOfCircle(double radius);
}
class Device{
public static String VendorName;
public static Integer RamSize;
public static Integer OsVersion;
static{
VendorName = "Reliance";
RamSize = 16;
OsVersion = 33;
}
}
interface Loader{
public void LoadOS();
}
class Student{
String Name;
int RollNo;
public Student(String Name, int RollNo){
this.Name = Name;
this.RollNo = RollNo;
System.out.println("Student Name :"+Name +" | Roll Number : " + RollNo);
}
}
10
// Write a program to define class Employee with members as id and salary.
Accept data for five employees and display
// details of employees getting highest salary
import java.util.Scanner;
class Employee {
private int id;
private double salary;
//Compilation Coomands
// javac -d . Department.java
// javac InstituteApp.java
// java InstituteApp
1
// Design an Applet to pass username and password as parameters
and check if password contains more than 8
// characters
import java.applet.*;
import java.awt.*;
try {
// Check if salary is negative
if (salary < 0) {
throw new NegativeSalaryException("Salary cannot be negative");
} else {
System.out.println("Employee name: " + name);
System.out.println("Employee salary: " + salary);
}
} catch (NegativeSalaryException e) {
System.out.println("Error: " + e.getMessage());
}finally {
scanner.close();
}
}
}
3
// write a program to create an applet for displaying four circle
one below the other and filled the alternate circle with
// red color
import java.awt.*;
import java.applet.*;
public class PT3_3 extends Applet {
public void paint(Graphics g) {
g.setColor(Color.RED);
g.fillOval(100, 100, 100, 100);
g.setColor(Color.BLACK);
g.drawOval(150, 150, 100, 100);
g.setColor(Color.RED);
g.fillOval(200, 200, 100, 100);
g.setColor(Color.BLACK);
g.drawOval(250, 250, 100, 100);
}
}
/*
* <applet code = PT3_3.class height = 1000 width = 1000>
* <applet/>
*/
4
//Write a program to define two threads for displaying even and odd
numbers respectively with a delay of 500 ms after
// each number
public class PT3_4 {
Even even = new Even();
Odd odd = new Odd();
}
// default priority
System.out.println("Default Priority of Thread 1: " +
thread1.getPriority());
System.out.println("Default Priority of Thread 2: " +
thread2.getPriority());
System.out.println("Default Priority of Main Thread: " +
Thread.currentThread().getPriority());
// user-defined priority
System.out.println("User-defined Priority of Thread 1: " +
thread1.getPriority());
System.out.println("User-defined Priority of Thread 2: " +
thread2.getPriority());
}
}
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
fw.close();
fr.close();
}catch(FileNotFoundException e){
System.out.println("File not found");
}
}
}