Skip to content

Commit

Permalink
feat: today's practice implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex4386 committed May 20, 2024
1 parent ec99b58 commit 48d9dba
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/me/alex4386/gachon/sw14462/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.*;

public class Main {
public static String currentTarget = "day22";
public static String currentTarget = "day23";
public static boolean fallbackToLatest = true;

public static Map<String, Class<?>> getAvailableTargetClassNames() {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day23/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package me.alex4386.gachon.sw14462.day23;

import me.alex4386.gachon.sw14462.utils.Chainloader;

public class Main {
public static String chainloadTarget = "ex13_1b";

public static void main(String[] args) throws Throwable {
String packageName = Main.class.getPackage().getName();
String chainLoadTargetClass = packageName + "." + chainloadTarget + ".Main";

try {
Chainloader.chainloadTarget(chainLoadTargetClass, args);
} catch (Exception e) {
throw e;
}
}
}
97 changes: 97 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day23/ex13_1a/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package me.alex4386.gachon.sw14462.day23.ex13_1a;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the filename: ");
String filename = scanner.nextLine();

// generate 10 random integer
int[] randomIntegers = new int[10];
for (int i = 0; i < 10; i++) {
randomIntegers[i] = (int) (Math.random() * 1000);
}

// write to file
File targetFile = new File(filename);
if (targetFile.exists()) {
// unlink the file
targetFile.delete();
}

FileOutputStream stream;
try {
targetFile.createNewFile();
stream = new FileOutputStream(targetFile);
} catch (Exception e) {
System.err.println("Failed to create file: " + e.getMessage());
return;
}

PrintWriter writer = new PrintWriter(stream);
for (int i = 0; i < 10; i++) {
writer.println(randomIntegers[i]);
}

writer.close();
System.out.println("File created successfully.");

scanner.close();

// reopen the file
targetFile = new File(filename);
if (!targetFile.exists()) {
System.err.println("File does not exists, is the file has been deleted in the meantime?");
return;
}

try {
scanner = new Scanner(targetFile);
} catch (Exception e) {
System.err.println("Failed to open file: " + e.getMessage());
return;
}

int count = 0;
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE, sum = 0;
double average;

List<Integer> integers = new ArrayList<>();
while (scanner.hasNextLine()) {
String content = scanner.nextLine();
if (content.trim().equals("")) continue;

int value = 0;
try {
value = Integer.parseInt(content);
} catch (NumberFormatException e) {
System.err.println("Invalid number: " + e.getMessage());
continue;
}

integers.add(value);
count++;
sum += value;
if (value < min) min = value;
if (value > max) max = value;
}

average = (double) sum / count;
scanner.close();


System.out.println("min: "+min);
System.out.println("max: "+max);
System.out.println("sum: "+sum);
System.out.println("average: "+average);

}
}
43 changes: 43 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day23/ex13_1b/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package me.alex4386.gachon.sw14462.day23.ex13_1b;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
String targetFilename = "names.txt";
String firstNameCache = "";

File file = new File(targetFilename);
if (!file.exists()) {
System.err.println("File not found: " + targetFilename);
System.exit(1);
}

Scanner scanner;
try {
scanner = new Scanner(file);
} catch (FileNotFoundException e) {
System.err.println("File not found: " + e.getMessage());
System.exit(1);
return;
}

while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] names = line.split(" ");

for (int i = 0; i < names.length; i++) {
if (firstNameCache.equals("")) {
firstNameCache = names[i];
} else {
System.out.println(firstNameCache + " " + names[i]);
firstNameCache = "";
}
}
}

scanner.close();
}
}

0 comments on commit 48d9dba

Please sign in to comment.