Module 4: Questions and Answers

Module 4: Questions and Answers

Some of our TAs have put together some short videos of answers to commonly asked questions from Module 4:

 

And here are some of questions that I found interesting and my answers:

 

Q: Can I supplement my learning with YouTube?

A. Of course!  You're welcome to use outside resources as needed.  I haven't fully vetted these, but here are some options that are promising:

 

Q. Why is white (255, 255, 255) and black (0, 0, 0) and not the other way around?

A. The "RGB Color Model Links to an external site." has become the standard for representing color in many graphics applications.  RGB Color is an additive color model Links to an external site., meaning that different intensities light are combined together to produce a color.  This approach was appropriate for the earliest computer and television screens, which used the Cathode Ray Tube Links to an external site. (CRT).  CRT based screens shot streams of electrons at the screen to produce the three primary colors (Red, Green, and Blue).  More electrons meant a greater intensity of the color.  It seems reasonable to use 0 to indicate "absence" of the color (and electrons) and a positive number (like 255) to indicate a high intensity (lots of electrons).  Consequently black, the absence of all color, is (0,0,0). And white, a uniform mixing of all three primary colors, is (255,255,255).    255 is also convenient because it fits in a singly byte of computer memory, but the three bytes together allow 16 million color combinations (2563)--- So it's small enough to be convenient but big enough to allow for enough colors for most practical purposes. (By the way, the vacuum "tube" of the CRT is why televisions were referred to as a "tube" and the "Tube" part of YouTube) 

 

Q. Is the Sedgewick API the only way to handle drawing in Java or are there other options?

A. Sedgewick's API is primarily a tool used to help teach Computer Science and isn't usually used for "real world" applications.  There are lots of other APIs for drawing, but they are often are much more complex to use.  Here are a few things that may be of interest:

 

Q. I don't know exactly what the data type "char" is? Why should we use ' ' instead of " " for char?

A. Note that "char" begins with a lower case letter, which indicates this is a "primitive type Links to an external site.". You can think of the set of primitive types like the set of atomic elements --- everything is built from them.  A String is a complex type made of many individual chars "strung together" in a particular order (Upper case letters in type names, like the S in String, are used to indicate a non-primitive type).  The double quotes ("x") vs. single quotes ('x') are used so the compiler will understand your meaning.  When the compiler encounters 'x' in your code, it knows you mean a single char with the value x. Whereas "x" indicates a String that happens to contain a single letter x.  Since String is a complex type, it's able to support other operations and may have properties.  For example, you can do the following:

System.out.println("x".length());  // Prints 1: String objects have a .length() method
System.out.println("x".toUpperCase());  //  Prints X: String objects have other methods too...
// Neither of the above can be done with primitive types, like 'x'

This distinction should be a little clearer when we get to Module 7 on Objects. 

 

Q. Will we be given a list of Sedgewick commands we can input?

A. We won't expect you to memorize (or use a crib sheet) for all of them on an Exam, but we would expect you to be able to read and understand documentation on them.  Various types of documentation are posted that show all the available libraries and methods:

 

Q. Can we use coupons toward exercises?

A. No, coupons are only for Labs.  Exercises are given credit based on an effort (not being "correct", like Labs) and each is worth a very very small part of your grade.  If you miss exercises you could easily make up the credit by doing some extra work on extensions. 

 

Q. How do we know when to use while(true)?

A. There are few circumstances when you should use it.  It should only be used if a program is expected to do the body of the loop forever (well, for the remainder of time the program is running).  Some cases where "while(true)" is appropriate:

  • In microcontroller applications (somewhat like those covered in CSE132).  Microcontrollers are small computers that are often in electronically controlled devices, like a television remote control or possibly controlling some aspects of a car's operation. Often they just run a single, simple set of instructions (program) forever. The "while(true)" ensures that the program runs forever. 
  • If you're developing a new GUI library or Operating System you may need one as part of an event loop Links to an external site..  These are instances where a program mainly "waits" for events and then deals with them.  The loop is repeatedly checking for new events (forever). 

 

