This is a closed notes, closed book exam.
int[] x; create an array of integers?
If yes, how many elements are in the array, if no, how do you create an array
of 100 integers?
No, it only creates a reference to an array.
x = new int[100];
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Quiz2Prob2 extends Applet
implements ActionListener
{
boolean first_pushed = false;
Button first_button = new Button("Push Me First");
Button second_button = new Button("Push Me Second");
public void init()
{
first_button.addActionListener(this);
second_button.addActionListener(this);
add(first_button);
add(second_button);
}
public void actionPerformed(ActionEvent e)
{
if(!first_pushed){
if(e.getSource() == first_button)
first_pushed = true;
repaint();// ADD THIS FOR SOLUTION TO THE NEXT PROBLEM
}
else if(e.getSource() == second_button)
second_button.setLabel("Good Job!");
}
}
The program creates two buttons. Nothing appears to change until the first time the button labeled ``Push Me Second'' is pushed AFTER the button labeled ``Push Me First'' is pushed. At that point the label on the button second_button changes to ``Good Job!'' 0.25in
public void paint(Graphics g)
{
if(first_pushed){
g.setColor(Color.blue);
g.fillRect(0,0,50,50);// Whoops it is fillRect not fillRectangle
}
}
// don't forget, array elements are initialized to 0 automatically
class Quiz2Prob4 {
public static void main(String[] args)
{
int[][] x;
x = new int[4][];
for(int i=0; i<x.length; i++)
x[i] = new int[i*2];
for(int i=0; i<x.length; i++){
for(int j=0; j<x[i].length; j++)
System.out.print(x[i][j]);
System.out.println();
}
}
}
OUTPUT:
<a blank line here>
00
0000
000000
class Quiz2Syntax {
public static void main(String[] args)
{
int int_val = 1;
double double_val = 1.1;
Foo foo1 = new Foo(); //no no-arg constructor, pass a value here
Foo foo2 = new Foo(10);
System.out.println(foo1.value); // value is private change to just foo1
System.out.println(foo2.fiddle(int_val,double_val)); // this call is ok as is
System.out.println(name); // can't reference non-static var from static method
}
String name = "testing"; // make this static
}
class Foo {
Foo(int x){ value = x; }
private int value; // could just make this non-private to fix foo1.value
double fiddle(double x, double y){
return value+x+y; // nothing wrong with this
}
}