Computer Science I  Exam 2 Review Material

 

·         Java conditional statements

o        Simple if statement
if (x < y)
{
   smaller = x;
   larger = y;
}

o        If-else statement
if (x < y)
{
   smaller = x;
   larger = y;
}
else
{
   smaller = y;
   larger = x;
}

o        Nested if-else statements
if (age < 13)
   status = “child”;
else if (age < 20)
   status = “teenager”;
else
   status = “adult”;

·         Java looping statements

o        While statement
int n = 1, sum = 0;
while (n < 100)
{
   sum += n;
   n++;
}

o        For
int sum = 0;
for (int n = 1; n < 100; n++)
{

   sum += n;
}

o        Do while
int value = 0;
do {
   System.out.print(“Enter a positive number: ");
   value = Integer.parseInt(inData.readLine().trim());
} while (value <= 0);


·         Processing text files in Java (import java.io.*;)

o        BufferedReader
BufferedReader inData, fileData;
inData = new BufferedReader(new InputStreamReader(System.in));
fileData = new BufferedReader(new FileReader(fileName));

String line = inData.readLine();

line = fileData.readLine();

 

while ((line = fileData.readLine()) != null)
{

   /* do something with line */
}
fileData.close();

o        PrintWriter
PrintWriter outFile = new PrintWriter(new FileWriter(fileName));
outFile.println(line);
outFile.close();

·         Exceptions in Java

o        Throws clause:
public static void main(String[] args) throws IOException…

o        Using try and catch
try
{
   int number = Integer.parseInt(inData.readLine().trim);
   value += number;
}
catch (IOException ie)
{
   /* code to handle IOException */
}
catch (NumberFormatException ne)
{
   /* code to handle NumberFormatException */
}

o        Throwing an exception:
throw new QuadraticException(“Equation not quadratic”);

·         Swing GUI programming

o        Files needed for most GUI apps:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

o        GUI class declaration:
public class MyGUI extends JFrame implements ActionListener
{
   /* instance fields such as JButtons, JTextFields, etc. */

   /* constructor to layout frame with components /
   public MyGUI() { … }

   public void actionPerformed(ActionEvent e)
   {
       /* action listener code */
   }
}

o        Containers we have used:

§          JFrame

§          JPanel

o        Layout managers

§          GridLayout

§          BorderLayout

§          Examples:
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
JPanel panel1 = new JPanel(new GridLayout(2,3));
pane.add(panel1, BorderLayout.NORTH);

o        Other components:

§          JButton
JButton quitButton;

quitButton = new JButton(“Quit”);
quitButton.addActionListener(this);
panel1.add(quitButton);

§          JTextField
JTextField nameField;


nameField = new JTextField();
panel1.add(nameField);

§          JLabel
JLabel label = new JLabel(“Name:”);

o        Writing actionPerformed            
public void actionPerformed(ActionEvent e)
{
   String cmd = e.getActionCommand();
   if (cmd.equals(“Quit”))
   {
      JOptionPane.showMessageDialog(null, “Exiting program”);
      System.exit(0);
   }
   else if (cmd.equals(“Get name”))
   {
      name = nameField.getText();
   }
   /* and so forth */
}

o        A typical GUI-related question might you develop a very simple application with a frame containing a label, a button and a text field.

o        Don’t expect any questions about “fancy” features like text alignment, fonts, mouse listeners, etc.