
Aua ... ja, ja, die Wetterstation ... ich erinnere mich.na das mit static musste doch nu wissen ^^ da haben wir doch dieses voll ätzende bsp da durchgenommen, wo wir 0 plan hatt (wie so oft ^^).

Danke, hab's verstanden.nochmal zur verdeutlichung:
static methoden: du brauchst keine objekt-instanz (myClass myObj = new myClass()der klasse anlegen, um auf die methode zuzugreifen, sondern du greifst über den klassennamen ganz allgemein darauf zu (statt myObj.methode(); eben myClass.methode()
. bsp: bestes beispiel dafür sind so math-klassen - von denen musst du auch kein objekt anlegen -> math.abs(var); für den absolutwert einer variablen (und nicht math mathObj = new math(); mathObj.abs(var)
.
static variablen: gelten objektübergreifend, wie phoenix ja schon schön "be-beispielt" hat ^^ du hast 2 objekte der selben klasse, in obj1 erhält die staticVar den wert 1, in obj2 darauf allerdings den wert 2. greift man nun über obj1 auf staticVar zu, so bekommt man die von ob2 gesetzte 2 ausgegeben.

import java.util.ArrayList;
public class Section extends Member {
private final int size; // Größe des Sektions-Arrays
private int allocation; // aktuelle Belegung mit Mitgliedern
private Member[] section;
private double overallIncome;
private double overallCosts;
private ArrayList<Section> sections;
public Section(String firstName, String lastName, int ID, int size) {
super(firstName, lastName, ID);
this.size = size;
this.allocation = 0;
this.section = new Member[this.size];
for (int i = 0; i < section.length; i++) {
this.overallIncome += this.section[i].getIncome();
this.overallCosts += this.section[i].getCosts();
}
this.sections = new ArrayList<Section>();
}
[COLOR=royalblue][B]public Section() {
this();
}[/B]
public void addMember(Member m) {
if (this.allocation < this.size) {
this.section[this.allocation] = m;
this.allocation++;
} else {
System.out.println("Member can't be added! Section already full.");
}
}
public void AddSection(Section section) {
if (this.allocation < this.size) {
this.sections.add(section);
this.allocation++;
} else {
System.out.println("Sub-Section can't be added! Section already full.");
}
}
}
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
public class SectionTest {
Section sectionOne = new Section();
@Before
public void setup() {
sectionOne.addMember(null);
sectionOne.addMember(null);
sectionOne.addMember(null);
}
@Test
public void test() {
}
}
sectionOne.addMember(Trainer);

Ohne irgendwas drinnen funktioniert's nicht. Da kommt vom Eclipse: Implicit super constructor Member() is undefinied. Must explicitly invoke another constructordas ist auch wieder rekursion
public Section() { this(); }
mach einfach public Section() { }

Hab's jetzt erstmal so gelöst:und in test dann
sectionOne.addMember(new Member("hans","wurst",1);
....
...
..
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
public class SectionTest {
Section sectionOne = new Section();
Trainer trainerOne = new Trainer("Anton", "Gruber", 32, 3);
AmateurAthlete amateurAthleteOne = new AmateurAthlete("Sepp", "Mueller", 28, 1);
ChairMember chairMemberOne = new ChairMember("Fritz", "Wallner", 65, 10);
Section sectionTwo = new Section();
Section sectionThree = new Section();
@Before
public void setup() {
sectionOne.addMember(trainerOne);
sectionOne.addMember(amateurAthleteOne);
sectionOne.addMember(chairMemberOne);
sectionOne.addSection(sectionTwo);
sectionOne.addSection(sectionThree);
}
@Test
public void test() {
}
}
Ja, genau.Wie ist das mit Section's von Section gemeint du hast eine Section mit Sections drin und in diesen Sections sind dann wieder Member ?
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
public class SectionTest {
Section sectionOne = new Section();
Trainer trainerOne = new Trainer("Anton", "Gruber", 32, 3);
AmateurAthlete amateurAthleteOne = new AmateurAthlete("Sepp", "Mueller", 28, 1);
ChairMember chairMemberOne = new ChairMember("Fritz", "Wallner", 65, 10);
Section sectionTwo = new Section();
TopAthlete topAthleteOne = new TopAthlete("Franz", "Berger", 43, 4);
SupportMember supportMemberOne = new SupportMember("Hans", "Mueller", 12);
Section sectionThree = new Section();
TopAthlete topAthleteTwo = new TopAthlete("Hans", "Wurst", 17, 6);
AmateurAthlete amateurAthleteTwo = new AmateurAthlete("Peppi", "Berger", 76, 81);
@Before
public void setup() {
sectionOne.addMember(trainerOne);
sectionOne.addMember(amateurAthleteOne);
sectionOne.addMember(chairMemberOne);
sectionTwo.addMember(topAthleteOne);
sectionTwo.addMember(supportMemberOne);
sectionThree.addMember(topAthleteTwo);
sectionThree.addMember(amateurAthleteTwo);
sectionOne.addSection(sectionTwo);
sectionOne.addSection(sectionThree);
}
@Test
public void test() {
}
}
Section(Section section) {
if (this.allocation < this.size) {
this.sections.add(section);
this.allocation++;
} else {
Throw new IrgendwasException ("Sub-Section can't be added! Section already full.");
}
}
Section sectionOne = new Section();
import java.util.ArrayList;
public class Section {
private final int size; // Größe des Sektions-Arrays
private int allocation; // aktuelle Belegung mit Mitgliedern
private Member[] section;
private double overallIncome;
private double overallCosts;
private ArrayList<Section> sections;
[COLOR=royalblue][B]public Section(int size)[/B] {
this.size = size;
this.allocation = 0;
this.section = new Member[this.size];
for (int i = 0; i < section.length; i++) {
this.overallIncome += this.section[i].getIncome();
this.overallCosts += this.section[i].getCosts();
}
this.sections = new ArrayList<Section>();
}
public void addMember(Member m) {
if (this.allocation < this.size) {
this.section[this.allocation] = m;
this.allocation++;
} else {
System.out.println("Member can't be added! Section already full.");
}
}
public void addSection(Section section) {
if (this.allocation < this.size) {
this.sections.add(section);
this.allocation++;
} else {
System.out.println("Sub-Section can't be added! Section already full.");
}
}
}
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
public class SectionTest {
Section sectionOne = new [COLOR=royalblue][B]Section(5)[/B];
Trainer trainerOne = new Trainer("Anton", "Gruber", 32, 3);
AmateurAthlete amateurAthleteOne = new AmateurAthlete("Sepp", "Mueller", 28, 1);
ChairMember chairMemberOne = new ChairMember("Fritz", "Wallner", 65, 10);
Section sectionTwo = new [COLOR=royalblue][B]Section(2)[/B];
TopAthlete topAthleteOne = new TopAthlete("Franz", "Berger", 43, 4);
SupportMember supportMemberOne = new SupportMember("Hans", "Mueller", 12);
Section sectionThree = new [COLOR=royalblue][B]Section(4)[/B];
TopAthlete topAthleteTwo = new TopAthlete("Hans", "Wurst", 17, 6);
AmateurAthlete amateurAthleteTwo = new AmateurAthlete("Peppi", "Berger", 76, 81);
@Before
public void setup() {
sectionOne.addMember(trainerOne);
sectionOne.addMember(amateurAthleteOne);
sectionOne.addMember(chairMemberOne);
sectionTwo.addMember(topAthleteOne);
sectionTwo.addMember(supportMemberOne);
sectionThree.addMember(topAthleteTwo);
sectionThree.addMember(amateurAthleteTwo);
sectionOne.addSection(sectionTwo);
sectionOne.addSection(sectionThree);
}
@Test
public void test() {
}
}
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
public class SectionTest {
Section sectionOne = new Section(3);
Trainer trainerOne = new Trainer("Anton", "Gruber", 32, 3);
AmateurAthlete amateurAthleteOne = new AmateurAthlete("Sepp", "Mueller", 28, 1);
ChairMember chairMemberOne = new ChairMember("Fritz", "Wallner", 65, 10);
Section sectionTwo = new Section(5);
TopAthlete topAthleteOne = new TopAthlete("Franz", "Berger", 43, 4);
SupportMember supportMemberOne = new SupportMember("Hans", "Mueller", 12);
Section sectionThree = new Section(4);
TopAthlete topAthleteTwo = new TopAthlete("Hans", "Wurst", 17, 6);
AmateurAthlete amateurAthleteTwo = new AmateurAthlete("Peppi", "Berger", 76, 81);
@Before
public void setup() {
sectionOne.addMember(trainerOne);
sectionOne.addMember(amateurAthleteOne);
sectionOne.addMember(chairMemberOne);
sectionTwo.addMember(topAthleteOne);
sectionTwo.addMember(supportMemberOne);
sectionThree.addMember(topAthleteTwo);
sectionThree.addMember(amateurAthleteTwo);
sectionOne.addSection(sectionTwo);
sectionOne.addSection(sectionThree);
}
@Test
public void testMemberAddFailure() {
[COLOR=royalblue][B]assertEquals("Member can't be added! Section already full.", sectionOne.addMember(topAthleteOne)); [/B]
}
@Test
public void testMemberAddSuccess() {
[COLOR=royalblue][B]assertEquals(true, (sectionTwo.overallIncome == 220.0 && sectionTwo.overallCosts == 55.0)); [/B] }
@Test
public void testSectionAddFailure() {
[COLOR=royalblue][B] assertEquals("Sub-Section can't be added! Section already full.", sectionOne.addSection(sectionThree))[/B];
}
@Test
public void testSectionAddSuccess() {
sectionTwo.addSection(sectionOne);
[COLOR=royalblue][B]assertEquals(true, (sectionTwo.overallIncome == 1640.0 && sectionTwo.overallCosts == 406.5));[/B] }
}

import java.util.ArrayList;
public class Section {
private final int size; // Größe des Sektions-Arrays
private int allocation; // aktuelle Belegung mit Mitgliedern
private Member[] section;
public double overallIncome;
public double overallCosts;
private ArrayList<Section> sections;
public Section(int size) {
this.size = size;
this.allocation = 0;
this.section = new Member[this.size];
for (int i = 0; i < section.length; i++) {
[COLOR=royalblue][B]this.overallIncome += this.section[i].getIncome();[/B] [COLOR=seagreen]// Zeile 18 [COLOR=royalblue][B]this.overallCosts += this.section[i].getCosts(); [/B][COLOR=seagreen]// Zeile 19 }
this.sections = new ArrayList<Section>();
}
public void addMember(Member m) {
if (this.allocation < this.size) {
this.section[this.allocation] = m;
this.allocation++;
} else {
System.out.println("Member can't be added! Section already full.");
}
}
public void addSection(Section section) {
if (this.allocation < this.size) {
this.sections.add(section);
this.allocation++;
} else {
System.out.println("Sub-Section can't be added! Section already full.");
}
}
}
public class Member {
private final String firstName;
private final String lastName;
private final int ID;
private double income;
private double costs;
public Member(String firstName, String lastName, int ID) {
this.firstName = firstName;
this.lastName = lastName;
this.ID = ID;
this.income = this.getIncome();
this.costs = this.getCosts();
this.getSurplus();
}
public void setIncome(double income) {
this.income = income;
}
[COLOR=royalblue][B]public double getIncome() {
return this.income;
}[/B]
public void setCosts(double costs) {
this.costs = costs;
}
[COLOR=royalblue][B]public double getCosts() {
return this.costs;
}[/B]
public double getSurplus() {
return this.getIncome()-this.getCosts();
}
public String toString() {
String result = new String();
result = "Name: " + this.firstName + " " + this.lastName + " | Income: " + this.getIncome() + " | Costs: " + this.getCosts() + " | Surplus: " + this.getSurplus();
return result;
}
}
public boolean addMember([COLOR=royalblue][B]Member m[/B]) {
if (this.allocation < this.size) {
[COLOR=royalblue][B]this.section[this.allocation] = m;[/B] [COLOR=royalblue][B]this.allocation++;[/B] } else {
System.out.println("Member can't be added! Section already full.");
return false;
}
return true;
}

public Section(int size) {
this.size = size;
this.allocation = 0;
this.section = new Member[this.size];
for (int i = 0; i < section.length; i++) {
[COLOR=royalblue][B]this.overallIncome += this.section[i].getIncome();
this.overallCosts += this.section[i].getCosts();[/B] }
this.sections = new ArrayList<Section>();
}



public ArrayList() {
this.initCap = 6;
this.realCap = this.initCap;
[COLOR=royalblue][B]this.list = new Element[this.realCap];[/B] this.size = 0;
}
/**
* Konstruktor 2 mit selbstgewählter Kapazität
* @param capacity = beliebige Kapazität
*/
public ArrayList(int capacity) {
int minCap = 4; // selbstgewählte Minimum Kapazität der Liste
if (capacity > minCap) {
this.initCap = capacity;
this.realCap = capacity;
} else {
this.initCap = 6;
this.realCap = 6;
}
[COLOR=royalblue][B]this.list = new Element[this.realCap];[/B] this.size = 0;
}
/**
* Füge Elemente am Ende der Liste hinzu
* @param element = hinzuzufügendes Element
*/
public void add(Element element) {
if (this.size == this.realCap) {
this.expandList();
[COLOR=royalblue][B]this.list[this.size] = element;[/B] } else {
[COLOR=royalblue][B]this.list[this.size] = element;[/B] // Element am Ende der Liste einfügen
}
this.size++; // Zähler für Elemente um 1 erhöhen
}

public double getOverallIncome() {
for (int i = 0; i < section.length; i++) {
this.overallIncome += this.section[i].getIncome();
}
return this.overallIncome;
}
public double getOverallCosts() {
for (int i = 0; i < section.length; i++) {
this.overallCosts += this.section[i].getCosts();
}
return this.overallCosts;
}

1 public Section(int size) {
2 this.size = size;
3 this.allocation = 0;
4 this.section = new Member[this.size];
5 for (int i = 0; i < section.length; i++) {
6 this.overallIncome += this.section[i].getIncome(); // Zeile 18
7 this.overallCosts += this.section[i].getCosts(); // Zeile 19
8 }
9 this.sections = new ArrayList<Section>();
}


das kann nicht gutgehenthis.section[this.allocation] = m;
public boolean addMember(Member m) {
if (this.allocation < this.size) {
this.section[this.allocation] = m;
this.allocation++;
for (int i = 0; i < section.length; i++) {
this.overallIncome += this.section[i].getIncome();
this.overallCosts += this.section[i].getCosts();
}
} else {
System.out.println("Member can't be added! Section already full.");
return false;
}
return true;
}
for (int i = 0; i < section.length; i++) {
this.overallIncome += this.section[i].getIncome(); // Zeile 18
this.overallCosts += this.section[i].getCosts(); // Zeile 19
}