cs111 Frequently Asked Questions (FAQ)


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 the course instructors.


Table of Contents

The following questions are linked to answers below.

The CS111 server

Java Programming


Questions and Answers

The CS111 Server

 


Q: I'm having trouble using Fetch/ws_ftp to connect to the CS111 server. 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 (/students/your-user-name) by default. From your home directory, click on the cs111 folder, and from within that folder, click on the drop folder. Within the drop folder, select the right folder for the problem set you are handing in (ps1-ps9).

 


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

A: In Winsock FTP, you have two windows. The window on the left represents your computer. The files and folders there are the ones stored on your own machine. The window on the right shows the files and folders stored on the cs111 server. To get to the drop folder, you need to change your directory on the cs111 server.


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.141
(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


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: I'm getting a code error for a specific line of my code, but I can't find the error. Why not?

A: The compiler reports an error as soon as it reaches a bit of code it cannot execute. Sometimes, when it cannot execute a piece of code, it is because of a mistake made earlier in the method. Check the line before the line that's receiving the error.