Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Classes: A class is a concept of something Vehicle 4 wheels, seats, engine, windows, color…… accelerate, start, stop, turn, …............... attributes.

Similar presentations


Presentation on theme: "1 Classes: A class is a concept of something Vehicle 4 wheels, seats, engine, windows, color…… accelerate, start, stop, turn, …............... attributes."— Presentation transcript:

1 1 Classes: A class is a concept of something Vehicle 4 wheels, seats, engine, windows, color…… accelerate, start, stop, turn, …............... attributes methods Design a class is to determine what attributes & methods Method: behaviors, service, operation, and functionalities of the object. Field (attribute): the information, status, and properties of the object. 2/29/2016IT 275

2 2/29/2016IT 2752 Point_1D - x: double; + Point_1D(); + Point_1D(double) ; + distance(): double; + distance(Point_1D): double; + get():double; + set(double):void; Services, methods Attribute (instance variable) UML class diagram for Point_1D Class name Constructor Default Constructor private public

3 3 Class Basic in Java 2/29/2016IT 275 public class Point_1D { private double x; public Point_1D() { this.x = 0;} public Point_1D(double x) { this.x = x;} public double distance() { return Math.abs(x); } public double distance(Point_1D p) { return Math.abs(x - p.x); } public double get() { return x; } public void set(double x) { this.x = x;} } Point_1D.java

