diff --git a/.vscode/settings.json b/.vscode/settings.json index 5055dd9..3d26bd3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,6 +3,9 @@ "java.project.outputPath": "bin", "java.project.referencedLibraries": [ "lib/**/*.jar" + ], + "cSpell.words": [ + "recursivity" ] } diff --git a/bin/lab13_streams/BMPWriter.class b/bin/lab13_streams/BMPWriter.class index f452af1..3676ec3 100644 Binary files a/bin/lab13_streams/BMPWriter.class and b/bin/lab13_streams/BMPWriter.class differ diff --git a/bin/lab13_streams/SimpleGraphics$DisplayFrame.class b/bin/lab13_streams/SimpleGraphics$DisplayFrame.class index f9d2c11..2d2f93d 100644 Binary files a/bin/lab13_streams/SimpleGraphics$DisplayFrame.class and b/bin/lab13_streams/SimpleGraphics$DisplayFrame.class differ diff --git a/bin/lab13_streams/SimpleGraphics.class b/bin/lab13_streams/SimpleGraphics.class index 42f68ea..1fe38d7 100644 Binary files a/bin/lab13_streams/SimpleGraphics.class and b/bin/lab13_streams/SimpleGraphics.class differ diff --git a/bin/lab13_streams/SimpleGraphicsBitmap.class b/bin/lab13_streams/SimpleGraphicsBitmap.class index bcdc1f8..c586c59 100644 Binary files a/bin/lab13_streams/SimpleGraphicsBitmap.class and b/bin/lab13_streams/SimpleGraphicsBitmap.class differ diff --git a/bin/lab14_recursivity/HanoiTower.class b/bin/lab14_recursivity/HanoiTower.class new file mode 100644 index 0000000..cfdab1a Binary files /dev/null and b/bin/lab14_recursivity/HanoiTower.class differ diff --git a/bin/lab14_recursivity/TestLogo.class b/bin/lab14_recursivity/TestLogo.class new file mode 100644 index 0000000..436f510 Binary files /dev/null and b/bin/lab14_recursivity/TestLogo.class differ diff --git a/bin/lab14_recursivity/Tree.class b/bin/lab14_recursivity/Tree.class new file mode 100644 index 0000000..c166936 Binary files /dev/null and b/bin/lab14_recursivity/Tree.class differ diff --git a/src/lab13_streams/BMPWriter.java b/src/lab13_streams/BMPWriter.java index 869b0cd..785695f 100644 --- a/src/lab13_streams/BMPWriter.java +++ b/src/lab13_streams/BMPWriter.java @@ -161,7 +161,5 @@ public class BMPWriter { * @param input * @return */ - static private short convertToLED(short input) { - return (short) (((input & 0xff) << 8) | ((input & 0xff00) >> 8)); - } + //static private short convertToLED(short input) {return (short) (((input & 0xff) << 8) | ((input & 0xff00) >> 8));} } diff --git a/src/lab13_streams/SimpleGraphics.java b/src/lab13_streams/SimpleGraphics.java index a05c2f2..dfbcde6 100644 --- a/src/lab13_streams/SimpleGraphics.java +++ b/src/lab13_streams/SimpleGraphics.java @@ -2,7 +2,6 @@ package lab13_streams; import java.awt.Color; import java.awt.Font; -import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Polygon; import java.awt.RenderingHints; diff --git a/src/lab13_streams/SimpleGraphicsBitmap.java b/src/lab13_streams/SimpleGraphicsBitmap.java index 8ae3079..24ca5d4 100644 --- a/src/lab13_streams/SimpleGraphicsBitmap.java +++ b/src/lab13_streams/SimpleGraphicsBitmap.java @@ -1,8 +1,6 @@ package lab13_streams; import java.awt.image.BufferedImage; -import java.io.File; -import java.io.IOException; import javax.imageio.ImageIO; diff --git a/src/lab14_recursivity/HanoiTower.java b/src/lab14_recursivity/HanoiTower.java new file mode 100644 index 0000000..32d018c --- /dev/null +++ b/src/lab14_recursivity/HanoiTower.java @@ -0,0 +1,16 @@ +package lab14_recursivity; + +public class HanoiTower { + + public static void main(String[] args) { + hanoi(3, 'a', 'b', 'c'); + } + + public static void hanoi(int n, char start, char aux, char end){ + if (n != 0){ + hanoi(n-1, start, end, aux); + System.out.println("Move disk " + n + " from " + start + "->" + end); + hanoi(n-1, aux, start, end); + } + } +} diff --git a/src/lab14_recursivity/TestLogo.java b/src/lab14_recursivity/TestLogo.java new file mode 100644 index 0000000..adee4df --- /dev/null +++ b/src/lab14_recursivity/TestLogo.java @@ -0,0 +1,31 @@ +package lab14_recursivity; + +import java.awt.Point; +import hevs.graphics.TurtleGraphics; + +public class TestLogo { + + private static TurtleGraphics turtle; + private static Point startPosition; + + public static void main(String[] args) { + turtle = new TurtleGraphics(400, 400, "Test logo"); + startPosition = turtle.getPosition(); + turtle.penDown(); + + polygoneGenerator(100, 120); + polygoneGenerator(100, 90); + polygoneGenerator(100, 72); + turtle.penUp(); + turtle.forward(50); + turtle.penDown(); + startPosition = turtle.getPosition(); + polygoneGenerator(5, 5); + } + + static void polygoneGenerator(int forward, int angle){ + turtle.forward(forward); + turtle.turn(angle); + if (!turtle.getPosition().equals(startPosition)) polygoneGenerator(forward, angle); + } +} diff --git a/src/lab14_recursivity/Tree.java b/src/lab14_recursivity/Tree.java new file mode 100644 index 0000000..c64e6ae --- /dev/null +++ b/src/lab14_recursivity/Tree.java @@ -0,0 +1,46 @@ +package lab14_recursivity; + +import java.awt.Point; + +import hevs.graphics.TurtleGraphics; + +public class Tree { + + private static TurtleGraphics turtle; + private static final int height = 500; + private static final int weight = 500; + public static void main(String[] args) { + turtle = new TurtleGraphics(weight, height, "Tree"); + turtle.forward(-(height/2)+20); + turtle.penDown(); + + drawTree(25, 100); + } + + private static void drawBranch(double length){ + turtle.forward(length); + } + + private static void drawTree(int n, double length){ + if(n > 1){ + drawBranch(length); + + Point position = turtle.getPosition(); + double angle = turtle.getTurtleAngle(); + + // Left subtree + turtle.turn(-20); + drawTree(n-1, (length/4)*3); + + turtle.jump(position.x, position.y); + turtle.setAngle(angle); + + // Right subtree + turtle.turn(20); + drawTree(n-1, (length/4)*3); + } else{ + drawBranch(length); + } + } + +} diff --git a/src/lab15_oop/15-oop-online-reportFR.pdf b/src/lab15_oop/15-oop-online-reportFR.pdf new file mode 100644 index 0000000..f87f962 Binary files /dev/null and b/src/lab15_oop/15-oop-online-reportFR.pdf differ diff --git a/src/lab15_oop/BankController.java b/src/lab15_oop/BankController.java new file mode 100644 index 0000000..cffb0ef --- /dev/null +++ b/src/lab15_oop/BankController.java @@ -0,0 +1,81 @@ +package labs.lab15_oop.bank; + +import hevs.utils.DateUtils; + +/** + * Simple bank account control class for lab 15 + * @author @author Pierre-André Mudry + */ +public class BankController { + + public BankController() { + /** + * Verify that checks are done correctly + * in constructor + */ + Checking c1; + + System.out.print("3 Should trigger error : minbalance positive\n\t"); + c1 = new Checking("Toto", 1000, 1000); + + System.out.print("4 Should trigger error : Amount < min and min < 0\n\t"); + c1 = new Checking("Toto", -2000, -1000); + + /** + * Functional tests that are asserted + */ + // Set a positive limit (impossible) + System.out.print("6 Should trigger error : minbalance positive\n\t"); + c1 = new Checking("Test", 1000, -1000); + c1.setMinBalance(10000.0); + + /** + * Savings account checks + */ + Savings s1; + + System.out.print("9 Should trigger error : abnormal init of savings\n\t"); + s1 = new Savings("Toto", 1000, -3.0); + + // Deposit of negative value + System.out.print("11 Should trigger error : negative deposit \n\t"); + s1 = new Savings("Test", 1000, 1.0); + s1.deposit(-100.0); + assert s1.getBalance() == 1000.0; + + // Withdrawing more than possible + System.out.print("12 Should trigger error : taking more than available\n\t"); + s1 = new Savings("Test", 1000, 1.0); + s1.withdraw(3000); + assert s1.balance == 1000 : s1.balance; + + // Interest computation check and normal operations checks + System.out.println("13 Should not trigger error : interest check and normal operations"); + s1 = new Savings("Test", 1000, 0.032); + s1.withdraw(20.0); + assert s1.balance == (1000 - 20.0); + s1.withdraw(100.0); + assert s1.balance == 1000 - 20.0 - 100.0; + s1.deposit(120.0); + assert s1.balance == 1000.0; + double interest = s1.calcInterest(DateUtils.createDate("1/1/2011"), DateUtils.createDate("11/1/2011")); + + // Manual check of interest + assert interest == ((1000.0 * 10 * 3.2 / 100) / 360.0); + } + + /** + * @param args + */ + public static void main(String[] args) throws RuntimeException{ + boolean assertionsAreEnabled = false; + assert (assertionsAreEnabled = true); + + if (!assertionsAreEnabled) { + throw new RuntimeException( "Assertions must be enabled ! Add -ea to the VM arguments " + + "(Run Configuration menu in Eclipse)" ); + } + + new BankController(); + } +} diff --git a/src/lab15_oop/GarageManager.java b/src/lab15_oop/GarageManager.java new file mode 100644 index 0000000..8be113c --- /dev/null +++ b/src/lab15_oop/GarageManager.java @@ -0,0 +1,191 @@ +package labs.lab15_oop.BillGUI; + +import java.io.FileOutputStream; +import java.io.PrintWriter; +import java.util.LinkedHashMap; +import java.util.Vector; + + +/** + * A sample garage application + * @author Pierre-André Mudry, HES-SO Valais 2010-2016 + */ +public class GarageManager { + private LinkedHashMap services = new LinkedHashMap(); + + + GarageManager() { + // The various services provided in this garage + services.put("Oil level control", 20); + services.put("Tire replacement ", 50); + services.put("Windshield exchange", 60); + services.put("Oil filter change", 210); + services.put("Battery replacement", 320); + services.put("Pollution control", 200); + services.put("Brake revision", 400); + } + + + String[] getServices() { + return services.keySet().toArray(new String[services.size()]); + } + + + int[] vectorToArray(Vector prestations) { + Integer[] v = prestations.toArray(new Integer[prestations.size()]); + int[] array = new int[v.length]; + + + for (int i = 0; i < v.length; i++) + array[i] = v[i].intValue(); + + + return array; + } + + + String generateBill(Vector prestations) { + return generateBill(vectorToArray(prestations)); + } + + + String generateHTMLBill(Vector prestations) { + return generateHTMLBill(vectorToArray(prestations)); + } + + + /** + * Generates an HTML formatted string with the bill + * + * @param prestations + * An array containing all the prestations + * @return The HTML formatted string + */ + String generateHTMLBill(int[] prestations) { + int total_sum = 0; + Object[] keys = services.keySet().toArray(); + + + String result = ""; + result += "

"; + result += "

Super Auto 20000 bill

"; + result += "
Version 1.0

"; + result += "

"; + + + // Create an HTML table + result += ""; + result += ""; + result += ""; + result += ""; + result += ""; + + + for (int i = 0; i < prestations.length; i++) { + if (prestations[i] > services.size()) { + System.out.println("Error, non existing prestation !"); + System.exit(-1); + } + + + String cKey = (String) keys[prestations[i]]; + + + // HTML row + result += ""; + result += ""; + result += ""; + total_sum += services.get(cKey); + } + + + result += ""; + result += ""; + result += ""; + result += ""; + + + // End of the HTML table + result += "
PrestationPrice
" + cKey + "" + services.get(cKey) + "
Total price" + total_sum + "
"; + + + result += "


"; // Horizontal line + + + result += "Lab 15 generator"; + result += ""; + return result; + } + + + /** + * Generates a text bill + * + * @param prestations + * An array containing all the prestations + * @return The generated text bill + */ + String generateBill(int[] prestations) { + int total_sum = 0; + Object[] keys = services.keySet().toArray(); + + + String result = ""; + result += "*************************\n"; + result += "* Super Auto 20000 bill ****\n"; + result += "*******************************\n\n"; + + + for (int i = 0; i < prestations.length; i++) { + if (prestations[i] > services.size()) { + System.out.println("Error, non existing prestation !"); + System.exit(-1); + } + + + String cKey = (String) keys[prestations[i]]; + result += "- " + cKey + " \t" + services.get(cKey) + "\n"; + total_sum += services.get(cKey); + } + + + result += "\n----------------------------------\n"; + result += " Bill total \t\t" + total_sum + "\n"; + result += "----------------------------------\n"; + result += "\nPayment in 30 days. Thank you !"; + return result; + } + + + public static void main(String[] args) { + GarageManager gm = new GarageManager(); + + + // Prestation 0 is "Oil level control" + // Prestation 1 is "Tire replacement " + // Prestation 2 is "Windshield exchange" + // Prestation 3 is "Oil filter change" + // Prestation 4 is "Battery replacement" + // Prestation 5 is "Pollution control" + // Prestation 6 is "Brake revision" + + + int[] client1 = { 1, 1, 1, 1, 4, 6, 0 }; + String bill1 = gm.generateBill(client1); + System.out.println(bill1); + + + PrintWriter outputStreamName; + + + try { + outputStreamName = new PrintWriter(new FileOutputStream("bill.txt")); + outputStreamName.print(bill1); + outputStreamName.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + +} \ No newline at end of file diff --git a/src/lab15_oop/hevs/utils/DateUtils.java b/src/lab15_oop/hevs/utils/DateUtils.java new file mode 100644 index 0000000..5a5a8d7 --- /dev/null +++ b/src/lab15_oop/hevs/utils/DateUtils.java @@ -0,0 +1,83 @@ +package hevs.utils; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * @author Pierre-André Mudry, HES-SO Valais 2010 + * @version 1.0 + */ +public class DateUtils { + + private static final long msPerHour = 60 * 60 * 1000; + private static final long msPerDay = 24 * msPerHour; + + /** + * Creates a date from a text representation of this date + * @param s The text representation, formatted as "dd/MM/yyyy" + * @return The corresponding date + */ + static public Date createDate(String s){ + SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); + + Date theDate = null; + + try { + theDate = sdf.parse(s); + } + catch(ParseException ex) { + System.err.println("Invalid date format specified !"); + ex.printStackTrace(); + } + + return theDate; + } + + static private long nMsec(Date a, Date b){ + if(b.after(a)){ + return b.getTime() - a.getTime(); + } + else + { + return a.getTime() - b.getTime(); + } + } + + /** + * Computes the number of hours between two dates + * @param a The first date + * @param b The second date + * @return The number of hours between the two dates + */ + static public int nHours(Date a, Date b){ + int n= Math.round(nMsec(a, b)/ msPerHour); + return n; + } + + /** + * Computes the number of day between two dates + * @param a The first date + * @param b The second date + * @return The number of days between the two dates + */ + static public int nDays(Date a, Date b){ + int nDays= Math.round(nMsec(a, b)/ msPerDay); + return nDays; + } + + // Some samples + public static void main(String args[]){ + Date first = DateUtils.createDate("1/1/2000"); + Date second = DateUtils.createDate("1/1/2001"); + + // It also takes into account leap years (2000) + System.out.println("There were " + DateUtils.nDays(first, second) + " days in 2000"); + + // How old are you in days ? + Date birthdate = DateUtils.createDate("12/10/1977"); + Date now = new Date(); + System.out.println("You are " + DateUtils.nDays(now, birthdate) + " days old"); + } + +} diff --git a/src/lab15_oop/hevs/utils/Input.java b/src/lab15_oop/hevs/utils/Input.java new file mode 100644 index 0000000..801afcc --- /dev/null +++ b/src/lab15_oop/hevs/utils/Input.java @@ -0,0 +1,135 @@ +package hevs.utils; +import java.io.*; + +/** + * * The Class Input is here to enter data with the keyboard.
+ * The types below are supported by the Input class.
+ *
+ * - String
+ * - Integer (int)
+ * - Double (double) - Boolean (boolean)
+ * - Character (char)
+ *
+ *
+ * + * @author Patrice Rudaz (patrice.rudaz@hevs.ch) + * @author Cathy Berthouzoz (cathy.berthouzoz@hevs.ch) + * @modified Pierre-André Mudry + * @version 3.0 - 6-10-2008 + * @see #readString() + * @see #readLong() + * @see #readLong() + * @see #readDouble() + * @see #readBoolean() + * @see #readChar() + */ +public class Input { + /** + * * Reads a valid char value from the console. + * + * @return The typed char + * @see java.lang.Character + */ + public static char readChar() { + boolean ok = false; + int res = -1; + while (!ok) { + try { + BufferedReader stdin = new BufferedReader( + new InputStreamReader(System.in)); + res = stdin.read(); + ok = Character.isDefined(res); + } catch (Exception ex) { + System.out.println("This is not a valid character. Try again"); + } + } + return (char) res; + } + + /** + * * Reads a String from the console. + * + * @return The typed string + * @see java.lang.String + */ + public static String readString() { + BufferedReader stdin = new BufferedReader(new InputStreamReader( + System.in)); + try { + return stdin.readLine(); + } catch (Exception ex) { + return "There is a problem. Try again."; + } + } + + /** + * * Reads a valid integer value from the console. + * + * @return The typed value + * @see java.lang.Integer + */ + public static int readInt() { + boolean ok = false; + int res = -1; + while (!ok) { + try { + BufferedReader stdin = new BufferedReader( + new InputStreamReader(System.in)); + String s = stdin.readLine(); + if (s.startsWith("0x") || s.startsWith("0X")) { + res = Integer.parseInt(s.substring(2), 16); + } else { + res = Integer.parseInt(s, 10); + } + ok = true; + } catch (Exception ex) { + System.out.println("This is not a valid number. Try again"); + } + } + return res; + } + + /** + * * Reads a valid double value from the console. + * + * @return The typed double value + * @see java.lang.Double + */ + public static double readDouble() { + boolean ok = false; + double res = -1; + while (!ok) { + try { + BufferedReader stdin = new BufferedReader( + new InputStreamReader(System.in)); + res = Double.parseDouble(stdin.readLine()); + ok = true; + } catch (Exception ex) { + System.out.println("This is not a valid number. Try again"); + } + } + return res; + } + + /** + * * Reads a valid boolean value from the console. + * + * @return the value true if the typed value is true, false otherwise. + * @see java.lang.Boolean + */ + public static boolean readBoolean() { + boolean ok = false; + boolean res = false; + while (!ok) { + try { + BufferedReader stdin = new BufferedReader( + new InputStreamReader(System.in)); + res = Boolean.parseBoolean(stdin.readLine()); + ok = true; + } catch (Exception ex) { + System.out.println("This is not a valid boolean. Try again"); + } + } + return res; + } +} \ No newline at end of file diff --git a/src/lab15_oop/logo_garage.png b/src/lab15_oop/logo_garage.png new file mode 100644 index 0000000..2f179f8 Binary files /dev/null and b/src/lab15_oop/logo_garage.png differ