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
85 changes: 85 additions & 0 deletions app.java
Original file line number Diff line number Diff line change
@@ -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<Double> scores;

public Student(String name, List<Double> 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<Student> readStudentsFromCSV(String filePath) {
List<Student> 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<Double> 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<String> generateReport(List<Student> students) {
List<String> 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<String> 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<Student> students = readStudentsFromCSV(inputFile);
List<String> reportLines = generateReport(students);
saveReportToFile(reportLines, outputFile);
}
}
29 changes: 29 additions & 0 deletions code-opt.java
Original file line number Diff line number Diff line change
@@ -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<String> words = new ArrayList<>();
words.add("apple");
words.add("banana");
words.add("apple");
words.add("orange");
words.add("banana");
words.add("banana");

Map<String, Integer> 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);
}
}