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

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Session 3 Algorithm. Algorithm Algorithm is a set of steps that are performed to solve a problem. The example below describes an algorithm Example Check.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
Object-Oriented Design Running Time Recursion - Ed. 2 and 3.: Chapter 2, 3 - Ed. 4: Chapter 2, 3, 4.
Computer Programming Lab(4).
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
BUILDING JAVA PROGRAMS CHAPTER 1 ERRORS. 22 OBJECTIVES Recognize different errors that Java uses and how to fix them.
JAVA COURSE LESSON2 BY OMPUTER ENGINEEING ASSOCIATION.
Java Generics Compiled from Core Java Technologies Tech Tips By Billy B. L. Lim.
Mixing integer and floating point numbers in an arithmetic operation.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Output Programs These slides will present a variety of small programs. Each program has some type of array that was introduced in this chapter. Our concern.
Java FilesOops - Mistake Java lingoSyntax
Wel come To Seminar On C#.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;
Object Oriented Programming Lecture 2: BallWorld.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Testing and Debugging UCT Department of Computer Science Computer Science 1015F Hussein Suleman March 2009.
Introduction of Java Fikri Fadlillah, S.T.
Modern Programming Tools And Techniques-I
Polymorphism.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Exercise Java programming
using System; namespace Demo01 { class Program
Compiling and Running a Java Program
CHAPTER 7 & 8 REVIEW QUESTIONS
Maha AlSaif Maryam AlQattan
Code Magnets problem for wk5
Testing and Exceptions
SELECTION STATEMENTS (1)
Computing Adjusted Quiz Total Score
March 29th Odds & Ends CS 239.
More inheritance, Abstract Classes and Interfaces
TO COMPLETE THE FOLLOWING:
LRobot Game.
Polymorphism.
An Introduction to Java – Part I, language basics
Exercise 11.1 Write a code fragment that performs the same function as the statement below without using the crash method Toolbox.crash(amount < 0,
METHOD OVERRIDING in JAVA
Assignment 7 User Defined Classes Part 2
CS110D Programming Language I
Method Overriding in Java
CS 180 Assignment 6 Arrays.
class PrintOnetoTen { public static void main(String args[]) {
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Developing Java Applications with NetBeans
Chapter 11 Inheritance and Polymorphism Part 1
Developing Java Applications with NetBeans
Debugging Exercise 00 Try compiling the code samples.
Methods/Functions.
Dasar-Dasar Pemrograman 2: Generics
ArrayLists Readings: 10.1 (pg. 559 – 571).
Data Structures and Algorithms 2/2561
CPSC 233 Tutorial 13 March 11/12th, 2015.
Presentation transcript:

Section 9: Ruby and Java 11-19-2015

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

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

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;

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

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;

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

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;

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

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?

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); for(int i = 0; i<a.length; i++) { System.out.print(a[i]); System.out.print(" "); } System.out.print("\n"); public static void test(Object[] c, int i) { c[i] = "clam"; } public static void test(String[] c, int i) { c[i] = "squid";

Java Code Exercise “oyster squid clam clam squid” 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); for(int i = 0; i<a.length; i++) { System.out.print(a[i]); System.out.print(" "); } System.out.print("\n"); “oyster squid clam clam squid” public static void test(Object[] c, int i) { c[i] = "clam"; } public static void test(String[] c, int i) { c[i] = "squid";

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"); test(a,1); test(b,2); for (String s : a) { System.out.print(s); System.out.print(" "); } System.out.print("\n"); public static void test(LinkedList<Object> c, int i) { c.add(i,"clam"); } public static void test(LinkedList<String> c,

Java Code Exercise 3 Errors: Incompatible types Cannot apply Already 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"); test(a,1); test(b,2); for (String s : a) { System.out.print(s); System.out.print(" "); } System.out.print("\n"); 3 Errors: Incompatible types public static void test(LinkedList<Object> c, int i) { c.add(i,"clam"); } public static void test(LinkedList<String> c, Cannot apply Already defined

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");

Java Code Exercise “added an oyster” Runtime exception. 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"); “added an oyster” Runtime exception.

Questions?