mirror of
https://github.com/MartinThoma/LaTeX-examples.git
synced 2025-04-26 06:48:04 +02:00
Added source files (Programmieren Tutorium)
This commit is contained in:
parent
1be8f17cc0
commit
ad81fa802b
244 changed files with 8252 additions and 0 deletions
|
@ -0,0 +1,98 @@
|
|||
/**
|
||||
* A bike model in Java.
|
||||
*
|
||||
* @author Markus Iser, Martin Thoma
|
||||
* @version 1.0
|
||||
*/
|
||||
|
||||
public class Bike {
|
||||
private final Gears gears;
|
||||
private final Wheels wheels;
|
||||
|
||||
enum Material {
|
||||
ALU, STEEL, TITAN
|
||||
}
|
||||
|
||||
private final Material material;
|
||||
|
||||
private final String modelId;
|
||||
|
||||
private final boolean hasBell;
|
||||
private final boolean hasLights;
|
||||
|
||||
/** Price of the bike in cents. */
|
||||
private int price;
|
||||
|
||||
Bike(String modelId) {
|
||||
this.gears = null;
|
||||
this.wheels = null;
|
||||
this.material = Material.ALU;
|
||||
this.modelId = modelId;
|
||||
this.hasBell = true;
|
||||
this.hasLights = true;
|
||||
}
|
||||
|
||||
Bike(Gears gears, Wheels wheels, Material material,
|
||||
String modelId, boolean bell, boolean lights) {
|
||||
this.gears = gears;
|
||||
this.wheels = wheels;
|
||||
this.material = material;
|
||||
switch (material) {
|
||||
case ALU:
|
||||
price = 20000;
|
||||
break;
|
||||
case STEEL:
|
||||
price = 30000;
|
||||
break;
|
||||
case TITAN:
|
||||
price = 40000;
|
||||
break;
|
||||
}
|
||||
this.modelId = modelId;
|
||||
this.hasBell = bell;
|
||||
this.hasLights = lights;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the bike is legal for usage on streets.
|
||||
*
|
||||
* @return {@code true} if the bike has a bell and has lights
|
||||
*/
|
||||
public boolean isStreetLegal() {
|
||||
return hasBell && hasLights;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the price of the bike.
|
||||
*
|
||||
* @return the sum of the bike's base-price and the price of the wheels and
|
||||
* gears
|
||||
*/
|
||||
public int getPrice() {
|
||||
return price + gears.getPrice() + wheels.getPrice();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the material of this bike
|
||||
*/
|
||||
public Material getMaterial() {
|
||||
return this.material;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the model id of this bike
|
||||
*/
|
||||
public String getModelId() {
|
||||
return this.modelId;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Bike[" + modelId + "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
public class BikeStorage {
|
||||
private Node head;
|
||||
|
||||
/**
|
||||
* Add a bike to the BikeStorage.
|
||||
*
|
||||
* @param bike the bike you want to add
|
||||
*/
|
||||
public void add(Bike bike) {
|
||||
Node newNode = new Node(bike);
|
||||
newNode.setNext(head);
|
||||
head = newNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if bike is in the BikeStorage
|
||||
*
|
||||
* @param bike the bike you want to search
|
||||
* @return the bike, if you found it, otherwise {@code null}
|
||||
*/
|
||||
public boolean contains(Bike bike) {
|
||||
// Normalerweise würde man hier nach einer Seriennummer
|
||||
// oder ähnlichem - also "identifizierenden" Attributen
|
||||
// von Bike - suchen und dann das gesamte Bike zurückgeben
|
||||
Node currentNode = head;
|
||||
|
||||
// usually you you should implement .equals(),
|
||||
// and not use != or == for objects
|
||||
while (currentNode != null && currentNode.getElement() != bike) {
|
||||
currentNode = currentNode.getNext();
|
||||
}
|
||||
|
||||
return currentNode != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a bike.
|
||||
*
|
||||
* @param bike the bike you want to get
|
||||
* @return
|
||||
*/
|
||||
public Bike get(Bike bike) {
|
||||
if (contains(bike)) {
|
||||
Node currentNode = head;
|
||||
while (currentNode.getElement() != bike) {
|
||||
currentNode = currentNode.getNext();
|
||||
}
|
||||
return bike;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove bike from the BikeStorage, if it exists.
|
||||
*
|
||||
* @param bike the bike you want to search
|
||||
* @return {@code bike} if it is in BikeStorage, otherwise {@code null}
|
||||
*/
|
||||
public Bike remove(Bike bike) {
|
||||
if (!contains(bike)) {
|
||||
return null;
|
||||
} else if (head.getElement() == bike) {
|
||||
head = head.getNext();
|
||||
return bike;
|
||||
} else {
|
||||
// Knoten und Vorgängerknoten finden
|
||||
Node previousNode = head;
|
||||
Node currentNode = head;
|
||||
|
||||
while (currentNode.getElement() != bike) {
|
||||
previousNode = currentNode;
|
||||
currentNode = currentNode.getNext();
|
||||
}
|
||||
|
||||
// Zeiger umbiegen
|
||||
previousNode.setNext(currentNode.getNext());
|
||||
return bike;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Print all bikes in BikeStorage.
|
||||
*/
|
||||
public void printBikes() {
|
||||
Node currentNode = head;
|
||||
System.out.print("start -> ");
|
||||
while (currentNode != null) {
|
||||
System.out.print(currentNode);
|
||||
System.out.print(" -> ");
|
||||
currentNode = currentNode.getNext();
|
||||
}
|
||||
System.out.println("null");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
/**
|
||||
* This class models the gears. It is restricted to derailleur gears.
|
||||
*
|
||||
* @author Markus Iser, Martin Thoma
|
||||
* @version 1.0
|
||||
*/
|
||||
|
||||
public class Gears {
|
||||
public static final int MIN_FRONT_SPROCKETS = 1;
|
||||
public static final int MAX_FRONT_SPROCKETS = 3;
|
||||
public static final int MIN_REAR_SPROCKETS = 1;
|
||||
public static final int MAX_REAR_SPROCKETS = 10;
|
||||
|
||||
private int frontSprockets;
|
||||
private int rearSprockets;
|
||||
|
||||
/** Price in cents. */
|
||||
private final int price;
|
||||
|
||||
Gears(int frontSprockets, int rearSprockets, int price) {
|
||||
setSprockets(frontSprockets, rearSprockets);
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the sprocket numbers. Uses default-values if consisteny criteria are
|
||||
* not met.
|
||||
*
|
||||
* @param sprockets
|
||||
* @param rearSprockets
|
||||
*/
|
||||
void setSprockets(int sprockets, int rearSprockets) {
|
||||
this.frontSprockets = sprockets;
|
||||
this.rearSprockets = rearSprockets;
|
||||
|
||||
if (!(this.frontSprockets >= 1)) { // A.1
|
||||
this.frontSprockets = 1;
|
||||
} else if (!(this.frontSprockets < 4)) { // A.2
|
||||
this.frontSprockets = 3;
|
||||
}
|
||||
|
||||
if (this.rearSprockets < 1) { // B.1
|
||||
this.rearSprockets = this.frontSprockets;
|
||||
}
|
||||
if (this.rearSprockets > 9) { // B.2
|
||||
this.rearSprockets = this.frontSprockets * 3;
|
||||
}
|
||||
|
||||
if (this.rearSprockets < this.frontSprockets) { // C.1
|
||||
this.rearSprockets = this.frontSprockets;
|
||||
} else if (this.rearSprockets > 3 * this.frontSprockets) { // C.2
|
||||
this.rearSprockets = 3 * this.frontSprockets;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the number of gears as the number of sprocket-combinations
|
||||
*/
|
||||
int getNumberOfGears() {
|
||||
return frontSprockets * rearSprockets;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the price of the gears
|
||||
*/
|
||||
int getPrice() {
|
||||
return price;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
public class Node {
|
||||
// Usually you would use "Generics" at this point to be able
|
||||
// to hold arbitrary Objects, not only Bikes.
|
||||
private final Bike element;
|
||||
private Node next;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param element the element you want to add
|
||||
*/
|
||||
public Node(Bike element) {
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the content of this node.
|
||||
*
|
||||
* @return the element
|
||||
*/
|
||||
public Bike getElement() {
|
||||
return element;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the next
|
||||
*/
|
||||
public Node getNext() {
|
||||
return next;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param next the next to set
|
||||
*/
|
||||
public void setNext(Node next) {
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Node[element=" + element + "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
/**
|
||||
* A bike model in Java. This class models the wheels.
|
||||
*
|
||||
* @author Markus Iser, Martin Thoma
|
||||
* @version 1.0
|
||||
*/
|
||||
|
||||
class Wheels {
|
||||
public static final int MIN_DIAMETER = 150;
|
||||
public static final int MAX_DIAMETER = 700;
|
||||
public static final int MIN_WHEEL_SIZE = 20;
|
||||
public static final int MAX_WHEEL_SIZE = 50;
|
||||
|
||||
/** The diameter is in range MIN_DIAMETER to MAX_DIAMETER. */
|
||||
private int diameter = 559;
|
||||
|
||||
/** The wheelsSize is in range MIN_WHEEL_SIZE to MAX_WHEEL_SIZE. */
|
||||
private double wheelsSize = 50;
|
||||
|
||||
/** Price measured in Euro-cents. */
|
||||
private final int price;
|
||||
|
||||
Wheels(int diameter, double wheelSize, int price) {
|
||||
this.diameter = diameter;
|
||||
this.wheelsSize = wheelSize;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the price of the wheels in cent
|
||||
*/
|
||||
int getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the diameter in mm
|
||||
*/
|
||||
public int getDiameter() {
|
||||
return this.diameter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the size of the wheel in mm
|
||||
*/
|
||||
public double getWheelsSize() {
|
||||
return this.wheelsSize;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
public class World {
|
||||
public static void main(String[] args) {
|
||||
BikeStorage ladenLutz = new BikeStorage();
|
||||
|
||||
Bike a = new Bike("a");
|
||||
Bike b = new Bike("b");
|
||||
Bike c = new Bike("c");
|
||||
Bike d = new Bike("d");
|
||||
|
||||
ladenLutz.printBikes();
|
||||
|
||||
ladenLutz.add(a);
|
||||
ladenLutz.printBikes();
|
||||
ladenLutz.add(b);
|
||||
ladenLutz.printBikes();
|
||||
|
||||
ladenLutz.add(c);
|
||||
System.out.println("c wurde hinzugefügt:");
|
||||
ladenLutz.printBikes();
|
||||
ladenLutz.remove(c);
|
||||
System.out.println("c wurde entfernt:");
|
||||
ladenLutz.printBikes();
|
||||
ladenLutz.add(d);
|
||||
ladenLutz.add(a);
|
||||
|
||||
ladenLutz.printBikes();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue