Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/Task01/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package Task01;

/*
Task: Write a method in Java to get the difference between the largest and smallest values in an array of integers. The length of the array must be 1 and above. Use loops and conditionals to develop the algorithm.
*/

import java.util.Scanner;

public class solution {
public static void main(String[] args) {
int n; // number of elements to read

Scanner sc = new Scanner(System.in); // Scanner to read user input from standard input
System.out.println("Enter the number of elements in the array: ");
n = sc.nextInt(); // read array length

// Validate that array length is at least 1, otherwise terminate early
if (n < 1) {
System.out.println("The length of the array must be greater than zero");
return;
}

int[] numbers = new int[n]; // allocate array with user-specified size

// Read n integers from the user and populate the array
for (int i = 0; i < n; i++) {
System.out.println("Enter the element " + (i + 1) + ": ");
numbers[i] = sc.nextInt();
}

// Initialize both largest and smallest with the first element
int largest = Integer.MIN_VALUE;
int smallest = Integer.MAX_VALUE;

// Traverse the array to find the true largest and smallest values
for (int i = 0; i < n; i++) {
// Update largest if the current element is greater
if (numbers[i] > largest) {
largest = numbers[i];
}

// Update smallest if the current element is smaller
if (numbers[i] < smallest) {
smallest = numbers[i];
}
}

// Output the results
System.out.println("The largest number is " + largest);
System.out.println("The smallest number is " + smallest);
System.out.println("The difference between largest and smallest is " + (largest - smallest));
}
}
52 changes: 52 additions & 0 deletions src/Task02/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package Task02;

/*
Write a method in Java to find the smallest and second smallest elements of a given array and print it in the console. Use loops and conditionals to develop the algorithm.
*/

import java.util.Scanner;

public class solution {
public static void main(String[] args) {
int n; // number of elements to read

Scanner sc = new Scanner(System.in); // Scanner to read user input from standard input
System.out.println("Enter the number of elements in the array: ");
n = sc.nextInt(); // read array length

// Validate that array length is at least 1, otherwise terminate early
if (n < 1) {
System.out.println("The length of the array must be greater than zero");
return;
}

int[] numbers = new int[n]; // allocate array with user-specified size

// Read n integers from the user and populate the array
for (int i = 0; i < n; i++) {
System.out.println("Enter the element " + (i + 1) + ": ");
numbers[i] = sc.nextInt();
}

int smallest = Integer.MAX_VALUE;
int secondSmallest = Integer.MAX_VALUE;

// Traverse the array to find the true largest and smallest values
for (int i = 0; i < n; i++) {
// Update the smallest element of array.
if (numbers[i] < smallest) {
secondSmallest = smallest;
smallest = numbers[i];
}

// Update the second smallest element of array.
if (numbers[i] < secondSmallest && numbers[i] != smallest) {
secondSmallest = numbers[i];
}
}

// Output results
System.out.println("The smallest number is " + smallest);
System.out.println("The secondSmallest number is " + secondSmallest);
}
}
88 changes: 88 additions & 0 deletions src/Task03/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package Task03;

/*
1. Create an Employee class to represent an employee of a company. Add all relevant properties and behaviors that you might need but you have to include a salary property. Don't forget to add getters and setters.
2. Create an Intern class that extends from Employee. All the Interns have a salary limit of 20000 (constant). You must validate if an intern is created (or salary updated) with a bigger salary than the max. The max value is set.
3. Write a program that creates 10 Employees and print it all the properties.
*/

import java.util.Scanner;
public class Employee {
private int id;
private String empName;
private String designation;
private double salary;
private boolean isIntern = false;

/**
* Sets the unique identifier for the employee.
*/
public void setId(int id) {
this.id = id;
}

/**
* Sets the employee's display name.
*/
public void setEmpName(String empName) {
this.empName = empName;
}

/**
* Sets the job title/role assigned to this employee.
*/
public void setDesignation(String designation) {
this.designation = designation;
}

/**
* Sets the salary for this employee.
* Note: Intern overrides this method to enforce a max salary.
*/
public void setSalary(double salary) {
this.salary = salary;
}

/**
* Marks this employee as an intern or not. This flag is informational; actual
* intern-specific constraints are implemented in Intern.
*/
public void setIsIntern(boolean isIntern) {
this.isIntern = isIntern;
}

/**
* @return the unique identifier of the employee.
*/
public int getId() {
return this.id;
}

/**
* @return the employee's display name.
*/
public String getEmpName() {
return this.empName;
}

/**
* @return the role/designation of the employee.
*/
public String getDesignation() {
return this.designation;
}

/**
* @return the current salary of the employee.
*/
public double getSalary() {
return this.salary;
}

/**
* @return true if this employee is flagged as an intern; false otherwise.
*/
public boolean getIsIntern() {
return this.isIntern;
}
}
29 changes: 29 additions & 0 deletions src/Task03/Intern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package Task03;

/*
2. Create an Intern class that extends from Employee. All the Interns have a salary limit of 20000 (constant). You must validate if an intern is created (or salary updated) with a bigger salary than the max. The max value is set.
*/

