CIS191-s12/Skills1

From MCIS Wiki

Jump to: navigation, search

We will cover the following this week:

  • Using Netbeans to edit and run programs
  • Writing a stand-alone program with a main method
  • Using System.out.println to print (sout) and using "+" to create strings for printing
  • Declaring variables and using primitive types such as int, double, boolean, and char and Placing an initial value in the declaration.
  • Arithmetic expression operators, including % (modulus)
  • Flow control: if statements, for loops
  • Building a simple GUI using buttons and labels
  • Adding variables to the GUI
  • Writing event handlers for buttons

Netbeans

There is an installer in the class pickup folder.

Once you start Netbeans:

  • File -> New Project. This gives you a list of various project types - choose Java / Java Application
  • Fill in a good project name. The folder containing the project should be on your S: drive if you use a school machine.
  • Check the "create main class" and "set as main project", then click "Finish"
  • The generated project has a method named "main" - inside this add "System.out.println("Hello World");"
  • Click on the green arrow to run your project.


Console Programs

A console program is one without a GUI - in Netbeans, it prints to the "output" window.

Here are some things to watch out for:

  • You can't reference variables or methods in your main class from the static main method unless the variables are also marked static. This usually leads to problems. When doing console programming, either use a small program that is entirely contained in the main method or have the main method create an object representing the application. For now, we will keep inside the main method.
  • When editing your program, use Source -> Format to keep the indentation correct
  • When typing a ".", you'll see that NetBeans gives you a list of completions. Use this rather that possibly misspelling a method name.
  • A little lightbulb will occasionally appear to help you fix problems in your code. Be careful about this - sometimes it does things that don't help.
  • There are buttons to comment / uncomment code - these are very useful!
  • You can change the name of a variable by right-clicking and then selecting refactor -> rename - this changes all references to the variable at once


Printing

Netbeans makes it easy to print - if you type sout<tab> then it will fill in System.out.println(""); for you.

If you use System.out.print instead of println the next print will continue on the same line.

Every Java object has a "toString" method - this is implicit in the call to println.

You can string together string constants and variables - for example,

System.out.println("x = " + x);

Of course this assumes that x is a variable in your program.

A more complex but powerful formatting capability is described at http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html - for example

System.out.format("x = %d, y = %d", x, y);

If you want to print multiple lines in one print, use \n to insert a line break:

System.out.println("x = " + x + "\ny = " + y);

Know how to turn an arbitrary object into a string using x.toString() (or take a shortcut: x + "")

Variable Declarations and Scoping

In Java, every variable must be declared to be a member of a given class. In a declaration such as

int i, j;

the "int" names a class while i and j are variables which hold values that belong to the class. This is referred to as a "declarator".

In Java, there is a simple convention for class names: names beginning with lower case letters are used for primitive types - you should know int, boolean, double, and char. Classes that start with an upper case letter are non-primitive. You should know about String and you will see many more in the Java libraries.

A declarator may include an initial value:

int x = 2;

Netbeans will point out variables that are not used after declaration using a grey underline.

You can put a declarator anywhere in a method - the variable will be in scope in statements following the declarator but not in an outer block. For example,

 for (int x = 1; x < 10; x++) {
    int y = x*x;  }
 // y is no longer available outside the braces

A class declares instance variables in the same way except that you'll see a public / private in front of the declarator. These variables are scoped throughout the class.

Arithmetic Expressions

Know the precedence of infix operators (+, -, *, /, and %). Be able to transcribe mathematical notation into Java.

You MUST KNOW about difficulties caused by integer division! For example,

double x = 1/2; // Sets y to 0 instead of .5 because 1 and 2 are integers.  1.0/2 would work fine.

Know how to use % (mod) to make a resetting counter:

i = (i+1)%10;  // Goes from 0 to 9 and then back to 0

Watch out - % is not really modulus in the math sense. -1 mod 2 should be 1 but it's really -1 which is really broken!

Know about the following functions in the Math library: Math.min, Math.max, Math.abs, Math.sqrt

Know the boolean comparison functions: <, >, <=, >=, ==, !=

Know the boolean conjunctions: && (and), || (or), and negation: !

Know how to convert between int and double.

Know that x is divisible by if x mod y = 0

Flow Control

Know your if statements, while loops, and for loops. Be able to use break and return in these loops.

Creating a GUI-Based project

Create a Java project as previously described but do not check the box "create Main class".

Create a package whose name is appropriate for your project (easiest way is to right-click the Source Packages and then do "New Package").

Under "New File" look in "Swing GUI Forms" and select "JFrame Form". Make sure it is in the package you just created. By convention, you should call this Main.

Right click the project to get to the properties. Under Run, look for "Main Class" and set this to Main (the JFrame class you just created).

Note that Main.java has two views: one of the source code and one of the design. Changing one view also changes the other.

In design view you can add components to the project such as buttons, panels, labels, and so on. When you select a particular object, there is a properties panel which allows you to adjust the default values of its properties.

There are a few key places where you add code to the JFrame:

  • Add instance variables (variables used by the entire GUI) immediately after the class declaration
  • Place initialization logic in the constructor - just after the call to InitializeComponents.
  • Create event handlers (methods which are called on GUI events) in the project by creating new handlers. Handlers are created under the "events" part of the property window. Some have shortcuts - for example, the button click handler is created by double-clicking the button.
Personal tools