From 86e5bec071e6db7bc20075330321cad961cb266f Mon Sep 17 00:00:00 2001 From: ANJANA-A-R-K <149779123+ANJANA-A-R-K@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:40:42 +0530 Subject: [PATCH 1/2] Create app.java --- app.java | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 app.java diff --git a/app.java b/app.java new file mode 100644 index 0000000..1116104 --- /dev/null +++ b/app.java @@ -0,0 +1,85 @@ +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +class Student { + private String name; + private List scores; + + public Student(String name, List scores) { + this.name = name; + this.scores = scores; + } + + public String getName() { + return name; + } + + public double averageScore() { + if (scores.isEmpty()) { + return 0; + } + double sum = 0; + for (double score : scores) { + sum += score; + } + return sum / scores.size(); + } +} + +public class StudentReport { + + public static List readStudentsFromCSV(String filePath) { + List students = new ArrayList<>(); + try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { + String line; + br.readLine(); + while ((line = br.readLine()) != null) { + String[] data = line.split(","); + String name = data[0]; + List scores = new ArrayList<>(); + for (int i = 1; i < data.length; i++) { + scores.add(Double.parseDouble(data[i])); + } + students.add(new Student(name, scores)); + } + } catch (IOException e) { + System.err.println("Error reading file: " + e.getMessage()); + } + return students; + } + + public static List generateReport(List students) { + List reportLines = new ArrayList<>(); + reportLines.add("Student Name, Average Score"); + for (Student student : students) { + reportLines.add(student.getName() + ", " + String.format("%.2f", student.averageScore())); + } + return reportLines; + } + + public static void saveReportToFile(List reportLines, String outputFile) { + try (BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile))) { + for (String line : reportLines) { + bw.write(line); + bw.newLine(); + } + System.out.println("Report saved to " + outputFile); + } catch (IOException e) { + System.err.println("Error writing to file: " + e.getMessage()); + } + } + + public static void main(String[] args) { + String inputFile = "students_scores.csv"; + String outputFile = "students_report.csv"; + + List students = readStudentsFromCSV(inputFile); + List reportLines = generateReport(students); + saveReportToFile(reportLines, outputFile); + } +} From c3f70567015e17298b5a7ba20794a34b1d112ce4 Mon Sep 17 00:00:00 2001 From: ANJANA-A-R-K <149779123+ANJANA-A-R-K@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:41:18 +0530 Subject: [PATCH 2/2] Create code-opt.java --- code-opt.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 code-opt.java diff --git a/code-opt.java b/code-opt.java new file mode 100644 index 0000000..f15c8a7 --- /dev/null +++ b/code-opt.java @@ -0,0 +1,29 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class WordCounter { + public static void main(String[] args) { + List words = new ArrayList<>(); + words.add("apple"); + words.add("banana"); + words.add("apple"); + words.add("orange"); + words.add("banana"); + words.add("banana"); + + Map wordCount = new HashMap<>(); + for (String word : words) { + int count = 0; + for (String w : words) { + if (w.equals(word)) { + count++; + } + } + wordCount.put(word, count); + } + + System.out.println(wordCount); + } +}