CS 100Lecture 261 CS100J Lecture 26 n Previous Lecture –Application of inheritance –“Higher-order" methods –Abstract classes –Visibility modifier: protected.

Slides:



Advertisements
Similar presentations
Geometry in real life title.
Advertisements

Let’s review our shapes….
Triangles and Quadrilaterals
A Parade of Four-Sided Polygons Created By: 2BrokeTeachers
QUADRILATERALS Polygons with four sides and four angles.
PinpointingProperties. Trapezoids Make these trapezoids on your geoboard. How many sides? How many angles? Are any sides congruent? No sides are congruent.
My Polygon Book By: Mrs. Gage Square Rectangle Equalateral Triangle
Quadrilaterals Polygons with four sides. Types of Quadrilaterals  Square: Quadrilateral with four equal sides and four right angles (90 degrees) Indicates.
2 dimensional shapes and other geometry terms
The Quads Copy down all of the notes in RED.. A trapezoid has 4 sides and exactly 1 pair of parallel sides.
Geometry – Grade 5 2-D Shapes.
Two - Dimensional Shapes
Geometry and English Language Learning
Geometry Quadrilaterals. Geometry: Plane Shapes quadrilateral: any closed, four-sided shape.
2 pt 3 pt 4 pt 5pt 1 pt 2 pt 3 pt 4 pt 5 pt 1 pt 2pt 3 pt 4pt 5 pt 1pt 2pt 3 pt 4 pt 5 pt 1 pt 2 pt 3 pt 4pt 5 pt 1pt Slope and Distance Trapezoids What.
CS 100Lecture 241 CS100J Lecture 24 n Previous Lecture –MatLab demonstration n This Lecture –Inheritance –Method overriding –Polymorphism –Reading: n Lewis.
Polygons with 4 sides and 4 angles
Quadrilaterals.
Geometry The shape of things to come….
Quadrilateral Classification Tree.
Classifying Quadrilaterals
All about Shapes Ask Boffin!.
1 Inheritance and Subclasses Instructor: Mainak Chaudhuri
The mathematical study of the properties, measurements, and relationships of points, lines, planes, surfaces, angles, and solids. Geometry.
A parallelogram has opposite sides and opposite angles equal.
A. Slack. A parallelogram has opposite sides and opposite angles equal.
Quadrilaterals project Dimera wideman 1/16/13. Quadrilateral Quadrilateral just means "four sides" (quad means four, lateral means side). Any four-sided.
Polygons Lesson What is a polygon? A polygon is a simple, closed, two-dimensional figure formed by three or more line segments (sides). Closed?
2 pt 3 pt 4 pt 5pt 1 pt 2 pt 3 pt 4 pt 5 pt 1 pt 2pt 3 pt 4pt 5 pt 1pt 2pt 3 pt 4 pt 5 pt 1 pt 2 pt 3 pt 4pt 5 pt 1pt Angles Quadri- laterals Ratio and.
Quadrilateral Properties
A plane figure with four sides and four angles.
POLYGONS & QUADRILATERALS
Geometric Figures: Polygons.
Has 4 sides all side LENGTHS congruent  LENGTHS of opposite sides congruent  both opposite sides PARALLEL  all right ANGLES
Polygons with four sides
+ Introduction to Shapes Squares and Rectangles. + Do Now: Chalk Talk.
Types of Triangles and Quadrilaterals “I can draw and label 2-D figures.”
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
Polygons with four sides
Geometry. Line segment ray line Right angle Acute angle.
Definition Quadrilateral – a polygon with 4 angles and 4 straight sides.
SHAPES AND PATTERNS GRADE 3.
Geometry Shapes A-Sauce STARS MATH 6/29/2010. Quadrilaterals Is a polygon that has four sides and all angles add up to 360 o.
Lesson 6.3/6.4 Objective: To find the two missing lengths of a 30°- 60°- 90°triangle. To classify four sided polygons. In a 30°-60°-90° triangle, The hypotenuse.
Section 4.1: polygons.
A quadrilateral is any 2- dimensional, four- sided shape.
1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.
Parallel lines are always the same distance apart They go in the same direction They never meet.
Classifying Quadrilaterals Learning Target: I can classify quadrilaterals.
PLOYGONS!! By:edilberto zeferino!!. Right Triangle Equilateral Triangle.
9-3: Classifying Polygons Logic Activity. I am a trapezoid I have two angles that each measure 45˚ I have two other angles that each measure 135˚ #1)
Quadrilaterals Lesson 11 – 5. Quadrilateral Def: a 4 sided polygon. or ‘ a figure with 4 sides and 4 angles’ **The SUM of the 4 angles must equal 360º**
CLASSIFYING QUADRILATERALS OBJ: TO DEFINE AND CLASSIFY SPECIAL TYPES OF QUADRILATERALS.
Properties of Quadrilaterals
Classifying Quadrilaterals
Geometry Unit. Identify the following shapes Important Definitions O Parallelogram: a four sided plane with opposite parallel sides. O Trapezoid: a quadrilateral.
J.Byrne Types of Triangles pg 1  The sum of the angles in any triangle is 180°  Equilateral Triangle  3 sides and 3 angles are equal  Isosceles.
1 More About Derived Classes and Inheritance Chapter 9.
Triangles and Quadrilaterals
Geometry Shapes J.Byrne 2017.
Polygons with four sides
Triangles and Quadrilaterals
Exploring Polygons.
A Parade of Four-Sided Polygons
Polygons Quadrilaterals.
Polygons with four sides
Grade 4 - Identify Quadrilaterals
CS100J Lecture 14 Previous Lecture
Quadrilaterals 4 sided shapes.
Polygons with four sides
Presentation transcript:

