740 likes | 871 Views
Chapter 5. Ch 1 – Introduction to Computers and Java. Defining Classes and Methods. Chapter 5. 5.1 Class and Method Definitions 5.2 Information Hiding and Encapsulation 5.3 Objects and References 5.4 Graphics Supplement. 5.1 Class and Method Definitions.
E N D
Chapter 5 Ch 1 – Introduction toComputers and Java Defining Classes and Methods
Chapter 5 5.1 Class and Method Definitions 5.2 Information Hiding and Encapsulation 5.3 Objects and References 5.4 Graphics Supplement
Java is Object Oriented It can model any real world object
Object Oriented Programming deals with the creation of objects and their relationships and interactions
Start by defining the class instance variables instance methods Use a UML class diagram
Code the class definition public class Car { private Color bodyPaintColor; private int numberOfTires; public Color getPaintColor() { return bodyPaintColor; } // end getPaintColor() public void setPaintColor(Color color) { bodyPaintColor= color; } // end setPaintColor() public int getNumberOfTires() { return numberOfTires; } // end getNumberOfTires() public void setNumberOfTires(int tireCount) { numberOfTires = tireCount; } // end setNumberOfTires() } // end Car instance variables instance methods
An Object consists of data ... greenCar object's memoryfootprint
and operations that store and manage the data methods are shared by all car objects Class Car methods greenCar redCar
Each class should be in a separate file public class Car { // code omitted } // end Car public class Driver { // code omitted } // end Driver Car.java Driver.java
new Creates an instanceof a class Car myCar = new Car();
Recap • An object is an instance of class • Use new to create an object • Objects have data (instance variables) • Objects offer functionality (methods)
There are two types of methods Methods that do not return a value (void) System.out.println("println does not return"); and methods that do return a value int num = keyboard.nextInt();
Let's see how methods work First create the object Car myCar = new Car(); Defaultvalues myCar
You can then call a method to setan instance variable myCar.setNumberOfTires(4); Receiving Object myCar
or a get method to retrieve an instance variable 4 int tireCount = myCar.getNumberOfTires() myCar
this Demystified Since each method is shared by all the objects, we need to be able to identify the receiving object. thisrefers to the receiving object, implicitly. public int getNumberOfTires() { return this.numberOfTires; } // end getNumberOfTires() public void setNumberOfTires(int tireCount) { this.numberOfTires = tireCount; } // end setNumberOfTires()
void Method Definition Parameter list can be empty or list parameters needed Method is accessible by defining class and any other class public void setNumberOfTires(int tireCount) { numberOfTires = tireCount; } // end setNumberOfTires() Instance Variable
return Method Definition return type of int Parameter list can be empty or list parameters needed public int getNumberOfTires() { return numberOfTires; } // end getNumberOfTires() Instance Variable
Recap • Methods expose a class's functionality • Call a method on a receiving object • this identifies the receiving object inside the method's definition • Each class is stored in its own .java file
Local variables are defined within a method public double updateSumAmount(double amount) { double newSumAmount += amount; return newSumAmount; } // end updateSumAmount() local variable
Methods can definesame name local variables public void method1() { double someDouble = 0; // Code omitted } // end method1() local to method1 public void method2() { double someDouble = 0; // Code omitted } // end method2() local to method2
A method should hide how it is implemented I know what the method does, just not how!
Methods can be public These define the class's interface
or private These are part of the implementation
Instance Variables are private They define the implementation
Accessor methods controlaccess to instance variables Getters retrieve instance variables Setters set instance variables
Recap • Local variables are defined within a method • Know "what" a method does, not "how" it does it • Public methods define the class's interface • Private instance variables/methods are part of the implementation
Application Deconstructed<Fraction.java> package fractiondemo; public class Fraction { private int numerator; private int denominator; private void reduce() { int u = numerator; int v = denominator; int temp; while (v != 0) { temp = u % v; u = v; v = temp; }// end while numerator /= u; denominator /= u; }// end reduce()
Application Deconstructed<Fraction.java> public int getNumerator() { return numerator; }// end getNumerator() public void setNumerator(int n) { setNumeratorAndDenominator(n, denominator); }// end setNumerator() public int getDenominator() { return denominator; }// end getDenominator() public void setDenominator(int d) { setNumeratorAndDenominator(numerator, d); }// end setDenominator()
Application Deconstructed<Fraction.java> public void setNumeratorAndDenominator(int n, int d) { numerator = n; if (d == 0) { System.err.println("ERROR: Invalid parameter (" + d + ") in setNumeratorAndDenonimator"); System.exit(1); } else { denominator = d; }// end if }// end setNumeratorAndDenominator() public Fraction add(Fraction f) { Fraction sum = new Fraction(); sum.setNumeratorAndDenominator(numerator * f.denominator + denominator * f.numerator, denominator * f.denominator); sum.reduce(); return sum; }// end add()
Application Deconstructed<Fraction.java> public Fraction subtract(Fraction f) { Fraction difference = new Fraction(); difference.setNumeratorAndDenominator( numerator * f.denominator - denominator * f.numerator, denominator * f.denominator); difference.reduce(); return difference; }// end subtract() public Fraction multiply(Fraction f) { Fraction product = new Fraction(); product.setNumeratorAndDenominator( numerator * f.numerator, denominator * f.denominator); product.reduce(); return product; }// end multiply()
Application Deconstructed<Fraction.java> public Fraction divide(Fraction f) { Fraction division = new Fraction(); division.setNumeratorAndDenominator( numerator * f.denominator, denominator * f.numerator); division.reduce(); return division; }// end divide() public void show() { System.out.print("(" + numerator + " / " + denominator + ")"); }// end show() }// end Fraction()
Application Deconstructed<FractionDemo.java> package fractiondemo; public class FractionDemo { public static void main(String[] args) { Fraction f1 = new Fraction(); Fraction f2 = new Fraction(); Fraction result = new Fraction(); // Set f1 to 1 / 4. f1.setNumeratorAndDenominator(1, 4); // Set f2 to 1 / 2. f2.setNumeratorAndDenominator(1, 2);
Application Deconstructed<FractionDemo.java> // Output their sum, difference, product and division. result = f1.add(f2); f1.show(); System.out.print(" + "); f2.show(); System.out.print(" = "); result.show(); System.out.println(); result = f1.subtract(f2); f1.show(); System.out.print(" - "); f2.show(); System.out.print(" = "); result.show(); System.out.println();
Application Deconstructed<FractionDemo.java> result = f1.multiply(f2); f1.show(); System.out.print(" * "); f2.show(); System.out.print(" = "); result.show(); System.out.println(); result = f1.divide(f2); f1.show(); System.out.print(" / "); f2.show(); System.out.print(" = "); result.show(); System.out.println(); }// end main() }// end FractionDemo
There are two types of variables Value: Stores the actual value 1 Reference:Stores a reference to the actual value 2
Value types store values int x = 100; x
Reference types store references Fraction f = new Fraction(); f 2040
Lets compare value types int x = 100; int y = 200; x y x y 100 200 200 200 x == y ? false x = y; x == y ? true
Now lets compare reference types numerator 1 denominator 2 Fraction f1 = new Fraction(); f1.setNumeratorAndDenominator(1,2); f1 200 200 numerator 1 denominator 2 Fraction f2 = new Fraction(); f2.setNumeratorAndDenominator(1,2); 208 f2 208 f1 == f2 ? false f1 208 f1 = f2; numerator 1 denominator 2 208 f2 208 f1 == f2 ? true
The solution to the == problem? Define an equals method
Code Deconstructed<equals method> public boolean equals(Fraction f) { return this.numerator == f.numerator && this.denominator == f.denominator; }// end equals() Two fractions are equal if both their numerator and denominator values are the same.