LIBRARIES – AWT = abstract window toolkit java.awt.event.* - needed for all listeners javax.swing.* - needed for timer LISTENERS ActionListener – needed for buttons public void actionPerformed (ActionEvent ae){ Object source = ae.getSource(); } MouseListener – needed for clicking of mouse to work public void mousePressed/mouseReleased/mouseEntered/mouseExited/mouseClicked (MouseEvent me){} MouseMotionListener – needed for dragging public void mouseDragged/mouseMoved (MouseEvent me){} ItemListener – needed for Choices public void itemStateChanged (ItemEvent ie){ Object source = ie.getSource(); } AdjustmentListener – needed for scrollbars public void adjustmentValueChanged(AdjustmentEvent ae){ someBar = someBar.getValue(); }
ERRORS – badArrayIndexValue – value is outside array (ie. <-1 or >length of array) nullPointer exception – have added add______Listener(this) but not initialized anything. not declaring variable – do stuff in public void init() but not at the top. not initializing variable – opposite of what is mentioned above. DEFINITIONS – inheritance (denoted by extends) – program inherits all functions/classes/other stuff instance variables – things u declare at the very top of program (example is private int, private double, etc.) local variables – declared inside function methods/member functions – example is public void distance (…){} function overloading – when u have public double ave (double a, double b){} and public int (int a, int b){} in the same function…..this is legal.
function overriding – example is public void paint(graphics g){}…..rewritten in every applet.
BUTTONS – (at top) private Button newButton; (in init) newButton = new Button(“New”); add(newButton); newButton.addActionListener(this); CHOICES – (at top) private Choice newChoice; (in init) newChoice = new Choice(); newChoice.addItem(“whatever stuff u need to add”); add(newChoice); newChoice.addItemListener(this); SCROLLBARS – (at top)private Scrollbar newbar; (in init) newBar = new Scrollbar (Scrollbar.HORIZONTAL/VERTICAL,start value(this can be a variable or number), 1(this always stays at 1), minimum, maximum); add(newBar); newBar.addAdjustmentListener(this); LABELS – (at top) private Label newLabel; (in init) newLabel = new Label(“New Label” + new); add(newLabel); to change stuff inside label – newLabel.setText("New Label”+new); TEXTFIELDS – (at top) private TextField newField; (in init) newfield = new Textfield (width of textfield); add(newfield); newField.setEditable(true or false); Note – If you want to fix where stuff goes yourself, do setLayout(null) in init and then after every add(textfield,label,etc) do textfield.setBounds(x,y,width,height)
ADAPTERS u do adapters because u don’t want to type out random methods u will never use. Adapters are in init. Do NOT put the Listener of the corresponding adapter in the “extends implements …..” part Here is an example of a MouseAdapter – addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me){ if (nbr<x.length-1){ nbr++; x[nbr] = me.getX(); y[nbr] = me.getY(); color[nbr] = (GL.randColor()); repaint(); } } } );
Keyadapters addKeyListener( new KeyAdapter() { public void keyPressed( KeyEvent ke) { if (ke.getKeyCode()==KeyEvent.VK_UP){ yinc--; } } repaint(); } } );
PROGRAMS – Fibonacci sequence – public class fibonacci { public static void main (String [] args) { long [] fib = new long [80]; Scanner sc = new Scanner (System.in); System.out.print("What is the first number? "); fib[0] = sc.nextInt(); System.out.print("What is the second number? "); fib[1] = sc.nextInt(); for (int ind = 2; ind < fib.length; ind++){
fib[ind] = fib[ind-2]+fib[ind-1]; } for (int ind = 0; ind < fib.length; ind++){ System.out.print(ind+"\t"+fib[ind]); if (ind >0 && fib[ind-1] != 0){ double ratio = (double) fib[ind]/fib[ind-1]; System.out.println("\t"+ratio); } else { System.out.println("\t--------"); } } } } Note - I included the whole program so u can review the other stuff in text based java as well line function – public static void line ( String ch, int howMany, boolean newLine){ for (int ind = 1 ; ind<= howMany; ind++) System.out.print (ch +" "); if (newLine) System.out.println(); }
slope function (from logo) – parameters – x1,y1,x2,y2 if ( :x2 - :x1) =0 [op “ undefined] [op ( :y2 - :y1 )/( :x2 - :x1)] distance function – parameters – x1, x2, y1, y2 op sqrt ((power ( :x1 - :x2)2) + (power (:y1 - :y2)2)) area functions – parameters – d1, d2, d3 (d is each side) make “s ( :d1 + :d2 + :d3)/2 op sqrt (:s*(:s - :d1)*(:s - :d2)*(:s - :d3)) Euclidean algorithm – this is same as gcf program (parameters – a, b) make “r remainder :b :a if :r = 0 [op :a stop] [ op gcf :r :a] end
Pascal’s triangle – only thing you have to know is that we did it in class like this. otherwise it should be easy. 1 1
2
1
1
3
3
1
1
4
6
4
1
public static void calculate (long [][] pas){ //top row pas[0][0] = 1; for (int row = 1; row < pas.length; row++){ //column 0 pas[row][0] = 1; for (int col = 1; col < pas[0].length; col++){ pas[row][col] = pas[row-1][col]+pas[row-1][col-1]; }
} } To do random numbers – int rand = (int)(Math.random()*(max no – min no +1)+(min no));
Magic square – start at top middle (u will always have a square of odd length) and go up 1 right 1 and place next number. if u reach edge, wraparound and continue. if you encounter multiple of side length, place next number directly below the previous number.
LOGO printlist – pararemters – message if empty? :message [pr “stop][] type se first :message “ printlist butfirst :message logo4 (mr oliver said this is going to be on test) – parameter – message if (count :message) = 1 [pr :message][]
type se first :message [*] logo4 butfirst :message logo5 (this can also be on test) – parameter – message if empty? :message [pr “ stop][] type butfirst first :message type first first :message type se “ay “ logo5 butfirst :message OTHER LOGO STUFF TO REMEMBER – I assume u know what first, last, butfirst, butlast are. type = does not go to next line pr = go to next line se = when you have 2 or more items to output