CompSci 100E http://www. cs. duke CompSci 100E http://www.cs.duke.edu/courses/spring07/cps100e Dietolf (Dee) Ramm http://www.cs.duke.edu/~dr.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Advertisements

Lecture 2: Object Oriented Programming I
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
CPS 100, Fall COMPSCI 100, Fall 2011 Owen Astrachan w/ Michael Hewner
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CPS 100, Spring COMPSCI 100, Spring 2010 Owen Astrachan
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The Java Programming Language
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
Classes CS 21a: Introduction to Computing I First Semester,
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CPS 100, Fall COMPSCI 100, Fall 2009 Owen Astrachan
Chapter 2: Java Fundamentals
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
CompSci 100E 1.1 CompSci 100E Dietolf (Dee) Ramm Robert A. Wagner
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
CPS What is Computer Science? What is it that distinguishes it from the separate subjects with which it is related? What is the linking thread.
CPS 100, Spring COMPSCI 100, Spring 2009 Owen Astrachan
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
OOP Basics Classes & Methods (c) IDMS/SQL News
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
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[]
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Java Fundamentals MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
CompSci 100 Prog Design and Analysis II
Information and Computer Sciences University of Hawaii, Manoa
Objects as a programming concept
JAVA MULTIPLE CHOICE QUESTION.
3 Introduction to Classes and Objects.
Java: Base Types All information has a type or class designation
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
CompSci 100 Dietolf (Dee) Ramm
Chapter 3: Using Methods, Classes, and Objects
Data types and variables
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
An Introduction to Java – Part I, language basics
An overview of Java, Data types and variables
Group Status Project Status.
Chapter 1: Computer Systems
Focus of the Course Object-Oriented Software Development
Chapter 2 Programming Basics.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Defining Classes and Methods
Classes CS 21a: Introduction to Computing I
Defining Classes and Methods
In this class, we will cover:
Unit 3: Variables in Java
Chap 2. Identifiers, Keywords, and Types
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
COMPSCI 100, Spring 2008 Owen Astrachan
Presentation transcript:

CompSci 100E http://www. cs. duke CompSci 100E http://www.cs.duke.edu/courses/spring07/cps100e Dietolf (Dee) Ramm http://www.cs.duke.edu/~dr

What is Computer Science? What is it that distinguishes it from the separate subjects with which it is related? What is the linking thread which gathers these disparate branches into a single discipline? My answer to these questions is simple --- it is the art of programming a computer. It is the art of designing efficient and elegant methods of getting a computer to solve problems, theoretical or practical, small or large, simple or complex. C.A.R. (Tony)Hoare

Programming != Computer Science What is the nature of intelligence? How can one predict the performance of a complex system? What is the nature of human cognition? Does the natural world 'compute'? It is the interplay between such fundamental challenges and the human condition that makes computer science so interesting. The results from even the most esoteric computer science research programs often have widespread practical impact. Computer security depends upon the innovations in mathematics. Your Google search for a friend depends on state-of-the-art distributed computing systems, algorithms, and artificial intelligence. http://www.post-gazette.com/pg/pp/04186/341012.stm

Efficient design, programs, code Using the language: Java (or C++, or Python, or …), its idioms, its idiosyncracies Object-oriented design and patterns. Software design principles transcend language, but … Know data structures and algorithms. Trees, hashing, binary search, sorting, priority queues, greedy methods, … Engineer, scientist: what toolkits do you bring to programming? Mathematics, design patterns, libraries --- standard and Duke CPS

Course Overview Lectures, Labs, Quizzes, Programs Lectures based on readings, questions, programs Online quizzes used to motivate/ensure reading In-class questions used to ensure understanding Programs Theory and practice of data structures and OO programming Fun, practical, tiring, … Weekly programs and longer programs Labs based on current work Get in practical stuff Become familiar with tools Exams/Tests (closed book) Two “midterms” Final

Questions If you gotta ask, you’ll never know Louis Armstrong: “What’s Jazz?” If you gotta ask, you ain’t got it Fats Waller: “What’s rhythm?” What questions did you ask today? Arno Penzias

Tradeoffs Simple, elegant, quick, efficient: what are our goals in programming? What does XP say about simplicity? Einstein? Programming, design, algorithmic, data-structural Fast programs, small programs, run anywhere-at-all programs. Runtime, space, your time, CPU time… How do we decide what tradeoffs are important? Tension between generality, simplicity, elegance, …

OO design in code/wordcount Count number of different words in an array, how can we accommodate more than one approach? public interface UniqueCounter { public int uniqueCount(String[] list); } Three (or more) approaches:

Fast, cheap, out-of-control? This is valid and correct Java code, questions? import java.util.*; public class SetUniqueCounter implements UniqueCounter { public int uniqueCount(String[] list) { TreeSet set = new TreeSet(); set.addAll(Arrays.asList(list)); return set.size(); }

Some Java / Matlab Differences Compile & Execute vs Interactive In Java, compile, then run (execute) – like .m files Matlab executes as you type in program Java requires declaration of variables Need to tell about the variable before creating Declaration is distinct from Definition (creation) Java is not matrix oriented Operators (+, -, *, /, %), do not work on matrices You must write code with loops for matrix operations - or use functions (methods)

Some Java / Matlab Differences No exponentiation operator!!! Cannot say X^3 for X3 Use X*X*X or a function Syntax differences Use of braces, { ... }, in place of xxx … end Semicolon has somewhat different meaning Use quotes, ” ... ”, for strings not ’... ’ Loops and if require parentheses ( ... ) You’ll find many more differences Will be an annoying, but transient problem

Some Java Vocabulary and Concepts Java has a huge standard library Organized in packages: java.lang, java.util, javax.swing, … API browseable online, but Eclipse IDE helps a lot Java methods have different kinds of access inter/intra class Public methods … Private methods … Protected and Package methods … Primitive types (int, char, double, boolean) are not objects but everything else is literally an instance of class Object foo.callMe();

Java Basics (ala Goodrich & Tamassia) Everything is in a class A minimal program: public class Hello { public static void main(String[] args) { System.out.println(”Hello Computer Science”); } Output? Where? Do colors mean something? Identify the pieces . . .

Java Basics Objects Every object is an instance of a class (which defines its type) Objects contain data (state) and function (methods) State Stored in instance variables (fields, members) Can be base types (e.g., integers) or instances of (objects) of other classes (e.g., String) Function Expressed as methods (subroutines, functions, procedures…) These define the behavior of objects of this class

Java Basics Writing a Class: Identify the methods by kind Constructor public class Counter { protected int count; public Counter() { count = 0; } public int getCount() { return count; public void incCount() { count = count + 1; public void decCount() { count = count – 1; Identify the methods by kind Constructor Accessor Mutator (modifier) Note Syntax from this and previous examples Braces Semicolons Parentheses Indentifiers . . .

Java Basics Class Modifiers Reserved Words Comments Abstract, final, public, default Reserved Words May not be used as identifiers Shown in red by Eclipse and in many of our examples See table in text (p4) or in any Java text Comments For human consumption: ignored by compiler Inline comments: // Effective for rest (to end) of current line Block comments: /* */ Effective between start and stop groups

Java Basics Primitive Types (base types) Built-in data types; native to most hardware Note: not objects (will use mostly first four) boolean (1bit) int (4 bytes) double (8 bytes) char (2 bytes) Constants/Literals (by example): boolean f = false; int i = 32769; double d = 0.333333; char c = ’x’; byte (1 byte = 8 bits) short (2 bytes) long (8 bytes) float (4 bytes) byte b = 33; short s = 21; long l = 289L; float x = 3.141592F;

Java Basics Creating and Using Objects (Example) public class Example { public static void main(String[] args) { Counter c; // Counter defined on a previous slide Counter d = new Counter(); c = new Counter(); System.out.println(”c = ” + c.getCount() + ” d = ” + d.getCount()); c.incCount(); d.decCount(); d = c; // what does this really mean??? d.incCount(); }

Java Basics String Objects String Concatenation (+) string is a sequences of characters (char) Unicode (16 bit) String is a built-in class Constants: ”this is an example” String Concatenation (+) String s = ”Happy birthday to you.”; s = s + ”\n” + s; System.out.println(s); // what ?

Java Basics Object References When creating object with new, get location or address of new object Typically assign this to a reference variable: Counter c = new Counter(); Every object reference variable refers to object or null Null is an important value that indicates object not created or not available. Can have multiple references to same object Access members of class using dot operator (“.”). c.incCount(); May have multiple methods with same name but different signature: e.g.: c.incCount(); c.incCount(5);

Java Basics Instance Variables Classes have 0 or more instance variables Also called fields Keep state of object May be primitive type E.g. int, double May be reference type (object) E.g., String, Counter, (an array), . . . If public can: Access or alter reference variables using dot operator Counter c = new Counter(); System.out.println(c.count + ” = ” + c.getCount());

Java Basics Variables Modifiers: scope Other Variable Modifiers public Anyone can access protected Only subclass or same package may access private Only methods of same class may access (omitted) default Anyone in same package may access Other Variable Modifiers static Associated with whole class, shared among instances final Must be initialized, then not changed: CONSTANT

Java Basics - Methods Methods Like functions, procedure, subroutines, . . . Has header and body Syntax: modifiers type name(parameter_declarations){ method_body } Modifiers like those of variables: public, private, protected, static, final Type is return type and give type of information being passed back Name is any valid Java identifier name Parameters define type of info being passed into method

Java Basics - Methods Method modifiers Return types public: anyone can invoke (call) protected: only called from subclass of same package private: only called from same class (omitted) (default): only called from same package abstract: has no code (must be dealt with in subclass) final: cannot be overridden (modified) in subclass static: associated with class, not with instance Return types Use void is no information to be returned (procedure) Use actual type of information to be returned (function) requires return statement(s) only one item returned (may be compound object, e.g., array)

Java Basics - Methods Parameters Constructors Parameter list may be empty (parentheses still required). Parameter list consists of comma separated pairs of types and parameter names. public void setAge(String name, int age){…} Constructors Used to initialize new objects Has same name as class and no return type public Counter() { count = 0; } public Professor(String aName, String aDept){ name = aName; department = aDept;

Java Basics Using a Constructor Invoked using a new operator Examples: Professor compSciProf = new Professor(”Jeff Chase”, ”Computer Science”); Counter tally = new Counter(); Class may have multiple constructors as long a signatures are different If class has no constructors defined, then a default constructor is used that does not initialize anything

Java Basics - Methods The main Method Required for an Application This is a stand-alone Java program Typically invoked from a command line Must include the following code: public static void main(String[] args){ // main body of the main method } (The parameter name args can actually be any name you choose.) Argument may be used to pass command line arguments to the program.

Java Basics - Methods Blocks and Local Variables Body of a method is a block: a sequence of statements and declarations enclosed in braces ( { }); Blocks may have blocks nested inside Variables declared with a block are known only in that block These variables care called local variables (We say their scope is limited to that block.) (Method parameters are also local to that method.) Examples: public static int sumThree(int a, int b, int c){ int sum; int partsum = a + b; sum = partsum + c; return sum; } a, b, c, sum, and partsum are all local to that method