CS 100Lecture 261 CS100J Lecture 26 n Previous Lecture –Application of inheritance –“Higher-order" methods –Abstract classes –Visibility modifier: protected n This Lecture –Interfaces –Comparable –Reflection –super –Reading: n Lewis & Loftus, re-read Section 5.4 n Savitch, Appendix 7

CS 100Lecture 262 Interfaces n Interface - Like an abstract class: –A collection of abstract methods that must be defined by any class that implements the interface. –Primitive types do not implement interfaces n Syntax of interfaces interface interface-name {list-of-method-signatures} n Extended syntax of classes class class-name 1 extends class-name 2 implements list-of-interface-names {... }

CS 100Lecture 263 Interface example: Comparable // Linearly ordered objects. interface Comparable { /* Compare this object with obj: - if this before obj, return negative, - if this equals obj, return 0, - if this after obj, return positive. */ public int compareTo(Object obj); } n The following predefined classes all implement the Comparable interface: –Byte –Character –Double –Float –Integer –Long –Short –String –java.math.BigDecimal –java.math.BigInteger –etc.

CS 100Lecture 264 An implementation of Comparable class Color implements Comparable { private int c; public Color(String s) { if (s.equals("red")) c = 0; else if (s.equals("white")) c = 1; else if (s.equals("blue")) c = 2; else throw new RuntimeException("bad color"); } public String toString() { if (c == 0) return "red"; else if (c == 1) return "white"; else return "blue"; } public int compareTo(Object obj) { return c-((Color)obj).c; }

CS 100Lecture 265 Recall the sort method /* Sort A into non-decreasing order. */ static void sort( int[] A ) { int m = A.length - 1; for ( int k = 0; k < m; k++ ) { /* Given that A[0..k-1] is finished, /* Given that A[0..k-1] is finished, finish A[0..k]. */ finish A[0..k]. */ int minLoc; // subscript of int minLoc; // subscript of // smallest in A[k..m] // smallest in A[k..m] /* Set minLoc so A[minLoc] is /* Set minLoc so A[minLoc] is smallest in A[k..m]. */ smallest in A[k..m]. */ minLoc = k; minLoc = k; for (int i=k+1; i <= m; i++) for (int i=k+1; i <= m; i++) if ( A[i] < A[minLoc] ) if ( A[i] < A[minLoc] ) minLoc = i; /* Exchange A[k] and A[minLoc] */ /* Exchange A[k] and A[minLoc] */{ int temp = A[k]; int temp = A[k]; A[k] = A[minLoc]; A[k] = A[minLoc]; A[minLoc] = temp; A[minLoc] = temp;}}}

CS 100Lecture 266 /* Sort A into non-decreasing order. */ static void sort( Comparable[] A ) { int m = A.length - 1; for ( int k = 0; k < m; k++ ) { /* Given that A[0..k-1] is finished, /* Given that A[0..k-1] is finished, finish A[0..k]. */ finish A[0..k]. */ int minLoc; // subscript of int minLoc; // subscript of // smallest in A[k..m] /* Set minLoc so A[minLoc] is /* Set minLoc so A[minLoc] is smallest in A[k..m]. */ smallest in A[k..m]. */ minLoc = k; minLoc = k; for (int i=k+1; i <= m; i++) for (int i=k+1; i <= m; i++) if (A[i].compareTo(A[minLoc])< 0 ) if (A[i].compareTo(A[minLoc])< 0 ) minLoc = i; /* Exchange A[k] and A[minLoc] */ /* Exchange A[k] and A[minLoc] */{ Comparable temp = A[k]; Comparable temp = A[k]; A[k] = A[minLoc]; A[k] = A[minLoc]; A[minLoc] = temp; A[minLoc] = temp;}}} A polymorphic sort method

CS 100Lecture 267 Sorting an array of Colors /* Print the elements of an arbitrary array of objects. */ static void printArray(Object[] A) {System.out.println(" "); for (int i=0; i < A.length; i++) System.out.println(A[i] + " "); System.out.println(" ");} Color[] C = { new Color("blue"), new Color("white"), new Color("red") }; sort(C); printArray(C); n Output: redwhiteblue

CS 100Lecture 268 Sorting an array of Integers Integer[] B = { new Integer(3), new Integer(1), new Integer(2) }; sort(B); printArray(B); n Output: n Note two kinds of polymorphism –Inheritance hierarchy In printArray(B), Integer is a subclass of Object In printArray(B), Integer is a subclass of Object –Interface implementations In Sort(B), Integer implements Comparable In Sort(B), Integer implements Comparable

CS 100Lecture 269 Taxonomy of Polygons Polygon Triangle Isosceles Equilateral Rhombus Square Trapezoid Parallelogram Quadrilateral

CS 100Lecture 2610 A taxonomy of Quadrilaterals Parallelogram Rhombus Square Rectangle Rhomboid: all sides equal length Paralleloid: opposite sides equal length Parallelopoid : opposite sides parallel

CS 100Lecture 2611 A different taxonomy of Quadrilaterals Parallelogram Rhombus Square Rectangle all angles equal opposite angles equal Parallelopoid : opposite sides parallel

CS 100Lecture 2612 Interface example: Shape // Objects with area and perimeter. interface Shape { public double area(); public double perimeter(); }

CS 100Lecture 2613 A class can implement multiple interfaces import java.lang.reflect.*; abstract class Parallelopoid implements Shape, Comparable { // Compare two shapes based on areas. public int compareTo(Object obj) public int compareTo(Object obj){ double d = area() -((Shape)obj).area(); double d = area() -((Shape)obj).area(); return (d 0) ? +1 : 0; return (d 0) ? +1 : 0;} public String toString() { return getClass().getName() + ":" + return getClass().getName() + ":" + " area: " + (float)area() + " perimeter: " + (float)perimeter(); }}

CS 100Lecture 2614 class Rhomboid abstract class Rhomboid extends Parallelopoid { double side; public double perimeter() { return 4*side; } Rhomboid(double side) { this.side = side; } } class Square extends Rhomboid { Square(double side) { super(side); } public double area() { return side*side; } } class Rhombus extends Rhomboid { double theta; Rhombus(double side, double theta) { super(side); this.theta = theta; } public double area() { return side*side*Math.sin(theta); } }

CS 100Lecture 2615 class Paralleloid abstract class Paralleloid extends Parallelopoid { double side1, side2; Paralleloid( double side1, double side2) { this.side1 = side1; this.side2 = side2; } public double perimeter() { return 2*(side1 + side2); } } class Rectangle extends Paralleloid { Rectangle(double side1, double side2) { super(side1, side2); } public double area() { return side1*side2; } } class Parallelogram extends Paralleloid { double theta; Parallelogram (double side1, (double side1, double side2, double theta ) { super(side1, side2); this.theta= theta; } public double area() { return side1 * side2 * Math.sin(theta); } }

CS 100Lecture 2616 Sorting an array of Parallelopoids Parallelopoid[] A = { new Square(1.0), new Rhombus(1.0, Math.PI/4), new Rectangle(1.0, 2.0), new Parallelogram(1.0, 2.0, Math.PI/4), }; sort(A); printArray(A); n Output: Rhombus: area: perimeter: 4.0 Square: area: 1.0 perimeter: 4.0 Parallelogram: area: perimeter: 6.0 Rectangle: area: 2.0 perimeter: