Week #1 Discussion Details

Welcome to Discussions

Please note discussion attendance to the section in which you are registered is mandatory. Please feel free to ask questions whenever you have them. During discussions, we will be reviewing course material and spend time working on solving problems together, similar to an open office hour environment.

Project Assignment 01

Continue to work on Project Assignment 01, see the Projects link on the menu.

Using a Linux Computer

Using a Text Editor

During lecture we have been using the BlueJ Environment to manipulate our Java programs. BlueJ will be installed in the CSE E113 discussion lab computers shortly.

In addition to BlueJ, you could use any plain text editor you are familiar with to edit your Java programs. Typical editors used in the Linux environment are vi and emacs. You may also choose to use a more powerful GUI-based editor such as eclipse.

Compilation

When using any text editor, you will need ensure that your programs work properly using the Sun Microsystems Java Compiler, since it is the compiler we will be using to test and grade your program solutions. Note, we have linked and used this compiler from BlueJ during lecture. As well, observe that you do not need to compile anything to complete the sample exercises given in this discussion and the Project Assignment 01.

Variable Declaration

Variables must be given a type and a name. Combining the declaration and initialization: type variableName = value; Always give variables descriptive names. Recall the naming scheme used when declaring a class versus a variable within a class. The class variable has the first letter of every word capitalized. A variable used within a class does not capitalize the first letter of the first word, but does capitalize the first letter of all the other words used in the variable.

Types

Thus far, we have examined the types: boolean, double, int, and String.

Example Declarations

Notice in each example, we are declaring more than one variable on the same line. Each variable declared is placed within a comma separated list. The type preceding the comma separated list defines the type of each variable in the list. The semicolon ends the line. In order to begin declaring variables again, a type must be listed, starting the next line.

boolean success, failure;
int number, sum;
double decimal;
String data, result;

Example Combined Declarations and Initializations

boolean open = true, closed = false;
int x = 4, y = 10;
double pi = 3.14;
String phrase = "Good Morning";

Operators

We have used three of the common mathematical operators: =, +, & -. The = performs assignment, assigning the value to the right of the equal sign to the variable on the left side of the equal sign. The + performs addition and concatenation operations. The - performs subtraction.

Classes

All java programs must be written within a class. Classes and the names given to them are used to categorize and give a general description of the program that is being written. As you have seen in the programs we have been creating, the convention is to capitalize the first letter of all key words within the class name. All classes have methods and attributes/data fields/properties associated with them. A method can be thought of as an action or behavior (verb) performed by the class. An property is a description or data item (noun) of the class. Recall our lecture discussion of the class Car, having properties: color, make, & model and methods: accelerate, start, & stop. When creating a class, recall the list of items (thus far) to be addressed:

Classes & Objects

Recall these definitions we have discussed:
Constructors

A constructor is a special kind of method. When an instance of an object is desired the constructor must be called in order to create the actual object that will be stored in memory. The constructor has the exact same name and spelling as the name of the class. It is declared to be public, but has no return type. The object created is what is returned, therefore no type is needed and Java handles returning the object created for us. The constructor can be passed parameters just like other methods.

Methods

A method is called/invoked upon an instance of a class. The method performs a specific operation associated with given class. Recall the method signature will list its accessibility (public or private), followed by if it returns a value (void or the return type), the name of the method, and finally a pair of parenthesis. If a parameter is passed to the method, this parameter will be listed within the parenthesis.

private

Private properties and methods of a class are not accessible outside of the class. Private properties can only be seen/modified within the class. Private methods can only be called from within the class.

public

Public properties and methods of a class are accessible anywhere the class is accessible. Public properties can be seen/modified from another class. Public methods can be called from another class.

Exercises

  1. Declare a variable of the types int, double, char, and boolean, using a different variable name than those shown in our examples. Initialize each variable to an arbitrary value at declaration.
  2. After declaration, assign a different value to each variable. Select arbitrary values for each assignment.
  3. Given the partially completed class CasinoCashier, implement the methods: getChips, setChips, exchangeCashIntoChips, and exchangeChipsIntoCash.
  4. Consider the class RationalNumber. What methods and properties would be included within this class? Practice declaring the properties of the class and provide the method signatures.
<<<< Prev     Next >>>>