1 / 18

Advanced Programming

Advanced Programming. TA Session 4. Constructors. Can constructors call other constructors?. A.java class A { int x; A() { // How can I construct with A(10) here? } A( int x) { this.x = x; } public static void main(String[] args ) {

britain
Télécharger la présentation

Advanced Programming

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

Presentation Transcript


  1. Advanced Programming Advanced Programing TA Session, Sharif University of Technology TA Session 4

  2. Constructors • Can constructors call other constructors? A.java class A { int x; A() { // How can I construct with A(10) here? } A(int x) { this.x = x; } public static void main(String[] args) { A a1 = new A(); A a2 = new A(1); System.out.println(a1.x + " " + a2.x); } } Advanced Programing TA Session, Sharif University of Technology

  3. Constructors • Can constructors call other constructors? A.java class A { int x; A() { this(10); } A(int x) { this.x = x; } public static void main(String[] args) { A a1 = new A(); A a2 = new A(1); System.out.println(a1.x + " " + a2.x); } } Advanced Programing TA Session, Sharif University of Technology

  4. Constructors • What is the output of running class B? A.java class A { A() { System.out.print("A"); } } B.java class B extends A { B() { System.out.print("B"); } public static void main(String[] args) { A a = new A(); B b= new B(); } } Advanced Programing TA Session, Sharif University of Technology • The output is “AAB”

  5. Constructors • What is the output of running class B? A.java class A { A(int x) { System.out.println("A “ + x); } } B.java:2: cannot find symbol symbol : constructor A() location: class A B() { ^ 1 error B.java class B extends A { B() { System.out.println("B"); } public static void main(String[] args) { A a = new A(1); B b = new B(); } } Advanced Programing TA Session, Sharif University of Technology

  6. Solution • What is the output of running class B? A.java class A { A(int x) { System.out.println("A “ + x); } } B.java class B extends A { B() { super(1); System.out.println("B"); } public static void main(String[] args) { A a = new A(1); B b = new B(); } } Advanced Programing TA Session, Sharif University of Technology

  7. Advanced Programing TA Session, Sharif University of Technology

  8. Advanced Programing TA Session, Sharif University of Technology

  9. Advanced Programing TA Session, Sharif University of Technology

  10. Advanced Programing TA Session, Sharif University of Technology

  11. class Clazz { ? int compute (inti) { return 0; } } Advanced Programing TA Session, Sharif University of Technology

  12. class Shape{ int r = 5 ; public Shape(){ printR(); } public void printR(){ System.out.println(r); } public static void main(String[] params){ Shape s = new Circle(); } }; class Ellipse extends Shape{ int r = 10 ; public Ellipse(intrd){ r = rd ; printR() ; } public void printR(){ System.out.println(r); } }; class Circle extends Ellipse{ public Circle(){ super(25); r = 20 ; printR(); } public void printR(){ System.out.println(r); } }; Advanced Programing TA Session, Sharif University of Technology

  13. Advanced Programing TA Session, Sharif University of Technology

  14. Advanced Programing TA Session, Sharif University of Technology

  15. درستی و یا نادرستی هر یک از عبارات زیر را مشخص کرده و دلیل خود را نیز برای این امر ذکر کنید. الف) در زبان جاوا، تنها در زمان ساختن اشیا بر روی آنها کنترل داریم و در زمان از بین بردن اشیا کنترلی روی آنها نداریم. ب) توابع پدر یک کلاس که قابل دسترسی در آن کلاس نیستند، قابل Override شدن نیستند. ج) هر کلاس باید تمامی توابع موجود در interfaceهای خود را (در صورتی که یک یا چند interface را استفاده کرده باشد) پیاده‌سازی کند. د) هر داده‌ای را که می‌توان توسط PrintWriterدر خروجی نوشت، می‌توان توسط OutputStream هم آن را در خروجی تولید کرد. Advanced Programing TA Session, Sharif University of Technology

  16. خروجی برنامه‌ی زیر را به صورت کامل بنویسید. Advanced Programing TA Session, Sharif University of Technology

  17. interface ای برای مقایسه اشیا با یکدیگر به نام comparator داده شده است. کلاسی از این interface پیاده‌سازی کنید که بتوان با آن دو رشته متنی را مقایسه کرد. interface ای دیگر نیز برای مرتب کردن اشیا داده شده است. با استفاده از این interface یک مرتب ساز برای آرایه‌ای از ‌String ها پیاده سازی کنید. (4 نمره) Advanced Programing TA Session, Sharif University of Technology

  18. interface زیر را به صورتی پیاده‌سازی کنید که دارای خصوصیات زیر باشد. (6 نمره) Advanced Programing TA Session, Sharif University of Technology

More Related