CSI1102 2005    Frequently Asked  Questions

Q: In A3 question 3, what kind of objects should I use in the driver class?

A: In the driver class you can create objects of an interest_bearing_account class that is derived for the class account.

 

Q: In A3 question 3, when do I compute the interest rate?
A: After you did the withdrawals, because one month has passed. Then display the new balances.

    

Q: In A3 question 1, the dialog window does not close properly.

A: Add the following line in your main method:

   System.exit(0);

 

Q: In A3 question 1, how do I display the table? How do I change the font?

A:   Use an array of two JTextArea, and pass it as second argument to the dialog box:
   JTextArea outputArea[] = {new JTextArea(header_string), new JTextArea(info_string)};
   outputArea[0].setFont(new Font("Courier", Font.BOLD, 12));
   JOptionPane.showMessageDialog(null, outputArea,
                "Rolling a Die 100 Times", JOptionPane.INFORMATION_MESSAGE);


Q: In A3 question 2, how do I hide a button?
A: JButton b = new JButton("text_on_button");     b.setVisible(false); //hides the button b

    
Q: In A3, how do I associate the BorderLayout with the container of the JFrame?
A:
  layout = new BorderLayout(); // layout could be a class variable, previously declared as being of type BorderLayout
    Container c = getContentPane();
    c.setLayout(layout);

        // make an array of JButton, and add each button to c in the right position (north, south, etc.)

 

Q: How do I draw the window for A2 question 1 (to exit when I press zero)? How do I draw thick lines?
A: See this file and this file for some code you can use as a model (starter code). One easy way to simulate thick lines is to draw several thin lines consecutively.

 

 

Q: Do I need a separate UPC class?

A: This is your design choice. Things to consider: Could you have several objects of the class? Do you need to pass it as parameter to the constructor of another class, for example WindowUPC?

Q: How do I extract each digit in a number?

A: You have several choices. You can transfrom the number into a String (using Integer.toString for example) and use methods of the class String. Or you can use arithmentic operations on int values. For exmaple if int N = 672, N % 10 = 2, N/10 - (N/100)*10 = 67-60 = 7, N/100 = 6.


Q: How do I draw the colors in the applet for A2 question 2?
A: To draw colors from Red to Yellow you need to vary only one component of the RGB code (in this case the G component, see the color codes below). An example of a code fragment of the paint method of the applet is:

   public void paint (Graphics page)

   {  int loc = 0;

     

      // From Red to Green through Yellow

      for (int j=0; j<256; j+=2, loc++){

         Color c = new Color(255, j, 0);

         page.setColor (c);

         page.drawLine (loc, 0, loc, APPLET_HEIGHT); 

      }

    // loops for the rest of the color changes ...
   }


The RGB color codes from Figure 2.21 are:
Color.red       255, 0, 0

Color.yellow 255,255,0
Color.green     0, 255, 0

Color.cyan      0, 255, 255
Color.blue       0, 0, 255

 

Color.magenta 255, 0, 255


 

 

Q: How do I generate random characters?

A: Generate random integers for the right ASCII codes, and then use cast to print them as characters not integers. You don’t need to know the ASCII code for letter a, just use ‘a’ and convert it into an integer.

 

Q: I see we need to use the Keyboard class in the labs. Where do I get it? How do I use it?


A: The Keyboard class was provided by the authors of the textbook and is on the CD. You can download it from here (unzip the file) or from the Examples directory (copy the directory cs1 with the file Keybord.class in it).


In RealJ, you need to set the CLASSPATH variable to the directory that contains the subdirectory cs1 (that contains the file Keyboard.class).  You can do this by selecting "Add folder" from the window "configure classpath" of your Project.

If the directory cs1 is in the same directory with your .java files, you do not need this step.
When you run it from RealJ use the menu "Run with console", not "Run Application" !!!

You could also run your program from the command line, so you can input the expected values from the keyboard. For example for the file Echo.class, you type: java Echo if the cs1 subdirectory is in your current directory, or java -classpath path_to_cs1_directory_; Echo  

You use the class by entering import cs1.Keyboard; as the first line of your code. See chapter 2 of the text book for an explanation; this is also explained in class.

 

// example of using the keyboard class

import cs1.Keyboard;

public class Echo
{
 // read from the user and print it out
 public static void main (String[] args)
 {
    String message;
    int a;

            System.out.println("Enter any text: ");
            message = Keyboard.readString();

            System.out.println("You entered: \"" + message);

 }

}


Q: How to run a Java program (of type application)?
A:
There are two ways. From a command line or using an integrated development environment (such as RealJ).

 

From RealJ  - Use the menu:
  Create a (new empty) project.

  Add your Java file(s). Use the Add Files option under the Project menu. Or use the option Add window to project if the file is already open.
  Specify which is the main file (that contains the "main" method).
  Compile by selecting Build from the menu.
  Run by selecting Run Application.

 

From a command line, run the Java compiler (javac) and then the Java interpreter (java):
   javac MyFile.java   (This produces MyFile.class
                                        You may need to specify the full path to javac.
                                        You need to be in the directory where your java files are.
                                        If your program has more files, compile each file).
   java MyFile              (assuming that MyFile.java contains the "main" method).