Initial commit

This commit is contained in:
github-classroom[bot]
2022-03-03 11:27:40 +00:00
commit 8ca02de8bc
47 changed files with 15983 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
/*
* Gradle build configuration for specific lab module / exercise
*/
// the Java plugin is added by default in the main lab configuration
plugins {
id 'java'
}
// Project/Module information
description = 'Lab01 PrimeChecker'
group = 'ch.zhaw.prog2'
version = '2022.1'
// Dependency configuration
// repositories to download dependencies from
repositories {
mavenCentral()
}
// required dependencies
dependencies {
}
// Java plugin configuration
java {
// By default the Java version of the gradle process is used as source/target version.
// This can be overridden, to ensure a specific version. Enable only if required.
// sourceCompatibility = JavaVersion.VERSION_17 // ensure Java source code compatibility
// targetCompatibility = JavaVersion.VERSION_17 // version of the created byte-code
// Java compiler specific options
compileJava {
// source files should be UTF-8 encoded
options.encoding = 'UTF-8'
// for more options see https://docs.gradle.org/current/dsl/org.gradle.api.tasks.compile.CompileOptions.html
}
}
@@ -0,0 +1,31 @@
package ch.zhaw.prog2.primechecker;
public class PrimeChecker {
private static final long LOWER_LIMIT = 10000L;
private static final long UPPER_LIMIT = 1000000000L;
private static final int NUM_PRIME = 500;
public static void main(String[] args) {
long starttime = System.currentTimeMillis();
long duration;
try {
checkPrimes(NUM_PRIME);
} catch (InterruptedException e) {
System.out.println("Interrupted - " + e.getMessage());
} finally {
duration = System.currentTimeMillis() - starttime;
}
System.out.println("Finished in " + duration + " ms");
}
private static void checkPrimes(int numPrimes) throws InterruptedException {
for (int i = 0; i < numPrimes; i++) {
new PrimeTask(nextRandom()).run(); // runs sequential in current thread
}
}
private static long nextRandom() {
return LOWER_LIMIT + (long)(Math.random() * (UPPER_LIMIT - LOWER_LIMIT));
}
}
@@ -0,0 +1,42 @@
package ch.zhaw.prog2.primechecker;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class PrimeCheckerExecutor {
private static final long LOWER_LIMIT = 10000L;
private static final long UPPER_LIMIT = 1000000000L;
private static final int NUM_PRIME = 500;
public static void main(String[] args) {
long starttime = System.currentTimeMillis();
long duration;
try {
checkPrimes(NUM_PRIME);
} catch (InterruptedException e) {
System.out.println("Interrupted - " + e.getMessage());
} finally {
duration = System.currentTimeMillis() - starttime;
}
System.out.println("Finished in " + duration + " ms");
}
private static void checkPrimes(int numPrimes) throws InterruptedException {
// TODO: create ExecutorService - What ThreadPool-Type/-Size fits best?
for (int i = 0; i < numPrimes; i++) {
// TODO: execute the runnable using the executor service
}
// stop ExecutorService
// wait for termination with timeout of 1 minute
}
private static long nextRandom() {
return LOWER_LIMIT + (long)(Math.random() * (UPPER_LIMIT - LOWER_LIMIT));
}
}
@@ -0,0 +1,46 @@
package ch.zhaw.prog2.primechecker;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class PrimeCheckerFuture {
private static final long LOWER_LIMIT = 10000L;
private static final long UPPER_LIMIT = 1000000000L;
private static final int NUM_PRIME = 500;
public static void main(String[] args) {
long starttime = System.currentTimeMillis();
long duration;
try {
checkPrimes(NUM_PRIME);
} catch (InterruptedException e) {
System.out.println("Interrupted - " + e.getMessage());
} finally {
duration = System.currentTimeMillis() - starttime;
}
System.out.println("Finished in " + duration + " ms");
}
private static void checkPrimes(int numPrimes) throws InterruptedException {
// TODO: create ExecutorService
// TODO: submit tasks to ExecutorService and collect the returned Futures in a List
for (int i = 0; i < numPrimes; i++) {
}
// TODO: Loop through List, wait for completion and print results
// TODO: stop ExecutorService
// TODO: await termination with timeout 1 minute
}
private static long nextRandom() {
return LOWER_LIMIT + (long)(Math.random() * (UPPER_LIMIT - LOWER_LIMIT));
}
}
@@ -0,0 +1,32 @@
package ch.zhaw.prog2.primechecker;
public class PrimeTask implements Runnable {
private final long primeCandidate;
public PrimeTask(long primeCandidate) {
this.primeCandidate = primeCandidate;
}
public void run() {
long smallestFactor = findSmallestFactor(primeCandidate);
System.out.println("Number: " + this.primeCandidate + " -> " +
( smallestFactor == 0 ? "PRIME" : "Factor: " + smallestFactor ));
}
/**
* Brute force check if submitted candidate is a prime number.
*
* @param primeCandidate Number to check if it is a
* @return 0 if prime number, smallest factor otherwise
*/
private long findSmallestFactor(long primeCandidate) {
if (primeCandidate>3) {
for(long factor = 2; factor <=primeCandidate/2; ++factor) {
if ( primeCandidate / factor * factor == primeCandidate) {
return factor; // found a factor -> is no prime
}
}
}
return 0; // is prime number
}
}
@@ -0,0 +1,51 @@
package ch.zhaw.prog2.primechecker;
import java.util.concurrent.Callable;
public class PrimeTaskCallable implements Callable<PrimeTaskCallable.Result> {
private final long primeCandidate;
public PrimeTaskCallable(long primeCandidate) {
this.primeCandidate = primeCandidate;
}
public Result call() {
return null;
}
/**
* Brute force check if submitted candidate is a prime number.
*
* @param primeCandidate Number to check if it is a
* @return 0 if prime number, smallest factor otherwise
*/
private long findSmallestFactor(long primeCandidate) {
if (primeCandidate>3) {
for(long factor = 2; factor <= primeCandidate/2; ++factor) {
if ( primeCandidate / factor * factor == primeCandidate) {
return factor; // found a factor -> is no prime
}
}
}
return 0; // is prime number
}
/**
* Small static helper class serving as a container to return the result.
* No accessor methods. Use direct access to fields to read values.
* (Starting from Java14, we could use Java Records for this)
*/
public static class Result {
public final long candidate;
public final long factor;
public final boolean isPrime;
public Result(long candidate, long factor) {
this.candidate = candidate;
this.factor = factor;
this.isPrime = factor == 0;
}
}
}