Created
November 4, 2014 15:21
-
-
Save Gofilord/1ad62b9aa78f7af34e0a to your computer and use it in GitHub Desktop.
This file contains 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
import java.awt.Point; | |
public class Main { | |
public static void printMatrix(int[][] mat) { | |
for (int i = 0; i < mat.length; i++) | |
{ | |
for (int j = 0; j < mat.length; j++) | |
System.out.print(mat[i][j] + " "); | |
System.out.print("\n"); | |
} | |
} | |
public static void handleDefault() { | |
Io.print("You have to choose between 1-3"); | |
} | |
/* | |
* grades | |
*/ | |
public static void task1() { | |
int[] grades = new int[6]; | |
int counter = 0, sum = 0; | |
double avg; | |
for (int i = 0; i < grades.length; i++) { | |
grades[i] = Io.promptInt("enter grade for " + (i + 1) + ": "); | |
try { | |
if (grades[i] > 100) | |
throw new Exception("illegal grade."); | |
else if (grades[i] < 0) | |
throw new Exception("grades cannot be negative."); | |
else { | |
counter++; | |
sum += grades[i]; | |
} | |
} | |
catch (Exception e) { | |
Io.print(e.getMessage()); | |
} | |
} | |
avg = (double)sum / counter; | |
Io.print("avg: " + avg); | |
Io.print("amount of legal grades: " + avg); | |
} | |
/* | |
* chess | |
*/ | |
public static void task2() { | |
int[][] matrix = new int[8][8]; | |
int row, col, prevRow = 5, prevCol = 7, counter = 0; | |
char cont; | |
// boolean ok = false; | |
// placing the horse | |
matrix[prevRow][prevCol] = 1; | |
printMatrix(matrix); | |
// getting new position from user | |
do { | |
row = Io.promptInt("enter new row:"); | |
col = Io.promptInt("enter new column:"); | |
try { | |
if (Math.abs(prevRow - row) == 2 && Math.abs(prevCol - col) == 1 || Math.abs(prevRow - row) == 1 && Math.abs(prevCol - col) == 2) { | |
matrix[row][col] = 1; | |
matrix[prevRow][prevCol] = 0; | |
prevRow = row; | |
prevCol = col; | |
} else { | |
throw new Exception("illegal horse move."); | |
} | |
} | |
catch (ArrayIndexOutOfBoundsException e1) { | |
Io.print("HEY! The new points are not in the matrix, Neo."); | |
counter++; | |
} | |
catch (Exception e2) { | |
Io.print(e2.getMessage()); | |
counter++; | |
} | |
finally { | |
Io.print("horse position: row: " + prevRow + ", col:" + prevCol); | |
printMatrix(matrix); | |
} | |
// end | |
Io.print("do you wish to continue? [y/n]"); | |
cont = Io.readChar(); | |
} while (cont != 'n'); | |
Io.print("amount of illegal movements: " + counter); | |
} | |
/* | |
* example 4 | |
*/ | |
public static void task3() { | |
boolean negative, outOfBounds; | |
int i, j, counter = 0, sum = 0; | |
int[] a = new int[5]; | |
for (i = 0; i < 5; i++) { | |
for (j = 0; j < 6; j++) { | |
negative = false; | |
outOfBounds = false; | |
try { | |
Io.print(i + ": " + a[i]); | |
if (a[i] < 0) | |
throw new Exception("-"); | |
} | |
catch (ArrayIndexOutOfBoundsException e) { | |
Io.print("error"); | |
counter++; | |
outOfBounds = true; | |
} | |
catch (Exception e2) { | |
if (e2.getMessage() == "-") | |
negative = true; | |
} | |
finally { | |
if (!negative && !outOfBounds) | |
sum += a[i]; | |
} | |
} | |
} | |
} | |
/* | |
* none-null counter | |
*/ | |
public static void task4() { | |
int counter = 0; | |
Point[] points = new Point[5]; | |
points[2] = new Point(4, 5); | |
points[0] = new Point(0, 10); | |
for (int i = 0; i < points.length; i++) { | |
try { | |
if (points[i] == null) | |
throw new Exception("no pointer: " + i); | |
else | |
Io.print("there is a pointer for " + i); | |
} | |
catch (Exception e) { | |
Io.print(e.getMessage()); | |
counter++; | |
} | |
} | |
Io.print("amount of none-null obejcts: " + (points.length - counter)); | |
} | |
public static void main(String[] args) { | |
int choice; | |
do { | |
choice = Io.promptInt("enter your choice (1-4):"); | |
} while (choice < 1 || choice > 4); | |
switch(choice) { | |
case 1: | |
task1(); | |
break; | |
case 2: | |
task2(); | |
break; | |
case 3: | |
task3(); | |
break; | |
case 4: | |
task4(); | |
break; | |
default: | |
handleDefault(); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment