0% found this document useful (0 votes)
10 views11 pages

Unit 11.2 PR

The document discusses problem-solving techniques in computing, specifically focusing on searching methods like Sequential and Binary Search. It includes practical examples for calculating wind speed averages, analyzing student test scores, and evaluating employee data in a corporate setting. Additionally, it provides Java code implementations for these tasks, demonstrating the use of arrays and data processing.

Uploaded by

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

Unit 11.2 PR

The document discusses problem-solving techniques in computing, specifically focusing on searching methods like Sequential and Binary Search. It includes practical examples for calculating wind speed averages, analyzing student test scores, and evaluating employee data in a corporate setting. Additionally, it provides Java code implementations for these tasks, demonstrating the use of arrays and data processing.

Uploaded by

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

Week 11 – Lecture 2 (Practical)

Problem Solving in Computing

Searching

1. When should you use the Binary Search method instead of the Sequential Search
method?

Explain the difference between the two methods and give examples of situations
where each method would be better.

1. Sequential Search

 checks each element one by one

 best for unsorted or small datasets.

 Example Sequential Search: Searching for a name in an unsorted list.

2. Binary Search:

 Binary Search works only on sorted datasets

 faster for large datasets.

 Example Binary Search: Finding a word in a dictionary.

2. John has a weather station at home.


He has been recording the highest wind speed for each day over two weeks. Help
him by:

 Calculating the average wind speed for the two weeks.


 Finding the days with the highest and lowest wind speeds.
 Calculating the difference between the highest wind speed and the average
wind speed for each day.
3. Mr. Smith’s class data is stored in parallel arrays.
The arrays contain the names of students and their grades in 4 tests. Create a
solution to:

 Enter a student’s name.


 Search for the name in the array using the sequential search method.
 Print the student’s name and test scores.
Sequential Search
4. Modify the solution in question 3 to use Binary Search instead of Sequential Search.
Explain how to change the solution and outline the steps.

Code

import java.util.Arrays;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

String[] studentNames = {"Ahmed", "Sara", "Fahad", "Laila"};

int[][] testScores = {

{85, 90, 88, 92},

{78, 80, 85, 83},

{91, 89, 84, 87},

{75, 70, 78, 72}

};

sortParallelArrays(studentNames, testScores);

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the student's name: ");

String searchName = scanner.nextLine();


// Binary search

int index = binarySearch(studentNames, searchName);

