Commit e61fe3b3 authored by lubna.alhalabi's avatar lubna.alhalabi

First Homework

parents
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Lubnaalhalabi</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
import java.util.UUID;
import java.util.ArrayList;
import java.util.List;
public class PrimeNumberFinder {
protected int start;
protected int end;
public List<Integer> primeNumbers;
public PrimeNumberFinder(int start, int end) {
primeNumbers = new ArrayList<>();
this.start = start;
this.end = end;
}
public void findPrimes() {
for (int num = start; num <= end; num++) {
if (isPrime(num)) {
primeNumbers.add(num);
}
}
return;
}
private boolean isPrime(int num) {
if (num <= 1) {
return false;
}
if (num <= 3) {
return true;
}
if (num % 2 == 0 || num % 3 == 0) {
return false;
}
for (int i = 5; i * i <= num; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) {
return false;
}
}
return true;
}
}
import java.util.List;
public class PrimeNumberFinderThread extends Thread{
public PrimeNumberFinder primes;
PrimeNumberFinderThread(PrimeNumberFinder _primes){
primes = _primes;
}
@Override
public void run(){
primes.findPrimes();
}
}
package org.example;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
\ No newline at end of file
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.List;
public class PrimeNumberFinderTest extends TestCase {
public void testPrimeNumberFinderUsingTHread(){
long startTime = System.currentTimeMillis();
PrimeNumberFinderThread[] primeNumberFinderThreads = new PrimeNumberFinderThread[4];
int[] values = {1, 25000000, 50000000, 75000000, 100000000};
List<List<Integer>> results = new ArrayList<>();
for(int i=0;i<4;i++) {
PrimeNumberFinder finder = new PrimeNumberFinder(values[i], values[i+1]);
primeNumberFinderThreads[i] = new PrimeNumberFinderThread(finder);
primeNumberFinderThreads[i].start();
}
for(int i=0;i<4;i++) {
try {
primeNumberFinderThreads[i].join();
results.add(primeNumberFinderThreads[i].primes.primeNumbers);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
long endTime = System.currentTimeMillis();
System.out.println("Time Taken: " + (endTime - startTime));
System.out.println(results);
}
public void testPrimeNumberFinderUsingTHread2(){
long startTime = System.currentTimeMillis();
PrimeNumberFinderThread[] primeNumberFinderThreads = new PrimeNumberFinderThread[8];
int[] values = {1,12500000 ,25000000,37500000 , 50000000,62500000 ,75000000,87500000, 100000000};
List<List<Integer>> results = new ArrayList<>();
for(int i=0;i<8;i++) {
PrimeNumberFinder finder = new PrimeNumberFinder(values[i], values[i+1]);
primeNumberFinderThreads[i] = new PrimeNumberFinderThread(finder);
primeNumberFinderThreads[i].start();
}
for(int i=0;i<4;i++) {
try {
primeNumberFinderThreads[i].join();
results.add(primeNumberFinderThreads[i].primes.primeNumbers);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
long endTime = System.currentTimeMillis();
System.out.println("Time Taken: " + (endTime - startTime));
System.out.println(results);
}
public void testPrimeNumberFinderUsingTHread3(){
long startTime = System.currentTimeMillis();
PrimeNumberFinderThread[] primeNumberFinderThreads = new PrimeNumberFinderThread[4];
int[] values = {1,12500000 ,25000000,37500000 , 50000000};
List<List<Integer>> results = new ArrayList<>();
for(int i=0;i<4;i++) {
PrimeNumberFinder finder = new PrimeNumberFinder(values[i], values[i+1]);
primeNumberFinderThreads[i] = new PrimeNumberFinderThread(finder);
primeNumberFinderThreads[i].start();
}
for(int i=0;i<4;i++) {
try {
primeNumberFinderThreads[i].join();
results.add(primeNumberFinderThreads[i].primes.primeNumbers);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
long endTime = System.currentTimeMillis();
System.out.println("Time Taken: " + (endTime - startTime));
System.out.println(results);
}
public void testPrimeNumberFinderUsingTHread4(){
long startTime = System.currentTimeMillis();
PrimeNumberFinderThread[] primeNumberFinderThreads = new PrimeNumberFinderThread[12];
int[] values = {1,6250000,12500000 ,18750000,25000000,31250000,37500000, 50000000,56250000,62500000 ,75000000,87500000, 100000000};
List<List<Integer>> results = new ArrayList<>();
for(int i=0;i<12;i++) {
PrimeNumberFinder finder = new PrimeNumberFinder(values[i], values[i+1]);
primeNumberFinderThreads[i] = new PrimeNumberFinderThread(finder);
primeNumberFinderThreads[i].start();
}
for(int i=0;i<4;i++) {
try {
primeNumberFinderThreads[i].join();
results.add(primeNumberFinderThreads[i].primes.primeNumbers);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
long endTime = System.currentTimeMillis();
System.out.println("Time Taken: " + (endTime - startTime));
System.out.println(results);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment