1 / 9

100 likes | 237 Views

1. UML class diagram for Point_1D, Point_2D, and Temperature. One method with more than one definitions; can be done in one class or between classes. public class Point_2D extends Point_1D{ private double y ; public Point_2D() { this . y = 0; }

Download Presentation
## UML class diagram for Point_1D, Point_2D, and Temperature

**An Image/Link below is provided (as is) to download presentation**
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.
Content is provided to you AS IS for your information and personal use only.
Download presentation by click this link.
While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

**1**UML class diagram for Point_1D, Point_2D, and Temperature ITK 179**One method with more than one definitions; can be done in**one class or between classes. • publicclass Point_2D extends Point_1D{ • privatedoubley; • public Point_2D() { this.y = 0; } • public Point_2D(double x, double y) { • super(x); this.y = y; • } • ……… • ……… • ……… • ……… • publicvoid set(double x, double y) { • set(x); this.y=y; • } • } Overloading: • publicclass Point_1D { • privatedoublex; • ……… • ……… • publicvoid set(double x) { this.x = x; • } • } Overloading ITK 179**Redefined a method in the derived class.**• publicclass Point_2D extends Point_1D{ • privatedoubley; • ……… • ……… • publicdouble distance() { • double w = super.distance(); • double h = Math.abs(y-0); • return Math.sqrt(w*w+h*h); • } • publicdouble distance(Point_2D p) { • double w = super.distance(p); • double h = Math.abs(y-p.y); • return Math.sqrt(w*w+h*h); • } • } • publicclass Point_1D { • ……… • publicdouble distance() { • return Math.abs(x); } • publicdouble distance(Point_1D p){ • return Math.abs(x - p.x); • } • } Overriding Overriding: Overloading ITK 179**the default method**use identities to do the job. Object’s toString andequals methods • Point_1D@181afa3 • Temperature@131f71a • Point_2D@15601ea Now, we have a better idea about how this job to be done. ITK 179**Using the default toString andequals**• Point_1D@181afa3 • Temperature@131f71a • Point_2D@15601ea 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 But, we should have a better idea about how these job to be done. ITK 179**publicclass Point_1D {**• …… • publicString toString() { • return"Point_1D:"+x; • } • } Overriding toString 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()); • publicclass Point_2D { • …… • publicString toString() { • return"Point_2D:("+get()+“,"+y+")“; • } • } • Point_1D:70.0 • Point_2D:(4.0, 3.0) Temperature:32.0 F • publicclass Temperature { • …… • public String read() { • returnd.get()+" "+Scale; • } • public String toString() { • return"Temperature:"+read(); • } • } ITK 179**publicclass Point_1D {**• …… • publicboolean equals(Point_1D p) { • return (p.x == x); • } • } Overloading equals • publicclass Point_2D { • …… • publicboolean equals(Point_2D p) { • return (y == p.y && • super.equals(p)); • } • } • publicclass Temperature { • …… • publicboolean equals(Temperature t) { • return (difference(t) == 0.0); • } • } ITK 179**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()); Test Overloaded equals • 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 ITK 179**StringTokenizer class**• import java.util.StringTokenizer; • publicstaticvoid testStringTokenizer(String str) { • StringTokenizer oneByOne; • oneByOne = new StringTokenizer(str); • while (oneByOne.hasMoreTokens()) • System.out.println(oneByOne.nextToken()); • } • oneByOne = new StringTokenizer(str, “, “); ITK 179

More Related