-
Notifications
You must be signed in to change notification settings - Fork 10
London | 26-Java-Jun | Elhadj Abdoul Diallo | Sprint 1 | Backlog Exercises #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eediallo
wants to merge
18
commits into
CodeYourFuture:main
Choose a base branch
from
eediallo:backlog-sprint1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0d2a904
ignore .idea and target folder
eediallo c0bc3f2
Revert "ignore .idea and target folder"
eediallo e2327f6
Reapply "ignore .idea and target folder"
eediallo ad2e612
add pom.xml file
eediallo 1d7ba9b
PrimeNumbers: create a method to check whether a given int is prime w…
eediallo 98aaaf3
PrimeNumbers: add method to print prime numbers
eediallo c4b8a3c
PrimeNumbers: refactor to add a share helper for perfect factor check
eediallo bfb3254
PrimeNumbers: add comments
eediallo 4c98ff5
reverseNumber: add logic to reverse given number
eediallo b33a43c
reverseNumber: refactor to use the StringBuilder class
eediallo d526bc8
ReverseNumbers: refactor to use math based calculation.
eediallo 6df6ae6
NumberReverser: update class name and add more test cases.
eediallo 627e635
NumberReverser: add JavaDoc for reverseInteger method
eediallo be4ea18
format code: remove empty lines above class/methods.
eediallo 850bf66
remove the `this` keyword when calling the private method `isPrime` t…
eediallo aa6b63c
update comment about even numbers not being prime numbers.
eediallo f902b9f
add `public` keyword to main methods to support old java versions.
eediallo 26cacd9
remove tenary operator and return the reversed number directly.
eediallo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| .idea | ||
| target |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?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>traiing</groupId> | ||
| <artifactId>track</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
|
|
||
| <properties> | ||
| <maven.compiler.source>26</maven.compiler.source> | ||
| <maven.compiler.target>26</maven.compiler.target> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| </properties> | ||
|
|
||
| </project> |
47 changes: 47 additions & 0 deletions
47
track/src/main/java/training/sprint1/backlog/prime_numbers/PrimeNumbers.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package training.sprint1.backlog.prime_numbers; | ||
|
|
||
| /** | ||
| * Exercise | ||
| * Create a private method to print all prime numbers up to 1000. Call this | ||
| * from a main method. Remember to use sensible method and variable names, | ||
| * and add documentation where necessary! Remember to use the debugger if something goes wrong! | ||
| */ | ||
| public class PrimeNumbers { | ||
| /** | ||
| * Checks if a divisor is a perfect factor of a target number. | ||
| */ | ||
| private boolean isFactor(int target, int divisor) { | ||
| return target % divisor == 0; | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether a given int is a prime number ( a whole number greater than 1 that has exactly two factors: 1 and itself) | ||
| * | ||
| * @param num the whole number to check | ||
| * @return a boolean value, true if prime, false otherwise. | ||
| */ | ||
| private boolean isPrime(int num) { | ||
| if (num <= 1) return false; | ||
| if (num == 2) return true; | ||
| if (isFactor(num, 2)) return false; // even numbers are not prime numbers a part from 2. | ||
|
|
||
| int factor = 3; | ||
| while (factor * factor <= num) { | ||
| if (isFactor(num, factor)) return false; | ||
| factor += 2; // make sure we only check odd numbers | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private void printPrimeNumbersUpTo1000() { | ||
| for (int i = 1; i <= 1000; i++) { | ||
| if (isPrime(i)) { | ||
| System.out.println(i); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| new PrimeNumbers().printPrimeNumbersUpTo1000(); | ||
| } | ||
| } | ||
38 changes: 38 additions & 0 deletions
38
track/src/main/java/training/sprint1/backlog/reverse_numbers/NumberReverser.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package training.sprint1.backlog.reverse_numbers; | ||
|
|
||
| /** | ||
| * Create a private method that accepts a number as an argument and returns the number in reverse. | ||
| * For example, an input of 3956 should return 6593. Create code to call this from a main method. Remember | ||
| * to use sensible method and variable names, and add documentation. Remember to use the debugger if something goes wrong! | ||
| */ | ||
| public class NumberReverser { | ||
| /** | ||
| * reverses the digits of an integer using Math.log10 to determine loop count. | ||
| * | ||
| * @param num The integer to be reversed | ||
| * @return The reversed integer | ||
| * @implNote this method assumes that the input fits within | ||
|
eediallo marked this conversation as resolved.
|
||
| * the integer boundaries and does not account for overflow with Integer.MAX_VALUE. | ||
| */ | ||
| private int reverseInteger(int num) { | ||
| int reversed = 0; | ||
|
|
||
| int digitCount = (int) Math.log10(Math.abs(num)) + 1; | ||
|
|
||
| for (int i = 0; i < digitCount; i++) { | ||
| int lastDigit = num % 10; | ||
| reversed = (reversed * 10) + lastDigit; | ||
| num = num / 10; | ||
| } | ||
|
|
||
| return reversed; | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| NumberReverser reverseNumbers = new NumberReverser(); | ||
| int reversedNumber = reverseNumbers.reverseInteger(-994994); | ||
| int reversedNumber1 = reverseNumbers.reverseInteger(0); | ||
| System.out.println(reversedNumber); | ||
| System.out.println(reversedNumber1); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.