Hallo, probiere gerade mit Klassen herum, und denke der fehler liegt irgendwie an den verschachtelten Arrays ... ? (soll mit IPs dezimal und binär umgehen können)
in der folgendenden Klasse gibts den Fehler :
any ideas? danke!
Java:
public class Rechner
{
// Attribute eine IP-Nr
Ip ipnr;
// Konstruktor
public Rechner()
{
}
// Dienste
public void neuIp(int b0, int b1, int b2, int b3)
{
// eine IP wird eingegeben
ipnr = new Ip(b0, b1, b2, b3);
}
public void rechnen()
{
ipnr.dnr.anzeige();
ipnr.bnr.anzeige();
}
}
Java:
public class Ip
{
// Attribute - die IP dezimal und binär
Deziip dnr;
Binärip bnr;
// Konstruktor
public Ip(int b0, int b1, int b2, int b3)
{
// eingegebne IP dezimal als Array speichern
dnr = new Deziip(b0, b1, b2, b3);
// die eigebene gleich in binär umrechnen und als dezimale speichern
bnr = new Binärip(dnr);
}
}
Java:
public class Deziip
{
// Attribute - als int array 4x
int[] dezip = new int[4];
// Konstruktor
public Deziip(int b0,int b1,int b2,int b3)
{
dezip[0]=b0; dezip[1]=b1; dezip[2]=b2 ; dezip[3]=b3;
}
public void anzeige()
{
for (int i =0; i<4; i++)
System.out.print(dezip[i]);
System.out.println(" ");
}
}
Java:
public class Binärip
{
// Attribute - die binäre als array 4x des Typs binärbyte
BinärByte[] binip = new BinärByte[4];
// Konstruktor
public Binärip(Deziip by)
{
binip[0].dezInBinByte(by.dezip[0]); // hier steigt es zur Laufzeit aus!:(
binip[1].dezInBinByte(by.dezip[1]);
binip[2].dezInBinByte(by.dezip[2]);
binip[3].dezInBinByte(by.dezip[3]);
}
public void anzeige()
{
for (int i =0; i<4; i++)
System.out.print(binip[i]);
System.out.println(" ");
}
}
Java:
public class BinärByte
{
// Attribute - 1 Byte als 8x boolean
public boolean[] bbyte = new boolean[8];
// Konstruktor
public BinärByte()
{
}
// Dienste
public void dezInBinByte(int zahl)
{
int i = 0;
int[] mem = new int[8];
while (zahl!=0)
{
mem[i] = zahl % 2;
i++;
zahl = zahl / 2;
}
for (i=0; i<8; i++)
{
if (mem[7-i] == 1) {bbyte[i] = true;}
if (mem[7-i] == 0) {bbyte[i] = false;}
}
}
public void anzeige()
{ String zahl ="";
for (int i = 7;i>=0;i--)
{
if (bbyte[i] == true) {zahl = "1"+zahl;}
if (bbyte[i] == false){zahl = "0"+zahl;}
}
System.out.println(zahl);
}
}