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.
Answer 1:
package Security;
// Defines class Encrypt
class Encrypt
{
// Method to encrypt the parameter plainNumber and
returns it
int encryptDigit(int plainNumber)
{
int one, two, three, four,
swapNo;
int encryptNumber = 0;
// Calculates the quotient, adding
6 to it and then gets the remainder
one = (plainNumber / 1000 + 6) %
10;
two = (plainNumber % 1000 / 100 +
6) % 10;
three = (plainNumber % 1000 % 100 /
10 + 6) % 10;
four = (plainNumber % 1000 % 100 %
10 + 6) % 10;
// Swapping process
swapNo = one;
one = three * 1000;
three = swapNo * 10;
swapNo = two;
two = four * 100;
four = swapNo * 1;
encryptNumber = one + two + three +
four;
// Returns the encrypted
number
return encryptNumber;
}// End of method
}// End of class Encrypt
// Defines class Decrypt
class Decrypt
{
// Method to decrypt the parameter encryptNumber and
returns it
int decryptDigit(int encryptNumber)
{
int first, second, third, fourth,
decryptedNumber, temp;
// Calculates the quotient
first = encryptNumber / 1000;
second = encryptNumber % 1000 /
100;
third = encryptNumber % 1000 % 100
/ 10;
fourth = encryptNumber % 1000 % 100
% 10;
// Swapping process
temp = ( first + 4) % 10;
first = ( third + 4) % 10;
third = temp;
temp = ( second + 4) % 10;
second = ( fourth + 4) % 10;
fourth = temp;
decryptedNumber = first * 1000 +
second * 100 + third * 10 + fourth;
// Returns the decrypted
number
return decryptedNumber;
}// End of method
}// End of class Decrypt
// Driver class definition
public class EncryptDecryptDigit
{
// main method definition
public static void main(String []s)
{
// Creates an object of class
Encrypt
Encrypt e = new Encrypt();
// Creates an object of class
Decrypt
Decrypt d = new Decrypt();
// Calls the method to
encrypt
int encryptNumber =
e.encryptDigit(1234);
System.out.println("\n Original
Number: 1234" +
"\n Encrypted Number: " +
encryptNumber);
// Calls the method to
decrypt
int decryptNumber =
d.decryptDigit(encryptNumber);
System.out.println("\n Encrypted
Number: " + encryptNumber +
"\n Decrypted Number: " +
decryptNumber);
}// End of main method
}// End of driver class
Sample Output:
Original Number: 1234
Encrypted Number: 9078
Encrypted Number: 9078
Decrypted Number: 1234
------------------------------------------------------------------------------------------------------------------------
// Defines class Employee
class Employee
{
// Instance variables to store employee
information
String firstName;
String lastName;
double monthlySalary;
// Parameterized constructor to assign parameter
values to instance variables
Employee(String firstName, String lastName, double
monthlySalary)
{
this.firstName = firstName;
this.lastName = lastName;
setMonthlySalary(monthlySalary);
}
// Getter methods
String getFirstName()
{
return firstName;
}
String getLastName()
{
return lastName;
}
double getMonthlySalary()
{
return monthlySalary;
}
// Setter methods
void setFirstName(String firstName)
{
this.firstName = firstName;
}
void setLastName(String lastName)
{
this.lastName = lastName;
}
void setMonthlySalary(double monthlySalary)
{
// Checks if parameter
monthlySalary is negative
if(monthlySalary < 0)
// Then assign
0.0
monthlySalary =
0.0;
// Otherwise assign parameter
value
else
this.monthlySalary = monthlySalary;
}
}// End of class Employee
// Driver class definition EmployeeTest
public class EmployeeTest
{
// main function definition
public static void main(String []s)
{
// Creates object of class Employee
using parameterized constructor
Employee emp1 = new
Employee("Pyari", "Sahu", 5000);
Employee emp2 = new
Employee("Sasmita", "Panda", 3000);
// Calculates and display annual
salary
System.out.println("\n Employee " +
emp1.getFirstName() + " "
+ emp1.getLastName() + " annual Salary: $"
+
(emp1.getMonthlySalary() * 12.0));
System.out.println("\n Employee " +
emp2.getFirstName() + " "
+ emp2.getLastName() + " annual Salary: $"
+
(emp2.getMonthlySalary() * 12.0));
// Sets 10% hide of monthly
salary
emp1.setMonthlySalary(emp1.getMonthlySalary() +
(emp1.getMonthlySalary() * 0.10));
emp2.setMonthlySalary(emp1.getMonthlySalary() +
(emp1.getMonthlySalary() * 0.10));
System.out.println("\n After 10%
hike \n");
// Calculates and display annual
salary
System.out.println("\n Employee " +
emp1.getFirstName() + " "
+ emp1.getLastName() + " annual Salary: $"
+
(emp1.getMonthlySalary() * 12.0));
System.out.println("\n Employee " +
emp2.getFirstName() + " "
+ emp2.getLastName() + " annual Salary: $"
+
(emp2.getMonthlySalary() * 12.0));
}// End of main method
}// End of driver class
Sample Output:
Employee Pyari Sahu annual Salary: $60000.0
Employee Sasmita Panda annual Salary: $36000.0
After 10% hike
Employee Pyari Sahu annual Salary: $66000.0
Employee Sasmita Panda annual Salary: $72600.0