0% found this document useful (0 votes)
18 views15 pages

Lab 4

The document contains multiple Java classes for different applications including a Polynomial Calculator, a Student Management System, an Employee Salary System, and a Caesar Cipher. Each application demonstrates basic functionalities such as polynomial evaluation, student average calculation, employee salary computation, and text encryption/decryption. The code is structured with classes and methods to handle specific tasks, utilizing user input for interaction.

Uploaded by

Emre Selvili
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views15 pages

Lab 4

The document contains multiple Java classes for different applications including a Polynomial Calculator, a Student Management System, an Employee Salary System, and a Caesar Cipher. Each application demonstrates basic functionalities such as polynomial evaluation, student average calculation, employee salary computation, and text encryption/decryption. The code is structured with classes and methods to handle specific tasks, utilizing user input for interaction.

Uploaded by

Emre Selvili
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

1- import java.util.

Scanner;

public class PolynomialCalculator {


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

// Get the degree of the polynomial


System.out.print("Enter the degree of the polynomial: ");
int degree = scanner.nextInt();

// Get the coefficients


int[] coefficients = getCoefficients(degree, scanner);

// Print the polynomial


printPolynomial(coefficients);

// Get the value of x


System.out.print("Enter the value of x: ");
int x = scanner.nextInt();

// Evaluate the polynomial


double result = evaluatePolynomial(coefficients, x);

// Print the result


System.out.println("P(" + x + ") = " + result);

scanner.close();
}

/**
* Reads coefficients from the user.
*
* @param degree The degree of the polynomial.
* @param scanner The Scanner object to read input.
* @return An array of coefficients.
*/
public static int[] getCoefficients(int degree, Scanner scanner) {
System.out.println("Enter " + (degree + 1) + " coefficients from highest to
lowest degree:");
int[] coefficients = new int[degree + 1];

for (int i = 0; i <= degree; i++) {


coefficients[i] = scanner.nextInt();
}

return coefficients;
}

/**
* Computes the polynomial result.
*
* @param coefficients The array of coefficients.
* @param x The value of x.
* @return The evaluated polynomial result.
*/
public static double evaluatePolynomial(int[] coefficients, int x) {
double result = 0;

for (int i = 0; i < coefficients.length; i++) {


// Calculate x^(degree-i)
int power = coefficients.length - 1 - i;
double term = coefficients[i] * Math.pow(x, power);
result += term;
}
return result;
}

/**
* Displays the polynomial in readable format.
*
* @param coefficients The array of coefficients.
*/
public static void printPolynomial(int[] coefficients) {
System.out.print("Polynomial: ");

for (int i = 0; i < coefficients.length; i++) {


int power = coefficients.length - 1 - i;

// Don't print anything for zero coefficients (except for constant term if all
are zero)
if (coefficients[i] == 0 && (power > 0 || i < coefficients.length - 1)) {
continue;
}

// Add plus sign for non-first positive terms


if (i > 0 && coefficients[i] > 0) {
System.out.print(" + ");
} else if (i > 0 && coefficients[i] < 0) {
System.out.print(" ");
}

// Print coefficient (unless it's 1 or -1 for non-constant terms)


if (power > 0) {
if (coefficients[i] == 1) {
// Don't print 1
} else if (coefficients[i] == -1) {
System.out.print("-");
} else {
System.out.print(coefficients[i]);
}
} else {
System.out.print(coefficients[i]);
}

// Print variable and power


if (power > 0) {
System.out.print("x");
if (power > 1) {
System.out.print("^" + power);
}
}
}

System.out.println();
}
}

2- import java.util.ArrayList;
class Student {
private String studentId;
private String name;
private double[] grades;

/**
* Constructor for Student
*
* @param studentId The student ID
* @param name The student name
* @param grades Array of student grades
*/
public Student(String studentId, String name, double[] grades) {
this.studentId = studentId;
this.name = name;
this.grades = grades;
}

/**
* Calculates the average grade for the student
*
* @return The average grade
*/
public double calculateAverage() {
if (grades.length == 0) {
return 0.0;
}

double sum = 0;
for (double grade : grades) {
sum += grade;
}
return sum / grades.length;
}

/**
* Gets the student ID
*
* @return The student ID
*/
public String getStudentId() {
return studentId;
}

/**
* Gets the student name
*
* @return The student name
*/
public String getName() {
return name;
}
}

class StudentManager {
private ArrayList<Student> students;

/**
* Constructor for StudentManager
*/
public StudentManager() {
students = new ArrayList<>();
}
/**
* Adds a student to the manager
*
* @param student The student to add
*/
public void addStudent(Student student) {
students.add(student);
}

/**
* Returns the student with the highest average
*
* @return The top student
*/
public Student getTopStudent() {
if (students.isEmpty()) {
return null;
}

Student topStudent = students.get(0);


double highestAverage = topStudent.calculateAverage();

for (Student student : students) {


double average = student.calculateAverage();
if (average > highestAverage) {
highestAverage = average;
topStudent = student;
}
}

return topStudent;
}

/**
* Calculates the average for a specific student
*
* @param studentId The ID of the student
* @return The student's average, or -1 if student not found
*/
public double calculateAverage(String studentId) {
for (Student student : students) {
if (student.getStudentId().equals(studentId)) {
return student.calculateAverage();
}
}

return -1; // Student not found


}
}

