for (int i=0; i< 5; i++){
for (int j=0; j< 7; i++){
System.out.println("Help! I'm stuck in an infinite loop!");
}
}(=) and the equality operator (==). public String getText(){
Show how to call that method and assign its results to a new string variable.String inputString = nameTextField.getText();Which variables are being declared in these lines of code? Which variables must have been declared earlier? How many different methods are being called? What is the signature of those methods? Guess as much as you can about what's going on in this program, just from what you see here.
myPet.setName(inputString);
Here is the code for a simple applet. It doesn't compile, due to two errors in the code. These errors are based on essentials of java-- you don't need to know anything about class Applet, class Label, etc. in order to find them. Find both errors and explain why they are a mistake. Then say how you would change the code to fix the errors.
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Label;
public class ExamApplet1 extends Applet {
boolean ready = false;
Label questionLabel = new Label("What is this thing?");
questionLabel.setSize(25,40);
public void init(){
setBackground(Color.black);
}
public void start(){
ready = true;
this.add(questionLabel);
g = this.getGraphics();
this.decorate(g);
}
public void decorate(Graphics g){
g.clearRect(0,0,250,400);
g.setColor(Color.red);
g.drawString("this is an applet", 30, 240);
}
public void paint(Graphics g){
if (ready = true){
this.decorate(g);
}
}
}
Here is another class, Zucchini. I made it up-there isn't really a package called "food" available from Sun, but pretend there is.
import java.food.*;
public class Zucchini extends Vegetable{
public void prepare(){
if (this.testFreshness()== true){
this.wash();
this.peel();
this.slice("Julienne");
}else {
System.out.println("Cannot prepare");
}
}
public void saute(FryingPan thePan){
Spatula theSpatula = Utensil.getSpatula();
thePan.setHeat("medium");
thePan.addOil(.5);
for (int i = 0; i< 12; i++){
theSpatula.flip(this);
}
}
public void serve(){
Cheese topping = new Cheese("romano");
topping.grate();
topping.sprinkle(this);
}
}
This code compiles and runs without exceptions, so there are no errors. If you were to look up class Vegetable in the API, what methods should appear there? What would be the signature of those methods?
import java.food.*;
public class Ratatouille extends Stew{
int cookTime = 45;
Vegetable[] veggies = new Vegetable[5];
public void setIngredients(){
veggie[1]= new Onion();
veggie[2]= new Zucchini();
veggie[3]= new Eggplant();
veggie[4]= new Tomato();
veggie[5]= new Garlic();
}
public void makeStew(){
for (int i = 0; i< 5; i++){
veggie[i].prepare();
veggie[i].saute(Utensil.getFryingPan());
this.add(veggie[i]);
}
}
public void simmer(StewPot thePot){
Spoon theSpoon = Utensil.getSpoon();
thePot.setHeat("low");
for (int i = 0; i< 15; i++){
theSpoon.stir(this);
}
}
}
public String spellNumber(int i)
Is the code below a correct call of that method? Say why or why not.
theLabel.setText(spellNumber("one"));
theLabel.setText(spellNumber(int i));
import java.applet.Applet;
import java.awt.Color;
import java.awt.Button;
import java.awt.event.ActionListener;
public class ExamApplet2 extends Applet implements ActionListener{
Button button1 = new Button("a");
Button button2 = new Button("b");
Button button3 = new Button("c");
Button button4 = new Button("d");
Button button5 = new Button("e");
Button button6 = new Button("f");
Button button7 = new Button("g");
public void init(){
setBackground(Color.black);
button1.setBackground(new Color(170,170,0));
button2.setBackground(new Color(170,170,0));
button3.setBackground(new Color(170,170,0));
button4.setBackground(new Color(170,170,0));
button5.setBackground(new Color(170,170,0));
button6.setBackground(new Color(170,170,0));
button7.setBackground(new Color(170,170,0));
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
add(button1);
add(button2);
add(button3);
add(button4);
add(button5);
add(button6);
add(button7);
}
...
}
As you can see, the author this code seems to enjoy copying and pasting. Your task is to rewrite the code using arrays and loops, to avoid the unnecessary repetition.