public class Intern extends Employee {
private static final double salaryLimit = 20000;

/**
* Caps the salary to salaryLimit if a higher value is provided.
*/
@Override
public void setSalary(double salary) {
if (salary > salaryLimit) {
salary = salaryLimit;
}

super.setSalary(salary);
}

/**
* Ignores the provided designation and forces it to "Intern".
*/
@Override
public void setDesignation(String designation) {
super.setDesignation("Intern");
}
}
166 changes: 166 additions & 0 deletions src/Task03/ListEmployees.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package Task03;

/*
3. Write a program that creates 10 Employees and print it all the properties.
*/

import java.util.ArrayList;
import java.util.Scanner;

public class ListEmployees {
static ArrayList<Employee> employees = new ArrayList<>();

public static void generateEmployeeData() {
String[] Names = {"Danial Khan", "Quintin Wise", "Vivian Gonzalez","Marisa Fennell","Katie Morley","Broderick Shumate","Gunnar Alvarado","Anjali Rollins","Jimmy Ott","Brigid Martinez","Joe Wood"};
String[] Designations = {"Chief","Central","District","Future","Chief","Human","Internal","Forward","International","Internal"};

for (int i = 1; i <= 10 ; i++) {
Employee emp = (i <= 8) ? new Employee() : new Intern();
emp.setId(i);
emp.setEmpName(Names[(int) (Math.random()*10)]);
emp.setDesignation(Designations[(int) (Math.random()*10)]);
emp.setSalary(Math.random()*50000);

employees.add(emp);
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean exit = false;
int choice;

generateEmployeeData();

// Main menu loop: read a choice and execute the corresponding action
do {
System.out.println("-------------MENU-------------");
System.out.println("1 - Add an employee");
System.out.println("2 - Update an employee");
System.out.println("3 - Delete an employee");
System.out.println("4 - Add an intern");
System.out.println("------------------------------");
System.out.println("5 - View all");
System.out.println("0 - Exit");

choice = sc.nextInt();

switch (choice) {
case 1 -> addEmployee(false);
case 2 -> {
System.out.println("Enter employee id to update: ");
int empId = sc.nextInt();
updateEmployee(empId);
}
case 3 -> {
System.out.println("Enter employee id to delete: ");
int empId = sc.nextInt();
deleteEmployee(empId);
}
case 4 -> addEmployee(true);
case 5 -> listAll();
case 0 -> exit = true;
}

} while (!exit);

sc.close();
}

/**
* Prints a simple table with all employees currently stored in ArrayList.
*/
static void listAll() {
System.out.printf("| %-6s | %-15s | %-15s | %-10s%n", "ID", "Name", "Designation", "Salary");
System.out.println("==========================================================");
for (Employee e : employees) {
System.out.printf("| %-6s | %-15s | %-15s | %-10s%n", e.getId(), e.getEmpName(), e.getDesignation(), Math.round(e.getSalary()));
}
}

/**
* Reads fields from the console and adds either a regular Employee or an Intern.
*
* @param isIntern if true, fills the shared Intern instance; otherwise the Employee instance.
* We object via a ternary so we can call common setters
* on the Employee reference.
*/
static void addEmployee(boolean isIntern) {
Scanner sc = new Scanner(System.in);

// Choose the underlying object based on the flag; both are Employees so
// we can call common getters/setters through the Employee reference.
Employee obj = isIntern ? new Intern() : new Employee();

System.out.println("Enter employee ID");
obj.setId(sc.nextInt());
sc.nextLine();

System.out.println("Enter employee name: ");
obj.setEmpName(sc.nextLine());

System.out.println("Enter employee designation: ");
obj.setDesignation(sc.nextLine());

System.out.println("Enter employee salary: ");
obj.setSalary(sc.nextDouble());

obj.setIsIntern(isIntern);

System.out.println("Employee added Successfully");
employees.add(obj);
}


/**
* Updates an existing employee by id. If not found, it reports and returns.
*/
static void updateEmployee(int id) {
// Find employee by id and update its fields
Employee _thisEmployee = null;
for (Employee e : employees) {
if (e.getId() == id) {
_thisEmployee = e;
break;
}
}

if (_thisEmployee == null) {
System.out.println("Employee with id " + id + " not found.");
return;
}

Scanner sc = new Scanner(System.in);

System.out.println("Updating employee with id: " + id + "");

System.out.print("Name [" + _thisEmployee.getEmpName() + "]: ");
_thisEmployee.setEmpName(sc.nextLine());

System.out.print("Designation [" + _thisEmployee.getDesignation() + "]: ");
_thisEmployee.setDesignation(sc.nextLine());

System.out.print("Salary [" + _thisEmployee.getSalary() + "]: ");
_thisEmployee.setSalary(sc.nextDouble());

System.out.println("Employee: " + id + " updated successfully.");
}


/**
* Deletes an employee with the given id from the Array list.
*/
static void deleteEmployee(int id) {
if (employees.isEmpty()) {
System.out.println("Employee list is empty.");
return;
}
boolean removed = employees.removeIf(e -> e.getId() == id);

if (removed) {
System.out.println("Employee with id " + id + " deleted successfully.");
} else {
System.out.println("Employee with id " + id + " not found.");
}
}
}