Q. Just as we produced different colors, is there a way we can produce different fonts?

A. Yes, in the sense that you can pick a different font and/or change the style and size of the font. Java has a "Font Links to an external site." class that can be used to create new variations of Fonts.  For instance, there's a way to get a Helvetica type of a particular size (doing so requires using Objects and may be easier to understand after Module 7).  

 

Q. What is human computer interaction beyond its basic definition, and how are researchers improving that area of study?

A. This is an active area of research and development.  New devices, like wearables, and new technologies, like Augmented Reality, are continually changing how people interact with Computers.  You may want to consider taking our Human Computer Interaction course, CSE 556A.

 

Q. What exactly does it mean for a frame to be drawn "off-screen" with Sedgewick's show() method?

A. This is an example of "double buffering", which is a simplified version of "Multiple Buffering Links to an external site.".  When doing animations you could either: 1) Let someone watch as you draw each individual part of a complex picture. They'd be able to see each circle, line, rectangle, etc. appear.  Or 2) You could draw everything and then reveal the complete drawing all at once. Double buffering is having two "screens" (aka buffers) and using approach 2. You are actively drawing on one "buffer", but it isn't being shown on the screen.  The show() command will cause the buffer to be copied to the screen and show an entire drawing all at once.  This avoids flickering that would be seen with the other approach. 

 

Q. Why does it (StdDraw) go from 0 to 1 in terms of the screen size?

A. These defaults were chosen by the authors of the library.  I suspect they choose a unit square for convenience. It's easy to think of a coordinate as being a percentage of the screen.  So if you want a line all the way across the screen it needs to start at x=0 and end at x=1.0.  A circle with a radius of 0.5 would have a diameter of 1.0, or 100% of the window width, etc.   You can use setCanvasSize(int width, int height) Links to an external site. to set it to specific sizes if you don't like 0-1. 

 

Q. I checked the course support package and find out that in the StdDraw class, there's no way to draw a triangle directly. If I add one to it, is that possible to use it directly instead of drawing three lines?

A. Use the polygon() or filledPolygon() method.  You could also create a new method/function that draws a particular type of triangle, like an equilateral triangle, with a particular size of side and center.   In general you shouldn't modify existing libraries that we provide, but you can create useful methods in other files. 

 

Q. Is there any other way to make a triangle?

A. The <a target="_blank">polygon()</a>and filledPolygon() methods allow one to draw triangles from three coordinates (as well as other more complex shapes).  Having another "triangle()" method that required three coordinates (and only three coordinates) would be a little redundant.

 

Q. What's an API?

A. An API is an "Application Programming Interface".  It's just a clearly defined way of interacting with a collection of pre-written code. APIs specify both how to interact with the code and what behavior is expected from these interactions. Here's a non-computing example:  A steering wheel is a type of interface.  It provides a "common way of interacting" and "expected behavior".  We know that turning the steering wheel a particular way will cause the vehicle (car, truck , riding lawn mower, etc.) to turn as well. We don't need to know how it works (rack and pinion Links to an external site. vs. power steering Links to an external site.), we just need an understanding of how we are expected to interact (turn rather than push/pull) and the expected behavior (turning the corresponding direction).  

 

We've been using APIs all semester.  For example, we've been making extensive use of the "println(String x) Links to an external site." method of the PrintStream Links to an external site. class when we do things like System.out.println("Hello, world").   Note that the method name, "println", is moderately descriptive of what it will do.  It will be provided a String and it will print it (and a move to the next "line").  The method name (println) and it's argument type (String) are part of an API.  The expected behavior is as well. 

APIs are one of the main reasons an individual can write a sophisticated program.  Other people have designed generic code that solves common problems and we leverage that work to facilitate our needs.  

Q. Do people tend to use a few popular libraries for the kinds of simple graphics and I/O things we've been doing?

