Lecture Notes, class 3 for csc211.
Prepared by: Anthony Larrain
These notes serve as an outline to the lecture, they are not intended to be complete. You should be reading the assigned sections. During class I may include or omit certain topics.
Recap Class 2
Recap Class 2Classes
Recall all Java programs are built from classes.In Java a class is used in several way's.
- To store a main method to launch an application.
- To create a data type from which objects are created.
- To store some static methods to be used by other classes.
- Some combination of the above.
- Servlets and Applets
Using a class to create Objects.
When you download the Java Development Kit, you also download a bunch of classes that you can use. These classes are called the Standard classes or the Java API .
The Java API documentation files
Later we will see how to design our own classes to produce objects but for now we work with some of the standard classes.
An object is comprised of
- data
- operations that interact with those data.
- The
String
Class - The
Date
class- The
Date
class is used to represent time instances. - Found in package
java.util
- A date object keeps track of the time it was created.
Date today = new Date() System.out.println(today.toString()); This will output depending on the day and time.
Wed Jan 05 11:30:45 CST 2005 - The
- The
SimpleDateFormat
class- SimpleDateFormat objects can modify the look of date objects
- Found in package
java.text
SimpleDateFormat sdf = new SimpleDateFormat("MM/DD/yy"); System.out.println(sdf.format(today)); // outputs 01/05/05 - The
DecimalFormat
class - An object that can be used to format decimals. IE convert 2.71828 to 2.72.
- Found in package
java.text
When creating an object we use the new
operator.
Typically, our program wants to work with these objects so we need a variable to reference the object.
But remember when we introduce a variable we have to declare it's type, the type in this case is a String.
Notice the similarity
Now the above String object provides us with some methods (Operations) that we can use (invoke) to interact with the string object. One such method is called length
. This method will tell us how many characters are in the String. To use it, we use the variable message along with the . dot operator.
Notice the parentheses, you always need these when invoking a method.
This method will return a value back to our program. For the length method this value is an integer that represents the number of characters in the String. Don't forget about the blank space. The blank space ' ' is a character.
We can do whatever we need to do with this value.
The String class provides many many services(Methods/Operation) that we can use. Another is called
charAt
. This method will return one of the characters in the String. Maybe we want to
see what character the String begins with. Notice each character occupies a position, so we have pass
this position number along with the method call.
This will return the character 'g' into our program.
In computer Science we typically start counting at 0.
In the above method call, the value 0 is called an argument .
Note String objects can and should be created using a special syntax.
To see what other methods are available to String clients, go to the API Documentation files. String class
Messages and Methods
- To instruct a class or an object to perform a task, we send a message to it.
- You can send a message only to the classes and objects that understand the message you sent to them.
- A class or an object must possess a matching method to be able to handle the received message.
- A method is a sequence of instructions that a class or an object follows to perform a task.
- A method defined for a class is called a class method.
- A method defined for an object is called an instance method.
- A value we pass to an object or class when sending a message is called an argument of the message. Example
"Enter temperature" and 0 are arguments
showInputDialog and charAt are methods
showInputDialog is a class method. Because we used the name of the class to call it.
charAt is an instance method. Because we used an object to call it.
More Features
- Each variable must be associated with a type.
int, double, String, DecimalFormat, Rectangle etc..
- Two classes of variables;
- Variables used to manage primitive data. integers, floats characters, boolean. These variables store the actual value of interest.
- Reference variables. These variables store a value which Java uses to locate an Object
DecimalFormat fmt = new DecimalFormat("0.00"); String name = "Ella"; Rectangle room = new Rectangle(30,50);
- Methods - A sequence of instructions that a class or an object follows to perform a task. The definition for the method is in a class.
Two types of methods.
- Static (or class) methods. Can use the name of the class to invoke.
int answer = Integer.parseInt("12"); String input = JOptionPane.showInputDialog("Enter name");
- Instance methods. Must use an object to call the method. With these types of the methods we are sending messages to a particular object.
String popStar = "Britney"; int length = popStar.length();
- Static (or class) methods. Can use the name of the class to invoke.
Classes and Objects
- Object-oriented programs use objects.
- An object is a thing, both tangible and intangible.
Account, Student, Car, Rectangle, String, Frame
etc. are examples of possible objects. - An object is comprised of data and operations that manipulate those data.
- Data - width,height.
- Operations - getWidth,getHeight,getPerimeter,getArea.
- Data - width, height, currentPosition, isVisible etc...
- Operations - setSize, setTitle, setVisible etc...
- Data - the characters that make up the String.
- Operations - length, charAt, substr
- For our programs to create and use an object, there must exist a definition. This definition is called a class .
Java provides many classes (standard classes) that you can use to design your programs.
JFrame, String, DecimalFormat
and many many more.Later you will write your own classes to use in your programs. Like Rectangle.
- An object is called an instance of a class.
- Important to distinguish between an object and class.
Consider a cookie cutter that when put in the dough will make a cookie in the shape of a star. The cookie cutter is the class, each star cookie is an object.
A TV is an object, the factory that makes tv's is the class.
- To use an object in a program, first we declare the type of object we will use, create the object and then send messages to it.
StringDemo Example
// Declares the type of object our program will use. String first,last; // Create two String objects first = "tony"; last = "larrain"; // Sending messages to each object. // Notice in this example each String object will answer us back. // For each operation we have to know what type of data the object will return int lengthOfFirst = first.length(); int lengthOfLast = last.length();
Rectangle Example
Rectangle room,square; room = new Rectangle(200,300); square = new Rectangle(20,20); int areaOfRoom = room.area(); int areaOfSquare = square.area();
For example a Rectangle
object.
For example a Frame
object.
For example a String
object.
Future Value Program
- Write a program to compute the future value of an investment compounded annually using the formula.
P * ( 1 + R / 100 ) ^ Y. Where P is the amount invested R is the annual rate Y is the number of years. Classes used
Here is a class diagram to show the relationship between the classes used in FutureValue program.
Program Flow
- Get the three input values, principle, rate, years.
- Parse them to the appropriate types
- Compute the future value
- Output the results.
Incremental Development
- Input values.
- Output
- Compute Future value.
Test Your Program