You are on page 1of 4

Winter Sem 2019-2020

CSE1007 – Java Programming Lab

Final Assessment Test

NAME: SHRUTI GARG REG NO:19BCE0994


LAB SLOT: L19+L20 DATE: 03-06-21

1. Write a java program to create a package Pack. a. Create a class A inside


Pack with method shift(int n) to perform a left shift of a number twice.
Create a class B inside Pack with shift(int n) to perform a right shift of a
number twice. Create a demo class that imports the package. Write a method
Encrypt(int) to encrypt a number by applying a left shift and store it in a file.
and another method Decrypt(int) to read the encrypted value from the file
and decrypt the number by performing a right shift. b. Create a user defined
exception unknownfomatException() if the input is not a number.

Code:
Pack A – Class A
package Pack;

public class A {
public int shift(int n){
int s=2;
return n<<s;
}
}

Pack A – Class B
package Pack;

public class B {
public int shift(int n) {
int s=2;
return n>>s;
}
}

Demo Class
package labfat;
import java.io.*;
import java.util.*;

import Pack.A;
import Pack.B;

public class demo {


int encrypt(int e){
A obj = new A();
return obj.shift(e);

}
int decrypt(int d){
B obj = new B();
return obj.shift(d);

class InvalidInputException extends Exception{


InvalidInputException(String s){
super(s);
}
}

public static void main(String[] args) throws IOException{

System.out.println("\nShruti Garg");
System.out.println("19BCE0994\n");

System.out.println("Enter the number: ");


Scanner sc = new Scanner(System.in);
int num = sc.nextInt();

FileOutputStream fout = new FileOutputStream("encrypt.txt");


demo obj = new demo();
int newdata = obj.encrypt(num);
fout.write((int)newdata);
fout.close();
System.out.println("Encrypted number: "+newdata);

FileInputStream fin = new FileInputStream("encrypt.txt");


int i=fin.read();
int data = ((int)i);
demo obj2 = new demo();
int decrypt = obj2.decrypt(data);
fin.close();
System.out.println("Decrypted number: "+decrypt);

}
Output:

You might also like