Hallo, hab da eine Basisklasse gemacht und muss mit einer Testklasse das testen.
1) Das Testprogramm soll eine Uhr erzeugen mit der aktuellen Systemzeit.
Ich bekomme es aber nicht so ganz hin.
1) Das Testprogramm soll eine Uhr erzeugen mit der aktuellen Systemzeit.
Ich bekomme es aber nicht so ganz hin.
Java:
public class Uhr {
private int h;
private int m;
private int s;
public Uhr() {
h = 0;
m = 0;
s = 0;
}
public Uhr(int h, int m, int s) {
this.h = h;
this.m = m;
this.s = s;
}
public void tick() {
this.s++;
if(this.s>59){
this.s-=60;
this.m+=1;
}
if(this.m>59){
this.m-=60;
this.h+=1;
}
if(this.h>23){
this.h-=24;
}
}
public void reset () {
h = m = s = 0;
}
public void setTime (int s) {
this.s = s;
}
public void setTime(int m, int s) {
this.m = m;
this.s = s;
}
public void setTime(int h, int m, int s ) {
this.h = h;
this.m = m;
this.s = s;
}
int geth () {
return h;
}
int getm () {
return m;
}
int gets () {
return s;
}
public int timeToSec() {
int gesamtSekunden = 0;
gesamtSekunden = gets() + getm()*60 + geth()*3600 ;
return gesamtSekunden;
}
public boolean equals (Uhr c) {
if (this == c) {
return true;
}
else
return false ;
}
public int compareTo (Uhr c) {
if (this.h > c.h) {
return -1;
}
if(this.h == c.h && this.m > c.m) {
return -1;
}
if(this.h == c.h && this.m == c.m && this.s > c.s) {
return -1;
}
if(this.h == c.h && this.m == c.m && this.s == c.s) {
return 0;
}
else
return -1;
}
public void showTime() {
System.out.println(geth() + ":" + getm()+ ":" + gets());
}
public void showTime (String s) {
s = "Die Uhrzeit ist : ";
System.out.println(s + geth() + ":" + getm()+ ":" + gets());
}
public String toString() {
String Uhr = geth() + ":";
if (geth()<10)Uhr = "0" + Uhr;
if (getm()<10) Uhr += "0";
Uhr += getm() + ":";
if (gets()<10) Uhr += "0";
Uhr += gets() + " Uhr";
return Uhr;
}
}
Java:
import java.util.Date;
public class TestprogrammA {
public static void main (String [] args) {
Date sekunden = new Date();
Date minuten = new Date();
Date stunden = new Date();
Uhr d = new Uhr();
//d.setTime(stunden.getHours(), minuten.getMinutes(), sekunden.getSeconds())
//System.out.println(sekunden.getSeconds());
//System.out.println(sekunden.getMinutes());
//System.out.println(sekunden.getHours());
}
}