In: Computer Science
1. A company that wants to send data over the Internet will use an encryption program to ensure data security. All data will be transmitted as four-digit integers. The application should read a four-digit integer entered by the user and encrypt it as follows: Replace each digit with the remainder of the new value divided by 10 by adding 6 to the digit. Then replace the number in the first digit with the third, and the number in the second digit with the fourth. Print the encrypted integer on the screen. Write a separate application where an encrypted four-digit integer is entered and decrypted (reversing the encryption scheme) and finds the original number.
2. Create a class called Employee that contains three instance variables (First Name (String), Last Name (String), Monthly Salary (double)). Create a constructor that initializes these variables. Define set and get methods for each variable. If the monthly salary is not positive, do not set the salary. Create a separate test class called EmployeeTest that will use the Employee class. By running this class, create two employees (Employee object) and print the annual salary of each employee (object). Then set the new salary by giving each employee a 10% raise and display each employee's annual salary on the screen again.
Note: code should be written in Java language.
Ans: Please read comments in the programs to get a better understanding.
Ques 1:
import java.util.*;
// --------X Encrypt.java X---------
class Encrypt{
// Returns encrypted value
public static int encrypt(int val){
int tempVal = val, idx = 0;
int [] newNum = new int [4];
// store each new value in an array of size 4
while(idx<4 && tempVal > 0){
newNum[idx++] = ((tempVal%10)+6)%10;
tempVal = tempVal/10;
}
return ((1000*newNum[1])+(100*newNum[0])+(10*newNum[3])+newNum[2]);
}
}
// --------X Decrypt.java X---------
class Decrypt{
// Returns decrypted value
public static int decrypt(int val){
int tempVal = val, idx = 0;
int [] newNum = new int [4];
// store each new value in an array of size 4
while(idx<4){
newNum[idx++] = ((tempVal%10)+10) - 6;
tempVal = tempVal/10;
}
return ((1000*newNum[1])+(100*newNum[0])+(10*newNum[3])+newNum[2]);
}
}
// --------X Test.java X---------
class Test{
// Driver Method
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
// Takes Input
int val = sc.nextInt();
// Encrypt and print value
int encryptedVal = Encrypt.encrypt(val);
System.out.println("Encrypted value: "+encryptedVal);
// Decrypt the encrypted value and print
int decryptedVal = Decrypt.decrypt(encryptedVal);
System.out.println("Decrypted value: "+decryptedVal);
}
}
Ques 2:
// --------X Employee.java X---------
class Employee{
private String firstName;
private String lastName;
private double salary;
// Constructor
public Employee(){
firstName = "";
lastName = "";
salary = 0;
}
// Parameterized constructor
public Employee(String fname, String lname, double sal){
firstName = fname;
lastName = lname;
if(sal >= 0)
salary = sal;
else
salary = 0;
}
public void setFirstName(String fname){
firstName = fname;
}
public void setLastName(String lname){
lastName = lname;
}
public void setSalary(double salary){
if(salary >=0)
this.salary = salary;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public double getSalary(){
return salary;
}
}
// --------X EmployeeTest.java X---------
class EmployeeTest{
// Print salary
public static void printDetails(Employee e){
System.out.print("Annual salary of "+e.getFirstName()+" "+e.getLastName()+" is ");
System.out.println((12*e.getSalary()));
}
// Driver Method
public static void main(String args[]){
Employee e1 = new Employee("John", "Cena", 52672.25);
Employee e2 = new Employee("Steve", "Austin", 32456.75);
// Salary of employee 1
printDetails(e1);
// Salary of employee 2
printDetails(e2);
// Give increment
double salary;
salary = e1.getSalary();
e1.setSalary(salary+(0.1*salary));
salary = e2.getSalary();
e2.setSalary(salary+(0.1*salary));
// Print updated values
// Salary of employee 1
printDetails(e1);
// Salary of employee 2
printDetails(e2);
}
}