Skip to content

Instantly share code, notes, and snippets.

@3355844
Created January 10, 2016 14:46
Show Gist options
  • Save 3355844/ab7df5c5cbc04718dc8c to your computer and use it in GitHub Desktop.
Save 3355844/ab7df5c5cbc04718dc8c to your computer and use it in GitHub Desktop.
package HomeWorkSecond;
/**
* Created by Andrey on 06.01.2016.
*/
public class ConditionsLoopsTask1DigitsPrinter {
public static final int RADIX = 10;
public static void main(String[] args) {
int n = -147292;
outputDigits(n);
}
public static void outputDigits(int n) {
indexChecker(n);
int absN = Math.abs(n);
for (int i = getLength(absN); i > 0; i--) {
System.out.print(getNumb(absN, i) + " ");
}
}
public static Object getNumb(int n, int i) {
i--;
int numb = ((n / getPow(i)) % RADIX);
return numb;
}
public static int getPow(int i) {
int pow = (int) Math.pow(RADIX, i);
return pow;
}
public static int getLength(int n) {
int length = 0;
while (n > 0) {
length++;
n /= RADIX;
}
return length;
}
public static void indexChecker(int n) {
if (n < 0) System.out.print("- ");
return;
}
}
package HomeWorkSecond;
/**
* Created by Andrey on 07.01.2016.
*/
public class ConditionsLoopsTask2RadixPrinter {
public static final int RADIX_TEN = 10;
public static void main(String[] args) {
int n = 147292;
int radix = 10;
printInRadix(n, radix);
}
public static void printInRadix(int n, int radix) {
checker(n, radix);
int count;
String temp = "";
while (n != 0) {
count = n % radix;
temp = count + temp;
n = n / radix;
}
System.out.print(temp);
}
public static void checker(int n, int radix) {
if (n < 0) throw new IllegalArgumentException("Your digit is less then zero");
if (n == 0) System.out.println("Your digit is zero ");
if (radix < 2 || radix > RADIX_TEN) throw new IllegalArgumentException();
}
}
package HomeWorkSecond;
/**
* Created by Andrey on 08.01.2016.
*/
public class ConditionsLoopsTask3PrimesPrinter {
public static void main(String[] args) {
int from = 101;
int to = 110;
printPrimes(from, to);
}
public static void printPrimes(int from, int to) {
getChecker(from, to);
String temp = "";
int count = 1;
for (int i = getCheck(from); i <= to; i++) {
if (i % i == 0 && getDivider(count, i, to) == 0) temp = temp + i+ ", ";
}
System.out.print(temp);
}
public static double getDivider(int count, int i, int to) {
int test = 0;
while (count < to) {
count++;
if (count == i) count++;
if (i % count == 0 ) test++;
}return test;
}
public static int getCheck(int from) {
if (from < 2) from = 2;
return from;
}
public static void getChecker(int from, int to) {
if (from > to || to < 0) throw new IllegalArgumentException("");
}
}
package HomeWorkSecond;
/**
* Created by Andrey on 08.01.2016.
*/
public class ConditionsLoopsTask4DigitsSumCalculator {
public static final int RADIX_TEN = 10;
public static void main(String[] args) {
int from = 10;
int to = 13;
System.out.print(digitSum(from, to));
}
public static int digitSum(int from, int to) {
check(from, to);
int temp = 0;
for (int i = from; i <= to; i++) {
temp += getSumLength(i);
}
temp = getAbs(temp);
return temp;
}
public static int getAbs(int i) {
int abs = Math.abs(i);
return abs;
}
public static void check(int from, int to) {
if (from > to) throw new IllegalArgumentException();
return;
}
public static int getSumLength(int i) {
i = getAbs(i);
int sumLength = 0;
while (i > 0) {
int count = 0;
int getSumLength = (i % RADIX_TEN);
count += getSumLength;
i /= RADIX_TEN;
sumLength += count;
}
return sumLength;
}
}
package HomeWorkSecond;
/**
* Created by Andrey on 09.01.2016.
*/
public class ConditionsLoopsTask5RhombusPrinter {
public static final String S_POINT = " . ";
public static final String S_STAR = " * ";
public static void main(String[] args) {
int size = 7;
printRhombus(size);
}
public static void printRhombus(int size) {
checkSize(size);
int middle = size / 2 + 1;
int rightStar = middle;
int leftStar = middle;
for (int i = 1; i <= size; i++) {
if (i < middle) {
printUpRhombus(leftStar, rightStar, size);
rightStar++;
leftStar--;
}
if (i >= middle) {
printBottomRhombus(leftStar, rightStar, size);
rightStar--;
leftStar++;
}
System.out.println();
}
}
public static void printBottomRhombus(int leftStar, int rightStar, int size) {
for (int j = size; j >= 1; j--) {
if (rightStar == j || leftStar == j) {
System.out.print(S_STAR);
} else {
System.out.print(S_POINT);
}
}
}
public static void printUpRhombus(int leftStar, int rightStar, int size) {
for (int j = 1; j <= size; j++) {
if (rightStar == j || leftStar == j) {
System.out.print(S_STAR);
} else {
System.out.print(S_POINT);
}
}
}
public static void checkSize(int size) {
int n = size % 2;
if (size < 0 && n == 0) throw new IllegalArgumentException();
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment