Docsity

Introduction to Java - Lecture Slides | CS 1316, Study notes of Computer Science

Toggle sidebar
bg1
1
Introduction to Java
CS1316: Representing
Structure and Behavior
Story
Getting started with 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.
Telling DrJava where to find
files
Parts of DrJava
Text of
your
class file
(.java)
Where you
interact
with Java
List of
class
files that
you have
open
Java is object-oriented
95% of everything in Java is an object.
In object-oriented programming, you
care about more than j ust specifying the
process.
•You care about who (or what) does the
process,
•And how the overall process emerges from
the interaction of different objects.
pf3
pf4
pf5

Partial preview of the text

Download Introduction to Java - Lecture Slides | CS 1316 and more Study notes Computer Science in PDF only on Docsity!

Introduction to Java

CS1316: Representing

Structure and Behavior

Story

 Getting started with 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.

Telling DrJava where to find

files

Parts of DrJava

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

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 the 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. Did you get an error?  If you got an error as soon as you typed Picture p; there are several possibilities.
  • All the Java files we provide you are in source

form.

  • You need to compile them to use them.
  • Open Picture.java and click Compile All
  • You might not have your Preferences set up

correctly. If Java can’t find Picture, you can’t

use it.

Explaining what’s going on

> Picture p;

> p = new

Picture("D:/cs1316/

MediaSources/Swa

n.jpg");

> p.show();

  • 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 In Java programs, You can only use declared variables! And you can only declare them once! Explaining what’s going on

> Picture p;

> p = new

Picture("D:/cs1316/

MediaSources/Swa

n.jpg");

> p.show();

  • 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)

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 import ed 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 Starting a line with // creates a^ duration comment— ignored by Java MIDI notes Making more notes

Note n2=new Note(64,0.5); View.notate(n1); Error: No 'notate' method in ' (jm.util.Viewjm.music.data.Note' with arguments:) 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 --- ----- 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

> import jm.JMC;

> 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 in a

sequence.

  • 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 The notes in the addNote(aNote) phrase

Phrase

<Nothing we’ve seen yet> A musical note and a duration

Note

What

instances of

this class can

do

What

instances of

this class

know