Skip to content

Instantly share code, notes, and snippets.

View wsadrak's full-sized avatar
🏠
Working from home

Wojtek wsadrak

🏠
Working from home
  • Kraków, Poland
View GitHub Profile
@wsadrak
wsadrak / my-array-list.java
Created February 28, 2019 10:39
Description: Write a program to implement your own ArrayList class. It should contain add(), get(), remove(), size() methods. Use dynamic array logic. It should increase its size when it reaches threshold
import java.util.Arrays;
public class MyArrayList {
private Object[] arrayList = new Object[1];
private int emptyPlace = 0;
public void add(Object object) {
if (emptyPlace == arrayList.length) {
arrayList = Arrays.copyOf(arrayList, arrayList.length * 2);
}
@wsadrak
wsadrak / prime-numbers.java
Last active February 28, 2019 11:37
Description: Write a program to check the given number is a prime number or not.
public class PrimeNumbers {
public static void main(String[] args) {
System.out.println("Is 17 prime number? " + isPrimeNumber(17));
System.out.println("Is 19 prime number? " + isPrimeNumber(19));
System.out.println("Is 15 prime number? " + isPrimeNumber(15));
}
@wsadrak
wsadrak / first-1000-prime-numbers.java
Last active February 28, 2019 11:38
Description: Write a program to find the sum of the first 1000 prime numbers.
public class SumOfPrimeNumbers {
private static final int START_VALUE = 2;
private static final int HOW_MANY_PRIME_NUMBERS = 1000;
public static void main(String[] args) {
int currentValue = START_VALUE;
int primeNumbersCounter = 0;
int sumOfPrimeNumbers = 0;
@wsadrak
wsadrak / multiplication-table.java
Last active February 28, 2019 11:39
Description: Write a program to create a simple, text-based, multiplication table based on user's input.
import java.util.Scanner;
public class MultiplicationTable {
public static void main(String[] args) {
int size = readValueFromUser();
generateTable(size);
}
private static int readValueFromUser() {
System.out.println("Please type a size of multiplication table: ");
@wsadrak
wsadrak / PrimeNumbers.java
Created February 28, 2019 12:33
Description: Write program to display prime numbers from 1 to n (entered by user)
import java.util.Scanner;
public class PrimeNumbers {
private static final int START_VALUE = 2;
public static void main(String[] args) {
int n = readValueFromUser();
printPrimeNumbers(n);
}
@wsadrak
wsadrak / factorial-using-recursion.java
Last active February 28, 2019 13:52
Description: Write program to find factorial of a given number using recursion
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
int factArg = readValueFromUser();
int result = calculateFactorial(factArg);
System.out.println("Factorial " + factArg + " = " + result);
}
@wsadrak
wsadrak / leap-year.java
Last active February 28, 2019 14:20
Description: Write a program to check whether the input year is a leap year or not.
public class LeapYear {
private static final int LEAP_YEAR_CONDITION_VALUE = 4;
private static final int LEAP_YEAR_CONDITION_VALUE_2 = 100;
private static final int LEAP_YEAR_CONDITION_VALUE_3 = 400;
public static void main(String[] args) {
int year = readYearFromUser();
if (isLeapYear(year)) {
System.out.println(year + " - is leap year");
@wsadrak
wsadrak / floyds-triangle.java
Last active March 1, 2019 08:21
Description: This program will prompt user for number of rows and based on the input, it would print the Floyd’s triangle having the same number of rows.
import java.util.Scanner;
public class FloydsTriangle {
public static void main(String[] args) {
int howManyRows = readRowsFromUser();
printFloydsTriangle(howManyRows);
}
@wsadrak
wsadrak / duplicate-characters-in-string.java
Created March 1, 2019 09:01
Description: Write a program to find out duplicate or repeated characters in a string, and calculate the count of repeatation.
import java.util.Map;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
String word1 = "Philadelhpia";
String word2 = "Spring Framework";
String word3 = "InputMismatchException";
findAndPrintDuplicates(word1);
@wsadrak
wsadrak / perfect-number.java
Created March 1, 2019 13:41
Description: Write a program to find perfect number or not.
import java.util.Scanner;
public class PerfectNumber {
public static void main(String[] args) {
int value = readValueFromUser();
isPerfectValue(value);
}
private static int readValueFromUser() {
System.out.println("Type a value to check: ");