First develop commit. Adding all existing files.
This commit is contained in:
52
srcDE/diesunddas/Strecke.java
Normal file
52
srcDE/diesunddas/Strecke.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package diesunddas;
|
||||
|
||||
public class Strecke{
|
||||
|
||||
enum Einheit{
|
||||
m(1),
|
||||
km(1e3),
|
||||
cm(0.01),
|
||||
mm(0.001);
|
||||
|
||||
private double faktor;
|
||||
|
||||
Einheit(){}
|
||||
Einheit(double faktor){ this.faktor = faktor; }
|
||||
|
||||
double faktor(){ return faktor; }
|
||||
}
|
||||
|
||||
double länge;
|
||||
Einheit einheit = Einheit.m;
|
||||
|
||||
public Strecke(double länge){
|
||||
this.länge = länge;
|
||||
}
|
||||
|
||||
public Strecke(double länge, Einheit einheit){
|
||||
this.länge = länge;
|
||||
this.einheit = einheit;
|
||||
}
|
||||
|
||||
public Strecke plus(Strecke dazu){
|
||||
return new Strecke(länge+dazu.länge*dazu.einheit.faktor()/einheit.faktor(), einheit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return länge+" "+einheit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Strecke heimweg = new Strecke(10, Einheit.km);
|
||||
Strecke bäcker = new Strecke(420, Einheit.m);
|
||||
System.out.println(heimweg);
|
||||
System.out.println(bäcker);
|
||||
System.out.println(heimweg.plus(bäcker));
|
||||
System.out.println(bäcker.plus(heimweg));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user