CompSci 001 11.1 Today’s topics Java Writing Functions/Methods Upcoming Information Retrieval Reading Great Ideas, Chapter 4.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
CompSci Today’s topics Java Recursion in Graphics Writing a Class Simple Animation Upcoming Simulation Reading Great Ideas, Chapters 5.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
CompSci Today’s topics Java The Database Program Upcoming Recursion Reading Great Ideas, Chapter 4.
1. Reference  2  Algorithm :- Outline the essence of a computational procedure, step by step instructions.  Program :- an.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
CPS Today’s topics Java Information Retrieval Upcoming Database Programs Reading Great Ideas, Chapter 4.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
SNU IDB Lab. 1 Great Ideas of CS with Java Part 1 WWW & Computer programming in the language Java Ch 1: The World Wide Web Ch 2: Watch out: Here comes.
CompSci Today’s topics Java Review Just a bunch of headings meant to trigger questions A contraction of previous notes Upcoming Midterm Exam Reading.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
CPS Today’s topics Java Applications Graphics Upcoming Review for Midterm Exam Reading Great Ideas, Chapters 5.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Java Applet Basics (2). The Body Mass Index Calculator.
CS 106 Introduction to Computer Science I 10 / 29 / 2007 Instructor: Michael Eckmann.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CompSci Today’s topics Java Applications Simulation Upcoming Software Engineering (Chapter 7) Reading Great Ideas, Chapters 6.
Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially supported by NSF CCLI grant DUE Java: An Eventful Approach An innovative.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
CompSci Today’s topics Java Numbers Iteration Upcoming More Java Reading Great Ideas, Chapter 3.
Interfaces and Inner Classes
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Building java programs, chapter 3 Parameters, Methods and Objects.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
1 Introduction to Object Oriented Programming Chapter 10.
Today’s topics Java Input More Syntax Upcoming Decision Trees More formal treatment of grammers Reading Great Ideas, Chapter 2.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Programming Fundamentals Enumerations and Functions.
Unified Modeling Language (UML)
Today’s topics Java Looping Upcoming Arrays in Java Reading Great Ideas, Chapter 3.
1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class l class myClassName extends SuperClass { –// your.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
CompSci 100E JB1.1 Java Basics (ala Goodrich & Tamassia)  Everything is in a class  A minimal program: public class Hello { public static void main(String[]
CompSci Today’s topics Java Recursion Upcoming Graphics Reading Great Ideas, Chapter 4 (begin Chapter 5 for graphics)
CompSci Announcements  Read Chap 3 for next time  Homework will be posted  Consulting Hours will start soon.
Today’s topics Java Upcoming Reading Recursion in Graphics
Today’s topics Java Information Retrieval Upcoming Database Programs
Today’s topics Java Arrays Upcoming Functions Reading
Today’s topics Java Applications Upcoming Reading Graphics
Method Mark and Lyubo.
User Defined Functions
Chapter 4 Writing Classes.
Corresponds with Chapter 5
Presentation transcript:

CompSci Today’s topics Java Writing Functions/Methods Upcoming Information Retrieval Reading Great Ideas, Chapter 4

CompSci Writing Functions/Methods  Function is Synonym of Method  Function is more generic term  Method is used in Java  Syntax of a function return-type name(parameter-list) { statement;... statement; return return-type-object // if return-type not void }  Return-type  May be void if no information returned  If not void, type must match type of info returned  If not void, 1 or more return statements required

CompSci Writing Functions/Methods  Parameter list  Type-object pairs, separated by commas  Can be empty  Parameters used to obtain info from outside of function  Return statement  Sends information back to statement that invoked function  Type must match return-type on function header  Miscellaneous  Header may be preceded by public to make function available anywhere in program  Can make private to insure “internal use only”

CompSci  Void Functions  Use by putting on line with parameters (if any)  E.g., object-name.setText(”Hello”);  Functions that return info  Must use in calculation or assignment  Include parameters (if any)  E.g., weight = object-name.getDouble();  What does the following do? Object-name.getInt(); Use of Functions

CompSci Simple Uses of a Simple Function // definition: public double sumTres(double a, double b, double c) { double sum; sum = a + b + c; return sum; } // uses: double measure = sumTres(14.3, 12.1, -4.2); m2.setText("Alltogether we have: " + measure); // or: double x = d1.getDouble(); double total = sumTres(27.9, x, d2.getDouble()); m2.setText("It all adds up to " + total); // or: m2.setText("Sum: " + sumTres(-3.2, d3.getDouble(), 1.0));

CompSci Writing Functions public class DiaFuncts extends java.applet.Applet implements ActionListener { TextField tf; TextArea ta; Button bDraw; String stars = "*******************"; String spaces = " "; public void init() { tf = new TextField("Hello "); ta = new TextArea(20, 20); ta.setFont(new Font("Monospaced", Font.BOLD, 12)); bDraw = new Button("Draw"); bDraw.addActionListener(this); add(tf); add(bDraw); add(ta); }

CompSci Writing Functions.2 void Dia() { int k = 0; while (k < 10) { ta.append(spaces.substring(0,10-k) + stars.substring(0,2*k+1)+"\n"); k = k + 1; } void Mond() { int k = 1; while (k < 10) { ta.append(spaces.substring(0,1+k) + stars.substring(0,19-2*k)+"\n"); k = k + 1; }

CompSci Writing Functions.3 public void actionPerformed(ActionEvent event){ Object cause = event.getSource(); if (cause == bDraw) { tf.setText("Goodbye"); Dia(); Mond(); }  No New Capability  Just packaged differently from Diamond.java code

CompSci Using Parameters  Now take advantage of parameters to set size of diamond public class DiaParms extends java.applet.Applet implements ActionListener { IntField gSize; TextArea ta; Button bDraw; String stars = "*******************"; String spaces = " "; public void init() { gSize = new IntField(10); ta = new TextArea(20, 20); ta.setFont(new Font("Monospaced", Font.BOLD, 12)); bDraw = new Button("Draw"); bDraw.addActionListener(this); add(gSize); add(bDraw); add(ta); }

CompSci Using Parameters.2 void Dia(int sz){ // this line changed int k = 0; while (k < sz){ // this line changed ta.append(spaces.substring(0,10-k) + stars.substring(0,2*k+1)+"\n"); k = k + 1; } void Mond(int sz){ // this line changed int k = sz; // this line changed while (k > 0) { k = k - 1; ta.append(spaces.substring(0,10-k) + stars.substring(0,2*k+1)+"\n"); }

CompSci Using Parameters.3 public void actionPerformed(ActionEvent event){ int sz; // this line new Object cause = event.getSource(); if (cause == bDraw) { sz = gSize.getInt(); // this line new Dia(sz); // this line changed Mond(sz-1); // this line changed }  Almost no difference in code for functions  Great difference in flexibility

CompSci The Partially Used Array  Often array is sized for worst case  Only use the “front” part of it in most cases  Need variable to keep track of how much we use o Such variable often called size or used  Saw an example in ArrayStats program: double mean(double[] list, int size) { int k = 0; double sum = 0.0; while (k < size) { sum = sum + list[k]; k = k + 1; } return sum/size; }

CompSci Functions that work on Arrays  The previous was an example of using an array parameter  Such functions that “work” on arrays are common  Possible Applications:  Sum values double arraySum(double data[], int size);  Count number of occurrences of a specific value int arrayCountZero(double data[], int size); int arrayCount(double data[], double val, int size)  Set all values to a constant value void arraySet(double data[], double val, int size);  Locate (search for) a particular value int arrayFind(double data[], double key, int size);

CompSci Information Retrieval  Often want to use program for information storage and retrieval  On-line phone book is good example  Using “Parallel” or “Corresponding Arrays”  Array (of Strings) for Names  Array (of Strings) for Phone Numbers Name Number “J.Able” “D.Ramm” “R.Odom” “M.Salter” “W.Tars” “ ” “ ” “ ” “ ” “ ” “ ”