public class StudentManagementSystem {


public static void main(String[] args) {
// Create a student manager
StudentManager manager = new StudentManager();

// Add students
manager.addStudent(new Student("123", "Alice", new double[]{90, 85,
88}));
manager.addStudent(new Student("124", "Bob", new double[]{75, 80, 70}));

// Get and display the top student


Student topStudent = manager.getTopStudent();
System.out.println("Top Student: " + topStudent.getName());
// Calculate and display Bob's average
double bobAverage = manager.calculateAverage("124");
System.out.println("Bob's Average: " + bobAverage);
}
}

3- import java.util.ArrayList;

class Employee {
private String name;
private double salary;
private int overtimeHours;
private static final double OVERTIME_RATE = 50.0; // Overtime pay rate

/**
* Constructor for Employee
*
* @param name The employee name
* @param salary The base salary
* @param overtimeHours The number of overtime hours
*/
public Employee(String name, double salary, int overtimeHours) {
this.name = name;
this.salary = salary;
this.overtimeHours = overtimeHours;
}

/**
* Calculates the total salary including overtime
*
* @return The total salary
*/
public double calculateSalary() {
return salary + (overtimeHours * OVERTIME_RATE);
}

/**
* Gets the employee name
*
* @return The employee name
*/
public String getName() {
return name;
}
/**
* Gets the base salary
*
* @return The base salary
*/
public double getSalary() {
return salary;
}

/**
* Gets the overtime hours
*
* @return The overtime hours
*/
public int getOvertimeHours() {
return overtimeHours;
}
}

class Company {
private ArrayList<Employee> employees;

/**
* Constructor for Company
*/
public Company() {
employees = new ArrayList<>();
}

/**
* Adds an employee to the company
*
* @param employee The employee to add
*/
public void addEmployee(Employee employee) {
employees.add(employee);
}

/**
* Returns the highest-paid employee
*
* @return The highest-paid employee
*/
public Employee getHighestPaid() {
if (employees.isEmpty()) {
return null;
}

Employee highestPaid = employees.get(0);


double highestSalary = highestPaid.calculateSalary();

for (Employee employee : employees) {


double salary = employee.calculateSalary();
if (salary > highestSalary) {
highestSalary = salary;
highestPaid = employee;
}
}

return highestPaid;
}
}
public class EmployeeSalarySystem {
public static void main(String[] args) {
// Create a company
Company company = new Company();

// Add employees
company.addEmployee(new Employee("Alice", 5000, 5));
company.addEmployee(new Employee("Bob", 5500, 2));

// Get the highest-paid employee


Employee highestPaid = company.getHighestPaid();

// Display details of the highest-paid employee


System.out.println("Highest Paid: " + highestPaid.getName());
System.out.println("Base Salary: " + highestPaid.getSalary());
System.out.println("Overtime Hours: " + highestPaid.getOvertimeHours());
System.out.println("Total Salary: " + highestPaid.calculateSalary());
}
}

4- import java.util.Scanner;

public class CaesarCipher {


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

// Get input text


System.out.print("Enter text: ");
String text = scanner.nextLine();
// Get shift value
System.out.print("Enter shift value: ");
int shift = scanner.nextInt();

// Perform encryption and decryption


String encrypted = encrypt(text, shift);
String decrypted = decrypt(encrypted, shift);

// Display results
System.out.println("Encrypted: " + encrypted);
System.out.println("Decrypted: " + decrypted);

scanner.close();
}

/**
* Encrypts text using Caesar cipher by shifting letters forward
*
* @param text The text to encrypt
* @param shift The shift value
* @return The encrypted text
*/
public static String encrypt(String text, int shift) {
StringBuilder result = new StringBuilder();

for (int i = 0; i < text.length(); i++) {


char ch = text.charAt(i);

if (Character.isLetter(ch)) {
// Determine if the character is uppercase or lowercase
char base = Character.isUpperCase(ch) ? 'A' : 'a';
// Apply the shift, wrapping around if necessary
ch = (char) (((ch - base + shift) % 26) + base);
}

result.append(ch);
}

return result.toString();
}

/**
* Decrypts text encrypted with Caesar cipher by shifting letters backward
*
* @param text The text to decrypt
* @param shift The shift value
* @return The decrypted text
*/
public static String decrypt(String text, int shift) {
// To decrypt, we shift in the opposite direction
// This is equivalent to encrypting with a negative shift
// To handle negative shifts properly, we add 26 and then take the modulo
return encrypt(text, (26 - (shift % 26)) % 26);
}
}

You might also like