Add PeriodicTable and Vehicle classes with implementations for elements and racing simulation
This commit is contained in:
parent
5f7a036bad
commit
c41409b9d9
2 changed files with 303 additions and 0 deletions
174
src/de/dhbwka/java/exercise/classes/periodic/PeriodicTable.java
Normal file
174
src/de/dhbwka/java/exercise/classes/periodic/PeriodicTable.java
Normal file
|
@ -0,0 +1,174 @@
|
|||
package de.dhbwka.java.exercise.classes.periodic;
|
||||
|
||||
public class PeriodicTable {
|
||||
private Element[] elements = new Element[118];
|
||||
|
||||
public void addElement(Element e) {
|
||||
if (elements[e.getOrdinal()] == null) {
|
||||
elements[e.getOrdinal()] = e;
|
||||
}
|
||||
}
|
||||
|
||||
public Element getElement(int ordinal) {
|
||||
return elements[ordinal];
|
||||
}
|
||||
|
||||
public boolean hasElement(Element e) {
|
||||
return elements[e.getOrdinal()] != null;
|
||||
}
|
||||
|
||||
public Element[] getMetals() {
|
||||
int count = 0;
|
||||
for (Element e : elements) {
|
||||
if (e instanceof Metal) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
Element[] metals = new Element[count];
|
||||
count = 0;
|
||||
for (Element e : elements) {
|
||||
if (e instanceof Metal) {
|
||||
metals[count] = e;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return metals;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
PeriodicTable pt = new PeriodicTable();
|
||||
// H
|
||||
pt.addElement(new Element("Wasserstoff", "H", 1, 'K', 2, true));
|
||||
// He
|
||||
pt.addElement(new Element("Helium", "He", 2, 'K', 2, true));
|
||||
// Na
|
||||
pt.addElement(new Metal("Natrium", "Na", 11, 'L', 0, true, false, 21.0 * Math.pow(10, 6)));
|
||||
// Fe
|
||||
pt.addElement(new Metal("Eisen", "Fe", 26, 'M', 0, true, false, 10.02 * Math.pow(10, 6)));
|
||||
// Ge
|
||||
pt.addElement(new Metal("Germanium", "Ge", 32, 'N', 0, true, true, 1.45));
|
||||
// Br
|
||||
pt.addElement(new Element("Brom", "Br", 35, 'N', 1, true));
|
||||
// Te
|
||||
pt.addElement(new Metal("Tellur", "Te", 52, 'O', 0, true, true, 0.005));
|
||||
// Au
|
||||
pt.addElement(new Metal("Gold", "Au", 79, 'P', 0, true, false, 44.0 * Math.pow(10, 6)));
|
||||
|
||||
for (Element e : pt.getMetals()) {
|
||||
System.out.println(e);
|
||||
}
|
||||
|
||||
System.out.println(pt.getElement(79));
|
||||
}
|
||||
}
|
||||
|
||||
class Element {
|
||||
private String name;
|
||||
private String symbol;
|
||||
private int ordinal;
|
||||
private char shell;
|
||||
private int phase;
|
||||
private boolean isMainGroup;
|
||||
|
||||
public Element(String name, String symbol, int ordinal, char shell, int phase, boolean isMainGroup) {
|
||||
this.name = name;
|
||||
this.symbol = symbol;
|
||||
this.ordinal = ordinal;
|
||||
this.shell = shell;
|
||||
this.phase = phase;
|
||||
this.isMainGroup = isMainGroup;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getSymbol() {
|
||||
return symbol;
|
||||
}
|
||||
|
||||
public void setSymbol(String symbol) {
|
||||
this.symbol = symbol;
|
||||
}
|
||||
|
||||
public int getOrdinal() {
|
||||
return ordinal;
|
||||
}
|
||||
|
||||
public void setOrdinal(int ordinal) {
|
||||
this.ordinal = ordinal;
|
||||
}
|
||||
|
||||
public char getShell() {
|
||||
return shell;
|
||||
}
|
||||
|
||||
public void setShell(char shell) {
|
||||
this.shell = shell;
|
||||
}
|
||||
|
||||
public int getPhase() {
|
||||
return phase;
|
||||
}
|
||||
|
||||
public void setPhase(int phase) {
|
||||
this.phase = phase;
|
||||
}
|
||||
|
||||
public boolean isMainGroup() {
|
||||
return isMainGroup;
|
||||
}
|
||||
|
||||
public void setMainGroup(boolean isMainGroup) {
|
||||
this.isMainGroup = isMainGroup;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name + " (" + symbol + ", " + ordinal + ")" + " Schale: " + shell + ", Phase: "
|
||||
+ (phase == 0 ? "fest" : (phase == 1 ? "flüssig" : "gasförmig"))
|
||||
+ (isMainGroup ? ", Hauptgruppe" : ", Nebengruppe");
|
||||
}
|
||||
|
||||
public boolean equals(Element e) {
|
||||
return this.ordinal == e.ordinal;
|
||||
}
|
||||
}
|
||||
|
||||
class Metal extends Element {
|
||||
private boolean metalloid;
|
||||
private double conductivity;
|
||||
|
||||
public Metal(String name, String symbol, int ordinal, char shell, int phase, boolean isMainGroup, boolean metalloid,
|
||||
double conductivity) {
|
||||
super(name, symbol, ordinal, shell, phase, isMainGroup);
|
||||
this.metalloid = metalloid;
|
||||
this.conductivity = conductivity;
|
||||
}
|
||||
|
||||
public boolean isMetalloid() {
|
||||
return metalloid;
|
||||
}
|
||||
|
||||
public void setMetalloid(boolean metalloid) {
|
||||
this.metalloid = metalloid;
|
||||
}
|
||||
|
||||
public double getConductivity() {
|
||||
return conductivity;
|
||||
}
|
||||
|
||||
public void setConductivity(double conductivity) {
|
||||
this.conductivity = conductivity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString() + (metalloid ? ", Halbleiter" : "") + ", Leitfähigkeit: " + conductivity;
|
||||
}
|
||||
|
||||
}
|
129
src/de/dhbwka/java/exercise/classes/vehicles/Race.java
Normal file
129
src/de/dhbwka/java/exercise/classes/vehicles/Race.java
Normal file
|
@ -0,0 +1,129 @@
|
|||
package de.dhbwka.java.exercise.classes.vehicles;
|
||||
|
||||
public class Race {
|
||||
public static void main(String[] args) {
|
||||
Vehicle[] vehicles = new Vehicle[4];
|
||||
vehicles[0] = new Bicycle(20.0);
|
||||
vehicles[1] = new Car(150.0);
|
||||
vehicles[2] = new RacingCar(200.0);
|
||||
vehicles[3] = new Ambulance(80.0, true);
|
||||
// 4 hours lead for the bike
|
||||
vehicles[0].drive(240.0);
|
||||
// 1 hour of driving for everyone
|
||||
for (int i = 0; i < vehicles.length; i++) {
|
||||
vehicles[i].drive(60);
|
||||
}
|
||||
// Output Race
|
||||
for (int i = 0; i < vehicles.length; i++) {
|
||||
System.out.println(vehicles[i].toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract class Vehicle {
|
||||
private int wheels;
|
||||
private double vmax;
|
||||
private double distance;
|
||||
private double speed;
|
||||
|
||||
public Vehicle() {
|
||||
this(0, 0, 0);
|
||||
}
|
||||
|
||||
public Vehicle(double speed) {
|
||||
this(0, 0, speed);
|
||||
}
|
||||
|
||||
public Vehicle(int wheels, double vmax, double speed) {
|
||||
this.wheels = wheels;
|
||||
this.vmax = vmax;
|
||||
this.speed = speed;
|
||||
}
|
||||
|
||||
public void setSpeed(double speed) {
|
||||
this.speed = Math.min(speed, vmax);
|
||||
}
|
||||
|
||||
void setVmax(double vmax) {
|
||||
this.vmax = vmax;
|
||||
}
|
||||
|
||||
public void drive(double minutes) {
|
||||
distance += speed / 60 * minutes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "distance=" + distance + ", speed=" + speed + ", vmax=" + vmax + ", wheels=" + wheels;
|
||||
}
|
||||
}
|
||||
|
||||
class Bicycle extends Vehicle {
|
||||
public Bicycle(double speed) {
|
||||
super(2, 30, speed);
|
||||
}
|
||||
|
||||
public Bicycle() {
|
||||
super(2, 30, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Bicycle [" + super.toString() + "]";
|
||||
}
|
||||
}
|
||||
|
||||
class Car extends Vehicle {
|
||||
public Car(double speed) {
|
||||
super(4, 140, speed);
|
||||
}
|
||||
|
||||
public Car() {
|
||||
super(4, 140, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Car [" + super.toString() + "]";
|
||||
}
|
||||
}
|
||||
|
||||
class RacingCar extends Car {
|
||||
public RacingCar(double speed) {
|
||||
super(speed);
|
||||
setVmax(200);
|
||||
}
|
||||
|
||||
public RacingCar() {
|
||||
super();
|
||||
setVmax(200);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "RacingCar [" + super.toString() + "]";
|
||||
}
|
||||
}
|
||||
|
||||
class Ambulance extends Car {
|
||||
private boolean isWailing;
|
||||
|
||||
public Ambulance() {
|
||||
super();
|
||||
isWailing = false;
|
||||
}
|
||||
|
||||
public Ambulance(double speed, boolean isWailing) {
|
||||
super(speed);
|
||||
this.isWailing = isWailing;
|
||||
}
|
||||
|
||||
public void setWailing(boolean isWailing) {
|
||||
this.isWailing = isWailing;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Ambulance [" + super.toString() + ", isWailing=" + isWailing + "]";
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue