CSC152 2005S, Class 10: Object Basics
Admin:
* Yes, the first few weeks of Java are nasty.
Overview:
* Anything in Java you want reviewed?
* What is a class, revisited?
* Three key components: Fields, methods, constructors
* Three kinds of classes
* Standard methods
Questions on numbers
* java.lang.Math.sqrt expects a double as a parameter
* But I can write
float f = ...;
pen.println(Math.sqrt(f));
* Why?
* Java likes to "help" you by converting stuff to related types.
* Can I force my own conversion? Yes
* The incredibly explicit way: Build an object, use the xxxValue.
* E.g., from double, d to integer, i
i = (new Double(d)).intValue();
* E.g., from integer i to short s (assume both are declared)
s = (new Integer(i)).shortValue();
* Shorthand: Put the desired type in parens before the expression
* i = (int) d;
* s = (short) i;
* Why can't I write "s = s + s", assuming s is a declared short
* Whenever you add two non-long "integers" (int, short, byte), you get an int
* s = (new Integer(s + s)).shortValue()
* s = (short) (s + s);
* Why can't I write "float f = 2.0;"?
* Java assumes that when you write a "real number", you mean "double"
* Solution
float f = new Double(2.0).floatValue();
float f = (float) 2.0; * Suppose I declare an integer, i, and don't assign a value to i. What value will I get when I print i?
* Experimental answer: Try it and find out.
* "Theoretical" answer: It is illegal to write a program in which you use a value without assigning to it, so the compiler will choke.
* When can/should I use primitive int vs. class Integer?
* If you expect to call a method, like toString(), you need the object
* If you expect to use basic arithmetic method, such as +, you should have the primitive thing
* Java 1.5 (Java 5) will do some conversion when "appropriate"
Integer i = new Integer(5);
int j = i + 2;
The main subject of today's class: Classes
* Fundamental idea of object-oriented programming?
* Make objects that gather data and capabilities
* When we create eyes of class InputStream (or whatever), eyes is an object that has capabilities (readLine) and data (where it's reading from)
* When we create foo of class Integer, it has a value and things you can do with the value (e.g., convert to float)
* Programming is more interesting when you can design your own "classes" of objects
* How to define your own classes:
* Analysis:
* What is the purpose of this class?
* How do I represent the data in this class?
* What methods/capabilties should I provide?
* What information will allow me to create a new one of these things?
* What potential problems do I have?
* Sample analysis: Fraction
* Purposes: Better representation of decimals; Useful for my 4th grader.
* Representing data?
* Two ints or longs
* Two BigIntegers CHOICE
* One double
* One BigDecimal
* Methods?
* Arithmetic: add, subtract, multiply, divide, exponentiate, ...
* simplify
* Convert to other types
* What information do I need to create a new fraction?
* BigInteger numerator and denominator
Fraction f = new Fraction(new BigInteger(1), new BigInteger(2));
* double value that we want to "convert" (or represent as a fraction)
Fraction f = new Fraction(0.5);
* int numerator and denominator
Fraction f = new Fraction(1,2);
* String
Fraction f = new Fraction("1/2");
* Potential problem
* Do we permit negative denominators?
* Do we permit zero denominators (or how do we check for them)?
* Do we require "reduced" form or permit unreduced form?
* ...
Defining a class
PROTECTION class ClassName
{
// Fields (specify the kind of data)
// Constructors (specify how to build)
// Methods (specify the operations)
} // class ClassName
Defining fields
PROTECTION TYPE name;
Example
BigInteger numerator;
BigInteger denominator;
Bad example: "Everyone can directly access numerator and denominator"
public BigInteger numerator;
public BigInteger denominator;
Very cautious example: Only things in this class can access numerator and denominator
private BigInteger numerator;
private BigInteger denominator;
Traditional: Only things in this package
BigInteger numerator;
BigInteger denominator;
Referring to fields:
A field in the "current" object (prefix by "this")
this.fieldName
A field in another object (prefix by the name of the object)
other.fieldName
Defining methods:
PROTECTION ReturnType name(ParameterList)
{
INSTRUCTIONS
} // name(ParameterList)
Note:
For now, use "static" only for the main method
Example:
/**
* Express this fraction as a double.
*/
public double doubleValue()
{
return this.numerator.doubleValue() / this.denominator.doubleValue();
} // doubleValue()
/**
* Add the fraction other to this fraction.
*/
public Fraction add(Fraction addMe)
{
BigInteger resultNumerator;
BigInteger resultDenominator;
// The denominator of the result is the
// product of this object's denominator
// and addMe's denominator
resultDenominator = this.denominator.multiply(addMe.denominator);
// The numerator is more complicated
resultNumerator = (this.numerator.multiply(addMe.denominator)).add(addMe.numerator.multiply(this.denominator));
} // add(Fraction)