Commit bb731230 authored by abdullh.alsoleman's avatar abdullh.alsoleman

HW1

parent f3850ed0
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
<?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_1_8" 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" />
<mapping directory="$PROJECT_DIR$/abdullah_alsoleman1" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="8a6c3590-8b4b-442a-8567-5f95c58e5885" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/abdullah_alsoleman1" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/vcs.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/abdullah_alsoleman1/src/main/java/Main.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/abdullah_alsoleman1/src/main/java/PrimeNumberService.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/abdullah_alsoleman1/src/main/java/org/example/Main.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/abdullah_alsoleman1/src/test/java/PrimeNumberServiceTest.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/Main.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
<option value="JUnit3 Test Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectId" id="2XOV6Qa3TeG2itjg7jNdFXGLD7O" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"com.intellij.testIntegration.createTest.CreateTestDialog.defaultLibrary": "JUnit3",
"com.intellij.testIntegration.createTest.CreateTestDialog.defaultLibrarySuperClass.JUnit3": "junit.framework.TestCase",
"last_opened_file_path": "C:/Users/Abdullah/Downloads/HW1",
"project.structure.last.edited": "Modules",
"project.structure.proportion": "0.15",
"project.structure.side.proportion": "0.2"
}
}]]></component>
<component name="RecentsManager">
<key name="CreateTestDialog.Recents.Supers">
<recent name="junit.framework.TestCase" />
</key>
<key name="CreateTestDialog.RecentsKey">
<recent name="" />
</key>
<key name="CopyClassDialog.RECENTS_KEY">
<recent name="" />
</key>
</component>
<component name="RunManager" selected="Application.Main">
<configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Main" />
<module name="HW1" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="PrimeNumberServiceTest.testFindPrimeNumbersInRange" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="HW1" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="PrimeNumberServiceTest" />
<option name="METHOD_NAME" value="testFindPrimeNumbersInRange" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="PrimeNumberServiceTest.testIsPrime" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="HW1" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="PrimeNumberServiceTest" />
<option name="METHOD_NAME" value="testIsPrime" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.Main" />
<item itemvalue="JUnit.PrimeNumberServiceTest.testIsPrime" />
<item itemvalue="JUnit.PrimeNumberServiceTest.testFindPrimeNumbersInRange" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="8a6c3590-8b4b-442a-8567-5f95c58e5885" name="Changes" comment="" />
<created>1698499941047</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1698499941047</updated>
</task>
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</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>Lec1-Threads</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
import java.util.ArrayList;
import java.util.List;
//Abdullah Alsoleman
public class Main {
public static void main(String[] args) throws InterruptedException {
int startRange = 1;
int endRange = 100_000_000;
int numThreads = 4;
int chunkSize = (endRange - startRange + 1) / numThreads;
List<Thread> threads = new ArrayList<>();
List<PrimeNumberService> allChunks_PrimeNumbers = new ArrayList<PrimeNumberService>();
for (int i = 0; i < numThreads; i++) {
int start = startRange + i * chunkSize;
int end = start + chunkSize - 1;
if (i == numThreads - 1) {
end = endRange;
}
PrimeNumberService chunk_primeNumber = new PrimeNumberService(start, end);
Thread thread = new Thread(chunk_primeNumber);
threads.add(thread);
allChunks_PrimeNumbers.add(chunk_primeNumber);
thread.start();
}
long startTime = System.currentTimeMillis();
for (Thread thread : threads) {
thread.join();
}
long endTime = System.currentTimeMillis();
List<Integer> allPrimeNumbers = new ArrayList<>();
for (PrimeNumberService primeNumbers : allChunks_PrimeNumbers) {
allPrimeNumbers.addAll(primeNumbers.getPrimeNumbers());
}
System.out.println("Total prime numbers found: " + allPrimeNumbers.size());
System.out.println("Execution time: " + (endTime - startTime) + " milliseconds");
}
}
import java.util.ArrayList;
import java.util.List;
public class PrimeNumberService implements Runnable {
private int start;
private int end;
private List <Integer> primeNumbers;
public PrimeNumberService() {
}
public PrimeNumberService(int start, int end) {
this.start = start;
this.end = end;
primeNumbers = new ArrayList<Integer>();
}
public int getStart() {
return start;
}
public int getEnd() {
return end;
}
public List<Integer> getPrimeNumbers() {
return primeNumbers;
}
public void setStart(int start) {
this.start = start;
}
public void setEnd(int end) {
this.end = end;
}
public boolean isPrime (int num){
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
public List<Integer> FindPrimeNumbersInRange(){
List <Integer> range_primeNumbers= new ArrayList<>();
for (int num = start; num <= end; num++) {
if (isPrime(num)) {
range_primeNumbers.add(num);
}
}
return range_primeNumbers;
}
@Override
public void run() {
primeNumbers.addAll(FindPrimeNumbersInRange());
}
}
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 org.junit.Test;
import java.util.Arrays;
import java.util.List;
public class PrimeNumberServiceTest extends TestCase
{
@Test
public void testIsPrime()
{
PrimeNumberService test = new PrimeNumberService();
assertTrue(test.isPrime(7));
}
@Test
public void testFindPrimeNumbersInRange(){
PrimeNumberService test = new PrimeNumberService(1,20);
List<Integer> expected = Arrays.asList(2,3,5,7,11,13,17,19);
assertEquals(expected,test.FindPrimeNumbersInRange());
}
}
\ No newline at end of file
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