4 2/29/2016IT 2754 Instantiate ( new ) an object import javax.swing.JOptionPane; public class TestPoints { public static void show(String message) { JOptionPane.showMessageDialog(null, message); } public static void main(String arg[]) { Point_1D p1,p2; String message; p1 = new Point_1D(70); p2 = new Point_1D(37); Message = "p1:"+p1.get()+"\np2:"+p2.get()+ "\n two points are "+ p1.distance(p2)+" apart."; Show(message); }

5 2/29/2016IT 2755 Temperature - Scale: char; - d: Point_1D; + Temperature(); + Temperature (double) ; + Temperature(double, char); + difference(Temperature): String; + get(): double; + set(double): void; + read(): String; Services, methods Attributes (instance variables) UML class diagram for Temperature Class name Constructor Default Constructor

6 6 Class Basic in Java 2/29/2016IT 275 public class Temperature { private Point_1D d; private char Scale; // 'C' denotes Celsius,‘F’ Fahrenheit public Temperature (){ // S can be 'F' or 'C'; this.d = new Point_1D(0); Scale = 'F'; // this is a default } public Temperature (double d){ // S can be 'F' or 'C'; this.d = new Point_1D(d); Scale = 'F'; // this is a default } public Temperature (double d, char S){ // S can be 'F' or 'C'; this.d = new Point_1D(d); if (S == 'F' || S == 'C') Scale = S; else Scale = 'F'; // this is a default } ………… Temperature.java

7 7 Class Basic in Java 2/29/2016IT 275 public class Temperature { …… public double difference (Temperature t) { if (Scale == t.Scale) return d.distance(t.d); if (Scale == 'F') return Math.abs(d.get()- (t.d.get()*8.0/5.0+32)); return Math.abs(d.get()-(t.d.get()-32.0)*5.0/8.0); } public String readDifference (Temperature t) { String s; if (Scale == t.Scale) s = “ “+ Scale; else if (Scale == 'F') s = “ F“; else s = “ C“; return difference(t)+s; } public double get() { return d.get(); } public void set(double t) { d.set(t); } public String read() { return d.get()+" "+Scale; } Temperature.java (continued)

8 8 Inheritance: Derived class Base class i.e., All vehicle can do, SUV can too, and more The SUV has every properties and function of the Vehicle, and more 2/29/2016IT 275 too Venn Diagram from functionality point of view If a class is not enough to meet the new need, we can extend it to a richer one based on the old one. The new one inherits every thing from the old one SUV Vehicle

9 2/29/2016IT 2759 Point_1D - x: double; + Point_1D(); + Point_1D(double) ; + distance(): double; + distance(Point_1D): double; + get():double; + set(double):void; UML class diagram for Point_1D and Point_2D Point_2D - y: double; + Point_2D(); + Point_2D(double, double) ; + getX():double; + getY():double; + setX(double):void; + setY(double):void; + set(double, double):void; + distance(): double; + distance(Point_2D): double; + area() + area(Point_2D);

10 2/29/2016IT 27510 public class Point_2D extends Point_1D{ private double y; public Point_2D() { this.y = 0; } public Point_2D(double x, double y) { super(x); this.y = y; } public double getX() { return get(); } public double getY() { return y; } public void setX(double x) { set(x); } public void setY(double y) { this.y=y; } public void set(double x, double y) { set(x); this.y=y; } … Point_2D.java

11 2/29/2016IT 27511 public class Point_2D extends Point_1D{ …………… public double distance() { double w = super.distance(); double h = Math.abs(y-0); return Math.sqrt(w*w+h*h); } public double distance(Point_2D p) { double w = super.distance(p); // p is a derived class double h = Math.abs(y-p.y); return Math.sqrt(w*w+h*h); } public double area() { double w = super.distance(); double h = Math.abs(y-0); return w*h; } public double area(Point_2D p) { double w = super.distance(p); // p is a derived class double h = Math.abs(y-p.y); return w*h; } Point_2D.java (continued)

12 12 Class Hierarchy: 2/29/2016IT 275 A SUV must be a vehicle, but a vehicle may not be a SUV Venn Diagram from membership point of view Vehicle TruckSedanSUV Derived class Base class SUV Vehicle A service that is designed for Vehicle is good for SUV, Truck, and Sedan

13 2/29/2016IT 27513 public class Point_2D extends Point_1D{ …………… public double distance() { double w = super.distance(); double h = Math.abs(y-0); return Math.sqrt(w*w+h*h); } public double distance(Point_2D p) { double w = super.distance(p); // p is a derived class double h = Math.abs(y-p.y); return Math.sqrt(w*w+h*h); } public double area() { double w = super.distance(); double h = Math.abs(y-0); return w*h; } public double area(Point_2D p) { double w = super.distance(p); // p is a derived class double h = Math.abs(y-p.y); return w*h; } Point_2D.java (continued)

14 14 UML (Unified Modeling Language) Diagram Vehicle TruckSedanSUV MatrixFocus Honda Pilot Matrix XRS 2/29/2016IT 275

15 15 Overloading: 2/29/2016IT 275 public class Point_2D extends Point_1D{ private double y; public Point_2D() { this.y = 0; } public Point_2D(double x, double y) { super(x); this.y = y; } ……… public void set(double x, double y) { set(x); this.y=y; } public class Point_1D { private double x; ……… public void set(double x) { this.x = x; } One method with more than one definitions; can be done in one class or between classes. Overloading

16 16 Overriding: 2/29/2016IT 275 public class Point_2D extends Point_1D{ private double y; ……… public double distance() { double w = super.distance(); double h = Math.abs(y-0); return Math.sqrt(w*w+h*h); } public double distance(Point_2D p) { double w = super.distance(p); double h = Math.abs(y-p.y); return Math.sqrt(w*w+h*h); } Redefined a method in the derived class. public class Point_1D { ……… public double distance() { return Math.abs(x); } public double distance(Point_1D p){ return Math.abs(x - p.x); } Overriding Overloading

17 17 Polymorphism // A is a base class public class A { private int my_a; Public A(int a) { my_a = a; } public string print_all(){ return “a: “ + my_a; } // B is a derived class public class B extends A { private int b; public B(int a, int b) { super(a); this.b = b; } public B(int a) { this(a,a); } public string print_all() { return super.print_all() + “b: “ + b; }............ A a; a = new B(2);..... a.print_all(); a = new A(2);..... a.print_all(); Dynamic polymorphism Dynamic binding 2/29/2016IT 275

18 2/29/2016IT 27518 O bject’s toString and equals methods Object + toString(): String + equals(o: Object): boolean the default method use identities to do the job. Now, we have a better idea about how this job to be done. Point_1D … … Point_2D … … Temperature … …

19 2/29/2016IT 27519 Point_1D@181afa3 Temperature@131f71a Point_2D@15601ea U sing the default toString and equals System.out.println(p1.toString()); System.out.println(t1.toString()); System.out.println(p3.toString()); Point_2D a = new Point_2D(3,5); Point_2D b = new Point_2D(3,5); if (a == b ) System.out.println("\na == b"); else System.out.println("\na != b"); a != b if (a.equals(b) ) System.out.println("\na == b"); else System.out.println("\na != b"); a != b

20 2/29/2016IT 27520 Point_1D:70.0 Point_2D:(4.0, 3.0) Temperature:32.0 F Overriding toString public class Point_1D { …… public String toString() { return "Point_1D:"+x; } public class Point_2D { …… public String toString() { return "Point_2D:("+get()+“,"+y+")“; } public class Temperature { …… public String read() { return d.get()+" "+Scale; } public String toString() { return "Temperature:"+read(); } p1 = new Point_1D(70); p2 = new Point_2D(4,3); T1 = new Temperature(32); System.out.println(p1.toString()); System.out.println(p3.toString()); System.out.println(t1.toString());

21 2/29/2016IT 27521 Overloading equals public class Point_1D { …… public boolean equals(Point_1D p) { return (p.x == x); } public class Point_2D { …… public boolean equals(Point_2D p) { return (y == p.y && super.equals(p)); } public class Temperature { …… public boolean equals(Temperature t) { return (difference(t) == 0.0); }

22 2/29/2016IT 27522 Test Overloaded equals Point_2D a = new Point_2D(3,5); Point_2D b = new Point_2D(3,5); Point_2D c = new Point_2D(5,3); System.out.print("\n"+a.toString()); if (a.equals(b)) System.out.println(" == "+b.toString()); else System.out.println(" != "+b.toString()); System.out.print(a.toString()); if (a.equals(c)) System.out.println(" == "+c.toString()); else System.out.println(" != "+c.toString()); t1 = new Temperature(32,'F'); t2 = new Temperature(0,'F'); t3 = new Temperature(0,'C'); System.out.print("\n"+t1.toString()); if (t1.equals(t2)) System.out.println(" == "+t2.toString()); else System.out.println(" != "+t2.toString()); System.out.print(t1.toString()); if (t1.equals(t3)) System.out.println(" == "+t3.toString()); else System.out.println(" != "+t3.toString()); Point_2D:(3.0, 5.0) == Point_2D:(3.0, 5.0) Point_2D:(3.0, 5.0) != Point_2D:(5.0, 3.0) Temperature:32.0 F != Temperature:0.0 F Temperature:32.0 F == Temperature:0.0 C

23 2/29/2016IT 27523 StringTokenizer class import java.util.StringTokenizer; public static void testStringTokenizer(String str) { StringTokenizer oneByOne; oneByOne = new StringTokenizer(str); while (oneByOne.hasMoreTokens()) System.out.println(oneByOne.nextToken()); } oneByOne = new StringTokenizer(str, “, “);


Download ppt "1 Classes: A class is a concept of something Vehicle 4 wheels, seats, engine, windows, color…… accelerate, start, stop, turn, …............... attributes."

Similar presentations


Ads by Google