if (index != -1) {

System.out.println("Student: " + studentNames[index]);

System.out.print("Test Scores: ");

for (int score : testScores[index]) {

System.out.print(score + " ");

System.out.println();

} else {

System.out.println("Student not found.");

scanner.close();

// Binary search function

public static int binarySearch(String[] arr, String key) {

int low = 0, high = arr.length - 1;

while (low <= high) {

int mid = (low + high) / 2;

int comparison = arr[mid].compareToIgnoreCase(key);

if (comparison == 0) {
return mid;

} else if (comparison < 0) {

low = mid + 1;

} else {

high = mid - 1;

return -1;

public static void sortParallelArrays(String[] names, int[][] scores) {

for (int i = 0; i < names.length - 1; i++) {

for (int j = i + 1; j < names.length; j++) {

if (names[i].compareToIgnoreCase(names[j]) > 0) {

String tempName = names[i];

names[i] = names[j];

names[j] = tempName;

int[] tempScores = scores[i];

scores[i] = scores[j];

scores[j] = tempScores;

}
}

}
5. The human resources manager of XYZ Corporation would like to analyze the following
characteristics of company employees:

 the wages of women compared with those of men


 the total number of employees in each of the 12 departments
 the number of women and men in each of the 12 departments
 the average age of the women and men in each department
1. Input Data:

 Employee data, including department, gender, age, and wage.


2. Process Data:

 Calculate:
 Average wages for women and men.
 Total number of employees in each department.
 Number of women and men in each department.
 Average age of women and men in each department.
3. Output Results:

 Display the calculated results.

Code java

import java.util.*;

public class HRAnalysis {


public static void main(String[] args) {
String[][] employees = {
{"HR", "Female", "29", "5000"},
{"HR", "Male", "35", "5500"},
{"IT", "Male", "40", "7000"},
{"IT", "Female", "30", "6200"},
{"Finance", "Female", "25", "4800"},
{"Finance", "Male", "45", "7500"},
{"HR", "Female", "32", "5200"},
{"IT", "Female", "28", "5800"},
{"Finance", "Male", "38", "7000"}
};

Map<String, Department> departments = new HashMap<>();


for (String[] emp : employees) {
String department = emp[0];
String gender = emp[1];
int age = Integer.parseInt(emp[2]);
int wage = Integer.parseInt(emp[3]);

departments.putIfAbsent(department, new Department(department));


Department dept = departments.get(department);
dept.addEmployee(gender, age, wage);
}

System.out.println("Analysis of XYZ Corporation Employee Data:");


for (Department dept : departments.values()) {
System.out.println("\nDepartment: " + dept.getName());
System.out.println("Total Employees: " + dept.getTotalEmployees());
System.out.println("Number of Women: " + dept.getNumWomen());
System.out.println("Number of Men: " + dept.getNumMen());
System.out.println("Average Age (Women): " + dept.getAverageAge("Female"));
System.out.println("Average Age (Men): " + dept.getAverageAge("Male"));
System.out.println("Average Wage (Women): " +
dept.getAverageWage("Female"));
System.out.println("Average Wage (Men): " + dept.getAverageWage("Male"));
}
}
}

class Department {
private String name;
private int numMen;
private int numWomen;
private int totalAgeMen;
private int totalAgeWomen;
private int totalWageMen;
private int totalWageWomen;

public Department(String name) {


this.name = name;
this.numMen = 0;
this.numWomen = 0;
this.totalAgeMen = 0;
this.totalAgeWomen = 0;
this.totalWageMen = 0;
this.totalWageWomen = 0;
}

public String getName() {


return name;
}

public void addEmployee(String gender, int age, int wage) {


if (gender.equalsIgnoreCase("Male")) {
numMen++;
totalAgeMen += age;
totalWageMen += wage;
} else if (gender.equalsIgnoreCase("Female")) {
numWomen++;
totalAgeWomen += age;
totalWageWomen += wage;
}
}

public int getTotalEmployees() {


return numMen + numWomen;
}

public int getNumMen() {


return numMen;
}

public int getNumWomen() {


return numWomen;
}

public double getAverageAge(String gender) {


if (gender.equalsIgnoreCase("Male")) {
return numMen == 0 ? 0 : (double) totalAgeMen / numMen;
} else if (gender.equalsIgnoreCase("Female")) {
return numWomen == 0 ? 0 : (double) totalAgeWomen / numWomen;
}
return 0;
}

public double getAverageWage(String gender) {


if (gender.equalsIgnoreCase("Male")) {
return numMen == 0 ? 0 : (double) totalWageMen / numMen;
} else if (gender.equalsIgnoreCase("Female")) {
return numWomen == 0 ? 0 : (double) totalWageWomen / numWomen;
}
return 0;
}
}

Sample Output

Analysis of XYZ Corporation Employee Data:

Department: HR
Total Employees: 3
Number of Women: 2
Number of Men: 1
Average Age (Women): 30.5
Average Age (Men): 35.0
Average Wage (Women): 5100.0
Average Wage (Men): 5500.0

Department: IT
Total Employees: 3
Number of Women: 2
Number of Men: 1
Average Age (Women): 29.0
Average Age (Men): 40.0
Average Wage (Women): 6000.0
Average Wage (Men): 7000.0

Department: Finance
Total Employees: 3
Number of Women: 1
Number of Men: 2
Average Age (Women): 25.0
Average Age (Men): 41.5
Average Wage (Women): 4800.0
Average Wage (Men): 7250.0

You might also like