cs046 ยป

Frequently Asked Questions

Why Java?

There have been a number of questions on the forum about this. Isn't language X more (modern|elegant|concise|beautiful|whatever)?

Of course it is. But so what?

This is the first course in the SJSU computer science curriculum. We have to pick a programming language, and it is a fact of life that it takes a year or two to become really comfortable in any programming language. Java works really well throughout the curriculum. It can be used for database access, mobile development, network programming, web services, cryptography, graphics, and many other domains. There simply are many more tools and libraries for Java than for language X.

So, are we bothered by the fact that you have to learn about public static void main when in language X you just start printing "Hello, World!" without that boilerplate? Sure, we are. But that's a drop in the bucket. Every language, including language X, has its share of idiosyncracies.

Also, keep in mind, that this is the first course. At SJSU, we believe you need to be competent in three languages to earn your bachelor's degree. A strongly typed object-oriented language, which for us is Java. A close-to-the-metal language, i.e. C. And a functional language such as Scheme.

What about language X? We teach you three languages. As a junior, you take a course in programming language principles. You'll learn a bit of another language in a senior level course to build your senior project. If, by the time that you graduate, you haven't learned how to learn enough of a fifth language to be dangerous in a few weeks, we have failed.

For now, relax and go with the flow. Except for that annoying public static void main, Java is a very nice language. It was specifically designed to be easy to read, to be unsurprising, and to allow support by high-quality tools. I use many languages (almost certainly including language X) in my job, and I am always happy to be back working with Java.

Why BlueJ?

Some people on the forum wondered why they can't use Eclipse, NetBeans, IntelliJ, Emacs and the JDK command line tools, or a hex editor to assemble byte codes by hand.

For most assignments, you can use any tools you like.

However, BlueJ has two advantages that make it much easier to learn Java.

First, the object workbench. You can make objects, put them on the workbench, and ask them to do something. Without having to write any code at all. That's cool, and, more importantly, it builds useful intuition. You see that red blob there. (It's a little known fact that all objects are red.) And you can't look inside. But you can call methods to change it. And call other methods to see how it has changed. That gives you a visceral feel for what objects are all about, and that's very, very good.

It also makes it very clear what the difference is between classes and objects. Classes are the tan things with square corners that make objects when you right-click and call a constructor. You know this, of course, but believe me, before I taught with BlueJ, I would get that question every semester: "Dr. Horstmann, I am so confused, what's the difference between an object and a class". And I would go on for an hour, explaining it. Everyone's eyes glazed over by minute eleven at the latest. Now I don't get that question very often. And if someone asks, I say "Classes are tan, and they make the red blobs, you know, the objects". And everyone nods.

Secondly, the code pad. It happens to me all the time. There is some class that I don't really understand. (Yes, I've been at this for many years, and there are many classes that I don't understand.) I could write a program (with public static void main), or I could just pull up the code pad and type

String river = "Mississippi";
river.replace("i", "x")

and see the result right there. It's better.

These are the two things that make BlueJ special. The object workbench and the codepad. They aren't important when you write that big app in the cloud that solves world hunger, but they really help you learn object-oriented programming. So, even if you love Eclipse, NetBeans, vi, or your hex editor, give BlueJ a chance.

Also keep in mind that, while it takes most people a year or so to be comfortable with a programming language, mastering a tool typically takes a few weeks. So, learning another tool is something you are expected to do many times over in your career.

How do I run a program in BlueJ?

Right-click on the class and select the void main(String[]) method. In the dialog that appears, click Ok.

Why doesn't my class have a main method when I right-click on it?

First compile by clicking on the botton to the left. Fix any syntax errors that appear. If you fixed them all and there still isn't a main, maybe you are looking at the wrong class?

Why do I have to compile before I can run a program?

I am not sure. They could have easily built BlueJ so that it automatically compiles files when you save them.

My guess is that they wanted to encourage a workflow where you edit a file and keep clicking on Compile, fixing compile-time errors until they are all gone. It makes the distinction between compile-time and run-time errors very clear.

Why is compilation needed at all? That's a bit more complex, and you don't really have to know the details. Some programming languages are never compiled. When you run a program, an "interpreter" reads and executes the source code (i.e., the code that you write). But that's quite slow. Other programming languages compile the source code into machine code, the instructions that the processor chip in your computer can execute. Then the program runs at full speed directly on the processor. Java takes an intermediate approach. It translates your source code into "byte code", instructions for a "virtual machine". The virtual machine executes the byte codes, and, when it finds that some code sequences are run more than once (in a loop), it translates those to machine instructions. Why not translate directly? The virtual machine watches what your program does, and with that information, it can generate machine code that is more efficient than a direct compiler could.

Like I said, you don't need to know any of that. But with BlueJ, you do need to know to click the Compile button until you have no more compile-time errors.

Why does BlueJ complain that it can't find the Rectangle or Picture class?

You need to include the graphics classes. We put them into the pre-made projects, but if you make your own projects, do this:

  • Download the graphics library from http://horstmann.com/sjsu/graphics/
  • Unzip in the same way as the lesson and homework files
  • Select Project -> Import in the menu, then select the graphics folder and click on Import

Why is (0, 0) at the top left corner of the graphics screen?

In math, you have the origin (0, 0) in the middle, and the y-axis grows upward:

        ^ y
--------+--------> x

But that doesn't work so well in a windowing system. What should happen when the window is resized? Should the origin move? Then one would have to update the coordinates of all shapes.

That's why in computer graphics systems, the origin is at the top left corner. And if the y-axis pointed upward, then all y-coordinates would be negative, which is inconvenient. So, the y-axis is flipped:

+---------------> x
| y 

Also note that x is the column index and y is the row index of each pixel, which can cause some confusion in Lesson 7 when we talk about two-dimensional arrays.

The good thing is that you can figure these things out pretty quickly in BlueJ. Just create rectangles or lines, right-click and select draw, and watch where they go.

Why isn't my progress recorded?

The most common reason is clicking test run and not clicking submit. For more info on the difference, check out make sure you get credit.