This commit is contained in:
femsci 2024-03-28 19:14:25 +01:00
parent fa546b47a9
commit db47818079
Signed by: femsci
GPG key ID: 21AC2CC03E5BBCD6
3 changed files with 86 additions and 3 deletions

50
CrossPrinter.java Normal file
View file

@ -0,0 +1,50 @@
/*
* TASK 3
* archived at https://git.femboy.science/femsci/ppj/src/branch/task/3
* by femsci
*/
public class CrossPrinter {
public CrossPrinter(int size) {
_size = size;
_ast = "*".repeat(_size);
_whi = " ".repeat(_size);
// 3n² chars + 3n of \n
_buf = new StringBuffer((int) Math.pow(size * 3, 2) + size * 3);
fillBuffer();
}
// yes i'm a C# dev
private final int _size;
private final StringBuffer _buf;
// :3333
private final CharSequence _whi;
private final CharSequence _ast;
public StringBuffer getBuffer() {
return new StringBuffer(_buf);
}
public void printToStdout() {
System.out.println(getBuffer().toString());
}
// populate the buffer
private void fillBuffer() {
fillTopBottom();
fillCore();
fillTopBottom();
}
// to fill upper and lower segments for strbuffer
private void fillTopBottom() {
_buf.append(String.format("%s%s%s\n", _whi, _ast, _whi).repeat(_size));
}
// fill the middle segment
private void fillCore() {
_buf.repeat(String.format("%s\n", _ast.toString().repeat(3)), _size);
}
}

34
Meow.java Normal file
View file

@ -0,0 +1,34 @@
/*
* TASK 3
* archived at https://git.femboy.science/femsci/ppj/src/branch/task/3
* by femsci
*/
public class Meow {
// printing logic decoupled to CrossPrinter.java
public static void main(String[] args) {
if (args.length == 0) {
System.err.println("No argument provided...");
System.exit(2);
}
int size = 0;
try {
size = Integer.parseInt(args[0]);
if (size <= 0) {
// short circuit
throw new Exception();
}
} catch (Exception __) {
System.err.println("Invalid argument provided...");
System.exit(1);
}
var cross = new CrossPrinter(size);
cross.printToStdout();
}
}

View file

@ -1,6 +1,5 @@
# PPJ # PPJ
This repository exists to archive PJATK tasks for the PPJ curriculum. This branch contains the solution for the **3rd task**.
Each task can be found under its **corresponding branch**: `task/n`. [Go back to main](/femsci/ppj/src/branch/nya)
Example: `task/2`.