A. There are many libraries, but some are more popular than others.  Java itself comes with libraries for many common uses of Java programs.  There are also a variety of outside libraries that are useful for particular niches.  For example, the Java Simple Serial Connector Links to an external site. (jSSC) library is commonly used for communicating with simple devices (it's used in CSE132) and both JFreeChart Links to an external site. and XChart Links to an external site. are popular libraries for creating graphs and charts. 

Q. Do touch-screen devices use sensory detectors that allows a touch to act like a mouse being pressed in Java? How does it work?

A. It depends on the Hardware and APIs.  Primitive touch screen devices can only detect a single point of contact at a time and may have an API somewhat like that used in StdDraw. Modern touch-based devices, like most mobile phones, support multi-touch and can often provide a lot more information.  The API and how it's used depend on the platform.  Usually mobile apps are based on "Event Driven Programming Links to an external site.", which is a different way of thinking about computer code.  In Event Driven Programming you think about what code should run in response to an event rather than repeatedly checking for an event.  For example, rather than using loops to check for a mouse being clicked (with an if-statement whose body indicates what to do when it is clicked), you focus on just the "what to do code" and configure it to automatically run in response to a mouse being clicked (no loop and no if-statement).  StdDrawdoes allow you to experience this sort of programming via the "Listener" interfaces, but to use this you will probably need a little more experience writing classes and using Objects, the topic of Module 7. 

Q.  If I were to code outside of this laptop, could I still use Sedgewick's API?

A. Yes, you can use Sedgewick's API on any computer if you install it.   Clark University's CS121 has a nice set of setup instructions for Eclipse: https://mathcs.clarku.edu/~cs121/exercises/week1/ConfigJavaIDE.html Links to an external site. (You wouldn't need to include the algs4.jar). 

Q. What's the weather?

A. According to dictionary.com Links to an external site. weather is "the state of the atmosphere with respect to wind, temperature, cloudiness, moisture, pressure, etc." (and, of course, weather is not the same as climate Links to an external site.).   

Q. So how does one go from this to minecraft? :^)

A. Objects and "Object Oriented Programming", the topic of Module 7, is one of the major advances in the design of computer programs that allow us to write complex software like Minecraft.   Stick with us...we won't get all the way there, but we'll get close.  Also, you may be interested in taking "Elements of Computing Systems" (CSE 365S) the next time it's offered.  It's also called the "Nand to Tetris" course.  It works through all the details from binary numbers through an implementation of Tetris.  

Q. Is Sedgewick's API the most commonly used API? Are there better graphics APIs in Java for creating data visualizations?

A. No, Sedgewick's API is designed as a bridge to allow students in introductory classes to do cool things (but at the sacrifice of some flexibility and power).  There are definitely much more powerful APIs for graphics. See the above question about other graphics APIs.

Q. What is the difference between Color.BLUE and Color.blue?

A. Try and see.  Do a "System.out.println(Color.BLUE.equals(Color.blue));"  (And also "System.out.println(Color.BLUE==Color.blue);")

Q. ...... how do computers work?

A. Take our "Machines" and "Systems" courses (ending in an "M" or "S") and we'll be happy to show you!   In particular: CSE260M, 362M (those two are largely about hardware aspects), 361S, and 422S (those two are largely about operating systems and systems software).  Or for a single course that gives a pretty good overview take CSE 365S the next time it's offered.  It's also called the "Nand to Tetris" course and gives an overview of building a computer from really primitive parts (NAND gates) up to modern concepts needed for games, like Tetris.  

Q. How can I analyze a website rather than a document stored in the computer?

A. See Extension 4.8 (Web Scraper) and Extension 4.9 (Stock Scraper).  Sedgewick's Library also provides an "In Links to an external site." object that can be used to read in from a URL. 

Q. How do I import Excel into Java?  

A. The easiest way is to export Excel files into the "Comma Separated Value" (CSV) format (see here Links to an external site.). Then you can read in the CSV file, as in Extension 4.11 (Gambler's Ruin with a CSV file).