0% found this document useful (0 votes)
27 views22 pages

JPR Pratical Exam Code

Uploaded by

adityamaurya9471
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)
27 views22 pages

JPR Pratical Exam Code

Uploaded by

adityamaurya9471
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
You are on page 1/ 22

1

// Find if the entered character is vowel or not using


switch case

import java.util.Scanner;

public class PT1_1{


public static void main(String[] args){
try{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a character");
char ch = sc.next().charAt(0);
switch(ch){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println("Vowel");
break;
default:
System.out.println("Consonant/Not
Vowel");
break;
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}

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;

while (i <= number) {


factorial *= i;
i++;
}

System.out.println("Factorial of " + number + " is: "+factorial);


}
}
5
// Write a program to accept a number as command line
argument and print the number is even or odd.
public class PT1_5 {
public static void main(String[] args) {
boolean Key = false;
try {
int i = Integer.parseInt(args[0]);
if (i % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
} catch (Exception e) {
System.out.println("Error parsing");
Key = true;
}
}
}

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;

public class PT2_2 {


public static void main(String[] args)
{
Vector<Integer> v = new 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;
}
}

class Book extends Bank{


public void SetRateOfInterest(double RateOfInterest){
this.RateOfInterest = 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 Manager extends Boss {


Manager() {
System.out.println("Class : Manager : 2");
}
}

class Employee extends Manager {


Employee() {
System.out.println("Class : Employee : 3");
}
}
6
//Develop a program to calculate the room area and volume to
illustrate the concept of single inheritance
public class PT2_6 {
public static void main(String[] args) {
Room room = new Room();
room.RoomDetails(5.0); // Pass the side length of the room
System.out.println("Area: " + room.GetArea());
System.out.println("Perimeter: " + room.GetPerimeter());
}
}

class Formula {
// Assume that room is a square shape
protected double Area;
protected double Perimeter;
protected double Side;

protected void Calculate() {


Area = Side * Side;
Perimeter = 4 * Side;
}

protected double GetArea() {


return Area;
}

protected double GetPerimeter() {


return Perimeter;
}

protected double GetSide() {


return Side;
}
}

class Room extends Formula {


public void RoomDetails(double Side) {
this.Side = Side;
Calculate();
}
}
7
// Develop a program to find the area of rectangle and circle using interfaces
public class PT2_7 {
Shape shape = new Shape(4, 6, 3);
}

interface Area{
public double AreaOfRectangle(double length,double Width);
public double AreaOfCircle(double radius);
}

class Shape implements Area{


public Shape(double length,double Width,double radius){
System.out.println("Area of Rectangle : " + AreaOfRectangle(length,Width));
System.out.println("Area of Circle : " + AreaOfCircle(radius));
}

public double AreaOfRectangle(double length,double Width){


return length*Width;
}

public double AreaOfCircle(double radius){


return Math.PI*radius*radius;
}
}
8
// Implement following inheritance
// class Device contains VendorName,OsVersion,RamSize
// interface Loader contains LoadOS()
// class Mobile made from both Device(class) , Loader(interface)

public class PT2_8 {


public static void main(String[] args){
Mobile A33 = new Mobile();
A33.LoadOS();
}
}

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 Mobile extends Device implements Loader{


public void LoadOS(){
System.out.println("Vendor Name: " + VendorName);
System.out.println("Os Version: " + OsVersion);
System.out.println("Ram Size: " + RamSize);
}
}
9
// Write a program to initialize object of a class student using
parameterized constructor
public class PT2_9 {
public static void main(String[] args) {
Student student = new Student("Daksh",42);
}
}

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;

public class PT2_10 {


public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
Employee employee[] = new Employee[5];
Double HighestSalary = 0.0D;
int EmpId = 0;
for(int i = 0; i < 5; i++){
System.out.println("Enter Employee Id for "+ i + " : ");
int id = scanner.nextInt();
System.out.println("Enter Employee Salary "+ i + " : ");
double salary = scanner.nextDouble();

employee[i] = new Employee();


employee[i].SetDetails(id, salary);

if(HighestSalary < salary){


HighestSalary = salary;
EmpId = id;
}
}

System.out.println("Employee With Highest salary : Id - " + EmpId + ",


Salary - " + HighestSalary);
}
}

class Employee {
private int id;
private double salary;

public void SetDetails(int id, double salary){


this.id = id;
this.salary = salary;
}
}
11
// Define a package named myinstitute include class named as department with
one method to display the staff of the
// department. Develop a program to import this package in a java application
and call the method defined in the
// package

//note write as it for exam

//Package Code Part Start


package myinstitute;

public class Department {


public void displayStaff() {
System.out.println("Staff of the department: Daksh, Naitik, Meet");
}
}
//Package Code Part End

//Execution Code Part Start


import myinstitute.Department;

public class InstituteApp {


public static void main(String[] args) {
Department department = new Department();
department.displayStaff();
}
}
//Execution Code Part End

//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.*;

public class PT3_1 extends Applet {


String U = "Empty";
String P = "Empty";
public void init(){
U = getParameter("Username");
P = getParameter("Password");
}
public void paint(Graphics g){
if(P.length() > 8){
drawString("More Than 8 chars in Password",20,20);
}
else{
drawString("Less Than 8 chars in Password",20,20);
}
}
}
/*
* <applet code = PT3_1.class height = 200 width = 200>
* <param name = Username Value = "Empty" />
* <param name = Password Value = "Empty" />
* <applet/>
*/
2
// Write a program to input name and salary of employee and throw user defined
exception if entered salary is negative
import java.util.Scanner;

// Custom exception class for negative salary


class NegativeSalaryException extends Exception {
public NegativeSalaryException(String message) {
super(message);
}
}

public class PT3_2 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter employee name: ");


String name = scanner.nextLine();

System.out.print("Enter employee salary: ");


double salary = scanner.nextDouble();

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();
}

class Even extends Processor {


public void run() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
Delay(500);
System.out.println(i);
}
}
}
}

class Odd extends Processor {


public void run() {
for (int i = 1; i <= 10; i++) {
if (i % 2!= 0) {
Delay(500);
System.out.println(i);
}
}
}
}

class Processor extends Thread {


public void Delay(long milliseconds){
try{
Thread.sleep(milliseconds);
}catch(InterruptedException e){
System.out.println(e.getStackTrace());
}
}
}
5
// Write a program to create two threads. One thread will display
the numbers from 1 to 50 (ascending order) and other
// thread will display numbers from 50 to 1 (descending order)
public class PT3_5 extends Thread{
Ascending A = new Ascending();
Desending D = new Desending();
A.start();
D.start();
}

class Ascending extends Thread {


public void run(){
for(int i = 1 ; i <= 50 ; i++){
System.out.println("i = " + i);
}
}
}

class Desending extends Thread {


public void run(){
for(int i = 50 ; i >= 50 ; i--){
System.out.println("i = " + i);
}
}
}
6
public class PT3_6 {
public static void main(String[] args) {
Thread thread1 = new Thread(new MyRunnable());
Thread thread2 = new MyThread();

// 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());

// Set custom priorities


thread1.setPriority(Thread.MIN_PRIORITY);
thread2.setPriority(Thread.MAX_PRIORITY);

// 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());
}
}

class MyRunnable implements Runnable {


public void run() {
// Some task
}
}

class MyThread extends Thread {


public void run() {
// Some task
}
}
7
//Write a program to copy content of one file into another file

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileNotFoundException;

public class PT3_7 {


public static void main(String[] args) throws IOException {
try{
FileWriter fw = new FileWriter("File1.txt");
FileReader fr = new FileReader("File2.txt");
int ch = fr.read();
while(ch != 0){
fw.write(ch);
}

fw.close();
fr.close();
}catch(FileNotFoundException e){
System.out.println("File not found");
}
}
}

You might also like