CS 180 Problem Solving and Object Oriented Programming Fall 2011

Similar documents
CS 180 Problem Solving and Object Oriented Programming Fall 2011

CS 180 Problem Solving and Object Oriented Programming Fall 2010

CS 180 Problem Solving and Object Oriented Programming Fall 2011

CS 180 Problem Solving and Object Oriented Programming Fall 2011

CS 180 Problem Solving and Object Oriented Programming Fall 2011

CS 180 Problem Solving and Object Oriented Programming Fall 2010

CS 180 Problem Solving and Object Oriented Programming Fall 2011

Use the Scantron sheets to write your answer. Make sure to write your Purdue ID (NOT Purdue Login ID) and your name on the Scantron answer sheet.

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

Programming Languages and Techniques (CIS120)

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015

EECS168 Exam 3 Review

Packages: Putting Classes Together

Principles of Object Oriented Programming. Lecture 4

CS 302 Week 9. Jim Williams

CS 180 Final Exam Review 12/(11, 12)/08

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Sample Examination Paper Programming and Software Development

Lecture 3: Java Graphics & Events

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Chapter 6 Introduction to Defining Classes

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

Lecture 5: Java Graphics

CS 101 Fall 2006 Midterm 3 Name: ID:

Midterm assessment - MAKEUP Fall 2010

The Islamic University Gaza Department of Electrical & Computer Engineering. Midterm Exam Spring 2012 Computer Programming II (Java) ECOM 2324

CS 251 Intermediate Programming GUIs: Components and Layout

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

CSC 111 Introduction to Computer Science (Section C)

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Chapter 12 Advanced GUIs and Graphics

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Computer Science II Data Structures

Graphical User Interfaces (GUIs)

CS11 Java. Fall Lecture 4

Encapsulation in Java

Introduction to the JAVA UI classes Advanced HCI IAT351

Final Exam CS 251, Intermediate Programming December 10, 2014

CS 3331 Advanced Object-Oriented Programming Final Exam

Lecture 2: Java & Javadoc

CS11 Java. Fall Lecture 3

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

H212 Introduction to Software Systems Honors

Preview from Notesale.co.uk Page 3 of 36

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

CS56 final (E03) W15, Phill Conrad, UC Santa Barbara Wednesday, 03/18/2015. Name: Umail umail.ucsb.edu. Circle one: 4pm 5pm 6pm

Part I: Learn Common Graphics Components

CSE 113 A. Announcements - Lab

Recitation 02/02/07 Defining Classes and Methods. Chapter 4

Chapter 6 Lab Classes and Objects

SELF-STUDY. Glossary

CS/ENGRD 2110 FALL2017. Lecture 4: The class hierarchy; static components

Final Examination Semester 2 / Year 2010

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 14: OCT. 25TH INSTRUCTOR: JIAYIN WANG

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Programming in the Large II: Objects and Classes (Part 2)

CS180 Review. Recitation Week 15

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Inheritance and Polymorphism. CS180 Fall 2007

CS 180 Problem Solving and Object Oriented Programming Fall 2011

Announcements. 1. Forms to return today after class:

CS18000: Problem Solving And Object-Oriented Programming

CS200: Advanced OO in Java

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Come & Join Us at VUSTUDENTS.net

Programming Exercise 14: Inheritance and Polymorphism

We are on the GUI fast track path

CS 1331 Fall 2016 Exam 2

Programming Languages and Techniques (CIS120)

What will this print?

CS 170 Java Programming 1. Week 12: Creating Your Own Types

Writing Parallel Programs COMP360

Announcements. Course syllabus Tutorial/lab signup form (due 4pm today) Lecture 1 notes Homework 1 Initial assessment

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

COE318 Lecture Notes Week 5 (Oct 3, 2011)

Object Oriented Programming

CS 180 Fall 2006 Exam II

CS 134 Programming Exercise 7:

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

CSCI 053. Week 5 Java is like Alice not based on Joel Adams you may want to take notes. Rhys Price Jones. Introduction to Software Development

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 00. WELCOME TO OBJECTVILLE. Speaking the Language of OO

Final Exam Practice. Partial credit will be awarded.

C++ Inheritance and Encapsulation

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Classes. Classes as Code Libraries. Classes as Data Structures

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

Transcription:

CS 180 Problem Solving and Object Oriented Programming Fall 20 hmp://www.cs.purdue.edu/homes/apm/courses/cs180fall20/ Notes for Week : Oct 31- Nov 4, 20 Aditya Mathur Department of Computer Science Purdue University West LafayeMe, IN, USA This Week: 10/31- /1 1. Layouts, menus, listeners 2. Methods and parameters 3. Inheritance 4. Thread class 5. Concurrency: Task decompositon Data decompositon

Readings and Exercises for Week Readings: GUI: 13.2, 13.3 Concurrency: 14.1, 14.2, 14.3, 14.4 Method and data hiding: 8.2, 8.3 Inheritance:.2,.3 Exercises: 9.1, 9.2, 9.3 Aditya Mathur. CS 180. Fall 20. Week

