CSIS 110 Lecture 2: Understanding Programs and Algorithms in Java - Prof. Brian F. Hanks, Study notes of Javascript programming

An introduction to the concepts of programs and algorithms using the java programming language. It covers the difference between a program and an algorithm, the role of a compiler and interpreter, and the basic structure of a java program. Students will learn about the importance of bytecode and the java virtual machine (jvm) in executing java programs on various devices.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-9cq-1
koofers-user-9cq-1 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 – Lecture 2
What is a program?
A recipe for doing something
A precise set of instructions - Generally from a limited set of available
instructions
Like the rules for a game, or how to build something, or directions to your house,
or a recipe for macaroni and cheese
What is an algorithm?
A set of steps for solving a problem.
Each step is effective
oDoable, in a finite amount of time
Each step is precise
oUnambiguous and uniquely interpreted
The algorithm terminates
There are Inputs and Outputs
Last time we looked a psuedocode: An English-like description of an algorithm.
Java
Start with source file
viewed with an editor
understandable by a human
Not directly executable by a computer – cannot run the source file.
The name of the .java file usually matches the name of the class it contains
oCountSpaces.java contains the class CountSpaces
oCapitalization must be the same.
Use compiler to translate source code into another language
For Java, compiler translates source code into Java bytecode
Java bytecode is machine independent
Compilers for other languages (such as C++ or FORTRAN) produce machine
code
oDirectly executable by a computer
oNot machine independent. Can only be executed on the type of computer
on which it was compiled.
javac CountSpaces.java
oproduces CountSpaces.class
DRAW PICTURE
Use interpreter to execute java bytecode
Java Virtual Machine (JVM) - java
Can execute java bytecode on any computer or device that has a JVM
pf3

Partial preview of the text

Download CSIS 110 Lecture 2: Understanding Programs and Algorithms in Java - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 2

What is a program?  A recipe for doing something  A precise set of instructions - Generally from a limited set of available instructions  Like the rules for a game, or how to build something, or directions to your house, or a recipe for macaroni and cheese What is an algorithm?  A set of steps for solving a problem.  Each step is effective o Doable, in a finite amount of time  Each step is precise o Unambiguous and uniquely interpreted  The algorithm terminates  There are Inputs and Outputs Last time we looked a psuedocode: An English-like description of an algorithm. Java Start with source file  viewed with an editor  understandable by a human  Not directly executable by a computer – cannot run the source file.  The name of the .java file usually matches the name of the class it contains o CountSpaces.java contains the class CountSpaces o Capitalization must be the same. Use compiler to translate source code into another language  For Java, compiler translates source code into Java bytecode  Java bytecode is machine independent  Compilers for other languages (such as C++ or FORTRAN) produce machine code o Directly executable by a computer o Not machine independent. Can only be executed on the type of computer on which it was compiled.  javac CountSpaces.java o produces CountSpaces.class DRAW PICTURE Use interpreter to execute java bytecode  Java Virtual Machine (JVM) - java  Can execute java bytecode on any computer or device that has a JVM

 Can create program on Windows computer and compile it using javac.  Mail class file to your friend who has a Mac  Your friend can execute your program on his Mac with his JVM  Why is this important? o Bytecode files are smaller than source files, so sending them over the Internet is more efficient o Companies who develop software don’t want people to have access to their source code. o Some devices may not be capable of compiling programs, but they can run the JVM. Let’s look at the language: Project DisplayForecast.java Key elements of the program

  • three statements that display the quote
  • method main() o Method is a named unit of code that performs some action o Every Java program must have a main() method o Your program starts running with the first statement in main(). o main() contains a set of statements. These are contained between the {}.
  • Main() is a method of the class DisplayForecast
  • Class defines the form of an object o Objects have methods (behaviors) o Objects may have attributes (data fields or values) o Class has same name as its file (with same upper/lower case)
  • Comments o Comments are for people o Many people look at programs – comments are there to help these people o Computer ignores the comments – doesn’t affect how the program is compiled.
  • Indenting o Lines are indented if they are a component of the preceding material. o Indent consistently o Again, computer doesn't care about this, but people do!
  • Keywords and identifiers o Keywords are reserved words that have specific meanings in Java o Examples: public, class, static, void o Identifiers are names we use to refer to methods, classes, variables o Is main a keyword?  main is not a keyword – it is an identifier (the name of a method).
  • Identifier rules o Must begin with alphabetic char, $, or _ o Subsequent chars: alpha, digit, $, or _ o Alpha includes foreign language chars such as π.