Last active
November 13, 2018 09:35
-
-
Save ph4r05/affcf341de705b22542dc53a902e4ada to your computer and use it in GitHub Desktop.
Simple java intro test
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
import javax.crypto.IllegalBlockSizeException; | |
class Main { | |
// Normally each class should be in a separate file. | |
static class Student { | |
private String name; | |
public Student(String name){ | |
this.name = name; | |
} | |
public String getName(){ | |
return name; | |
} | |
} | |
// Simple function taking a student | |
public static void someFunction(Student a){ | |
System.out.println(" a:stud " + a.getName()); | |
} | |
// Simple function throwing an exception | |
public static void someFunctionBroken(Student a) throws IllegalBlockSizeException { | |
if (a.getName().length() == 3){ | |
throw new IllegalBlockSizeException("Broken"); | |
} | |
System.out.println(" b:stud " + a.getName()); | |
} | |
// Main method called on program start (i.e., execution entry point for Main class) | |
public static void main(String[] args) { | |
Student s1 = new Student("Adam"); | |
Student s2 = new Student("Eve"); | |
System.out.println("Hello " + s1.getName() + " and " + s2.getName()); | |
// Task 1: | |
// Add age field to the student, correct next 2 prints to print student UCO as integer. | |
System.out.println(String.format("Name1: %10s, uco: %6d", s1.getName(), 123)); | |
System.out.println(String.format("Name2: %10s, uco: %6d\n", s2.getName(), 456)); | |
someFunction(s1); | |
someFunction(s2); | |
// Simple try-catch for exceptions | |
try { | |
someFunctionBroken(s1); | |
someFunctionBroken(s2); | |
System.out.println("Never printed out..."); | |
} catch(IllegalBlockSizeException e){ | |
System.out.println("IllegalBlockSizeException: " + e); | |
} catch(Exception e){ | |
System.out.println("General Exception: " + e); | |
} | |
// Demo for formatting integer as hexa string | |
System.out.println(String.format("\nHexa string: %02x", 32)); | |
// Task 2: | |
byte[] arr = new byte[256]; | |
// Implement function which | |
// Initializes the array with 0,1,2,3... and prints hexadecimally | |
// the whole array (use for-loop). | |
// Call the function on "arr" | |
// Task 3: | |
// 3.1 Convert string "123" to integer | |
// 3.2 Convert string "a123" to integer (use try-catch to prevent program crash, print error on exception) | |
// Task 4: | |
// 4.1 Get and print file size of arbitrary file on your file system with using class File, documentation: https://docs.oracle.com/javase/7/docs/api/java/io/File.html | |
// (will need import the File class) | |
// 4.2 Do the same for non-existent file, what happens? use try-catch, print error on exception | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment