- January 14th: Test.java and TestApplet.
- January 23rd: ReadingLine.java (reading any text), ReadingData.java (reading an
integer), ReadingData1.java (reading a
double), and computing cube of a number Power.java.
Lab assignment: 1. Modify the program ReadingData.java so that it reads two
integers and prints out their sum. A sample run of the program should
be like this:
Please enter an integer: 6
Please enter another integer: 7
The sum is: 13
2. Similarly modify the program ReadingData1.java so that it reads two
doubles and prints out their sum.
- January 26th. API's: String,
StringBuffer,
Programs: StringBufferTest.java,
StringTest.java.
- January 28th. Program: StringQuestion.java.
- February 2nd. Programs: IfElse.java, IceCream.java.
- February 4th. Programs: Minimum.java (exercise), Booleans.java, SwitchTest.java.
- February 6th. The lab will be in the Linux lab today. Here is a brief intro to Linux. The intro is
probably too brief, please ask questions if you get stuck.
Lab exercise:
- The method IndexOf of the class String returns the index of the
first occurrence of a given substring in the string or -1 if the
substring does not occur. For instance, if we define
String str = new String("coconut");
then
str.indexOf("nut")
returns 4,
str.indexOf("net")
returns -1.
Write a program that reads a string that the user enters and checks
whether the word "red" appears in the string. The program
will print out YES if the string appears and NO otherwise. Test for
lower-case occurrences only.
- The method compareTo of the class String compares two strings
alphabetically. More specifically, given two strings s1 and s2,
s1.compareTo(s2)
returns 0 if the two strings are the
same, a negative integer if s1 is before s2 in alphabetical order, and
a positive integer if s1 is after s2.
Part 1. Write a program that reads two words from the user and prints
them out in alphabetical order. Here are some sample runs of the
program:
Please enter a word: banana
Please enter another word: apple
apple comes before banana
Please enter a word: orange
Please enter another word: orange
You have typed orange twice
Part 2. Try your program on the words Zebra (with a capital Z) and
antelope (all lower case). Experiment with the program to figure out
how it behaves on lower-case and upper-case letters. Then fix the
problem by converting all the words to lower-case letters before
comparison.
- February 9th. Programs:
- February 11th. Programs: DoWhileInput.java,
For.java.
Exercises: compute a factorial using a for loop; count the number of
occurrences of a letter 'a' in a given string (see CharCounting.java for a
solution).
- February 13th. Programs: NestedForLoops.java.
Lab exercises:
- Debugging a program with an infinite loop: the following program Infinite.java goes into an infinite
loop. Run the program. To stop the program, press Ctrl-C (works
on Windows and UNIX machines, should work on a Mac as well).
Getting more information about the program: add a print statement in
the body of the loop to print out the values
of sum and n. Run the program again (it still goes into an infinite
loop, but now you should get more information about its behavior).
Correct the mistake. Run the program to make sure that it works
correctly.
Follow these debugging steps when you suspect (or know) that your
program goes into an infinite loop.
- Modify the NestedForLoops.java program to
draw triangles. The last
two pictures will be on the problem set (but start with the first
two!).
- February 16th. Programs: IntArray.java, BoolArray.java,
StringArray.java.
- February 18th. Exercises:
- write a program to print out an array of
integers backwards.
- write a program that reads integers from the user and stores them
in an array. The program prompts the user for the length of the array
and then reads the numbers. solution will be available
after the class.
- write a program that reads strings from the user input and stores
them in an array. As in the previous exercise, assume that the user
first enters the number of the strings.
- find the longest string in an array of strings. Start with the
program you have written for the previous question.
- circular shift: write a program that shifts all the elements of
an array of integers by 1 to the right. The last element must become
the first one. The following example
of switching the first and the last element might be helpful.
More programs: RandomElements.java. What's
wrong with the following program: RandomStringBuffers.java?
- February 20th. A brief intro to Java
vectors.
Programs: VectTest.java shows how
to create vectors and how to transfer their contents into an array;
Vect.java shows the difference
between the size and the capacity of a vector.
- February 23rd. Complex.java and
ComplexDriver.java
- February 25th Exercise: CDPlayerTest.java
- February 27th. Continue exercise from the previous class. Here is
where we stopped last time: CDPlayerTest.java
- March 5th. Finishing up the CD player example. Here is the result.
- March 15th. Welcome back after the spring break!
Static variables and methods: StaticCounter with a public
static variable, StaticCounter with a private
static variable, a class TemperatureCelsius which provides
static methods for computations with degrees Celsius.
Recursion: FactTest.java -- a
recursive function which computes a factorial; RecursiveMin.java finds the minimum
element in an array.
- March 17th. Another example on recursion: RecursivePrint.java
Inheritance.
- March 19th. An exercise on
inheritance.
An abstract class (a
"partially" implemented class)
An interface (a
"non-implemented class")
- March 22nd. Generic
programming example: Using interfaces to write general programs.
Type wrappers for int and double: Integer,
Double.
- March 24th. Java graphics. In this lab you will get experience
with reading Java APRs to be able to draw cool pictures (click here to see the code). Here is a text example, and its code.
Some APRs that you will need: Applet
and its superclass Component,
Graphics2D
and its superclass Graphics.
There may be other APIs that you will use, for instance if you want to
draw a polygon, you may use the method drawPolygon(Polygon p) of
Graphics, and therefore you will need the API Polygon.
- March 26th. Intro to swing. An example with just a label: FirstSwing.java.
SecondSwing.java: an example
with a label and two buttons. Can you change it into an example with
two labels and two buttons: one button increments one label, another
increments the other one?
- March 29th. Two-dimensional arrays: TwoD.java.
Problem: write a program to multiply
two matrices (see Problem 8 on p. 417 in the book for details). You
don't need to read the input data, just initialize the matrices in
your program. Here is
what we've got so far. It doesn't quite work -- Why?
- March 31st. We will look at other Swing elements. Try putting an
image on a label using the interface Icon
(actually, a class ImageIcon
will be helpful).
- April 5th. We will look at Tutorial
on event listeners,
Tutorial
on Layouts and Different
swing components.
- April 7th.
Your goal is to implement (and possibly extend) a class GUI which
behaves as demonstrated by running these classes: GUI.class and MyListSelectionListener.class.
Some helpful classes/interfaces are described in:
The array that you will be using can be copied from here:
private String [][] PersonInfo = {{"Alice", "apples", "anaconda"},
{"Bob", "bananas", "bat"},
{"Carol", "carrots", "camel"}};
The imports that you need are:
import java.awt.*;
import java.applet.*;
import javax.swing.*; // Using swing
import java.awt.event.*; // using listeners
import javax.swing.event.*;
- April 9th. Continue working on the example. Here are the new
files:
More helpful tutorials and classes:
And now, finally, the
code!
- April 14. classes: Exception, a Tutorial on exceptions
Examples: ExceptionTest.java
- April 19th.
Our finished example on exception from last time (showing inheritance
of exceptions): ExceptionTest.java.
Reading from and writing to files. Reading from a file: FileRead.java, writing to a file:
FileWrite.java, and reading to a
file with the name specified by the user: FileReadTest.java.
- April 23rd. StringTokenizer
API, example StringTok.java.
Problem 1: using a string tokenizer, write a program that reads integers
(several on one line) from the standard input and stores them into an
array. The first piece of data is the number of integers that follow.
Problem 2: using a string tokenizer, read a number in the format
$100,000
and store its value as an integer.
- April 26th. Today we will talk about:
- April 28th. ReviewOne.java