ppsolutions
This is an old revision of the document!
Table of Contents
Solutions for assignments in Parallele Programmierung.
See ETH.
Assignment 1
/** * * @author Moritz Hoffmann * */ public class Assignment1 { /** * Main method * * @param args * List of numbers. */ public static void main(String[] args) { int max = (1 << 16) - 1; int min = (-1 ^ (1 << 16)) + 1; for (String str : args) { try { int i = Integer.parseInt(str); if (i >= min && i <= max) { System.out.println(i + 1); } else { System.out.println("Number " + i + " out of range."); } } catch (NumberFormatException e) { System.out.println("Failed to convert to number: " + str); } } } }
Assignment 2
/** * * @author Moritz Hoffmann * */ public class Assignment2 { private final class MyException extends Exception { public MyException() { super(); } public MyException(String message) { super(message); } public MyException(String message, Throwable cause) { super(message, cause); } public MyException(Throwable cause) { super(cause); } } /** * Main method * * @param args * List of numbers. */ public static void main(String[] args) { new Assignment2().process(args); } /** * Process the string array. * * @param args * Same as from {@link Assignment1#main(String[])} */ private void process(String[] args) { for (String str : args) { try { int i = Integer.parseInt(str); try { if (i < 0) { throw new MyException(Integer.toString(i)); } System.out.println(i + 1); } catch (MyException e) { System.err.println("Negative value " + e.getMessage() + " -- bailing out."); break; } } catch (NumberFormatException e) { System.out.println("Failed to convert to number: " + str); } } } }
ppsolutions.1235644116.txt.gz · Last modified: 2009/02/26 10:28 by moritz