This commit is contained in:
Rémi Heredero 2022-04-13 20:49:31 +02:00
parent 99762353e9
commit fb187effcc
20 changed files with 587 additions and 6 deletions

View File

@ -3,6 +3,9 @@
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
],
"cSpell.words": [
"recursivity"
]
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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));}
}

View File

@ -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;

View File

@ -1,8 +1,6 @@
package lab13_streams;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

View File

@ -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);
}
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}
}

Binary file not shown.

View File

@ -0,0 +1,81 @@
package labs.lab15_oop.bank;
import hevs.utils.DateUtils;
/**
* Simple bank account control class for lab 15
* @author @author <a href='mailto:pandre.mudry&#64;hevs.ch'> Pierre-André Mudry</a>
*/
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();
}
}

View File

@ -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<String, Integer> services = new LinkedHashMap<String, Integer>();
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<Integer> 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<Integer> prestations) {
return generateBill(vectorToArray(prestations));
}
String generateHTMLBill(Vector<Integer> 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 = "<html>";
result += "<hr><br>";
result += "<center><h1>Super Auto 20000 bill</h1></center>";
result += "<center><small>Version 1.0</small></center><br>";
result += "<hr><br>";
// Create an HTML table
result += "<table cellpadding=\"2\" cellspacing=\"2\" width=\"100%\">";
result += "<tr>";
result += "<td bgcolor=\"#cccccc\">Prestation</td>";
result += "<td bgcolor=\"#cccccc\">Price</td>";
result += "</tr>";
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 += "<tr>";
result += "<td>" + cKey + "</td><td>" + services.get(cKey) + "</td>";
result += "</tr>";
total_sum += services.get(cKey);
}
result += "<tr>";
result += "<td><i>Total price</i></td>";
result += "<td bgcolor=\"#dddddd\"><b>" + total_sum + "</b></td>";
result += "</tr>";
// End of the HTML table
result += "</table>";
result += "<br><hr width=\"50%\" noshade /><br>"; // Horizontal line
result += "<small>Lab 15 generator</small>";
result += "</html>";
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();
}
}
}

View File

@ -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");
}
}

View File

@ -0,0 +1,135 @@
package hevs.utils;
import java.io.*;
/**
* * The Class Input is here to enter data with the keyboard.<br>
* The types below are supported by the Input class. <br>
* <br>
* - String <br>
* - Integer (int) <br>
* - Double (double) - Boolean (boolean) <br>
* - Character (char) <br>
* <br>
* <br>
*
* @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;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB