package Hypotheek; import java.util.* ; import static Hypotheek.Util.*; public class Util { static public float toFloat(int x) { return (float) x ; } static public double toDouble(int x) { return (double) x ; } static public int toInt(float x) { return (int) Math.round(x) ; } static public int toInt(double x) { return (int) Math.round(x) ; } static public float calcPercentage(float percentage, int x) { return toFloat(x) * percentage / 100f ; } static public int div(int x, int y) { return toInt(Math.round(toDouble(x)/toDouble(y))) ; } static public double round(double x, int placesAfterComma) { assert placesAfterComma>=0 : "PRE" ; double m = Math.pow(x, placesAfterComma) ; return Math.round(x * m) / m ; } static public int parseInt (String input, int minval, int maxval) throws NumberFormatException { int x ; x = Integer.parseInt(input) ; if (xmaxval) throw new NumberFormatException() ; return x ; } static public float parseFloat (String input, float minval, float maxval) throws NumberFormatException { float x ; x = Float.parseFloat(input) ; if (xmaxval) throw new NumberFormatException() ; return x ; } static public float cummulativeGrowth(int base, float growthRate, float period) { assert growthRate>=0 ; float multiplier = (float) Math.pow(growthRate, period) ; return multiplier * toFloat(base) ; } /** * Convert cent to euro, rounded to the nearest euro. */ static public int cent2euro(int cent) { return toInt(Math.round (toDouble(cent) / 100d)) ; } static public int euro2cent(int euro) { return 100 * euro ; } /** * Convert a euro-cent to its string format, and expressed in * euro. The string will be right-aligned, padded to the * "width" number of characters. * * E.g. 1234 should be converted to "12.34" */ static public String showEuro(int cent,int width) { assert width >=0 : "PRE" ; boolean negative = cent < 0 ; if (negative) cent = - cent ; String euro = "" + (cent / 100) ; String cent_ = "" + (cent % 100) ; if (cent % 100 < 10) cent_ = "0" + cent_ ; if (negative) euro = "-" + euro ; if (cent % 100 == 0) euro = euro + " " ; else euro = euro + "." + cent_ ; return rightAlign(euro,width) ; } static public String leftAlign(String s, int width) { int n = s.length() ; assert width >= n : "PRE" ; int padding = width - n ; for (int i=0; i= n : "PRE" ; int padding = width - n ; for (int i=0; i=0 : "PRE" ; assert epsilon<1 : "PRE" ; int eps = toInt(toFloat(x) * epsilon) ; int delta = Math.abs(x - y) ; return delta <= eps ; } static public boolean approxEq(float x, float y, float epsilon) { assert epsilon>=0 : "PRE" ; assert epsilon<1 : "PRE" ; float eps = x * epsilon ; return Math.abs(x - y) <= eps ; } static List groupSum(List s, int n) { List acc = new LinkedList() ; int cummulative = 0 ; int i = 0 ; for (Integer x : s) { i++ ; cummulative += x ; if (i % n == 0) { acc.add(cummulative) ; cummulative = 0 ; i = 0 ; } } if (i>0) acc.add(cummulative) ; return acc ; } static List groupAccumulate(List s, int n) { List acc = new LinkedList() ; int cummulative = 0 ; int i = 0 ; for (Integer x : s) { i++ ; cummulative += x ; if (i % n == 0) { acc.add(cummulative) ; i = 0 ; } } if (i>0) acc.add(cummulative) ; return acc ; } static public void main(String[] args) { List s = new LinkedList() ; s.add(1) ; s.add(1) ; s.add(2) ; s.add(2) ; s.add(3) ; List t = groupSum(s,2) ; List u = groupAccumulate(s,2) ; System.out.println(s) ; System.out.println(t) ; System.out.println(u) ; } }