Beruflich Dokumente
Kultur Dokumente
LISTENERS -
ActionListener – needed for buttons
public void actionPerformed (ActionEvent ae){
Object source = ae.getSource();
}
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);
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
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
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();
}
1 2 1
1 3 3 1
1 4 6 4 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