Chapter Review


x

Lesson:
Review:
  1. Elements of a Source File: The elements of a source file must appear in this order: package declaration, import statements, class definitions. You will often create source files which do not have package declarations, but if you do have package declarations, they must be the first things (other than comments) which appear in your file.
  2. Source Files: A source file may have only one public class definition. All other classes which are defined in a file may not be declared public. The name of the file must match the name of the public class which is defined in it.
  3. Identifiers: Identifiers must begin with a letter (upper or lower case), a dollar sign, or an underscore. Other letters used in an identifier name may be letters, dollar signs, underscores, or digits.
  4. Primitive Data Types: The four signed integral primitive data types are byte, short, int, and long. The two floating-point primitive data types are float and double. The char type is unsigned. The boolean type may take on the values of true and false only.
  5. Arrays: Arrays must be (in order): 1) declared, 2) constructed (or allocated), 3) initialized. The default values are zero for numerical types, the null reference for object references, the null character for char, and false for boolean. Applying .length to an array returns the number of elements in the array.
  6. Class Fundamentals: A class with a main() method can be run from the command line as a Java application. The signature for main() is "public static void main(String args[])". The args[] array contains all command-line arguments that appeared after the name of the application class.
  7. Argument Passing: When a variable is given as an argument to a method, a copy of the value is passed to the argument, not a reference to the variable. This means that any modifications made to the value passed to the method will not have an effect on the original variable. Objects are passed by reference.
  8. Garbage Collection: Unused memory is recovered by Java's garbage collection mechanism. Although you can control when objects become eligible for garbage collection, you cannot control when garbage collection will occur.

x

Assignment:
You will modify the following source code so that it contains methods for addition, subtraction, multiplication, and division. You will also add comments to identify import statements, class definitions, identifiers, method calls, and method definitions.

import java.awt.*; import java.awt.event.*; import java.applet.*; public class ex09 extends Applet{ int x, y; char op='a'; TextField v1, v2, fop, answer; Label l1, l2, lop, lan; Button pro; Dimension d; public void init(){ setLayout(new GridLayout(5,2)); setBackground(Color.white); d=getSize(); v1 = new TextField(15); l1 = new Label("INPUT 1:"); v2 = new TextField(15); l2 = new Label("INPUT 2:"); fop = new TextField(10); lop = new Label("OPERATION:"); answer = new TextField(25); lan = new Label("OUTPUT:"); pro = new Button("PROCESS"); add(l1); add(v1); add(l2); add(v2); add(lop); add(fop); add(lan); add(answer); add(pro); pro.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent a){ String opstr=fop.getText(); op=opstr.charAt(0); try{ x=Integer.parseInt(v1.getText()); y=Integer.parseInt(v2.getText()); }catch(NumberFormatException nfe){ } //THIS SAMPLE ONLY TAKES ^ AS A VALID OPERATION if(op=='^'){ doExp(x,y); } //remove comments and make correct method calls //else if(op='+'){ //} //else if(op='-'){ //} //else if(op='*'){ //} //else if(op='/'){ //} else{ //put in your operation options answer.setText("OPERATION OPTIONS: ^"); } v1.setText(""); v2.setText(""); fop.setText(""); } }); } public void doExp(int a, int b){ int out=1; for(int i=0; i<b; i++){ out*=a; } answer.setText(""+out); } public void paint(Graphics g){ g.drawRect(0,0,d.width-2,d.height-2); } }

x