Difference between revisions of "Statistical Algorithms Importer: Java Project FAQ"

From Gcube Wiki
Jump to: navigation, search
(How Use File Input)
Line 15: Line 15:
 
[[Image:StatisticalAlgorithmsImporter_JavaBlackBox_FileInputParameter.png|thumb|center|800px|File Input Parameter, SAI]]
 
[[Image:StatisticalAlgorithmsImporter_JavaBlackBox_FileInputParameter.png|thumb|center|800px|File Input Parameter, SAI]]
  
:Java code in sample:
+
:Java source code in sample:
  
 
<source lang='java'>
 
<source lang='java'>
Line 49: Line 49:
 
:Java code in sample:
 
:Java code in sample:
 
[[File:JavaBlackBox_FileInputPrameter.zip|JavaBlackBox_FileInputPrameter.zip]]
 
[[File:JavaBlackBox_FileInputPrameter.zip|JavaBlackBox_FileInputPrameter.zip]]
 +
 +
== How Use Enumerates Input ==
 +
:Consider the Las Vegas algorithm:
 +
[[Image:StatisticalAlgorithmsImporter_LasVegas-0.png|thumb|center|800px|Las Vegas Info, SAI]]
 +
 +
:Indicates the java version
 +
[[Image:StatisticalAlgorithmsImporter_LasVegas-1.png|thumb|center|800px|Las Vegas Interpreter, SAI]]
 +
 +
 +
:Indicates the I/O parameters:
 +
[[Image:StatisticalAlgorithmsImporter_LasVegas-2.png|thumb|center|800px|Las Vegas Interpreter, SAI]]
 +
 +
 +
:Java source code in Las Vegas:
 +
 +
<source lang='java'>
 +
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();
 +
}
 +
}
 +
 +
}
 +
</source>
 +
 +
:Java code in Las Vegas:
 +
[[File:JavaBlackBox_LasVegas.zip|JavaBlackBox_LasVegas.zip]]
 +
  
  
 
[[Category:Statistical Algorithms Importer]]
 
[[Category:Statistical Algorithms Importer]]

Revision as of 14:16, 6 November 2017

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:
File Input Parameter, SAI
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:
Las Vegas Info, SAI
Indicates the java version
Las Vegas Interpreter, SAI


Indicates the I/O parameters:
Las Vegas Interpreter, SAI


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:

File:JavaBlackBox LasVegas.zip