Statistical Algorithms Importer: Java Project FAQ
From Gcube Wiki
Revision as of 14:20, 6 November 2017 by Giancarlo.panichi (Talk | contribs) (→How Use Enumerates Input)
F.A.Q. of Statistical Algorithms Importer (SAI), here are common mistakes we have found in Java Project.
Main Class
The first parameter of a Java process is the main class that will be executed. Please note the full package name must be entered as default value, for example:
- org.d4science.projectx.XClass
How Use File Input
- Add input file parameter in Java project:
- Java source code in sample:
package org.myfactory.specialgroup; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; /** * * @author Giancarlo Panichi * */ public class FileConsumer { public static void main(String[] args) { try { Path fileInput=Paths.get(args[0]); //second parameter in the project Path fileOutput=Paths.get("output.txt"); Files.copy(fileInput, fileOutput, StandardCopyOption.REPLACE_EXISTING); } catch (Throwable e) { System.out.println("Error in process: " + e.getLocalizedMessage()); e.printStackTrace(); } } }
- Java code in sample:
File:JavaBlackBox FileInputPrameter.zip
How Use Enumerates Input
- Consider the Las Vegas algorithm:
- Indicates the java version
- Indicates the I/O parameters:
- Java source code in Las Vegas:
package org.myfactory.specialgroup; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.function.ToIntFunction; import java.util.stream.Stream; /** * * Las Vegas * * @author Giancarlo Panichi * */ public class Casino { private static String game; private static boolean bluff; private static Path betsFile; private static void init(String g, String f, String b) { game = g; betsFile = Paths.get(f); try { bluff = Boolean.valueOf(b); } catch (Exception e) { bluff = false; } } private static ToIntFunction<String> play = new ToIntFunction<String>() { @Override public int applyAsInt(String beat) { Integer b = 0; try { b = Integer.valueOf(beat); } catch (NumberFormatException e) { } Integer winnings = 0; if (b > 0) { winnings = playSpecificGame(b); } return winnings; } }; private static Integer playSpecificGame(Integer beat) { Integer winnings = 0; int factor = 0; switch (game) { case "slots": // 4 factor = (int) (Math.random() * 4); if (factor > 2) { winnings = beat * 4; } else { winnings = 0; } break; case "roulette": // 38 factor = (int) (Math.random() * 38); if (factor > 19) { winnings = beat * 38; } else { winnings = 0; } break; case "poker": // 52 factor = (int) (Math.random() * 52); if (factor > 26 || (bluff && factor > 13)) { winnings = beat * 52; } else { winnings = 0; } break; default: winnings = 0; break; } return winnings; } public static void main(String[] args) { try { System.out.println("Las Vegas"); System.out.println("Game: " + args[0]); System.out.println("Bets: " + args[1]); System.out.println("Bluff: " + args[2]); init(args[0], args[1],args[2]); Integer winnings = 0; // read stream try (Stream<String> stream = Files.lines(betsFile)) { winnings = stream.mapToInt(play::applyAsInt).sum(); } catch (IOException e) { System.out.println("Error reading the file: " + e.getLocalizedMessage()); e.printStackTrace(); } String result = "You Won: " + winnings; Path fileOutput = Paths.get("win.txt"); Files.write(fileOutput, result.getBytes(), StandardOpenOption.CREATE); } catch (Throwable e) { System.out.println("Error in process: " + e.getLocalizedMessage()); e.printStackTrace(); } } }
- Java code in Las Vegas: