CS230 Data Structures, Wellesley College, Fall 1999

CS230 FAQ

[CS230 Home Page] [Assignments] [Documentation] [Software Installation] [FAQ] [CS Dept.] [CWIS]

When you have a CS230 question, check the FAQ first! We will try to update it frequently to respond to your common questions.

If you have a question you would like answered on this list, or if you are not satisfied with an answer that appears below, please send email to fturbak@wellesley.edu.


Table of Contents

The following questions are linked to answers below.

Nike

Symantec Cafe

Invoking Applets

Java Programming


Questions and Answers


Nike


Q: I'm having trouble using Fetch to connect to Nike. What am I doing wrong?

A: There are many possible mistakes you may be making. Here are some important tips to follow:


Q: How do I navigate to a CS111 drop folder in Fetch?

A: When you connect to Fetch, you are connected to your home directory (/usr/users/your-user-name) by default. Your goal is to navigate to the CS111 drop folders, which are located in /usr/users/cs111/drop. Follow these steps:

  1. Navigate to the users directory, which is the parent of your home directory. To do this, find the pull-down menu at the top of the Fetch window. The pull-down menu always has the name of the current directory (in this case, your username) with a downward pointing triangle next to it. When you select the current directory name with the mouse, a menu of all ancestor directories will pop up. Select users.
  2. You are now connected to the /usr/users directory. Your goal is to descend to /usr/users/cs111. Do this by scrolling to cs111 in the directory listing within the main Fetch window and double-clicking on cs111.
  3. You are now connected to the /usr/users/cs111 directory. Your goal is to descend to /usr/users/cs111/drop. Do this by selecting drop in the directory listing within the main Fetch window and double-clicking on it..
  4. Continue in this fashion until you navigate to your directory within the appropriate drop folder.
  5.  

Q: When I upload files to/download files from Nike, files don't have the right name/format. What's the fix?

A: If your uploaded files end up having .bin or .hqx extensions, or if downloaded files don't have the right icons, your Fetch preferences are probably incorrect. You can fix them by installing the CS111 patches.


Symantec Cafe


Q: How do I tell if Symantec Cafe is installed on my Macintosh?

A: Follow the instructions for checking for Symantec Cafe that are on the CS111 Software Installation page.


Q: How do I install Symantec Cafe on my Macintosh?

A: Follow the instructions for installing Symantec Cafe that are on the CS111 Software Installation page.


Q: Symantec Cafe does not appear in the apple menu. How do I launch it?

A: First of all, the item that appears in the apple menu is labelled Symantec Project Manager, not Symantec Cafe. If this item is in the apple menu, select it.

If Symantec Project Manager does not appear in the apple menu, do not give up hope! It may just that no one has installed an alias to the Symantec Project Manager in the apple menu on your machine. You can instead launch the project manager by navigating through the following sequence of folders from your desktop:

  1. Macintosh HD (or whatever the hard drive folder is named on your computer)
  2. Key Servered Software
  3. Symantec Cafe 1.5

If there is no Symantec Cafe 1.5 folder, you're out of luck and must install Symantec Cafe from scratch. However, if there is such a folder, it should contain a file called Symantec Project Manager. Double-click on it to launch Symantec Cafe.

To avoid navigating to the Symantec Project Manager application every time you need to launch it, you can add an alias for the Symantec Project Manager to the apple menu. You can do this as follows:

  1. Navigate to the Symantec Project Manager as described above.
  2. Select the Symantec Project Manager by single-clicking on it. (Do not double-click on it, as that will launch it).
  3. In the File pull-down menu at the top of the screen, select Make Alias. This will create a Symantec Project Manager alias icon, which you should drag to an open space on the desktop.
  4. Navigate to the Apple Menu Items folder via the following sequence of folders: (1) Macintosh HD (2) System Folder (3) Apple Menu Items.
  5. Drag the Symantec Project Manager alias icon into the Apple Menu Items folder.
  6. Congratulations! You've just added a new item to the apple menu. From now on, you can just select the Symantec Project Manager alias item from the apple menu.
  7. You may also wish to add to the applet menu an alias for the Applet Viewer application (which is also in the Symantec Cafe 1.5 folder).


Q: Why doesn't CS111Applet appear as one of items in the Project Model menu within the new project dialog?

A: The Symantec Cafe 1.5 (Project Model) folder has not been properly initialized for CS111. You need to install the CS111 patch files on your machine.


Q: Why are question marks showing up in my project window?

A: Within a project window, a question mark indicates that the project manager doesn't know where to find the associated file name. Here are some typical reasons for this situation:


Q: What does an underlined entry within my project window mean?

A: It means that the file with the underlined name in a different directory from the project file.


Q: When I update a Symantec Cafe Project, the .class files don't show up in the right directory. Why?

A: It's likely that you're using a PC-formatted disk. Symantec Cafe 1.5 does not interact well with PC formatted disks. Your disk is PC-formatted if the disk icon says "PC". If you have a PC-formatted disk, you should copy its contents to a Mac-formatted disk, and should use only Mac-formatted disks for the rest of the semester!


Applets


Q: Why can't I get a remote applet to run in my home page?

A: There are numerous reasons why an applet might not run on your home page. Before you do anything else, read the applet invocation notes, and follow the steps explained there. If you still have problems, here are some possible reasons:


Q: I can get a remote applet to run, but the images don't appear. Why?

A: If the applet is running, then your <applet ..> tag codebase attribute is correct. But if images are not appearing, it means that the addresses for data resources are not correct. See the notes on referencing data resources at the end of the applet invocation notes for how to fix this.


Java Programming


Q: How do I change the background color of my applet?

A: Add the following method to your applet

	public void init() {
	  this.setBackground(bgcolor);
	}

where bgcolor is an expression denoting the background color you want. Important: do not use the method invocation this.setBackground(bgcolor); within an applet's paint method -- this will cause the applet to get stuck in flicker mode.


Q: My applet gets stuck in a flickering mode. What's wrong?

A: It's likely that you have a method invocation this.setBackground(bgcolor); within the applet's paint method. As noted in the answer to the previous question, this invocation should appear in the applet's init method, not its paint method.


Q: I invoked a graphics method to draw something on the screen,but it didn't appear in the window. Why?

A: Here are several possible causes for missing graphics:


Q: Where can I read about arithmetic in Java?

A: Here are several sources for details on Java arithmetic:


Q: What are floating-point numbers?

A: A floating-point number is a number that has a fractional part; it is written with a decimal point, e.g.: 2.5, 3.141, -5.99. Floating point numbers are distinguished from integers, which have no fractional part. In Java, there are two types of floating-point numbers, those of type float and those of type double. Numbers of type double have higher precision than those of type float. By default, we will use double for floating-point numbers in CS111.


Q: How can I convert between ints and doubles?

A: If exp is an expression denoting an int, it can be converted to a double by means of a so-called "cast" expression:

(double) exp

If exp is an expression denoting a double, it can be converted to an int by means of a "cast" expression that truncates the information after the decimal point:

(int) exp

Here are some examples:

Java Expression

Value

(double) 17

17.0

(int) 3.141

3

(int) 5.99

5

(int) -3.141

-3

(int) -5.99

-5

In practice, converting an int to a double is rarely needed, since most binary operators (e.g. +, -, * , /, <, >, ==, !=) automatically convert an int argument to a double when the other argument is a double. (But see the question about dividing two ints, below.) Here are some examples involving addition:

Java Expression

Value

10 + 3

13

10.9 + 3.141

14.041

10.9 + 3

13.9

10 + 3.141

14.141

((int) 10.9) + 3

13

(int) (10.9 + 3.141)

14

(int) 10.9 + 3.141

13.141

2.5 + 3.5

6.0

Note in the example

(int) 10.9 + 3.141

that (int) binds more strongly than +; that is, the above example is treated as:

((int) 10.9) + 3.141

Sometimes truncating the fractional portion of a double is not the desired behavior -- instead, we want to find the floor (round toward negative infinity), find the ceiling (round toward infinity), or round toward the nearest integer. These calculations are performed by the Math.floor, Math.ceil, and Math.round library functions, as shown below:

Java Expression

Value

Math.floor(3.141)

3

Math.ceil(3.141)

4

Math.round(3.141)

3

Math.floor(5.9)

5

Math.ceil(5.9)

6

Math.round(5.9)

6

Math.round(2.5)

3

Math.round(3.5)

4

Math.floor(-3.141)

-4

Math.ceil(-3.141)

-3

Math.round(-3.141)

-3

Math.floor(-5.9)

-6

Math.ceil(-5.9)

-5

Math.round(-5.9)

-6

Math.round(-2.5)

-2

Math.round(-3.5)

-3

See the java.lang.Math API for more information on library math functions.


Q: How can I get a double result from dividing two ints?

A: In Java, the division of two numbers of which at least one is a double always yields a double result. However, dividing two ints always yields an int -- this is the result of so-called "integer division", in which any fractional portion of the result is truncated. How can we get a double result from dividing two ints? By first casting one or both of the integer arguments to a double. Here are some examples:

Java Expression

Value

7 / 2

3

7.0 / 2

3.5

7 / 2.0

3.5

7.0 / 2.0

3.5

((double) 7) / 2

3.5

7 / ((double) 2)

3.5

(double) (7 / 2)

3

(double) 7 / 2

3.5

((int) 7.0) / 2

3

Q: How can I convert strings of digits to integers in Java?

A Java string is a collection of elements with primitive datatype "char". Your question essentially asks how you convert between characters that are digits ('0', '1', ...,'9') and their corresponding integer values (0, 1, ..., 9). Unfortunately, this is not well-documented in most Java books I have read. But I can tell you what you need to know.

The American Standard Code for Information Interchange (ASCII) was developed as a standard way of converting between character representations and one-byte integers (range 0 to 255). In Java, you convert a character to an ASCII integer via an explicit cast; e.g., ((int) 'A') denotes the ASCII value for capital 'A', which happens to be 65. You can use this to write a simple Java program for converting printing strings as the integer values of their characters:

	public static void printStringAsInts (String s) {
		System.out.print("\"" + s + "\" => ");
		for (int i = 0; i < s.length(); i++) {
			System.out.print((int) s.charAt(i) + " ");
		}
		System.out.println("");
	}

Here is the result of running this program on some sample strings:

	"Abby Stracksen" => 65 98 98 121 32 83 116 114 97 99 107 115 101 110 
	"3.14159" => 51 46 49 52 49 53 57 

(It turns out that Java actually supports mappings between so-called Unicode characters and 16-bit integers (ranging from 0 to 65535), but this is not important to the present discussion.)

Similarly, in Java, you convert an ASCII integer to a character by a cast; e.g., ((char) 65) denotes the character 'A'. You can use this fact to generate a table of the characters associated with the ASCII values between lo and hi:

	public static void printIntsAsChars (int lo, int hi) {
		for (int i = lo; i <= hi; i++) {
			System.out.print(i + " => \'"  + ((char) i) + "\'\t");
			// Print out results in 4 columns 
			if ((i - lo) % 4 == 3) {
				System.out.print("\n");
			}
		}
	}

 

Invoking this on lo=0 and hi=255 yields the following table mapping ASCII values to characters:

