This commit is contained in:
Sobottasgithub 2025-11-12 11:45:31 +01:00
commit b8ea28c915
7 changed files with 181 additions and 0 deletions

1
.envrc Normal file
View File

@ -0,0 +1 @@
use flake .

24
.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*

12
GNUmakefile Normal file
View File

@ -0,0 +1,12 @@
JAVA_FMT = google-java-format
JAVA_FILES = $(shell find . -name '*.java')
XML_FMT = xmlindent
XML_FILES = $(shell find . -name '*.xml')
.PHONY: fmt
fmt:
@echo "Formatting all Java files..."
@for f in $(JAVA_FILES); do \
echo " $$f"; \
$(JAVA_FMT) -i $$f; \
done

15
IntToBinary.java Normal file
View File

@ -0,0 +1,15 @@
public class IntToBinary {
// !!!!! DER REST DER AUFGABE IST IN DER README.md BEANTWORTED !!!!!
public int binary(int n) {
if (n < 0) {
// Invalid input
return -1;
} else if (n <= 1) {
return n;
} else {
// Prevent adding the numbers by converting to String and convert it back to int after
return Integer.parseInt(Integer.toString(binary(n/2)) + (n%2));
}
}
}

11
Main.java Normal file
View File

@ -0,0 +1,11 @@
public class Main {
// !!!!!DER REST DER AUFGABE IST IN DER README.md BEANTWORTET!!!!!
public static void main(String[] args) {
IntToBinary intToBinary = new IntToBinary();
// Test function:
System.out.println(intToBinary.binary(3));
System.out.println(intToBinary.binary(6));
System.out.println(intToBinary.binary(9));
}
}

61
flake.lock generated Normal file
View File

@ -0,0 +1,61 @@
{
"nodes": {
"flake-parts": {
"inputs": {
"nixpkgs-lib": "nixpkgs-lib"
},
"locked": {
"lastModified": 1762810396,
"narHash": "sha256-dxFVgQPG+R72dkhXTtqUm7KpxElw3u6E+YlQ2WaDgt8=",
"owner": "hercules-ci",
"repo": "flake-parts",
"rev": "0bdadb1b265fb4143a75bd1ec7d8c915898a9923",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "flake-parts",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1762844143,
"narHash": "sha256-SlybxLZ1/e4T2lb1czEtWVzDCVSTvk9WLwGhmxFmBxI=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "9da7f1cf7f8a6e2a7cb3001b048546c92a8258b4",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-lib": {
"locked": {
"lastModified": 1761765539,
"narHash": "sha256-b0yj6kfvO8ApcSE+QmA6mUfu8IYG6/uU28OFn4PaC8M=",
"owner": "nix-community",
"repo": "nixpkgs.lib",
"rev": "719359f4562934ae99f5443f20aa06c2ffff91fc",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "nixpkgs.lib",
"type": "github"
}
},
"root": {
"inputs": {
"flake-parts": "flake-parts",
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

57
flake.nix Normal file
View File

@ -0,0 +1,57 @@
{
description = "int to binary";
inputs = {
flake-parts.url = "github:hercules-ci/flake-parts";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs = inputs@{ flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
imports = [
];
systems = [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" "x86_64-darwin" ];
perSystem = { config, self', inputs', pkgs, system, ... }: let
jdk = pkgs.jdk21.override {
};
buildInputs = [
jdk
];
devTools = [
pkgs.google-java-format
];
in {
devShells.default = pkgs.mkShell {
buildInputs = buildInputs ++ devTools;
};
packages = {
default = pkgs.stdenv.mkDerivation {
pname = "int-to-binary";
version = "1.0.0";
src = ./.;
nativeBuildInputs = buildInputs ++ [ pkgs.makeWrapper ];
buildPhase = ''
javac -Werror -g:none -deprecation -verbose Main.java
'';
installPhase = ''
mkdir -p $out/{bin,lib}
cp *.class $out/lib
makeWrapper ${pkgs.lib.getExe jdk} $out/bin/int-to-binary --add-flags "-cp $out/lib/ Main"
'';
};
};
};
flake = {
};
};
}