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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/lab-java-basics.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added out/production/lab-java-basics/Challenge1.class
Binary file not shown.
Binary file added out/production/lab-java-basics/Challenge4.class
Binary file not shown.
Binary file added out/production/lab-java-basics/Employee.class
Binary file not shown.
Binary file added out/production/lab-java-basics/Intern.class
Binary file not shown.
Binary file added out/production/lab-java-basics/challenge2.class
Binary file not shown.
Binary file added out/production/lab-java-basics/main.class
Binary file not shown.
Binary file added out/production/lab-java-basics/main1.class
Binary file not shown.
Binary file added out/production/lab-java-basics/main3.class
Binary file not shown.
37 changes: 37 additions & 0 deletions source/Challenge1.java
Original file line number Diff line number Diff line change
@@ -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;

}
}

2 changes: 2 additions & 0 deletions source/Challenge4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class Challenge4 {
}
60 changes: 60 additions & 0 deletions source/Employee.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
35 changes: 35 additions & 0 deletions source/Intern.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

45 changes: 45 additions & 0 deletions source/challenge2.java
Original file line number Diff line number Diff line change
@@ -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);
}
}



17 changes: 17 additions & 0 deletions source/main.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
11 changes: 11 additions & 0 deletions source/main1.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

25 changes: 25 additions & 0 deletions source/main3.java
Original file line number Diff line number Diff line change
@@ -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("--------------");
}
}
}