0 => ' '	1 => ''	2 => ''	3 => ''	
4 => ''	5 => ''	6 => ''	7 => ''	
8 => ''	9 => '	'	10 => '
'	11 => ''	
12 => ''	13 => '
'	14 => ''	15 => ''	
16 => ''	17 => ''	18 => ''	19 => ''	
20 => ''	21 => ''	22 => ''	23 => ''	
24 => ''	25 => ''	26 => ''	27 => ''	
28 => ''	29 => ''	30 => ''	31 => ''	
32 => ' '	33 => '!'	34 => '"'	35 => '#'	
36 => '$'	37 => '%'	38 => '&'	39 => '''	
40 => '('	41 => ')'	42 => '*'	43 => '+'	
44 => ','	45 => '-'	46 => '.'	47 => '/'	
48 => '0'	49 => '1'	50 => '2'	51 => '3'	
52 => '4'	53 => '5'	54 => '6'	55 => '7'	
56 => '8'	57 => '9'	58 => ':'	59 => ';'	
60 => '<'	61 => '='	62 => '>'	63 => '?'	
64 => '@'	65 => 'A'	66 => 'B'	67 => 'C'	
68 => 'D'	69 => 'E'	70 => 'F'	71 => 'G'	
72 => 'H'	73 => 'I'	74 => 'J'	75 => 'K'	
76 => 'L'	77 => 'M'	78 => 'N'	79 => 'O'	
80 => 'P'	81 => 'Q'	82 => 'R'	83 => 'S'	
84 => 'T'	85 => 'U'	86 => 'V'	87 => 'W'	
88 => 'X'	89 => 'Y'	90 => 'Z'	91 => '['	
92 => '\'	93 => ']'	94 => '^'	95 => '_'	
96 => '`'	97 => 'a'	98 => 'b'	99 => 'c'	
100 => 'd'	101 => 'e'	102 => 'f'	103 => 'g'	
104 => 'h'	105 => 'i'	106 => 'j'	107 => 'k'	
108 => 'l'	109 => 'm'	110 => 'n'	111 => 'o'	
112 => 'p'	113 => 'q'	114 => 'r'	115 => 's'	
116 => 't'	117 => 'u'	118 => 'v'	119 => 'w'	
120 => 'x'	121 => 'y'	122 => 'z'	123 => '{'	
124 => '|'	125 => '}'	126 => '~'	127 => ''	
128 => 'Ä'	129 => 'Å'	130 => 'Ç'	131 => 'É'	
132 => 'Ñ'	133 => 'Ö'	134 => 'Ü'	135 => 'á'	
136 => 'à'	137 => 'â'	138 => 'ä'	139 => 'ã'	
140 => 'å'	141 => 'ç'	142 => 'é'	143 => 'è'	
144 => 'ê'	145 => 'ë'	146 => 'í'	147 => 'ì'	
148 => 'î'	149 => 'ï'	150 => 'ñ'	151 => 'ó'	
152 => 'ò'	153 => 'ô'	154 => 'ö'	155 => 'õ'	
156 => 'ú'	157 => 'ù'	158 => 'û'	159 => 'ü'	
160 => 'Ý'	161 => '°'	162 => '¢'	163 => '£'	
164 => '§'	165 => ''	166 => '¶'	167 => 'ß'	
168 => '®'	169 => '©'	170 => '™'	171 => '´'	
172 => '¨'	173 => ''	174 => 'Æ'	175 => 'Ø'	
176 => ''	177 => '±'	178 => ''	179 => ''	
180 => '¥'	181 => 'µ'	182 => ''	183 => ''	
184 => ''	185 => ''	186 => ''	187 => 'ª'	
188 => 'º'	189 => ''	190 => 'æ'	191 => 'ø'	
192 => '¿'	193 => '¡'	194 => '¬'	195 => ''	
196 => ''	197 => ''	198 => ''	199 => '«'	
200 => '»'	201 => ''	202 => ' '	203 => 'À'	
204 => 'Ã'	205 => 'Õ'	206 => ''	207 => ''	
208 => '&endash;'	209 => '&emdash;'	210 => '"'	211 => '"'	
212 => '''	213 => '''	214 => '÷'	215 => ''	
216 => 'ÿ'	217 => ''	218 => ''	219 => '¤'	
220 => 'Ð'	221 => 'ð'	222 => 'Þ'	223 => 'þ'	
224 => 'ý'	225 => '·'	226 => ''	227 => ''	
228 => ''	229 => 'Â'	230 => 'Ê'	231 => 'Á'	
232 => 'Ë'	233 => 'È'	234 => 'Í'	235 => 'Î'	
236 => 'Ï'	237 => 'Ì'	238 => 'Ó'	239 => 'Ô'	
240 => ''	241 => 'Ò'	242 => 'Ú'	243 => 'Û'	
244 => 'Ù'	245 => ''	246 => ''	247 => ''	
248 => '¯'	249 => ''	250 => ''	251 => ''	
252 => '¸'	253 => ''	254 => ''	255 => ''	

Note that many of the characters are gobbledygook. What you care about is that the digits '0' through '9' have consecutive ASCII values 48 through 57. You can use this fact to convert a digit character to an integer as follows:

	public static int digitValue (char c) {
		return (int) c - (int) '0';
	}

It turns out that Java will implicitly perform a cast from char to int if the context requires it. So you can in fact write the above as:

	public static int digitValue (char c) {
		return c - '0';
	}

Now typically you'll want to convert a sequence of digit characters to a single integer value. You do this by converting the individual digit characters to integers, and then combine the resulting individual integers into one integer. Below is some code from Cornell and Horstmann's Core Java book that does this. In fact, it also handles + and - signs and works for any base, not just base 10. The parts dealing with 'A' and 'a' is used for parsing hexadecimal numbers (where 'A' through 'F' are the 11th through 16th digits, respectively).

 

   // From Cornell and Horstmann's Core Java
   private static long parseLong(String s, int base)
   {  int i = 0;
      int sign = 1;
      long r = 0;
      
      while (i < s.length() && Character.isSpace(s.charAt(i))) i++;
      if (i < s.length() && s.charAt(i) == '-') { sign = -1; i++; }
      else if (i < s.length() && s.charAt(i) == '+') { i++; }
      while (i < s.length())
      {  char ch = s.charAt(i);
         if ('0' <= ch && ch < '0' + base)
            r = r * base + ch - '0';
         else if ('A' <= ch && ch < 'A' + base - 10)
            r = r * base + ch - 'A' + 10 ;
         else if ('a' <= ch && ch < 'a' + base - 10)
            r = r * base + ch - 'a' + 10 ;
         else 
            return r * sign;
         i++;
      }
      return r * sign;      
   }