- Aggregation - UML diagram - Self-Referential Classes - Generisity
|
|
- Amber Hampton
- 6 years ago
- Views:
Transcription
1 - Aggregation - UML diagram - Self-Referential Classes - Generisity 1
2 Class Circle public class Circle private double xcenter; // x center coordinate private double ycenter; // y center coordinate private double radius; // circles radius public Circle(double xcenter, double ycenter, double radius ) this.xcenter = xcenter; this.ycenter = ycenter; this.radius = radius; } // class Circle constructor // rest methods... } // class Circle 2
3 Class Point public class Point private double x; // x coordinate private double y; // y coordinate public Point(double x, double y) this.x = x; this.y =y; } // class Point constructor public double getx( ) return this.x; } public void printpoint() System.out.println( "x = + this.x + "y = + this.y); } // printpoint() // rest methods } // class Point 3
4 (הכלה) Aggregation public class Circle private Point center; // another object! private double radius; // circles radius public Circle( Point center, double radius ) this.center = center; this.radius = radius; } // class Point constructor public Point getcenter() return this.center; } public double getradius() return this.radius; } // rest methods Aggregation is a relationship between two classes. The aggregate class contains a reference to another class. These methods use reference center to another object Point.
5 Aggregation example 1 Point p1 = new Point(7.4,9.5); // create new point Circle c1 = new Circle(p1,5.0); // create new circle : reference to object p1 c1 Circle (c1) center radius p1 5.0 Point (p1) x y p 1 5
6 Access to point coordinates public static void main(string[ ] args) Point p1 = new Point(7.4,9.5); // create new point Circle c1 = new Circle(p1,5.0); // create new circle Point c1center = c1.getcenter(); double c1centerx = c1center.getx(); We use getter methods double c1centery = c1center.gety(); from Point object double c1radius = c1.getradius(); System.out.println( x= + c1centerx+ y= +c1centery + r= + c1radius); // another choice c1centerx = c1.getcenter(). getx(); c1centery = c1.getcenter(). gety(); p1.printpoint(); } // main We use getter methods from Circle object This program generates the following output: x = 7.4 y = 9.5 r = 5.0 x = 7.4 y = 9.5 6
7 Rectangle definition Rectangle is any quadrilateral with four right angles. The word rectangle comes from the Latin rectangulus, which is a combination of rectus ( right ) and angulus ( angle ). y - axis topright y - axis width height bottomleft x - axis bottomleft x - axis 7
8 Class Rectangle public class Rectangle private Point bottomleft; private Point topright; public Rectangle (Point bottomleft, Point topright) this.bottomleft = bottomleft; this.topright = topright; } // constructor1 public Rectangle (Point bottomleft, double width, double height) this.bottomleft = new Point(bottomLeft); this.topright = new Point(bottomLeft.getX() + width, bottomleft.gety() + height); } // constructor2 public double getarea() double width = this.topright.getx() - this.bottomleft.getx(); double height = this.topright.gety() - this.bottomleft.gety(); return width * height; } // getarea 8
9 Class Rectangle, cont. public double getperimeter() double width = this.topright.getx() - this.bottomleft.getx(); double height = this.topright.gety() - this.bottomleft.gety(); return (2 * width) + (2 * height); } // getperimeter public void move (double deltax, double deltay) this.topright.setx(this.topright.getx() + deltax); this.topright.sety(this.topright.gety() + deltay); this.bottomleft.setx(this.bottomleft.getx() + deltax); this.bottomleft.sety(this.bottomleft.gety() + deltay); } // move public String tostring() return "Rectangle:" + "\n" + "bottom-left point : " + this.bottomleft.tostring() + "\n" + "top-right point : " + this.topright.tostring(); } // tostring } // class Rectangle 9
10 Class Rectangle, cont. public static void main(string[] args) Point p1 = new Point(2,1); Point p2 = new Point(7,5); Rectangle rect1 = new Rectangle(p1,p2); System.out.println(rect1); double width = 6.0; double height = 3.0; Rectangle rect2 = new Rectangle(p1,width,height); System.out.println(rect2); System.out.println("area = " + rect1.getarea()); System.out.println("perimeter = "+ rect2.getperimeter()); } // main Rectangle: bottom-left point : x = 2.0 y =1.0 top-right point : x = 7.0 y = 5.0 Rectangle: bottom-left point : x = 2.0 y =1.0 top-right point : x = 8.0 y = 4.0 area = 20.0 perimeter =
11 (הכמסה) Encapsulation One of the important object-oriented techniques is hiding the ( הסתרת מידע ) within the class, and making it available only through the methods. This technique is often known as encapsulation, because it seals the class's (and internal methods) safely inside the "capsule" of the class, where it can be accessed only by trusted users- i.e., by the methods of the class. The most important reason is to hide the internal implementation details of your class. If a variable is visible only in your class, you have to document it. 11
12 UML Diagram Example1 UML stands for Unified Modeling Language. It's a way to represent object-oriented applications using graphical diagrams. UML shows the classes of the system, their relationships, and the operations and attributes of the classes. Point public double x public double y Point(double x, double y) double getx() double gety() void setx(double x) void sety(double y) String tostring() Rectangle Point bottonleft Point topright Rectangle( Point bottonleft, Point topright) Rectangle( Point bottonleft, double width, double height) double getarea() double getperimeter() void move(double deltax, double deltay) String tostring() 12
13 UML Diagram Example2 UML diagrams allow us to denote the relationships between classes. In a UML diagram we can show the class member variables and class methods. Lecturer String name int depcode Lecturer(String name,int depcode) String getname() void setdepcode( int depcode) Student String name int ID Lecturer lecturer Student(String name, int ID, Lecturer lecturer) String getname() void setid( int ID) Lecturer getlecturer() 13
14 Aggregation example 2 Lecturer lect = new Lecturer( Alex, 777); Student s1 = new Student( David, , lect); Student s2 = new Student( Ronit, , lect); Student name ID lecturer David lect Student name ID lecturer Ronit lect s1 s2 lect Lecturer name depcode Alex
15 Array of objects Student [ ] arr = new Student [ 5 ]; arr arr[ ] null null null null null Lecturer lect = new Lecturer( Alex, 777); arr[0] = new Student( David, ,lect); arr[1] = new Student( Ronit, ,lect); arr[ ] arr[0] arr[1] null null null Student name ID lecturer David lect Student name ID lecturer Ronit lect 15
16 Insertion sort - reminder The insertion sort algorithm works by inserting each value into a previously sorted subset of the list is sorted. Shift nothing. Insert and 9 are sorted. Shift 9 to the right. Insert 6. 3,6 and 9 are sorted. Shift 9,6 and 3 to the right. Insert 1. 1,3,6 and 9 are sorted. Shift 9,6 and 3 to the right. Insert All values are sorted16
17 Sorting array of objects public class Student private String name; private int id; public Student( int id, String name) this.name = name; this.id = id; } // class Student constructor public int getid( ) return this.id; } // rest methods... } // class Student Insertion sort of integer array int [ ] arr = 3, 9, 6, 1, 2 }; for (int i = 1; i < arr.length; i++) int j = i; int a = arr[i]; while ( j > 0 && arr[j-1] > a) arr[j] = arr[j-1]; j--; } // block while arr[j] = a; } // block for How compare two object values? 17
18 Sorting array of objects,cont. public static void insertsort( Student [ ] arr) for (int i = 1; i < arr.length; i++) Student val = arr[i]; // reference to arr[i] int j = i; while( j>0 && ( arr[j-1].getid() > val.getid())) arr[j] = arr [j-1]; j--; } // block while arr[j] = val; } // block for } // insertsort We use only getter methods to access student ID value 18
19 Invoke insertsort method public static void main(string[ ] args) System.out.print("enter number of student "); int num = reader.nextint(); Student [ ] arr = new Student[num]; for( int i = 0; i < num; i++) System.out.print("enter student ID " ); int id = reader.nextint(); System.out.print("enter student name " ); We use getid() getter method String name = reader.next(); to access j student ID value arr[i] = new Student( id, name ); } // for insertsort(arr); // invoke insertsort method for( int j = 0; j < num; j++) System.out.println("studID = + arr[j].getid()); } // main 19
20 Class StudList - UML UML diagram to define class StudList StudList int MAX_STUD Student [ ] list int lastpos StudList() void addstud(string name) Student delstud(string name) Student getstud(string name) void printstudlist() Class name Class variables Constructor Class methods 20
21 Class StudList - implementation public class StudList public static final int MAX_STUD = 100; private Student [ ] list; // array of Student type private int lastpos; // last free position public StudList() this.list = new Student[MAX_STUD]; this.lastpos = 0; } // StudList class constructor public void addstud( Student st ) this.list[ this.lastpos ] = st; this.lastpos++; } // addstud This method adds new student to StudList class. 21
22 Class StudList implementation,cont. public Student delstud(string name) Student st = null; int i = 0; while( i < this.lastpos && this.list[i].getname().compareto(name)!= 0 ) i++; if(i < this.lastpos) st = this.list[i]; for( int k = i+1; k < this.lastpos; k++ ) this.list[k-1] = this.list[k]; this.lastpos--; this.list[this.lastpos] = null; } return st; } // delstud This method removes the student from StudList class and returns a reference to removed value. If the student does not exist in the class, the method returns null. 22
23 Class StudList implementation,cont. public Student getstud( String name ) Student st = null; int i = 0; while( i< this.lastpos && this.list[i].getname().compareto(name)!= 0 ) if(i < this.lastpos) st = this.list[i]; return st; } // getstud public void printstudlist() for( int i = 0; i< this.lastpos; i++ ) System.out.println( "ID = + this.list[i].getid() + " name = + this.list[i].getname()); } // printstudlist } // class StudList i++; This method returns a reference to a student if his name exists in the StudList class. If the student does not exist in the class, the method returns null. 23
24 Class StudList delsdud test public static void main(string[ ] args) System.out.print( Enter number of student " ); int num = reader.nextint( ); StudList stl = new StudList( ); for( int i = 0; i < num; i++) int id = reader.nextint(); String name = reader.next(); Student st = new Student(id,name); stl. addstud(st); } // for stl.printstudlist( ) ; System.out.print( Enter student name to delete " ); String nmd = reader.next( ) ; Student d = stl.delstud(nmd); if(d == null) System.out.println( Student not found!" ); else System.out.println( Student + nmd + delete" ); stl.printstudlist(); } // main 24
25 Class StudList getstud test public static void main(string[ ] args) System.out.print( Enter number of student " ); int num = reader.nextint( ); StudList stl= new StudList( ); for( int i = 0; i < num; i++) int id = reader.nextint(); String name = reader.next(); Student st= new Student( id,name); stl. addstud(st); } // for stl.printstudlist( ) ; System.out.print( Enter student name " ); String nmf = reader.next( ) ; Student f = getstud(nmf); if(f == null) System.out.println( Student not found! " ); else System.out.println( Student + nmf + found" ); } // main 25
26 Self - Referential Classes A self-referential class contains a reference member that refers to a class object of the same class type. public class Node private int ; private Node ; public Node( int ) this. = ; } // class Node constructor1 public Node( int,node ) this. = ; this. = ; } // class Node constructor2 public void setdata( int ) this. = ; } Field references a Node object, an object of the same class. First constructor has only one argument for the item; the next field is set to null. Node object can be created to point to the next Node object. 26
27 Class Node,cont. public int getdata() return this.; } public void setnext( Node ) this. = ; } public Node getnext() return this.; } public String tostring() return " " + this..tostring(); } } // end class Node This method allows modification of the field. Java can link self-referential objects together to form such useful structures as lists, queues, stacks and trees. 27
28 Linked List vs Array 28
29 Building a linked list from Node objects Node n = new Node(9); n Node 9 null Node n1 = new Node(4,n); n1 Node n Node 4 9 null Node n2 = new Node(5,n1); n2 Node n1 Node n 5 4 Node 9 null 29
30 Building a linked list from Node objects public static void main(string[ ] args) System.out.print("enter the number of nodes "); int num = reader.nextint(); // num = 5 System.out.print("enter the value "); int = reader.nextint(); Node n = new Node(); for( int i = 1; i < num; i++) System.out.print("enter the value "); = reader.nextint(); n = new Node(,n); } // for while( n!= null ) String str = n.tostring(); System.out.println(n); System.out.println(str); n = n.getnext(); } //while } // main Input values: Output? 30
31 Inserting a node in a linked list temp Node 7 Inserted Node temp Node Node temp = new Node(7, n.getnext()); 7 n Node Node Node null n.setnext(temp); 31
32 Deleting a node from a linked list n Node temp Node Node null n Node temp Node Node null n Node temp Node Node 5 4 null 9 null 1.Node temp=n.getnext(); 2.n.setNext( temp.getnext() ); 3.temp.setNext(null); 32
33 Calculate sum in a linked list public static int calcsum( Node n) int sum = 0; Node currpos = n; // reference to first node while( currpos!= null ) sum = sum + currpos.getdata(); currpos = currpos.getnext(); } // while return sum; } // calcsum 33
34 Max value position in a linked list public static Node findmaxpos( Node n) Node maxpos = n; // searching from this place n = n.getnext(); while(n!= null ) if( n.getdata() > maxpos.getdata() ) maxpos = n; n = n.getnext(); } // while return maxpos; } // findmaxpos 34
35 What are the differences? public class Node private int ; private Node ; public Node( int ) this. = ; } public Node(int,Node ) this. = ; this. = ; } public void setdata( int ) this. = ; } public class Node private String ; private Node ; public Node( String ) this. = ; } public Node(String,Node ) this. = ; this. = ; } public void setdata( String ) this. = ; } 35
36 (מנגנון הגנריות) Genericity Genericity is a mechanism to specify the types of objects that a class can work with via parameters passed at declaration-time and evaluated at compile-time. Generic programming is the creation of programming constructs that can be used with many different types. UML diagram to define generic class Node T is a place holder (מחזיק מקום ( Node<T> private T private Node<T> Node(T x) Node( T, Node<T> ) T getdata() Node<T> getnext() Void setdata( T ) Void setnext(node<t> ) String tostring() Class variables constructors Class methods 36
37 Generic class Node public class Node <T> private T ; private Node<T> ; public Node( T ) this. = ; } // class Node<T> constructor1 public Node(T, Node<T> ) this. = ; this. = ; } // class Node<T> constructor2 public void setdata( T ) this. =; } 37
38 Generic class Node, cont. public T getdata() return this.; } public void setnext( Node<T> ) this. = ; } public Node<T> getnext() return this.; } public String tostring() return " " + this.; } } // end class generic Node 38
39 Generic class Node - implementation Point p = new Point(4.0,5.0)); Node<Point> np = new Node<Point>(p); String s = Hello ; Node<String> ns = new Node<String>(s); np Node<Point> ns Node<String> p null s null Point X Y String Hello 39
40 Generic class Node - implementation,cont. np.setnext(new Node<Point>(new Point(8.0,9.0))); ns.setnext(new Node<String>( Java )); np Node<Point> Node<Point> ns Node<String> Node<String> p null s null Point Point X Y X Y String String Hello Java 40
41 Generic class Node - test public static void main(string[ ] args) System.out.print( "enter number of student " ); int num = reader.nextint( ); // num=4 System.out.print("enter the name "); String name = reader.next(); Node<String> ns = new Node<String>(name); Node<String> startpos = ns; // help variable for(int i =1; i < num; i++) System.out.print("enter the name "); name = reader.next(); ns.setnext(new Node<String>(name)); ns = ns.getnext(); } // for printclass(startpos); // recursive method (next slide) } // main 41
42 Linked list recursion 1 public static void printclass(node<string> s) System.out.print(s.getData()); if(s.getnext()!= null) System.out.print(" -> "); printclass(s.getnext()); } } // printclass Base case Input: Ofir Galit David Ronit Output: Ofir -> Galit -> David -> Ronit 42
43 Linked list recursion 2 Recursive method getclasslength returns the length of linked list. public static int getclasslength(node<string> s) if(s.getnext( ) == null) return 1; Base case return 1 + getclasslength(s.getnext( )); } // getclasslenght 43
44 Linked list recursion 3 Recursive method getxposition returns the reference to integer X value position in the linked list ( null if not found). public static Node<Integer> getxposition(node<integer> pos, int x) if( pos.getdata( ) == x ) return pos; if( pos.getnext( ) == null ) return null; return getxposition( pos.getnext( ), x); } // getposition Base case
Object-Oriented Programming Concepts
Object-Oriented Programming Concepts Object-oriented programming מונחה עצמים) (תכנות involves programming using objects An object ) represents (עצם an entity in the real world that can be distinctly identified
More information141214 20219031 1 Object-Oriented Programming concepts Object-oriented programming ( תכנות מונחה עצמים ) involves programming using objects An object ) (עצם represents an entity in the real world that
More informationClasses. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.
Classes Introduce to classes and objects in Java. Classes and Objects in Java Understand how some of the OO concepts learnt so far are supported in Java. Understand important features in Java classes.
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract
More informationCS Week 13. Jim Williams, PhD
CS 200 - Week 13 Jim Williams, PhD This Week 1. Team Lab: Instantiable Class 2. BP2 Strategy 3. Lecture: Classes as templates BP2 Strategy 1. M1: 2 of 3 milestone tests didn't require reading a file. 2.
More information1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'
Practice questions: 1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4' 2. Will System.out.println((char)4) display 4? a. Yes b. No 3. The expression "Java
More informationTutorial 12. Exercise 1: Exercise 2: CSC111 Computer Programming I
College of Computer and Information Sciences CSC111 Computer Programming I Exercise 1: Tutorial 12 Arrays: A. Write a method add that receives an array of integers arr, the number of the elements in the
More informationSheet1. S.No Prio. Story Test Example Notes. Lab 0 gives instructions on setting up a Scrapbook page.
S.No Prio. Story Test Example Notes 1 1 Evaluate expressions in a Scrapbook page. 1. Declare an integer. 2. Set variable. 3. Declare an integer array 4. Write a loop to initialize array 5. Write a loop
More informationCSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:
PROGRAMMING ASSIGNMENT 3: Read Savitch: Chapter 7 Programming: You will have 5 files all should be located in a dir. named PA3: ShapeP3.java PointP3.java CircleP3.java RectangleP3.java TriangleP3.java
More informationCS170 Introduction to Computer Science Midterm 2
CS170 Introduction to Computer Science Midterm 2 03/25/2009 Name: Solution You are to honor the Emory Honor Code. This is a closed book and closednotes exam, and you are not to use any other resource than
More informationCSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014
CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014 Name: This exam consists of 8 problems on the following 8 pages. You may use your two- sided hand- written 8 ½ x 11 note sheet during the exam.
More informationExample: Fibonacci Numbers
Example: Fibonacci Numbers Write a program which determines F n, the (n + 1)-th Fibonacci number. The first 10 Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34. The sequence of Fibonacci numbers
More informationBirkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions
Birkbeck (University of London) Software and Programming 1 In-class Test 2.1 16 Mar 2017 Student Name Student Number Answer ALL Questions 1. What output is produced when the following Java program fragment
More informationRecursion 1. Recursion is the process of defining something in terms of itself.
Recursion 1 Recursion is the process of defining something in terms of itself. A method that calls itself is said to be recursive. Recursion is an alternative form of program control. It is repetition
More informationObjects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes
Based on Introduction to Java Programming, Y. Daniel Liang, Brief Version, 10/E 1 Creating Classes and Objects Classes give us a way of defining custom data types and associating data with operations on
More informationCH. 2 OBJECT-ORIENTED PROGRAMMING
CH. 2 OBJECT-ORIENTED PROGRAMMING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OBJECT-ORIENTED
More informationCIS3023: Programming Fundamentals for CIS Majors II Summer 2010
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Objects and Classes (contd.) Course Lecture Slides 19 May 2010 Ganesh Viswanathan Objects and Classes Credits: Adapted from CIS3023 lecture
More informationCS2 Assignment A1S The Simple Shapes Package
CS2 Assignment A1S The Simple Shapes Package Overview In this project you will create a simple shapes package consisting of three interfaces and three classes. In abstract terms, you will establish classes
More informationObject-Oriented Programming Concepts
Object-Oriented Programming Concepts Real world objects include things like your car, TV etc. These objects share two characteristics: they all have state and they all have behavior. Software objects are
More informationIntroduction To Data Structures
Introduction To Data Structures This section introduces the concept of a data structure as well as providing the details of a specific example: a list. Tip For Success: Reminder Look through the examples
More informationProgramming in the Large II: Objects and Classes (Part 1)
Programming in the Large II: Objects and Classes (Part 1) 188230 Advanced Computer Programming Asst. Prof. Dr. Kanda Runapongsa Saikaew (krunapon@kku.ac.th) Department of Computer Engineering Khon Kaen
More informationReviewing OO Concepts
Reviewing OO Concepts Users want to draw circles onto the display canvas. public class Circle { // more code here SWEN-261 Introduction to Software Engineering Department of Software Engineering Rochester
More informationCOMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed
1 1 COMP200 INHERITANCE OOP using Java, from slides by Shayan Javed 2 Inheritance Derive new classes (subclass) from existing ones (superclass). Only the Object class (java.lang) has no superclass Every
More informationBBM 102 Introduction to Programming II Spring 2017
BBM 102 Introduction to Programming II Spring 2017 Classes and Objects in Java Instructors: Ayça Tarhan, Fuat Akal, Gönenç Ercan, Vahid Garousi TAs: Selma Dilek, Selim Yılmaz, Selman Bozkır 1 Today Defining
More informationJAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects
Islamic University of Gaza Faculty of Engineering Computer Engineering Dept Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn how to describe objects and classes and how to define
More information중간고사 학과학번이름. 1. Point 클래스이다. 다음물음에답하라. (40 점 ) 단국대학교응용컴퓨터공학 JAVA 프로그래밍 I 중간고사 (2016 년가을학기 ) 2016 년 10 월 27 일 담당교수 : 단국대학교응용컴퓨터공학박경신
중간고사 담당교수 : 단국대학교응용컴퓨터공학박경신 답은반드시답안지에기술할것. 공간이부족할경우반드시답안지몇쪽의뒤에있다고명기한후기술할것. 그외의경우의답안지뒤쪽이나연습지에기술한내용은답안으로인정안함. 답에는반드시네모를쳐서확실히표시할것. 답안지에학과, 학번, 이름외에본인의암호 (4자리숫자 ) 를기입하면성적공고시학번대신암호를사용할것임. 1. Point 클래스이다. 다음물음에답하라.
More informationAbstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof
Abstract Class Lecture 21 Based on Slides of Dr. Norazah Yusof 1 Abstract Class Abstract class is a class with one or more abstract methods. The abstract method Method signature without implementation
More informationComputer Science II (20082) Week 1: Review and Inheritance
Computer Science II 4003-232-08 (20082) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Syntax and Semantics of Formal (e.g. Programming) Languages Syntax
More informationLecture Notes Chapter #9_b Inheritance & Polymorphism
Lecture Notes Chapter #9_b Inheritance & Polymorphism Inheritance results from deriving new classes from existing classes Root Class all java classes are derived from the java.lang.object class GeometricObject1
More informationECOM 2324 COMPUTER PROGRAMMING II
ECOM 2324 COMPUTER PROGRAMMING II Object Oriented Programming with JAVA Instructor: Ruba A. Salamh Islamic University of Gaza 2 CHAPTER 9 OBJECTS AND CLASSES Motivations 3 After learning the preceding
More informationMore About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario
More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario The Story So Far... Classes as collections of fields and methods. Methods can access fields, and
More informationReviewing OO Concepts
Reviewing OO Concepts Users want to draw circles onto the display canvas. public class Circle { // more code here SWEN-261 Introduc2on to So3ware Engineering Department of So3ware Engineering Rochester
More informationGetter and Setter Methods
Example 1 namespace ConsoleApplication14 public class Student public int ID; public string Name; public int Passmark = 50; class Program static void Main(string[] args) Student c1 = new Student(); Console.WriteLine("please..enter
More informationCMSC 132: Object-Oriented Programming II. Inheritance
CMSC 132: Object-Oriented Programming II Inheritance 1 Mustang vs Model T Ford Mustang Ford Model T 2 Interior: Mustang vs Model T 3 Frame: Mustang vs Model T Mustang Model T 4 Compaq: old and new Price:
More informationCS 177 Week 15 Recitation Slides. Review
CS 177 Week 15 Recitation Slides Review 1 Announcements Final Exam on Friday Dec. 18 th STEW 183 from 1 3 PM Complete your online review of your classes. Your opinion matters!!! Project 6 due Just kidding
More informationProgramming 2. Inheritance & Polymorphism
Programming 2 Inheritance & Polymorphism Motivation Lame Shape Application public class LameShapeApplication { Rectangle[] therects=new Rectangle[100]; Circle[] thecircles=new Circle[100]; Triangle[] thetriangles=new
More informationThe return Statement
The return Statement The return statement is the end point of the method. A callee is a method invoked by a caller. The callee returns to the caller if the callee completes all the statements (w/o a return
More informationObject Oriented Relationships
Lecture 3 Object Oriented Relationships Group home page: http://groups.yahoo.com/group/java CS244/ 2 Object Oriented Relationships Object oriented programs usually consisted of a number of classes Only
More informationINHERITANCE AND OBJECTS. Fundamentals of Computer Science I
INHERITANCE AND OBJECTS Fundamentals of Computer Science I Outline Inheritance Sharing code between related classes Putting similar objects in the same bucket Extremely common in modern OOP languages Managing
More informationObject Oriented Programming in C#
Introduction to Object Oriented Programming in C# Class and Object 1 You will be able to: Objectives 1. Write a simple class definition in C#. 2. Control access to the methods and data in a class. 3. Create
More informationProgramming Languages and Techniques (CIS120)
Programming Languages and Techniques (CIS120) Lecture 24 October 29, 2018 Arrays, Java ASM Chapter 21 and 22 Announcements HW6: Java Programming (Pennstagram) Due TOMORROW at 11:59pm Reminder: please complete
More informationCSCI 135 Midterm Fundamentals of Computer Science I Fall 2011
CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011 Name: This exam consists of 12 problems on the following 11 pages. You may use your single- side hand- written 8 ½ x 11 note sheet during the
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Interface Abstract data types Version of January 26, 2013 Abstract These lecture notes are meant
More informationCIS 120 Midterm II March 31, 2017 SOLUTIONS
CIS 120 Midterm II March 31, 2017 SOLUTIONS 1 1. OCaml and Java (14 points) Check one box for each part. a. The following OCaml function will terminate by exhausting stack space if called as loop 10: let
More informationLists. Arrays. CPSC 233: Introduction to Data Structures, Lists 1. James Tam. James Tam
CPSC 233: Introduction to Data Structures, Lists 1 Lists Lists are a type of data structure (one of the simplest and most commonly used). - e.g., grades for a lecture can be stored in the form of a list
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation
More informationProblems with simple variables
Problems with simple variables Hard to give up values high number of variables. Complex to sort a high number of variables by value. Impossible to use loops. Example problem: Read one hundred numbers,
More informationCSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013
CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013 Name: This exam consists of 6 problems on the following 6 pages. You may use your two-sided hand-written 8 ½ x 11 note sheet during the exam.
More informationDay 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University
Day 3 COMP 1006/1406A Summer 2016 M. Jason Hinek Carleton University today s agenda assignments 1 was due before class 2 is posted (be sure to read early!) a quick look back testing test cases for arrays
More informationCSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013
CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013 Name: This exam consists of 6 problems on the following 6 pages. You may use your two-sided hand-written 8 ½ x 11 note sheet during the exam.
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation
More informationMIDTERM REVIEW. midterminformation.htm
MIDTERM REVIEW http://pages.cpsc.ucalgary.ca/~tamj/233/exams/ midterminformation.htm 1 REMINDER Midterm Time: 7:00pm - 8:15pm on Friday, Mar 1, 2013 Location: ST 148 Cover everything up to the last lecture
More informationSelected Questions from by Nageshwara Rao
Selected Questions from http://way2java.com by Nageshwara Rao Swaminathan J Amrita University swaminathanj@am.amrita.edu November 24, 2016 Swaminathan J (Amrita University) way2java.com (Nageshwara Rao)
More informationCSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012
CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012 Name: This exam consists of 6 problems on the following 7 pages. You may use your two-sided hand-written 8 ½ x 11 note sheet during the exam.
More informationCS101 Part 2: Practice Questions Algorithms on Arrays, Classes and Objects, String Class, Stack Class
CS1 Part 2: Algorithms on Arrays, Classes and Objects, String Class, Stack Class 1. Write a method that, given two sorted arrays of integers, merges the two arrays into a single sorted array that is returned.
More informationClasses - 2. Data Processing Course, I. Hrivnacova, IPN Orsay
Classes - 2 Data Processing Course, I. Hrivnacova, IPN Orsay OOP, Classes Reminder Requirements for a Class Class Development Constructor Access Control Modifiers Getters, Setters Keyword this const Member
More informationClass 5: Classes and Objects. Objects
Class 5: Classes and Objects 1.00/1.001 - Introduction to Computation and Problem Solving Fall 2005 Objects Objects are things Recall the description of libraries, books, paperback books from Session 1
More informationLecture 12: Classes II
Lecture 12: Classes II Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson 2013. All rights reserved. Encapsulation Encapsulation encapsulation: Hiding
More information8359 Object-oriented Programming with Java, Part 2. Stephen Pipes IBM Hursley Park Labs, United Kingdom
8359 Object-oriented Programming with Java, Part 2 Stephen Pipes IBM Hursley Park Labs, United Kingdom Dallas 2003 Intro to Java recap Classes are like user-defined types Objects are like variables of
More information1.00 Lecture 5. Objects
All code produced with Java software. Java is a trademark of Sun Microsystems Inc. 1.00 Lecture 5 Java Classes and Objects Objects Objects are things 8.01 defined matter as stuff We decompose programming
More informationCS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java
1 CS/ENGRD 2110 SPRING 2014 Lecture 2: Objects and classes in Java http://courses.cs.cornell.edu/cs2110 Java OO (Object Orientation) 2 Python and Matlab have objects and classes. Strong-typing nature of
More informationCS 113 PRACTICE FINAL
CS 113 PRACTICE FINAL There are 13 questions on this test. The value of each question is: 1-10 multiple choice (4 pt) 11-13 coding problems (20 pt) You may get partial credit for questions 11-13. If you
More informationJava Comparable interface
Java Comparable interface Recall that to define a binary search tree, the elements that we are considering must be comparable to each other, meaning that there must be a well-defined ordering. For strings
More informationCOMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading
COMP 202 CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading More on OO COMP 202 - Week 7 1 Static member variables So far: Member variables
More informationAbstract Class (2) Abstract Classes and Interfaces. Abstract Class (1) Abstract Class (3) EECS2030: Advanced Object Oriented Programming Fall 2017
Abstract Class (2) Abstract Classes and Interfaces EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG public abstract class Polygon { double[] sides; Polygon(double[] sides) { this.sides
More informationFall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.
Grading Page 1 / 4 Page3 / 20 Page 4 / 13 Page 5 / 10 Page 6 / 26 Page 7 / 17 Page 8 / 10 Total / 100 1. (4 points) What is your course section? CS 101 CS 101E Pledged Page 1 of 8 Pledged The following
More informationEsc101 Mid Semester Exam - II
Esc101 Mid Semester Exam - II Time Allowed: 1 Hour Max Marks: 75 Instructions: 1. Do not turn this page until the bell rings. 2. YOU MUST WRITE YOUR NAME, ROLL NUMBER & SECTION ON EACH SHEET. 3. Please
More informationComputer Science II (20073) Week 1: Review and Inheritance
Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system
More informationCS1004: Intro to CS in Java, Spring 2005
CS1004: Intro to CS in Java, Spring 2005 Lecture #13: Java OO cont d. Janak J Parekh janak@cs.columbia.edu Administrivia Homework due next week Problem #2 revisited Constructors, revisited Remember: a
More informationChapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber
Chapter 10 Inheritance and Polymorphism Dr. Hikmat Jaber 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the
More informationBirkbeck (University of London) Software and Programming 1 In-class Test Mar 2018
Birkbeck (University of London) Software and Programming 1 In-class Test 2.1 22 Mar 2018 Student Name Student Number Answer ALL Questions 1. What output is produced when the following Java program fragment
More informationDepartment of Networks College of Bardarash Technical Institute DUHOK Polytechnic University Subject: Programming Fundamental by JAVA Course Book
1 Department of Networks College of Bardarash Technical Institute DUHOK Polytechnic University Subject: Programming Fundamental by JAVA Course Book Year 1 Lecturer's name: MSc. Sami Hussein Ismael Academic
More informationCSCI 1101 Winter 2017 Laboratory No Submission deadline is p.m. (5 minutes to midnight) on Saturday, February 4th, 2017.
CSCI 1101 Winter 2017 Laboratory No. 3 This lab is a continuation of the concepts of object-oriented programming, specifically the use of static variables and static methods, and object interactions. If
More informationAbstract Classes and Interfaces
Abstract Classes and Interfaces EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Abstract Class (1) Problem: A polygon may be either a triangle or a rectangle. Given a polygon, we
More informationCS1150 Principles of Computer Science Objects and Classes
CS1150 Principles of Computer Science Objects and Classes Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Object-Oriented Thinking Chapters 1-8
More informationSimple Java Reference
Simple Java Reference This document provides a reference to all the Java syntax used in the Computational Methods course. 1 Compiling and running... 2 2 The main() method... 3 3 Primitive variable types...
More informationChapter 9. Objects and Classes
Chapter 9 Objects and Classes 1 OO Programming in Java Other than primitive data types (byte, short, int, long, float, double, char, boolean), everything else in Java is of type object. Objects we already
More informationCISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College
CISC 3115 TY3 C09a: Inheritance Hui Chen Department of Computer & Information Science CUNY Brooklyn College 9/20/2018 CUNY Brooklyn College 1 Outline Inheritance Superclass/supertype, subclass/subtype
More informationLecture 3. Lecture
True Object-Oriented programming: Dynamic Objects Static Object-Oriented Programming Reference Variables Eckel: 30-31, 41-46, 107-111, 114-115 Riley: 5.1, 5.2 D0010E Object-Oriented Programming and Design
More informationChapter 8 Objects and Classes
Chapter 8 Objects and Classes 1 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops, methods, and arrays. However, these Java
More informationChapter 9 Objects and Classes. OO Programming Concepts. Classes. Objects. Motivations. Objectives. CS1: Java Programming Colorado State University
Chapter 9 Objects and Classes CS1: Java Programming Colorado State University Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops,
More informationMethod Invocation. Zheng-Liang Lu Java Programming 189 / 226
Method Invocation Note that the input parameters are sort of variables declared within the method as placeholders. When calling the method, one needs to provide arguments, which must match the parameters
More informationIntroduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017
Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 Lecture 11: Inheritance and Polymorphism Part 1 Instructor: AbuKhleif, Mohammad Noor Sep 2017 Instructor AbuKhleif, Mohammad
More informationChapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {
Chapter 21- Using Generics Case Study: Geometric Bunch In this example a class called GeometricBunch is made to wrap around a list of GeometricObjects. Circle and Rectangle are subclasses of GeometricObject.
More informationTopic 7: Algebraic Data Types
Topic 7: Algebraic Data Types 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 5.5, 5.7, 5.8, 5.10, 5.11, 5.12, 5.14 14.4, 14.5, 14.6 14.9, 14.11,
More informationObject Oriented Programming
Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Lab 11 Object Oriented Programming Eng. Mohammed Alokshiya December 16, 2014 Object-oriented
More informationAdvanced Placement Computer Science. Inheritance and Polymorphism
Advanced Placement Computer Science Inheritance and Polymorphism What s past is prologue. Don t write it twice write it once and reuse it. Mike Scott The University of Texas at Austin Inheritance, Polymorphism,
More informationCSEN401 Computer Programming Lab. Topics: Introduction and Motivation Recap: Objects and Classes
CSEN401 Computer Programming Lab Topics: Introduction and Motivation Recap: Objects and Classes Prof. Dr. Slim Abdennadher 16.2.2014 c S. Abdennadher 1 Course Structure Lectures Presentation of topics
More informationOverview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting
Overview ITI 1121. Introduction to Computing II Rafael Falcon and Marcel Turcotte (with contributions from R. Holte) Electrical Engineering and Computer Science University of Ottawa Interface Abstract
More informationReusing Classes. Hendrik Speleers
Hendrik Speleers Overview Composition Inheritance Polymorphism Method overloading vs. overriding Visibility of variables and methods Specification of a contract Abstract classes, interfaces Software development
More informationCsci 102: Sample Exam
Csci 102: Sample Exam Duration: 65 minutes Name: NetID: Student to your left: Student to your right: DO NOT OPEN THIS EXAM UNTIL INSTRUCTED Instructions: Write your full name and your NetID on the front
More informationCSE 11 Midterm Fall 2008
Signature cs11f Name Student ID CSE 11 Midterm Fall 2008 Page 1 (10 points) Page 2 (22 points) Page 3 (23 points) Page 4 (17 points) Page 5 (12 points) Total (84 points = 80 base points + 4 points EC [5%])
More informationChapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.
Chapter 9 Objects and Classes 1 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops, methods, and arrays. However, these Java
More informationClass. A class is an unit of Java programs; that is, Java programs consist only of classes. School. Department. Student. Course.
Classes - Basics Classes Information hiding Method overloading Useful methods: tostring() and equals() Parameter passing: call by reference Final fields Static fields and methods Objects Initialization
More informationCOE318 Lecture Notes Week 4 (Sept 26, 2011)
COE318 Software Systems Lecture Notes: Week 4 1 of 11 COE318 Lecture Notes Week 4 (Sept 26, 2011) Topics Announcements Data types (cont.) Pass by value Arrays The + operator Strings Stack and Heap details
More informationCSIS 10A Practice Final Exam Name:
CSIS 10A Practice Final Exam Name: Multiple Choice: Each question is worth 2 points. Circle the letter of the best answer for the following questions. 1. For the following declarations: int area; String
More informationLecture 5: Methods CS2301
Lecture 5: Methods NADA ALZAHRANI CS2301 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Solution public static int sum(int i1, int i2) { int
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance (part II) Polymorphism Version of January 21, 2013 Abstract These lecture notes
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance (part II) Polymorphism Version of January 21, 2013 Abstract These lecture notes
More information