Ruby and Java Programming Reflection Exercises

section 9 ruby and java n.w
1 / 17
Embed
Share

Dive into reflective exercises in Ruby and Java programming languages, exploring inheritance, subclassing, and generic types. Test your skills with hands-on coding challenges and understand the nuances of object-oriented programming through practical examples.

  • Ruby
  • Java
  • Programming
  • Reflection
  • Inheritance

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Section 9: Ruby and Java 11-19-2015

  2. Ruby Reflection Exercise class C1 class C2 < C1 What is the result of: def test include M1 1) c1.test "C1 test " + animal end 2) c2.test end class C3 < C1 3) c3.test include M1, M2 4) c4.test def animal end 5) C4.superclass "squid" 6) C4.ancestors end class C4 < C2 7) C4.class end def test 8) C4.class.class "C4 test " + super module M1 end def test "M1 test " + super def animal end "octopus" end end end module M2 def test c1 = C1.new "M2 test " + super c2 = C2.new end c3 = C3.new end c4 = C4.new

  3. Ruby Reflection Exercise class C1 class C2 < C1 What is the result of: def test include M1 1) c1.test C1 test squid "C1 test " + animal end 2) c2.test M1 test C1 test squid end class C3 < C1 include M1, M2 3) c3.test M1 test M2 test C1 test squid def animal end "squid" end class C4 < C2 4) c4.test "C4 test M1 test C1 test octopus" end def test "C4 test " + super 5) C4.superclass C2 module M1 end def test 6) C4.ancestors [C4, C2, M1, C1, Object, Kernel, BasicObject] "M1 test " + super def animal end "octopus" 7) C4.class Class end end end 8) C4.class.class Class module M2 def test c1 = C1.new "M2 test " + super c2 = C2.new end c3 = C3.new end c4 = C4.new

  4. Java Generics Exercise Does the following code compile correctly? Does it execute without errors? Hint: Ellipse2D.Double is a subclass of Ellipse Rectangle2D.Double is a subclass of Rectangle Ellipse2D and Rectangle2D are subclasses of RectangularShape RectangularShape r = new Rectangle2D.Double(0.0, 0.0, 50.0, 100.0); Ellipse2D[] a1 = new Ellipse2D[100]; RectangularShape[] a2; a2 = a1; a2[0] = r;

  5. Java Generics Exercise Does the following code compile correctly? Does it execute without errors? Hint: Ellipse2D.Double is a subclass of Ellipse Rectangle2D.Double is a subclass of Rectangle Ellipse2D and Rectangle2D are subclasses of RectangularShape RectangularShape r = new Rectangle2D.Double(0.0, 0.0, 50.0, 100.0); Ellipse2D[] a1 = new Ellipse2D[100]; RectangularShape[] a2; a2 = a1; a2[0] = r; Runtime Exception

  6. Java Generics Exercise Does the following code compile correctly? Does it execute without errors? Hint: Ellipse2D.Double is a subclass of Ellipse Rectangle2D.Double is a subclass of Rectangle Ellipse2D and Rectangle2D are subclasses of RectangularShape RectangularShape r = new Rectangle2D.Double(0.0, 0.0, 50.0, 100.0); Ellipse2D[] a1 = new Ellipse2D[100]; RectangularShape[] a2; a2 = a1; a1[0] = r;

  7. Java Generics Exercise Does the following code compile correctly? Does it execute without errors? Hint: Ellipse2D.Double is a subclass of Ellipse Rectangle2D.Double is a subclass of Rectangle Ellipse2D and Rectangle2D are subclasses of RectangularShape RectangularShape r = new Rectangle2D.Double(0.0, 0.0, 50.0, 100.0); Ellipse2D[] a1 = new Ellipse2D[100]; RectangularShape[] a2; a2 = a1; a1[0] = r; Compiletime Error

  8. Java Generics Exercise Does the following code compile correctly? Does it execute without errors? Hint: Ellipse2D.Double is a subclass of Ellipse Rectangle2D.Double is a subclass of Rectangle Ellipse2D and Rectangle2D are subclasses of RectangularShape RectangularShape r = new Rectangle2D.Double(0.0, 0.0, 50.0, 100.0); Rectangle2D[] a1 = new Rectangle2D[100]; RectangularShape[] a2; a2 = a1; a2[0] = r;

  9. Java Generics Exercise Does the following code compile correctly? Does it execute without errors? Hint: Ellipse2D.Double is a subclass of Ellipse Rectangle2D.Double is a subclass of Rectangle Ellipse2D and Rectangle2D are subclasses of RectangularShape RectangularShape r = new Rectangle2D.Double(0.0, 0.0, 50.0, 100.0); Rectangle2D[] a1 = new Rectangle2D[100]; RectangularShape[] a2; a2 = a1; a2[0] = r; Executes

  10. Java Code Exercise For each of the following: Does it compile? If not, which lines are responsible and what are the errors? What is the program output? Is there a runtime exception? If there is a runtime exception, what is the output up to that point?

  11. Java Code Exercise class Test1 { public static void main(String[] args) { String[] a; Object[] b; a = new String[5]; b = a; a[0] = "oyster"; test(a,1); test(b,2); test( (Object[]) a, 3); test( (String[]) b, 4); public static void test(Object[] c, int i) { for(int i = 0; i<a.length; i++) { c[i] = "clam"; System.out.print(a[i]); } System.out.print(" "); } public static void test(String[] c, int i) { System.out.print("\n"); c[i] = "squid"; } } }

  12. Java Code Exercise class Test1 { public static void main(String[] args) { String[] a; Object[] b; a = new String[5]; oyster squid clam clam squid b = a; a[0] = "oyster"; test(a,1); test(b,2); test( (Object[]) a, 3); test( (String[]) b, 4); public static void test(Object[] c, int i) { for(int i = 0; i<a.length; i++) { c[i] = "clam"; System.out.print(a[i]); } System.out.print(" "); } public static void test(String[] c, int i) { System.out.print("\n"); c[i] = "squid"; } } }

  13. Java Code Exercise import java.util.LinkedList; class Test2 { public static void main(String[] args) { LinkedList<String> a; LinkedList<Object> b; a = new LinkedList<String>(); b = a; a.addFirst("oyster"); public static void test(LinkedList<Object> c, test(a,1); int i) { test(b,2); c.add(i,"clam"); for (String s : a) { } System.out.print(s); System.out.print(" "); public static void test(LinkedList<String> c, } int i) { System.out.print("\n"); c.add(i,"clam"); } } }

  14. Java Code Exercise import java.util.LinkedList; class Test2 { 3 Errors: public static void main(String[] args) { LinkedList<String> a; LinkedList<Object> b; a = new LinkedList<String>(); Incompatible types b = a; a.addFirst("oyster"); public static void test(LinkedList<Object> c, test(a,1); Cannot apply int i) { test(b,2); c.add(i,"clam"); for (String s : a) { } System.out.print(s); System.out.print(" "); public static void test(LinkedList<String> c, } Already int i) { System.out.print("\n"); c.add(i,"clam"); defined } } }

  15. Java Code Exercise class Test3 { public static void main(String[] args) { String[] a; Object[] b; a = new String[2]; b = a; a[0] = "oyster"; System.out.println("added an oyster"); b[1] = new Integer(5); System.out.println("added an integer"); for(int i = 0; i<a.length; i++) { System.out.print(a[i]); System.out.print(" "); } System.out.print("\n"); } }

  16. Java Code Exercise class Test3 { public static void main(String[] args) { String[] a; Object[] b; a = new String[2]; b = a; a[0] = "oyster"; added an oyster System.out.println("added an oyster"); b[1] = new Integer(5); Runtime exception. System.out.println("added an integer"); for(int i = 0; i<a.length; i++) { System.out.print(a[i]); System.out.print(" "); } System.out.print("\n"); } }

  17. Questions?

Related


More Related Content