CS1316: Representing Structure and Behavior

Slides:



Advertisements
Similar presentations
INTRODUCTION Chapter 1 1. Java CPSC 1100 University of Tennessee at Chattanooga 2  Difference between Visual Logic & Java  Lots  Visual Logic Flowcharts.
Advertisements

CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 1: Objects for Modeling a World.
1 CS428 Web Engineering Lecture 18 Introduction (PHP - I)
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Games and Simulations O-O Programming in Java The Walker School
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 2: Introduction to Java.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Introduction to Java CS1316: Representing Structure and Behavior.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Object Orientation “Thinking” Object Oriented, Further Constraints, and Documentation.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
JAVA Practical Creating our first program 2. Source code file 3. Class file 4. Understanding the different parts of our program 5. Escape characters.
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Working With Objects Tonga Institute of Higher Education.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Object Orientated Programming in Perl Simulated Models of Termites.
Introduction to Programming (CS 201) Lecture 01 - Introduction.
Tarik Booker CS 120 California State University, Los Angeles.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
Classes, Objects, and Interfaces CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
Content Programming Overview The JVM A brief look at Structure – Class – Method – Statement Magic incantations – main() – output Coding a Dog Programming.
CSC 108H: Introduction to Computer Programming
CS1316: Representing Structure and Behavior
Andrew(amwallis) Classes!
Whatcha doin'? Aims: To start using Python. To understand loops.
Building Java Programs
Content Programming Overview The JVM A brief look at Structure
CS1316: Representing Structure and Behavior
Introduction to Programming (CS 201)
Containers and Lists CIS 40 – Introduction to Programming in Python
Week 7 - Wednesday CS 121.
CS1315: Introduction to Media Computation
CS1316: Representing Structure and Behavior
Creating and Modifying Text part 2
Introduction to Objects
Stack Data Structure, Reverse Polish Notation, Homework 7
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Representing Structure and Behavior with Trees
CSE341: Programming Languages Section 1
CS1316: Representing Structure and Behavior
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
CSE341: Programming Languages Section 1
Introduction to Java part 2
CS1316: Representing Structure and Behavior
CS005 Introduction to Programming
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Introduction to Media Computation
Type & Typeclass Syntax in function
Declaring Variables – Mod 4
Barb Ericson Georgia Institute of Technology Oct 2005
Workshop for Programming And Systems Management Teachers
CLASSES, AND OBJECTS A FIRST LOOK
Tonga Institute of Higher Education IT 141: Information Systems
Tonga Institute of Higher Education IT 141: Information Systems
Methods 16-May-19.
References Revisted (Ch 5)
Object-Oriented Programming and class Design
Introduction to Objects
Presentation transcript:

CS1316: Representing Structure and Behavior Introduction to Java CS1316: Representing Structure and Behavior

Story Getting started with Java Basics of Java Installing it Using DrJava Basics of Java It’s about objects and classes, modeling and simulation, what things know and what they can do. Examples from Pictures, Sounds, and music.

Things to do to get started Download and install JDK (Java Development Kit) Download and install DrJava Download JMusic Download the Java source files for class Then, tell Java where to find the JMusic and Java source files. DrJava will ask you for tools.lib first time. Should do this live (will see it in recitation, too). Mention: Common problem – copying files in from CD, but becoming READ ONLY.

Telling DrJava where to find files For JMusic, need jmusic.jar and inst/ folder

Parts of DrJava List of class files that you have open Text of your class file (.java) Where you interact with Java

Java is object-oriented 95% of everything in Java is an object. In object-oriented programming, you care about more than just specifying the process. You care about who (or what) does the process, And how the overall process emerges from the interaction of different objects.

Object-oriented programming is about modeling and simulation The whole idea of object-oriented programming is to create a model of the part of the world (real or imaginary). Creates constraints: The real world doesn’t have one set of rules/steps. ( You don’t write one big program. In the real world, no one knows everything, no one can do everything. Each object has it’s own things it knows and things it can do.

Variables in Java know their types Variables in Java know that kinds of things (values) they can hold. Objects in Java are organized into classes. A class specifies what all the objects of that class know and can do. All pictures can show themselves, even though each picture is different. Variables in Java are specific to particular classes. We declare a variable to only hold objects of particular classes.

Do this live. The variable “p” knows that it holds pictures.

Explaining what’s going on Every line ends with a semi-colon in Java. (DrJava doesn’t always require it.) Picture is the name of a class in Java. p is the variable that we’re declaring > Picture p; > p = new Picture("D:/cs1316/MediaSources/Swan.jpg"); > p.show(); You’re safest always putting in semi-colons, unless you want to get a print of the meaning. DrJava will actually let you use a variable before declaring it, but that’s dangerous. It gets you in bad habits. In Java programs, You can only use declared variables! And you can only declare them once!

Explaining what’s going on new Picture() creates a new picture. The pathname provided as an argument tells it which picture. You can always use “/” and it’ll always work (on any platform) p now refers to the new object (instance of class Picture) > Picture p; > p = new Picture("D:/cs1316/MediaSources/Swan.jpg"); > p.show();

Explaining what’s going on Instances of the class Picture (objects created from the class Picture) know how to show themselves. We access what the object knows and can do with the dot operator. p.show() says “Object that p refers to, would you please execute your show() method?” > Picture p; > p = new Picture("D:/cs1316/MediaSources/Swan.jpg"); > p.show();

Semicolons or not in DrJava Interactions Pane No semi-colon says “Evaluate this, and show me the result.” Semi-colon says “Treat this like a line of code, just as if it were in the Code Pane.” > p Picture, filename D:/cs1316/MediaSources/Swan.jpg height 360 width 480 > p; >

Summarizing the terms so-far Just about everything in Java is an object Objects know specific things and can do specific things Things they know are stored in variables (data) Things they can do are grouped into methods Think of methods as “functions known only to instances of that class.” Objects are instances of a given class in Java. All the instances know the same things and can do the same things. Variables are specific to a given class, and can only refer to objects of that type.

Let’s create a sound

Explaining what’s going on > Sound s = new Sound(FileChooser.pickAFile()); > s.play(); We can create an object as we declare the variable. FileChooser is an object that knows how to pickAFile() which puts up a file picker and returns a string. Instances of the class Sound know how to play, thus s.play()

What if we get it wrong?

An example with Music > import jm.util.*; > import jm.music.data.*; > Note n1; > n1 = new Note(60,0.5); > // Create an eighth note at C octave 4 JMusic pieces need to be imported first to use them.

An example with Music > import jm.util.*; > import jm.music.data.*; > Note n1; > n1 = new Note(60,0.5); > // Create an eighth note at C octave 4 Declare a Note variable.

An example with Music > import jm.util.*; > import jm.music.data.*; > Note n1; > n1 = new Note(60,0.5); > // Create an eighth note at C octave 4 Note instances have nothing to do with filenames. To create a note, you need to know which note, and a duration Starting a line with // creates a comment—ignored by Java

MIDI notes No, they don’t have to write these all down. They’re in the book.

Making more notes > Note n2=new Note(64,0.5); > View.notate(n1); Error: No 'notate' method in 'jm.util.View' with arguments: (jm.music.data.Note) > Phrase phr = new Phrase(); > phr.addNote(n1); > phr.addNote(n2); > View.notate(phr); -- Constructing MIDI file from'Untitled Score'... Playing with JavaSound ... Completed MIDI playback -------- There is no method notate in the View class that takes a note. You need to pass it a Phrase object.

What’s going on here? > Note n2=new Note(64,0.5); > View.notate(n1); Error: No 'notate' method in 'jm.util.View' with arguments: (jm.music.data.Note) We’ll make another Note (at E4, another eighth note) There is an object named View that knows how to notate parts of music, but not an individual note.

What’s going on here? > Phrase phr = new Phrase(); > phr.addNote(n1); > phr.addNote(n2); > View.notate(phr); -- Constructing MIDI file from'Untitled Score'... Playing with JavaSound ... Completed MIDI playback -------- We’ll create a new Phrase instance and make a variable phr to refer to it. (phr has to be declared to be a Phrase.) Phrase instances know how to addNote notes to them. These are methods that take an argument—a Note instance. The View object does know how to notate an input Phrase instance. It generates this cool window where you see the notes and can play them (or save them as MIDI.)

Playing a different Phrase > Phrase nuphr = new Phrase(0.0,JMC.FLUTE); > nuphr.addNote(n2); > nuphr.addNote(n1); > View.notate(nuphr); We can specify when a phrase starts and with what instrument. We can add notes (even the same notes!) in different orders

Modeling Music The JMusic package is really modeling music. Notes have tones and durations. Musical Phrases are collections of notes. We can play (and View) a musical phrase. A phrase doesn’t have to start when other phrases do, and a phrase can have its own instrument.

Objects know things and can do things What instances of this class know What instances of this class can do Note A musical note and a duration <Nothing we’ve seen yet> Phrase The notes in the phrase addNote(aNote)