Announcements Feast with faculty: Wednesday Nov 2, 20. 6:30pm. Ford Dining Hall Exam 2: Wednesday November 9, 8-10pm. WTHR 200. Review for Exam 2: Monday November 7, 20 Special class: Sunday November 6, 20. 4pm. LWSN 3102AB Project 4 available. HW 8 available. Aditya Mathur. CS 180. Fall 20. Week

Problem: GUI Write a Java app that creates the GUI shown below and performs tasks as explained. Menu Menu bar bumons Aditya Mathur. CS 180. Fall 20. Week

Problem: Menu an Menu items Menu items Aditya Mathur. CS 180. Fall 20. Week

Problem: AcTons Bird sound Bird picture Terminate program Aditya Mathur. CS 180. Fall 20. Week

Problem: CreaTng Sounds import java.applet.*; import java.net.url; Could also be a URL birdurl; complete URL AudioClip birdclip; try{ birdurl=new URL("file:bird.au"); catch(excepton e){ birdclip=applet.newaudioclip(birdurl); Aditya Mathur. CS 180. Fall 20. Week

Problem: CreaTng Image Icons import javax.swing.*; ImageIcon birdpic// Declare an ImageIcon object; JBuMon bumon=new JBuMon(); birdpic=new ImageIcon("bird.jpg );// Create an icon from a picture; bumon.seticon(birdpic);// Add an icon to a bumon Could also be a URL Aditya Mathur. CS 180. Fall 20. Week

Live demo Aditya Mathur. CS 180. Fall 20. Week

Announcements Feast with faculty: Wednesday Nov 2, 20. 6:30pm. Ford Dining Hall Exam 2: Wednesday November 9, 8-10pm. WTHR 200. Review for Exam 2: Monday November 7, 20 Special class: Sunday November 6, 20. 4pm. LWSN 3102AB Aditya Mathur. CS 180. Fall 20. Week

Back to GUIs, Classes, objects, methods! Aditya Mathur. CS 180. Fall 20. Week

GUIs: What have we covered so far? Widgets: Frame, panel, bumon, text field, label, menu bar, menu, menu item Listeners: AcTonLsitener, MouseListener, KeyListener Layouts: FlowLayout, BorderLayout, GridLayout Incremental development: Layout the GUI in steps startng with the frame and ending with the listeners. Aditya Mathur. CS 180. Fall 20. Week

GUIs: What have we not covered? A LOT! There are other widgets, layouts, and listeners that we have not covered and will not be able to cover. There are many methods available with each widget that we have not covered and will not be able to cover. Given what you now know, it should not be difficult to learn more about GUIs on your own! Aditya Mathur. CS 180. Fall 20. Week

Instance variables public class Fruit{ public String name; public String color; public int type public class Market{ public statc void main(string [] args){ System.out.println(Fruit.color); Will this compile? Aditya Mathur. CS 180. Fall 20. Week

Instance variables public class Fruit{ public String name; public String color= Red ; public int type; public void changecolor (String c){ color=c; public class Market{ public statc void main(string [] args){ Fruit apple=new Fruit(); Fruit pear=new Fruit(); pear.changecolor( Green ); System.out.println(apple.color); System.out.println(pear.color); Will this compile? Aditya Mathur. CS 180. Fall 20. Week

Class variables public class Fruit{ public String name; public statc String color; public int type; public class Market{ public statc void main(strings[] args){ System.out.println(Fruit.color); Will this compile? Yes, because color is statc and public. Aditya Mathur. CS 180. Fall 20. Week

Class: variables public class Fruit{ public String name; public statc String color; public int type public class Market{ public statc void main(strings[] args){ Fruit apple=new Fruit(); System.out.println(apple.color); Will this compile? Yes, because color will be obtained from Fruit which is the parent class of apple. Aditya Mathur. CS 180. Fall 20. Week

Private/Public variables public class Fruit{ public String name; private String color= Red ; int type; public class Market{ public statc void main(string [] args){ Fruit apple=new Fruit(); Fruit pear=new Fruit(); System.out.println(apple.color); System.out.println(pear.color); Will this compile? Aditya Mathur. CS 180. Fall 20. Week

Private/Public variables public class Fruit{ public class Market{ public String name; private String color= Red ; int type; public void changecolor (String c){ color=c; public String getcolor(){ return color; public statc void main(string [] args){ Fruit apple=new Fruit(); Fruit pear=new Fruit(); pear.changecolor( Green ); System.out.println(apple.getColor()); System.out.println(pear.getColor()); Will this compile? Aditya Mathur. CS 180. Fall 20. Week

Accessor (get) and mutator (set) methods public class Fruit{ public String name; private String color= Red ; int type; public void changecolor(string c){ color=c; public String get Color(){ return color; Mutator method Accessor method Aditya Mathur. CS 180. Fall 20. Week

Inheritance Aditya Mathur. CS 180. Fall 20. Week

Inheritance: What and Why? What: A mechanism to create new classes from existng classes. Why: Commonly used stat (variables, objects) and behavior (methods) is encapsulated in one class and can be used by other classes. Example: JApplet: Your class can extend JApplet and inherit all state and behavior methods in JApplet. Aditya Mathur. CS 180. Fall 20. Week

Simple inheritance Base class D inherits from A Class A Class D B inherits from A Class B Subclass C inherits from B Class C Aditya Mathur. CS 180. Fall 20. Week

Simple inheritance: Example RV inherits from Vehicle Class Vehicle Class RecreaTonalVehicle Car inherits from Vehicle Class Car MyCar inherits from Car Class MyCar Aditya Mathur. CS 180. Fall 20. Week

MulTple inheritance: Not Allowed in Java! Class Vehicle Class Engine Car inherits from Vehicle Class Car and from Engine Aditya Mathur. CS 180. Fall 20. Week

Inheritance: Code for a Base class public class Vehicle{ String manufacturer="unknown"; int wheels=4; int steering=1; int capacity=4; boolean seatbelts=true; public void drive(){ System.out.print("I am driving."); public void oilchange(string vehiclename){ System.out.println("Oil changed for "+vehiclename); Aditya Mathur. CS 180. Fall 20. Week

Inheritance: Code for a subclass public class Car extends Vehicle{ int capacity=5; // Overridden instance variable int maxspeed=130; // Added instance variable public void drive(){ // Overridden method System.out.println("This car drives great. I am driving."); public statc void main(string[] args){ Car c=new Car(); Vehicle v=new Vehicle(); System.out.println("This car has "+ c.capacity+ " seats."); System.out.println("This vehicle has a seatng capacity of "+ v.capacity+"."); System.out.println("This car has seat belts:"+ c.seatbelts); Aditya Mathur. CS 180. Fall 20. Week

Inheritance public class Fruit{ public String name; private String color= Red ; int type; public void changecolor (String c){ Color=c; public String getcolor(){ return color; public class Mango extends Fruit{ public String origin; public void harvest(){ public String getorigin(){ return color; All methods and local variables/ objects are available to Mango. Aditya Mathur. CS 180. Fall 20. Week

Inheritance: Another example public class Gui extends JFrame{ All methods and local variables/ objects of JFrame are available to Gui. Aditya Mathur. CS 180. Fall 20. Week

Accessibility rules: Let us fill this table If a variable or an object declaraton uses this modifier then can this variable or object be used inside? Y: Yes. N: No. Modifier Class Package Subclass World public Y Y Y Y private Y N N N None (package private) Y Y Y/N N Package: A collecton of classes identfied as a Java package. World: CollecTon of packages Aditya Mathur. CS 180. Fall 20. Week

Classes etc.: summary Base class: Any class, not final, can serve as a base class Sub class: A class that extends another class Interface: Not a class but a contract; must be implemented in full before use Abstract class: Must be extended for use; may contain some methods with implementaton (concrete methods) and others to be implemented (abstract methods). Aditya Mathur. CS 180. Fall 20. Week

Concurrent Programming Aditya Mathur. CS 180. Fall 20. Week

Dividing work into small segments Program class class class Instance variables Class variables Methods Instance variables Class variables Methods Instance variables Class variables Methods Aditya Mathur. CS 180. Fall 20. Week

Concurrency Main thread controls the distributon of work Distribute work T T 1 T 2.... T N Task T is divided into N simpler tasks and executed in parallel N threads executng concurrently to execute N tasks T 1, T 2 T N. T Aditya Mathur. CS 180. Fall 20. Week

Threads Thread is a class A thread is a sequence of computatons that can run in parallel with other threads. One uses the Thread class to create a thread. Aditya Mathur. CS 180. Fall 20. Week

Problem Given doubles x and y and a boolean z, write a program to compute the following sin(x)/cos(y)+(x 2 +y 2 ); if z is true sin(x)/cos(y)+(x 2 - y 2 ); if z is false Aditya Mathur. CS 180. Fall 20. Week

Problem: SoluTon architecture T T: Perform the given task T 1 : Compute a part of the expression T 1 T 2 T 2 : Compute a other part of the expression How many threads? T Combine the results of T 1 and T 2 Aditya Mathur. CS 180. Fall 20. Week

Problem: Algorithm Thread A: Input: x and y Compute sin(x)/cos(y) Thread B: Input: x, y, z Compute x 2 +y 2 x 2 - y 2 if z is true or if z is false Thread C (control thread): Input: x, y, z; Create an object e1(thread A ); Create can object e2(thread B); Start e1; Start e2; Wait for e1 and e2 to complete; Get value computed by e1; Get value computed by e2; Add the two values and display the result; Aditya Mathur. CS 180. Fall 20. Week

Problem: Program Aditya Mathur. CS 180. Fall 20. Week

Week : October 31- November 4, 20 Hope you enjoyed this week! QuesTons? Contact your recitaton instructor. Make full use of our office hours. Aditya Mathur. CS 180. Fall 20. Week