Vorige topic [Java] voor dummies - Deel 1quote:Op zaterdag 14 oktober 2006 15:44 schreef Messenga het volgende:
Het doet het toch echt.Cijfers staat al eerder gedeclareerd, helemaal in de bovenste class.
public class Applet1 extends Applet
{ private int AANTAL = 10;
private int[]cijfers = new int [AANTAL];
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | { int som=0; Double gemiddeld; for (int x=0; x < 10; x++){ som = som + getallen[x]; } gemiddeld = (double)(som/10); return gemiddeld.toString(); } |
1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | A basic extension of the java.applet.Applet class */ import java.awt.*; import java.applet.*; public class Applet1 extends Applet { private int AANTAL = 10; private int[]cijfers = new int [AANTAL]; public void init() { // Take out this line if you don't use symantec.itools.net.RelativeURL or symantec.itools.awt.util.StatusScroller symantec.itools.lang.Context.setApplet(this); // This code is automatically generated by Visual Cafe when you add // components to the visual environment. It instantiates and initializes // the components. To modify the code, only use code syntax that matches // what Visual Cafe can generate, or Visual Cafe may be unable to back // parse your Java file into its visual environment. //{{INIT_CONTROLS setLayout(null); setSize(567,617); add(CijferLijst); CijferLijst.setBounds(48,72,231,392); SomButton.setLabel("Som"); add(SomButton); SomButton.setBackground(java.awt.Color.lightGray); SomButton.setFont(new Font("Dialog", Font.BOLD, 12)); SomButton.setBounds(324,144,72,40); SorteerButton.setLabel("Sorteer"); add(SorteerButton); SorteerButton.setBackground(java.awt.Color.lightGray); SorteerButton.setFont(new Font("Dialog", Font.BOLD, 12)); SorteerButton.setBounds(324,72,72,40); MinButton.setLabel("Minimum"); add(MinButton); MinButton.setBackground(java.awt.Color.lightGray); MinButton.setFont(new Font("Dialog", Font.BOLD, 12)); MinButton.setBounds(324,216,72,40); MaxButton.setLabel("Maximum"); add(MaxButton); MaxButton.setBackground(java.awt.Color.lightGray); MaxButton.setFont(new Font("Dialog", Font.BOLD, 12)); MaxButton.setBounds(324,288,72,40); MaakButton.setLabel("Maak Getallen : "); add(MaakButton); MaakButton.setBackground(java.awt.Color.lightGray); MaakButton.setFont(new Font("Dialog", Font.BOLD, 12)); MaakButton.setBounds(48,504,204,40); add(MaxLabel); MaxLabel.setBounds(432,288,99,34); add(MinLabel); MinLabel.setBounds(420,216,99,34); add(SomLabel); SomLabel.setBounds(420,144,99,34); GemButton.setLabel("Gemiddelde"); add(GemButton); GemButton.setBackground(java.awt.Color.lightGray); GemButton.setFont(new Font("Dialog", Font.BOLD, 12)); GemButton.setBounds(324,348,72,40); add(GemLabel); GemLabel.setBounds(432,348,99,34); //}} //{{REGISTER_LISTENERS SymAction lSymAction = new SymAction(); MaakButton.addActionListener(lSymAction); SorteerButton.addActionListener(lSymAction); MaxButton.addActionListener(lSymAction); MinButton.addActionListener(lSymAction); SomButton.addActionListener(lSymAction); GemButton.addActionListener(lSymAction); //}} } //{{DECLARE_CONTROLS java.awt.List CijferLijst = new java.awt.List(0); java.awt.Button SomButton = new java.awt.Button(); java.awt.Button SorteerButton = new java.awt.Button(); java.awt.Button MinButton = new java.awt.Button(); java.awt.Button MaxButton = new java.awt.Button(); java.awt.Button MaakButton = new java.awt.Button(); java.awt.Label MaxLabel = new java.awt.Label(); java.awt.Label MinLabel = new java.awt.Label(); java.awt.Label SomLabel = new java.awt.Label(); java.awt.Button GemButton = new java.awt.Button(); java.awt.Label GemLabel = new java.awt.Label(); //}} class SymAction implements java.awt.event.ActionListener { public void actionPerformed(java.awt.event.ActionEvent event) { Object object = event.getSource(); if (object == MaakButton) MaakButton_ActionPerformed(event); else if (object == SorteerButton) SorteerButton_ActionPerformed(event); else if (object == MaxButton) MaxButton_ActionPerformed(event); else if (object == MinButton) MinButton_ActionPerformed(event); else if (object == SomButton) SomButton_ActionPerformed(event); } } void MaakButton_ActionPerformed(java.awt.event.ActionEvent event) {CijferLijst.removeAll();//verwijderd cijfers for(int t = 0; t < AANTAL; t++)// maakt een int genaamd t, zolang t kleiner is dan aantal //komt er 1 bij { cijfers[t] = (int)(Math.random()*100);//hij maakt een random getal van 0 tot 99. CijferLijst.add(String.valueOf(cijfers[t]),t);//die 10 getallen voegt hij toe aan de lijst } // to do: code goes here. } void SorteerButton_ActionPerformed(java.awt.event.ActionEvent event) { int lengte =CijferLijst.getItemCount(); // pakt de lengte for ( int aantal = lengte; aantal >1; aantal--)// Als aantal gelijk is aan lengte for (int index =0; index < aantal-1; index++)//gaat ie verder, index moet kleiner zijn dan aantal-1 { String dit_element = CijferLijst.getItem(index);//pakt het element if(dit_element.compareTo(CijferLijst.getItem(index+1)) > 0)//vergelijkt 2 elementen uit de lijst { CijferLijst.remove(index);// verwijderd het tijdelijk CijferLijst.add(dit_element, index+1);// voegt het op de juiste plaats toe } } } // to do: code goes here. void MaxButton_ActionPerformed(java.awt.event.ActionEvent event) {int maximum = cijfers[0];// het maximum komt uit cijfers for (int t =1;t < AANTAL;t++)// zolang t kleiner is dan aantal komt er 1 bij { if(maximum < cijfers[t])//als maximum kleiner is dan het cijfers[t] maximum=cijfers[t];// maakt hij het maximum gelijk aan cijfers[t] } MaxLabel.setText(String.valueOf(maximum));//maakt een label met het getal } // to do: code goes here. void MinButton_ActionPerformed(java.awt.event.ActionEvent event) {int minimum = cijfers[0]; for (int t =1;t < AANTAL;t++) { if(minimum > cijfers[t]) minimum=cijfers[t]; } MinLabel.setText(String.valueOf(minimum)); } // Zie code van de MaxButton, alleen hier moet het minimum groter zijn dan cijfers[t] void SomButton_ActionPerformed(java.awt.event.ActionEvent event) { int som = 0;//som word gelijkgesteld aan 0. for(int i=0; i<cijfers.length; i++){//zolang i kleiner is dan cijfer.length komt er 1 bij. som += cijfers[i];//de som wordt gelijkgesteld aan alle cijfers. } SomLabel.setText(String.valueOf(som));//maakt et label van de som } } public String gemiddelde(int [] getallen) { int som=0; Double gemiddeld; for (int x=0; x < 10; x++){ som = som + getallen[x]; } gemiddeld = (double)(som/10); return gemiddeld.toString(); } |
Lol, wat maakt VC er een rommel vanquote:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | { public String gemiddelde(int [] getallen) { int som=0; Double gemiddeld; for (int x=0; x < 10; x++){ som = som + getallen[x]; } gemiddeld = (double)(som/10); return gemiddeld.toString(); } // to do: code goes here. } |
1 2 3 4 5 6 7 8 9 10 11 12 | documentenRekenProject;C:VisualCafeSEjava2librt.jar;C:VisualCafeSEjava2libdt.jar;C:VisualCafeSEjava2libi18n.jar;C:VisualCafeSEjava2libjaws.jar;C:VisualCafeSEjava2libplugprov.jar;C :VisualCafeSEJAVALIB;C:VisualCafeSEJAVALIBSYMCLASS.ZIP;C:VisualCafeSEJAVALIBCLASSES.ZIP;C:VisualCafeSEswing-1.1SWINGALL.JAR;C:VisualCafeSEbincomponentssfc.jar;C:VisualCafeSEbincom ponentssymbeans.jar;C:VisualCafeSEjavalibcollections.zip;C:VisualCafeSEjavalibicebrowserbean.jar;C:VisualCafeSEjavalibjspengine.jar;C:VisualCafeSEjavalibxml.jar;C:VisualCafeSEjavali bsymtools.jar;C:VisualCafeSEbincomponentstemplates.jar;C:VisualCafeSEbincomponentsflexlm.zip" "C:Documents and SettingsNiekMijn documentenRekenProjectApplet1.java" WebGain Java! JustInTime Compiler Version 4.00.007(x) for JDK 1.2 Copyright (C) 2000 WebGain, Inc. Error: C:Documents and SettingsNiekMijn documentenRekenProjectApplet1.java(172): Incompatible type for =. Can't convert double to java.lang.Double. gemiddeld = (double)(som/10); ^ 1 error Build Failed |
Je bent er toch al een tijdje mee bezig gezien het vorige topicquote:Op zaterdag 14 oktober 2006 16:19 schreef Messenga het volgende:
[..]
Ik zeg toch ook dat ik er vrij weinig van snap
1 |
1 |
De enhanced for-loopquote:Op zaterdag 14 oktober 2006 18:32 schreef Wolfje het volgende:
Sinds java 1.5 kun je ook
[ code verwijderd ]
1 2 3 4 5 6 7 8 9 10 11 | { public class Club { public String naam; public int punten; } private int MAXAANTAL =4; private Club[]ClubRij = new Club[MAXAANTAL]; int teller = 0; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | { sortpunten(ClubRij); // to do: code goes here. } public void sortpunten (Club[ ] array) { int i, j; String s; Club temp; for ( i = 1; i < array.length; i++ ) for ( j = 0; j < array.length-i; j++ ) if ( array[j].punten > array[j+1].punten ) { temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } EindLijst.clear(); for (i = 0; i < array.length; i++) { if (array[i].naam != null) { s = array[i].naam + " - " + array[i].punten; EindLijst.add(s); } } } //zie commentaar van de namen van de actionpeformed hierboven, alleen de variabele namen //zijn gewijzigd. } // to do: code goes here. |
What am i doing wrong?quote:Exception occurred during event dispatching:
java.lang.NullPointerException
at VoetbalApplet.sortpunten(VoetbalApplet.java, Compiled Code)
at VoetbalApplet.SorteerPunt_ActionPerformed(VoetbalApplet.java:217)
at VoetbalApplet$SymAction.actionPerformed(VoetbalApplet.java:157)
at java.awt.Button.processActionEvent(Button.java:308)
at java.awt.Button.processEvent(Button.java:281)
at java.awt.Component.dispatchEventImpl(Component.java, Compiled Code)
at java.awt.Component.dispatchEvent(Component.java, Compiled Code)
at java.awt.EventQueue.dispatchEvent(EventQueue.java, Compiled Code)
at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java, C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | A basic extension of the java.applet.Applet class */ import java.awt.*; import java.applet.*; public class VoetbalApplet extends Applet { public class Club { public String naam; public int punten; } private int MAXAANTAL =4; private Club[]ClubRij = new Club[MAXAANTAL]; int teller = 0; public void init() { // Take out this line if you don't use symantec.itools.net.RelativeURL or symantec.itools.awt.util.StatusScroller symantec.itools.lang.Context.setApplet(this); // This code is automatically generated by Visual Cafe when you add // components to the visual environment. It instantiates and initializes // the components. To modify the code, only use code syntax that matches // what Visual Cafe can generate, or Visual Cafe may be unable to back // parse your Java file into its visual environment. //{{INIT_CONTROLS setLayout(null); setSize(711,663); add(EenVeld); EenVeld.setBounds(180,84,160,42); label1.setText("Vul je eerste club in : "); add(label1); label1.setFont(new Font("Dialog", Font.BOLD, 12)); label1.setBounds(36,84,132,36); label2.setText("Vul je tweede club in : "); add(label2); label2.setFont(new Font("Dialog", Font.BOLD, 12)); label2.setBounds(24,156,132,36); label3.setText("Vul je derde club in : "); add(label3); label3.setFont(new Font("Dialog", Font.BOLD, 12)); label3.setBounds(36,216,132,36); label4.setText("Vul je vierde club in : "); add(label4); label4.setFont(new Font("Dialog", Font.BOLD, 12)); label4.setBounds(36,300,132,36); add(TweeVeld); TweeVeld.setBounds(180,144,160,42); add(DrieVeld); DrieVeld.setBounds(180,216,160,42); add(VierVeld); VierVeld.setBounds(180,288,160,42); label5.setText("Naam Club :"); add(label5); label5.setFont(new Font("Dialog", Font.BOLD, 12)); label5.setBounds(192,24,121,25); label6.setText("Aantal Punten :"); add(label6); label6.setFont(new Font("Dialog", Font.BOLD, 12)); label6.setBounds(372,24,121,25); add(Punt1Veld); Punt1Veld.setBounds(396,84,52,36); add(Punt2Veld); Punt2Veld.setBounds(396,144,51,36); add(Punt3Veld); Punt3Veld.setBounds(396,216,51,36); add(Punt4Veld); Punt4Veld.setBounds(396,288,51,36); add(EindLijst); EindLijst.setBounds(72,360,195,160); NaamButton.setLabel("Sorteer op naam"); add(NaamButton); NaamButton.setBackground(java.awt.Color.lightGray); NaamButton.setFont(new Font("Dialog", Font.BOLD, 12)); NaamButton.setBounds(300,372,193,48); SorteerPunt.setLabel("Sorteer op punten"); add(SorteerPunt); SorteerPunt.setBackground(java.awt.Color.lightGray); SorteerPunt.setFont(new Font("Dialog", Font.BOLD, 12)); SorteerPunt.setBounds(300,444,193,48); add(WinnaarLabel); WinnaarLabel.setBounds(312,540,187,37); label7.setText("En de kampioen op basis van punten is : "); add(label7); label7.setFont(new Font("Dialog", Font.BOLD, 12)); label7.setBounds(24,540,252,40); DobbelButton.setLabel("Dobbel : "); add(DobbelButton); DobbelButton.setBackground(java.awt.Color.lightGray); DobbelButton.setFont(new Font("Dialog", Font.BOLD, 12)); DobbelButton.setBounds(528,408,135,49); label8.setText("En de kampioen op basis van dobbelen is :"); add(label8); label8.setFont(new Font("Dialog", Font.BOLD, 12)); label8.setBounds(12,612,252,40); add(DobbelLabel); DobbelLabel.setBounds(300,612,187,37); DobbelLabel.setVisible(false); VoegButton.setLabel("Voeg toe"); add(VoegButton); VoegButton.setBackground(java.awt.Color.lightGray); VoegButton.setFont(new Font("Dialog", Font.BOLD, 12)); VoegButton.setBounds(492,132,174,68); //}} //{{REGISTER_LISTENERS SymAction lSymAction = new SymAction(); VoegButton.addActionListener(lSymAction); NaamButton.addActionListener(lSymAction); SorteerPunt.addActionListener(lSymAction); DobbelButton.addActionListener(lSymAction); //}} } //{{DECLARE_CONTROLS java.awt.TextField EenVeld = new java.awt.TextField(); java.awt.Label label1 = new java.awt.Label(); java.awt.Label label2 = new java.awt.Label(); java.awt.Label label3 = new java.awt.Label(); java.awt.Label label4 = new java.awt.Label(); java.awt.TextField TweeVeld = new java.awt.TextField(); java.awt.TextField DrieVeld = new java.awt.TextField(); java.awt.TextField VierVeld = new java.awt.TextField(); java.awt.Label label5 = new java.awt.Label(); java.awt.Label label6 = new java.awt.Label(); java.awt.TextField Punt1Veld = new java.awt.TextField(); java.awt.TextField Punt2Veld = new java.awt.TextField(); java.awt.TextField Punt3Veld = new java.awt.TextField(); java.awt.TextField Punt4Veld = new java.awt.TextField(); java.awt.List EindLijst = new java.awt.List(4); java.awt.Button NaamButton = new java.awt.Button(); java.awt.Button SorteerPunt = new java.awt.Button(); java.awt.Label WinnaarLabel = new java.awt.Label(); java.awt.Label label7 = new java.awt.Label(); java.awt.Button DobbelButton = new java.awt.Button(); java.awt.Label label8 = new java.awt.Label(); java.awt.Label DobbelLabel = new java.awt.Label(); java.awt.Button VoegButton = new java.awt.Button(); //}} class SymAction implements java.awt.event.ActionListener { public void actionPerformed(java.awt.event.ActionEvent event) { Object object = event.getSource(); if (object == VoegButton) VoegButton_ActionPerformed(event); else if (object == NaamButton) NaamButton_ActionPerformed(event); else if (object == DobbelButton) DobbelButton_ActionPerformed(event); else if (object == SorteerPunt) SorteerPunt_ActionPerformed(event); } void VoegButton_ActionPerformed(java.awt.event.ActionEvent event) { EindLijst.clear(); EindLijst.addItem(EenVeld.getText()+" - "+ Punt1Veld.getText()); EindLijst.addItem(TweeVeld.getText()+" - "+ Punt2Veld.getText()); EindLijst.addItem(DrieVeld.getText()+" - "+ Punt3Veld.getText()); EindLijst.addItem(VierVeld.getText()+" - "+ Punt4Veld.getText()); } void NaamButton_ActionPerformed(java.awt.event.ActionEvent event) {int lengte =EindLijst.getItemCount(); // pakt de lengte for ( int aantal = lengte; aantal >1; aantal--)// Als aantal gelijk is aan lengte for (int index =0; index < aantal-1; index++)//gaat ie verder, index moet kleiner zijn dan aantal-1 { String dit_element = EindLijst.getItem(index);//pakt het element if(dit_element.compareTo(EindLijst.getItem(index+1)) > 0)//vergelijkt 2 elementen uit de lijst { EindLijst.remove(index);// verwijderd het tijdelijk EindLijst.add(dit_element, index+1);// voegt het op de juiste plaats toe } } // to do: code goes here. } } void DobbelButton_ActionPerformed(java.awt.event.ActionEvent event) { int getal = (int)Math.floor(Math.random()*3 + 1); DobbelLabel.setVisible(true); if (getal == 1){ DobbelLabel.setText(EenVeld.getText()); } else if (getal == 2){ DobbelLabel.setText(TweeVeld.getText()); } else if (getal == 3){ DobbelLabel.setText(DrieVeld.getText()); } else if ( getal ==4){ DobbelLabel.setText(VierVeld.getText()); } } void SorteerPunt_ActionPerformed(java.awt.event.ActionEvent event) { sortpunten(ClubRij); // to do: code goes here. } public void sortpunten (Club[ ] array) { int i, j; String s; Club temp; for ( i = 1; i < array.length; i++ ) for ( j = 0; j < array.length-i; j++ ) if ( array[j].punten > array[j+1].punten ) { temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } EindLijst.clear(); for (i = 0; i < array.length; i++) { if (array[i].naam != null) { s = array[i].naam + " - " + array[i].punten; EindLijst.add(s); } } } //zie commentaar van de namen van de actionpeformed hierboven, alleen de variabele namen //zijn gewijzigd. } // to do: code goes here. |
Ik zie het!quote:Op zaterdag 28 oktober 2006 15:12 schreef FallenAngel666 het volgende:
Je zou kunnen overwegen om je array van Club objecten ook daadwerkelijk te vullen met Club objecten.
Op regel 228 gaat het nu mis.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public String naam; public int punten; public int compareTo( Club c ) { if ( punten > c.punten ) return -1; if ( punten < c.punten ) return 1; return 0; } } Club[] lijst = new Club[100]; // initialiseer de objecten, nu zijn ze nog null .... Arrays.sort( lijst ); |
Hmm, zodra ik de naam wijzig van de eerste 'punten' in rij 228, in Club, geeft Visual Cafe (quote:Op zaterdag 28 oktober 2006 15:12 schreef FallenAngel666 het volgende:
Je zou kunnen overwegen om je array van Club objecten ook daadwerkelijk te vullen met Club objecten.
Op regel 228 gaat het nu mis.
|
Forum Opties | |
---|---|
Forumhop: | |
Hop naar: |