Skip to content

Instantly share code, notes, and snippets.

@DenisZm
Last active October 31, 2016 11:44
Show Gist options
  • Save DenisZm/3baf0754d5c0dc8069fe9b9eb3a3a95a to your computer and use it in GitHub Desktop.
Save DenisZm/3baf0754d5c0dc8069fe9b9eb3a3a95a to your computer and use it in GitHub Desktop.
homework3
package homework3;
/**
* Create array with random fill.
* Find minimal in array.
*
* Created by Denis on 25.10.2016.
*/
public class Array1 {
public static void main(String[] args) {
int n = Helper.inputInt("Input array size:");
int[] arr = Helper.createRandomArray(n);
System.out.println("Array: ");
Helper.printArray(arr);
System.out.println();
System.out.println("Minimal:");
System.out.printf("%4d", Helper.findMinInArray(arr));
}
}
package homework3;
/**
* Calculate sums of rows in 2D array.
*
* Created by Denis on 26.10.2016.
*/
public class Array2 {
static final int START_RAND = -10;
static final int END_RAND = 10;
public static void main(String[] args) {
int n = Helper.inputInt("Array width :");
int m = Helper.inputInt("Array height:");
int[][] arr = new int[m][n];
for (int i = 0; i < arr.length ; i++) {
arr[i] = Helper.createRandomArray(arr[i].length, START_RAND, END_RAND);
}
System.out.println("Array:");
for (int i = 0; i < arr.length; i++) {
Helper.printArray(arr[i]);
System.out.println();
}
System.out.println("Row sums:");
int[] sum = Helper.sumArrayRows(arr);
Helper.printArray(sum);
}
}
package homework3;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* Helper Class for common functions.
*
* Created by Denis on 25.10.2016.
**/
public class Helper {
public static int inputInt(String prompt) {
while (true) {
System.out.print(prompt + " ");
try {
Scanner in = new Scanner(System.in);
return in.nextInt();
} catch (InputMismatchException ex) {
System.out.println("Please input integer");
}
}
}
public static void printArray(int[] arr) {
for (int x: arr
) {
System.out.printf("%4d", x);
}
}
public static int[] createRandomArray(int n) {
Random rand = new Random(System.currentTimeMillis());
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = rand.nextInt(n);
}
return arr;
}
public static int[] createRandomArray(int size, int start, int end) {
int range = end - start;
int shift = start;
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = (int) (Math.random() * range) + shift;
}
return arr;
}
public static int findMinInArray(int[] arr) {
int min = arr[0];
for (int i = 0; i < arr.length ; i++) {
if (min > arr[i]) {
min = arr[i];
}
}
return min;
}
public static int[] sumArrayRows(int[][] arr) {
int[] result = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length ; j++) {
result[i] += arr[i][j];
}
}
return result;
}
}
package homework3;
/**
* Print Multiplication Table AxB size.
*
* Created by Denis on 25.10.2016.
*/
public class MultiplicationTableFunction {
public static void main(String[] args) {
int x = Helper.inputInt("Input A:");
int y = Helper.inputInt("Input B:");
System.out.println();
printMultiplicationTable(x, y);
}
static void printMultiplicationTable(int x, int y) {
for (int i = 1; i <= y; i++) {
for (int j = 1; j <= x; j++) {
System.out.printf("%-4d", (i * j));
}
System.out.print("\n");
}
}
}
package homework3;
/**
* Calculate Right Triangle Area.
*
* Created by Denis on 25.10.2016.
*/
public class RightTriangleArea {
public static void main(String[] args) {
int a = Helper.inputInt("Input A side:");
int b = Helper.inputInt("Input B side:");
System.out.println();
System.out.println("Area of triangle is: " + area(a, b));
}
static float area(int a, int b) {
float s = (float) a * b / 2;
return s;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment