diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/lab-java-basics.iml b/.idea/lab-java-basics.iml new file mode 100644 index 0000000..b7b3be2 --- /dev/null +++ b/.idea/lab-java-basics.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a9182a4 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..505d07d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/lab-java-basics/Challenge1.class b/out/production/lab-java-basics/Challenge1.class new file mode 100644 index 0000000..8a805a9 Binary files /dev/null and b/out/production/lab-java-basics/Challenge1.class differ diff --git a/out/production/lab-java-basics/Challenge4.class b/out/production/lab-java-basics/Challenge4.class new file mode 100644 index 0000000..1b1a559 Binary files /dev/null and b/out/production/lab-java-basics/Challenge4.class differ diff --git a/out/production/lab-java-basics/Employee.class b/out/production/lab-java-basics/Employee.class new file mode 100644 index 0000000..f160655 Binary files /dev/null and b/out/production/lab-java-basics/Employee.class differ diff --git a/out/production/lab-java-basics/Intern.class b/out/production/lab-java-basics/Intern.class new file mode 100644 index 0000000..2d2839e Binary files /dev/null and b/out/production/lab-java-basics/Intern.class differ diff --git a/out/production/lab-java-basics/challenge2.class b/out/production/lab-java-basics/challenge2.class new file mode 100644 index 0000000..c62be67 Binary files /dev/null and b/out/production/lab-java-basics/challenge2.class differ diff --git a/out/production/lab-java-basics/main.class b/out/production/lab-java-basics/main.class new file mode 100644 index 0000000..8275388 Binary files /dev/null and b/out/production/lab-java-basics/main.class differ diff --git a/out/production/lab-java-basics/main1.class b/out/production/lab-java-basics/main1.class new file mode 100644 index 0000000..75bacb1 Binary files /dev/null and b/out/production/lab-java-basics/main1.class differ diff --git a/out/production/lab-java-basics/main3.class b/out/production/lab-java-basics/main3.class new file mode 100644 index 0000000..83da4fa Binary files /dev/null and b/out/production/lab-java-basics/main3.class differ diff --git a/source/Challenge1.java b/source/Challenge1.java new file mode 100644 index 0000000..dc02ac7 --- /dev/null +++ b/source/Challenge1.java @@ -0,0 +1,37 @@ +import java.util.Arrays; +import java.util.Scanner; + +public class Challenge1 { + // 1) Difference between largest and smallest values in an int array (length >= 1) + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.print("Enter the number of elements: "); + int n = sc.nextInt(); + int[] arr = new int[n]; + System.out.println("Enter " + n + " integers in different lines:"); + for (int i = 0; i < n; i++) { + arr[i] = sc.nextInt(); + } + + // Call the getdifference method + int diff = getdifference(arr); + System.out.println("Difference between max and min: " + diff); + sc.close(); + } + public static int getdifference(int[] arr) { + if (arr == null || arr.length == 0) { + System.out.println("invalid array"); + } + int min = Integer.MAX_VALUE; + int max = Integer.MIN_VALUE; + + // Loop + conditionals + for (int i = 0; i < arr.length; i++) { + if (arr[i] < min) min = arr[i]; + if (arr[i] > max) max = arr[i]; + } + return max - min; + + } + } + diff --git a/source/Challenge4.java b/source/Challenge4.java new file mode 100644 index 0000000..3d27c07 --- /dev/null +++ b/source/Challenge4.java @@ -0,0 +1,2 @@ +public class Challenge4 { +} diff --git a/source/Employee.java b/source/Employee.java new file mode 100644 index 0000000..591e085 --- /dev/null +++ b/source/Employee.java @@ -0,0 +1,60 @@ +public class Employee { + private int id; + private String name; + private String role; + private double salary; + + // --- Constructor --- + public Employee(int id, String name, String role, double salary) { + this.id = id; + this.name = name; + this.role = role; + this.salary = salary; + } + + // --- Getters (to read values) --- + public int getId() { + return id; + } + + public String getName() { + return name; + } + + public String getRole() { + return role; + } + + public double getSalary() { + return salary; + } + + // --- Setters (to update values) --- + public void setId(int id) { + this.id = id; + } + + public void setName(String name) { + this.name = name; + } + + public void setRole(String role) { + this.role = role; + } + + public void setSalary(double salary) { + if (salary >= 0) { // basic validation + this.salary = salary; + } else { + System.out.println("Salary cannot be negative!"); + } + } + + + public void displayInfo() { + System.out.println("Employee ID: " + id); + System.out.println("Name: " + name); + System.out.println("Role: " + role); + System.out.println("Salary: " + salary); + } +} diff --git a/source/Intern.java b/source/Intern.java new file mode 100644 index 0000000..8cbce11 --- /dev/null +++ b/source/Intern.java @@ -0,0 +1,35 @@ +public class Intern extends Employee{ + public static final double MAX_SALARY = 20000.0; + + // Constructor + public Intern(int id, String name, String role, double salary) { + // Set a temporary salary (0), then use our overridden setter + // so the validation (cap) is applied. + super(id, name, role, 0); + setSalary(salary); // will validate against MAX_SALARY + } + + // Override setSalary to enforce the cap + @Override + public void setSalary(double salary) { + if (salary < 0) { + System.out.println("Salary cannot be negative!"); + return; + } + if (salary > MAX_SALARY) { + System.out.println("Intern salary cannot exceed " + MAX_SALARY+"."); + super.setSalary(MAX_SALARY); + } else { + super.setSalary(salary); + } + } + + // (Optional) A helper to show it's an intern when printing + @Override + public void displayInfo() { + System.out.println("=== Intern ==="); + super.displayInfo(); + System.out.println("Intern Max Salary: " + MAX_SALARY); + } +} + diff --git a/source/challenge2.java b/source/challenge2.java new file mode 100644 index 0000000..b8cbb44 --- /dev/null +++ b/source/challenge2.java @@ -0,0 +1,45 @@ +public class challenge2 { + public static void findSmallest(int[] arr) { + if (arr == null || arr.length < 2) { + System.out.println("Need at least 2 elements"); + return; + } + + // Start by assuming first two numbers are smallest and second smallest + int smallest = arr[0]; + int secondSmallest = arr[1]; + + // Make sure smallest < secondSmallest + if (smallest > secondSmallest) { + int temp = smallest; + smallest = secondSmallest; + secondSmallest = temp; + } + + // Check rest of the array + for (int i = 2; i < arr.length; i++) { + int current = arr[i]; + + if (current < smallest) { + // Found new smallest, shift previous smallest to secondSmallest + secondSmallest = smallest; + smallest = current; + } else if (current < secondSmallest && current != smallest) { + // Found new second smallest (different from smallest) + secondSmallest = current; + } + } + + // Print results + System.out.println("Smallest: " + smallest); + System.out.println("Second smallest: " + secondSmallest); + } + + public static void main(String[] args) { + int[] data = {7, 2, 9, 2, 15, 8, 3}; + findSmallest(data); + } +} + + + diff --git a/source/main.java b/source/main.java new file mode 100644 index 0000000..bc18d2e --- /dev/null +++ b/source/main.java @@ -0,0 +1,17 @@ +public class main { + public static void main(String[] args) { + // Create Employee objects + Employee e1 = new Employee(1, "Swikriti", "Associate", 55000); + Employee e2 = new Employee(2, "Harsha", "Professor", 48000); + + // Display information + e1.displayInfo(); + System.out.println("----"); + e2.displayInfo(); + + // Update salary + e1.setSalary(60000); + System.out.println("Updated Salary for " + e1.getName() + ": " + e1.getSalary()); + } + +} diff --git a/source/main1.java b/source/main1.java new file mode 100644 index 0000000..77ea321 --- /dev/null +++ b/source/main1.java @@ -0,0 +1,11 @@ +public class main1 { + public static void main(String[] args) { + Intern i1 = new Intern(101, "Riya", "Intern - QA", 18000); + i1.displayInfo(); + + System.out.println("---- Trying to set salary above the cap ----"); + i1.setSalary(25000); // will be capped to 20000 + i1.displayInfo(); + } +} + diff --git a/source/main3.java b/source/main3.java new file mode 100644 index 0000000..8651ce7 --- /dev/null +++ b/source/main3.java @@ -0,0 +1,25 @@ +public class main3 { + public static void main(String[] args) { + // Create an array to hold 10 employees + Employee[] employees = new Employee[10]; + + // Fill with sample employees + employees[0] = new Employee(1, "Harsha", "Professor", 55000); + employees[1] = new Employee(2, "Swikriti", "Associate", 48000); + employees[2] = new Employee(3, "Abhishek", "Manager", 72000); + employees[3] = new Employee(4, "Diana", "Tester", 40000); + employees[4] = new Employee(5, "Sudha", "Teacher", 35000); + employees[5] = new Employee(6, "Saras", "HR", 45000); + employees[6] = new Employee(7, "Suyash", "Chemist", 50000); + employees[7] = new Employee(8, "Hannah", "Intern", 20000); + employees[8] = new Employee(9, "Isha", "Tech Lead", 80000); + employees[9] = new Employee(10, "Julia", "Product Owner", 90000); + + // Print all employees + System.out.println("=== Employee List ==="); + for (Employee e : employees) { + e.displayInfo(); + System.out.println("--------------"); + } + } +}