Hallo Stefke,
Ik dacht dat een som een optelling was van 2 getallen. Deze vind ik echter niet terug in je method.
return som += getal hoort niet thuis in een loop zoals dizzl reeds aanhaalt. De loop wordt slechts 1 keer uitgevoerd. Uiteindelijk ga je alleen je getal dat je er instopte als argument terug krijgen. Een nutteloze bewerking dus.
Faculteit. Als ik me goed herinner is een faculteit het volgende
1 * 2 = 2
2 * 3 = 6
6 * 4 = 24
24 * 5 = 120 ...
In code
private double faculteit (int eindwaarde){
double dblFac = 1;
for(int cnt = 1; cnt < eindwaarde; cnt++){
dblFac *= cnt;
}
return dblFac;
}
Geef je variabelen ook het type mee. integer = int, double = dbl, String = str, .... zo zie je direct met welk type je te maken hebt. Bij globale variabelen wordt dit mInt, mDbl, mStr, .... . Zo is ook meteen duidelijk of het een globaal gedeclareerde variabele gaat of een lokale variabele. Neem de scope ook zo klein mogelijk. Zo krijg je niet de verleiding om bijvoorbeeld cnt globaal te declareren en te pas en te onpas te gebruiken. Uiteindelijk heb je dan geen controle meer over je variabelen.
Wat de driehoek betreft. Bekijk de javadoc eens. Je hebt de beschikking over
drawPolygon
public abstract void drawPolygon(int[] xPoints,
int[] yPoints,
int nPoints)
Draws a closed polygon defined by arrays of x and y coordinates. Each pair of (x, y) coordinates defines a point.
This method draws the polygon defined by nPoint line segments, where the first nPoint - 1 line segments are line segments from (xPoints[i - 1], yPoints[i - 1]) to (xPoints[i], yPoints[i]), for 1 ≤ i ≤ nPoints. The figure is automatically closed by drawing a line connecting the final point to the first point, if those points are different.
Parameters:
xPoints - a an array of x coordinates.
yPoints - a an array of y coordinates.
nPoints - a the total number of points.
en
fillPolygon
public abstract void fillPolygon(int[] xPoints,
int[] yPoints,
int nPoints)
Fills a closed polygon defined by arrays of x and y coordinates.
This method draws the polygon defined by nPoint line segments, where the first nPoint - 1 line segments are line segments from (xPoints[i - 1], yPoints[i - 1]) to (xPoints[i], yPoints[i]), for 1 ≤ i ≤ nPoints. The figure is automatically closed by drawing a line connecting the final point to the first point, if those points are different.
The area inside the polygon is defined using an even-odd fill rule, also known as the alternating rule.
Parameters:
xPoints - a an array of x coordinates.
yPoints - a an array of y coordinates.
nPoints - a the total number of points.
Ook met de drawlines kan je hetzelfde bekomen als met de gewone polygon. Met drawline wordt een gevulde driehoek een stuk ingewikkelder.
Ik hoop dat je hier wat mee bent. En leer vooral de javadoc te begrijpen en te gebruiken.
mvg
Theo
edit: foutje in faculteit method aangepast.