






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Variable: Stores either a primitive type or an object reference. Objects: Any object consists of: State: Behavior: Each individual object you create in your program is called an object instance. Class: A definition (blueprint) for an object. A class consists of: Instance variables: (also called fields) Class methods: new: (unlike primitive types) object instances are created by the “new” operator, which allocates space in the heap. Reference: Visibility:
Methods define an object’s behavior Method visibility: public: Accessible from both inside and outside the class private: Accessible from only inside the class Method types: Each method returns a value of a specified type or no value (void). Parameters: Static/Non-static Methods: Non-Static (the default): associated with a single instance Static: are not associated with any one instance, but are shared by all. Overloading: Having the same name, but different parameter types.
Variables come in two basic types: Instance variables: sit within a class, but not within any method. Local variables: (are not instance variables) They sit within a particular method. Visibility: (private, public) is the same as for methods. Static/Non-static Instance Variables: Non-Static (the default): associated with a single instance. Static: not associated with any one instance, but are shared by all instances.
Numerator: int numer; Denominator: int denom;
No-argument (default) constructor: Standard constructor: Integer-valued constructor: Copy constructor:
set( int n, int d )
public class Rational { private int numer; // numerator private int denom; // denominator private void set( int n, int d ) { numer = n; denom = d; } public Rational( ) { set( 0, 1 ); } public Rational( int n, int d ) { set( n, d ); } public Rational( int n ) { set( n, 1 ); } public Rational( Rational r ) { if ( r == null ) { // cannot initialize from null! System.out.println( "Illegal construction from null reference" ); System.exit( 0 ); } set( r.numer, r.denom ); } // … (rest of class omitted for now) }
Rational r0 = new Rational( ); Rational r1 = new Rational( 2, 5 ); Rational r2 = new Rational( 4, 10 ); Rational r3 = r1; Rational r4 = new Rational( r1 ); Rational r5 = null;
A constructor with no arguments (sometimes called a default constructor). It is a good idea to provide such a constructor, since a class user may not have a good initial setting for class known.
If you do not provide any constructor, Java provides one that sets:
public class Rational { // … (instance variables and constructors omitted)… public String toString( ) { String result = numer + "/" + denom + " ( " + doubleValue( ) + " )"; return result; } public int getNumerator( ) { return numer; } public int getDenominator( ) { return denom; } public double doubleValue( ) { return ( double ) numer / ( double ) denom; } public float floatValue( ) { return ( float ) numer / ( float ) denom; } public void setNumerator( int n ) { numer = n; } public void setDenominator( int d ) { denom = d; } public void setValue( int v ) { set( v, 1 ); } // … (rest of class omitted for now)… }
public static void main( String[ ] args ) { Rational u0; u0.setNumerator( 3 ); Rational u1 = null; u1.setNumerator( 3 ); Rational u2 = new Rational( ); u2.setNumerator( 2 ); u2.setDenominator( 3 ); System.out.println( u2 ); System.out.println( u2.toString( ) ); System.out.println( "float = " + u2.floatValue( ) ); System.out.println( "double = " + u2.doubleValue( ) ); u2.setDenominator( u2.getNumerator( ) ); }
Static multiplication of two arguments: Returns a new rational: Rational p = Rational.multiply( q, r ); // p = qr This method will call new to create a new Rational number and return its reference. Nonstatic equivalent of the above: Returns a new rational: Rational p = q.multiply( r ); // p = qr This behaves the same as the above method. But the style is little uglier, because it lacks symmetry. (We won’t do this.) Nonstatic multiplication, which modifies q: q.multiplyBy( r ); // q *= r No new object is created, and q is modified as a result. It is okay for this to be asymmetric, since only q is modified.
public class Rational { // … (previous stuff omitted)… public static Rational multiply( Rational q, Rational r ) { return new Rational( q.numer * r.numer, q.denom *r.denom ); } public void multiplyBy( Rational r ) { numer *= r.numer; denom *= r.denom; } // … (rest of class omitted for now)… } public static void main( String[ ] args ) { Rational s1 = new Rational( -3, 8 ); Rational s2 = new Rational( 2 ); Rational s3 = Rational.multiply( s1, s2 ); System.out.println( s1 ); System.out.println( s2 ); System.out.println( s3 ); s1.multiplyBy( s2 ); System.out.println( s1 ); }
Primitive types (int, float, double, …): Class Objects (String, Date, Rational, …):
Primitive type Wrapper byte Byte short Short int Integer long Long double Double char Character boolean Boolean
Constructor: Integer x = new Integer( 324 ); (no default constructor is provided) Max and min: Integer.MAX_VALUE largest positive int Integer.MIN_VALUE smallest negative int Conversions: byte b = x.byteValue( ); cast x to byte double d = x.doubleValue( ); cast x to double int i = x.intValue( ); return integer value … Convert string to int: int k = Integer.parseInt( “123” ); Convert int to string in various bases: String s1 = Integer.toBinaryString( 21 ); base 2 String s2 = Integer.toHexString( 21 ); base 16 String s3 = Integer.toOctalString( 21 ); base 8