Computer Science 302, Section 20
Fall 2002
General Info
| Announcements
| Lectures
General Info
This is the homepage for CS302, Section 20. Click
here
for general information about CS302 (syllabus, grading, etc).
Class Days: |
Tuesday, Thursday |
Location: |
Noland Zoology Building, Room 342 |
Time: |
7:00PM - 8:15PM |
|
|
Instructor: |
Andy Hutcheson |
E-mail: |
ahutch@cs.wisc.edu |
Office: |
1306 Computer Science Building |
Phone Number: |
262-6601 |
Office Hours: |
Monday, Thursday 6pm - 7pm (and, usually 8am-10am weekdays) |
Lab Hours: |
Tuesday, Wednesday 8am - 9am |
Back to
top
Announcements
- The final exam will be in B371 Chemistry on Dec 15th at 2:45pm.
- There will be a review session Monday evening starting at 7:00PM. The location will be posted on the door of my office.
- The first exam will be in Room 1221 of the CS building. The 1200 section of the building is the southwestern part (towards the intersection of Randall and Dayton). The date, as previousl mentioned, is Wednesday, October 9th, from 7:15 to 9:15 PM. Remember to bring your ID and a pencil.
- Instructions on how to forward your CS email to a different account are here.
- To avoid conflicts with class the two midterm exams will be moved up one day to Wednesday. Thus the first midterm will be on October 9th from 7:15pm to 9:15pm and the second will be on November 13th from 7:15pm to 9:15pm. If you have conflicts with either of these times, or the final on December 15th from 2:45pm to 4:45pm, please let me know by September 12th.
Back to
top
Lectures
Week 9
Thursday
- String toString()
- the toString() method is defined in Object
- you many override toString() in your class
- the toString() method is called when an object variable is concatentated with a String
- Inheritance
- a subclass inherits the non-private methods and data members of its superclass
- a subclass can have only one superclass
- if no superclass is declared, Object is assumed
- non-public data members and methods that must be visible to subclasses should be declared protected
Tuesday
- char
- the last primitive data type (others are boolean, byte, short, int, long, float, double)
- treated as an int for purposes of arithmetic
- precision the same as short (16 bits) but is non-negative where short can be negative
- uses the UNICODE encoding scheme
- UNICODE is a superset of ASCII
- ASCII contains all the characters in common English usage (the only characters our computers can draw without special font packages)
- String
- String is not primitive - it is a class in the java.lang package
- String is the only non-primitive that has a literal form
- enclosing any sequence of characters in quotation marks creates a String literal
- a String is immutable - once created the characters that make up the String cannot be changed
- useful methods:
- int length()
- char charAt( int index )
- String substring( int startIndex, int endIndex) // remember - start inclusive, end exclusive
- boolean equals(String otherString)
- String Buffer
- a mutable version of the String class
- likewise in the java.lang package
- StringBuffer has all of the methods String has (plus more)
- additional useful methods:
- StringBuffer(String s) // construct a StringBuffer from a String
- void setCharAt(int index, char c)
- String toString()
Week 8
Tuesday
The Format class (from the javabook package)
A possible implementation of the centerAlign method -
public static String centerAlign(int width, long num) {
String result = "" + num;
//The minus one insures that we don't overshoot
while ( result.length() < width - 1 ) {
result = " " + result + " ";
}
//Arbitrarily add the odd space to the end if needed
if (result.length() < width ){
result = result + " ";
}
return result;
}
Week 7
Thursday
- while statement
- loop body happens repeatedly until the condition becomes false
- the condition is checked before the body is executed for the first time
- this makes while a pretest loop
- do-while statement
- loop body happens repeatedly until the condition becomes false
- the condition is checked after the body is executed for the first time
- this makes do-while a posttest loop
- remember to include a semicolon after the while part of the statement
- for statement
- in addition to a condition clause the for loop has initialization and increment clauses
- the initialization clause is the first thing executed when the loop starts
- the initialization clause only executes once
- the condition is checked before the body is executed for the first time
- this makes for a pretest loop
- the increment clause is executed after each execution of the body
Tuesday
- switch statements
- the "argument" to the switch must be of an integer type - byte, short, int, long, or char (we'll talk about char soon)
- the values given in the case statements must be constant (either named or literal)
- duplicate cases are illegal
- as soon as a matching case is found other case lines are ignored
- when a matching case is found the code sections will be executed until a break (or the end of the switch) is encountered
- the default case is an automatic match - thus if a default case is given it must be the last case
- ListBox
- Like InputBox and OutputBox, ListBox requires a owner window to be sent as an argument to the constructor
- Before the ListBox is shown items must be added using the addItem(String item) method
- Calling the getSelectedIndex() method makes the ListBox visible and waits for the user to respond
- The return value of the getSelectedIndex() method indicates which item the user chose - the first item you added is index 0, the second is index 1, etc.
- If no item was selected getSelectedIndex() returns ListBox.NO_SELECTION
- If the user hit the cancel button getSelectedIndex() returns ListBox.CANCEL
Exam #1
Week 4
Lecture 6
Topics Covered
- Constructors
- A constructor is a method that creates an object of the class it belongs to
- Though constructors are methods but they have the following special properties
- They cannot have a return type
- The must have the same name as the class (and capitalization matters)
- They cannot be declared static
- They are invoked with the word new instead of the usual class or object name followed by a period
- Scope of Variables
- The scope of a variable is the area of code, within a class, where a variable is usable
- The general rule is that a variable is usable anywhere inside the curly-braces that enclose its declaration
- An instance data member cannot be used by a static method (similarly an instance method cannot be called by a static method)
- Parameters are usable inside the method that defines them and only there
Week 3
Lecture 5
Topics Covered
- The Math class
- All of the methods of Math are static so you never need a Math object
- It is impossible to create a Math object - Math is non-instantiable
- Math.abs(x)
- Takes one numeric argument
- Return type same as argument type
- If the argument is non-negative returns the argument. Otherwise returns the negative of the argument
- Math.min(x,y) and Math.max(x,y)
- Given two argument of different precision these methods promote the lower precision argument and return a value in the higher precision
- min returns the smaller of the two numbers
- max returns the larger of the two numbers
- Math.pow(x,y)
- Returns a double no matter what the argument types are
- Returns x raised to the power y
- Math.random()
- Returns a real number in the range [0.0,1.0) (that is including 0.0 but excluding 1.0)
- For practical purposes the value returned as random and uniformly distributed
- Example : To simulate rolling a die we would write (int)(Math.random()*6) +1
- InputBox
- You must import InputBox from the javabook (or javabook2) package
- To create an InputBox object you must supply a MainWindow as an argument to the constructor
- Calling one of the get methods (getInteger,getDouble,getString) makes the InputBox visible
- The get methods can take String arguments. If an argument is given it is printed as a message in the InputBox
- OutputBox
- You must import OutputBox from the javabook (or javabook2) package
- To create an OutputBox object you must supply a MainWindow as an argument to the constructor
- Call the show() method to make an OutputBox object visible
- The print and printLine methods each require a String argument. The argument is printed in the OutputBox. If the printLine method was used the insertion point advances to the next line.
Lecture 4
Topics Covered
- Data Types
- Distinction between primitive and reference types
- Distinction between numeric primitives and others (char,boolean)
- Distinction between integral numerics and floating point numerics
- Integral type sizes (byte = 8 bits, short = 16, int = 32, long = 64)
- Floating point type sizes (float = 64 bits , double = 128)
- Reminder : a bit is the same thing as a binary digit - a one or a zero
- Caution : remember String is NOT primitive
- Literals
- A literal is a explicitly stated date value
- Written integers are literals of type int (examples: -10,0,1,2,3)
- Written real numbers are literals of type double (example: 5.6)
- A number followed by 'l' is a literal of type long (example: 1l)
- A number followed by 'f' is a literal of type float (example: 2f)
- Letters contained within quotation marks are a String literal (example: "hello")
- Strings are the only non-primitive type that have a literal form (all other reference types require constructors)
- Memory Diagrams
- Aritmetic
- When an integer type is divided by another integer type the remainder is discarded
- The remainder of an integer division can be found by using the modulus operator (%).
- When arithmetic is performed on two numbers of different precision the lower precision number is promoted to the higher precision and the result is in the higher precision (example : if we write (6 / 2l) the 6 is promoted to 5l and the result is 3l)
- The complete precision ordering is : byte < short < int < long < float < double
- Type Casting
- Writing a type in parentheses converts the number following to the type (example : (int) 5l is a complicated way of just writing 5)
- Casting from a high precision to a low precision simply throws away the high order bits. Example - byte b = (byte) 256; sets b to zero because the binary representation of 256 is 100000000.d
Week 2
Lecture 3
Topics Covered
- Data members
- A data member is written within a class
- A data member stores a piece of information
- By convention the name of a data member starts with a lowercase letter
- Like a method a data member can be class (static) or instance (non-static)
- A static data member is shared among all objects belonging to the class
- Each object belonging to a class gets a separate copy of each instance data member in the class
- By default data members should be made private to protect them from outside influence.
- Most data members are variable but it is possible, and sometimes useful, to make constant data members.
- Software Life Cycle
- Analysis
- Design
- Coding
- Testing
- Debugging
- Syntax
- Import statements
- Class declaration
- Data member declaration
- Method declaration
- Object creation
- Method invocation
- Local variable declaration
Week 1
Lecture 2
Topics Covered
- Classes and Objects
- When you write a program everything you write must be in a class
- Classes tell the computer how to create objects
- Objects created from a class are called instances of that class
- Instances of a class are said to belong to that class
- By convention class names begin with a capital letter
- By convention object names begin with a lowercase letter
- Methods
- A method is written within a class
- A method is a recipe - a list of instructions for accomplishing a task
- By convention method names begin with a lowercase letter
- A method can be either a class (static) method or an instance (non-static) method
- A method is used by sending a message to a class or object
- Class methods are called by sending a message to a class. EXAMPLE: Cow.giveAverageWeight() tells the class, Cow, to perform the instructions in the method, averageWeight()
- Instance methods are called by sending a message to an object. EXAMPLE: bessy.giveMilk() tells the object, bessy, to perform the instructions in the method, milk
- Using a method is called invoking, executing or calling the method
- A method can return something to the location it was invoked from. In the examples above the giveAverageWeight method would return a number while the giveMilk method would return a Milk object
- A method may return nothing. In this case it is called a void method or said to have a return type of void
- Information, called arguments, can be sent to a method. Arguments, if any are written in the parentheses following the method. The parentheses are required even if the method has no arguments, as in the examples above
- Inheritance
- Each class has exactly one parent class
- A child class is said to inherit from its parent class
- An ancestor of a class is called a superclass of its descendant
- A descendant of a class is called a subclass of its ancestor
- Inheritance means that the child has all of the methods of its parent
- The purpose of inheritance is to avoid repitition EXMAPLE The method openDoor applies to all cars so we define it in the Car class to avoid defining it in each of the subclasses of Car (StationWagon, SUV, etc).
- The Object class is the root of the inheritance tree. Anything class that doesn't specifically inherit from anything inherits from Object. Object has no parent. All classes are subclasses of Object.
Lecture 1
Topics Covered
- History of computer hardware
- Charles Babbage, Analytical Engine, clockwork
- Mark I, electro-mechanical
- 1st generation, ENIAC, vacuum tubes
- 2nd generation, transistors
- 3rd generation, mini-computers, integrated circuits
- 4th generation, micro-computers / personal computers, VLSI (Very Large Scale Integrated Circuits)
- Hierarchy of programming languages
- Machine language
- Language the processor understands
- Written in binary
- Assembly language
- Each statement corresponds to a function built into the processor (ADD, SUBTRACT, JUMP, etc)
- Translated into machine language by a program called an assembler
- High level languages
- Examples include Cobol, Fortran, C, C++ and Java
- A single statement corresponds to many assembly language statements
- Translated into assembly language by a program called a compiler
- Binary notation
- Why? Because it makes hardware design easier (faster, cheaper).
- Anything that can be written can be written in binary.
- Want to write a number in binary? Use the decimal to binary conversion procedure on pages 6-8. Here's the procedure written as a java program.
- Want to write a letter in binary? Convert it to a number using the ASCII code on page 357 and then use the number conversion above.
Back to
top