CS 1312 - PowerPoint PPT Presentation

1 / 61
About This Presentation
Title:

CS 1312

Description:

www.cc.gatech.edu/classes/AY2001/cs1312. Office: College of Computing Room 121 ... Everything's a number. at some level. Operators. Assignment: ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 62
Provided by: ccGa
Category:

less

Transcript and Presenter's Notes

Title: CS 1312


1
CS 1312
  • Introduction to
  • Object Oriented Programming

2
Agenda
  • Brief Introduction
  • Course Mechanics
  • Warning Danger Will Robinson
  • Textbook
  • Some Java Basics

3
Brief Introduction (3pm)
  • Instructor Bill Leahy
  • bleahy_at_cc.gatech.edu
  • Web pages
  • www.cc.gatech.edu/classes/AY2001/cs1312
  • Office College of Computing Room 121
  • Office Hours Tues/Thurs 130 - 300 p.m. and by
    appointment email me! (bleahy_at_cc.gatech.edu)
  • Class CRN see OscarWeb

4
Course Mechanics
5
Contact Elements (305 pm)
  • Lecture
  • 2/week L4 Howey-Physics 305 p.m. lectures
  • quizzes administered here

6
Contact Elements
Recitation Normally 1 per week 605 - 755
p.m. Tues or Wed See your personal Oscar schedule
for details as to which night and which room.
This week only Help Sessions Tuesday and
Wednesday Instructional Center Room 209 605 -
755 p.m. Locations Tues or Wed unless noted
InstrCenter Rooms 105, 109(W), 111, 113,
115(T), 119, 205(T), 207, 209, 211, 213,
215, 217, 219
7
Contact Elements
Lab 1/week approx. (will be 3 per week early) On
your own. Unlike CS1311 (i.e., linked to course
content) Turn in via WebWork (help on web page
soon)
8
Your Lifelines
  • Your TA
  • Is your friend
  • Will have office hours
  • First point of contact for problems
  • Lecturer Office Hours
  • Tues/Thurs 130 - 300 or by appointment
  • Help Sessions
  • To be announced
  • Use them to your advantage

9
Evaluation
  • Quizzes
  • 2 / semester
  • Given during lecture
  • Labs
  • Coding Final
  • Timed
  • Watch out tends to have a bipolar grade
    distribution
  • Be prepared!!!

10
Evaluation
  • Final
  • Oscar is tentative
  • Official time/date will be announced
  • Comprehensive
  • Some coding
  • Some theory/general knowledge
  • Grading
  • TA by hand
  • Autograding
  • Cheating

11
Grade
  • 25 Programs
  • 5 Designs
  • 30 Tests (quizzes)
  • 5 BuzzBack!! (can even get extra credit)
  • 15 Labs
  • 20 Written final

12
BuzzBack
  • http//survey.opaion.com
  • Worth 5 of grade!
  • Due approximately weekly
  • Regular Buzzbacks get ½ each for completion
  • Last Buzzback get 1 for completion
  • 1st BuzzBack
  • Given this Friday 9am
  • Due Wednesday 8am
  • You need your gt number

13
Questions?
14
Why CS 1312?
  • Was CS 1302, was CS 1502
  • University System of the State of Georgia Course
    Numbering System
  • Large number of students received transfer credit
    for cs1501/cs1301/cs1311
  • Results

Most transfer students who skip cs1311 fail or
drop cs1312
If you skipped cs1311, used excessive word, or
forgot things, you will find 1312 difficult.
15
NOTE
  • CS 1311 is not a
  • Pseudocode or Scheme programming course
  • CS 1311 is about computing and algorithms
  • Similarly...
  • CS 1312 is not a Java programming course
  • CS 1312 is about programming

16
Tip 1
Dont wait until the last minute to get help
17
Tip 2
Hey, Ill still pass if I can get enough partial
credit.
Bad things happen while learning a new skill.
You will probably crash and burn on some
programs. Start early give yourself time for
mistakes.
18
Tip 3
Dont be too ambitious with your course load.
You CANNOT slack off in this class, even for a
few days.
19
Tip 4
Watch out for the big picture. Dont forget
this is a programming course, not a Java
course. Its dangerous to hide from the
programming part of the course. You may be
crushed on the final.
20
Bottom Line
  • If youre not adequately prepared for this
    course
  • Option 1 Get prepared
  • Option 2 Drop Now...Do us both a favor!
  • Will have take home quiz to test your knowledge
    of the prerequisites.

21
Textbooks
  • Data Structures Problem Solving Using Java
  • Mark Allen Weiss
  • CD-ROM Edition!!!

Crude Drawing of CD-ROM
22
Additional Resources
  • Download Bruce Eckels Book, Thinking in Java.
  • 1,000 pages.
  • Quite good.
  • Might just help.
  • Surf to
  • http//www.mindview.net
  • http//www.mindview.net/TIJ2/index.html

Possibly the worst web design ever seen keep
looking for the link!
23
Additional Resources
Sun also has a free 1,000 page book on
Java. You should download or bookmark
this resource.
http//www.javasoft.com/docs/books/tutorial/index.
html
24
Additional Resources The API for Java
We asked students a while ago what we could do to
improve the course. Many said
Why didnt you tell us about the API on the
first day?
The API is the Application Programmers
Interface, a useful document describing many of
Javas builtin features. For now, it might look
mysterious later it will be very useful.
25
Where to get this API thing
http//www.javasoft.com/docs/
26
NEW PACKAGING
2.
The Basics (Just the FAQs, Maam)
27
Introduction to Java
  • What Java is
  • A tool for programming well
  • Portable across any hardware platform that has a
    JVM interpreter
  • Relatively easy to learn if you have a good
    foundation
  • An object-oriented language
  • What Java is not
  • The Ultimate Programming Language
  • HTML or another web-content language
  • Only useful for web applets
  • Just Another Vacuous Acronym

28
Introduction to Java (contd)
  • Strengths of Java
  • A real language, in demand in industry
  • Portability
  • Comparatively easy to learn
  • Difficult to destroy a machine with it -)
  • Advanced built-in GUI/Graphics features
  • Weaknesses of Java
  • Slow interpreted and OO
  • GUI/Graphics via Least Common Denominator
    approach (due to platform independence)
  • Awkward/annoying syntax obscures some concepts
    and principles

29
Javas Popularity
  • Keys to Javas Popularity
  • An OO language thats relatively simple.
  • Virtual Machine approach, allowing
    transportability to various different kinds of
    computers (operating systems).
  • Presence of JVM as part of Web browsers,
    encouraging movement of Java programs over the
    Web.

30
What is Java?
"A simple, object-oriented, distributed,
interpreted, robust, secure, architecture
neutral, portable, high-performance,
multithreaded, and dynamic language -- Sun
Sounds like marketing lets take a closer look .
. .
31
object oriented
  • Java is an object oriented (or OO) language.
  • It is possible (but seldom desirable) to write
    non-object oriented Java code (so-called hybrid
    OO).
  • In an OO language, classes are used to
    encapsulate data (state) and behavior.
  • Instances of a class are then used to manipulate
    data and drive the program.
  • Classes are arranged in an hierarchy, or package
    structure.

32
object oriented,
Example A Stack is a class found in the
package java.util. We create an instance of the
class Stack using the keyword new thus import
java.util. Stack c new Stack() c.push(3)
java.util.Stack x new Stack()
This differs from procedural (e.g., C, Pascal)
and functional languages (e.g., Lisp) where other
data structures would be used to model the Stack.
More on classes in later slides . . .
33
distributed,
Java is also a distributed language. It was
built with networking in mind Fully supports
IPv4, with structures to support IPv6--protocols
used on the internet. Includes support for
Applets small programs embedded in HTML
documents. See java.net package
RMI/CORBA
34
interpreted,
Java is an interpreted language, meaning that
each instruction is translated into machine code
at the time of execution, not during
compilation. This allows for platform
neutrality WORA, or write once run
anywhere. This also allows one to rewrite
and change a program while it is
running. Penalty speed
35
robust,
Java is simple--no pointers/stack concerns (for
the most part) In-bound checking at runtime of
array pointers--no memory corruption and cryptic
error messages. Exception handling
try/catch /finally series allows for simplified
error recovery Strongly typed language many
errors caught during compilation.
36
secure,
Byte-code verification on loading (not just
compilation). Applet code runs in 'sandbox',
with significant restrictions Security is
enforced by the SecurityManager class
Work-arounds for applet security restrictions
include digitally signing code, and Servlets
37
Structure of Java Programs
  • Applications (normal computer programs)
  • Create one or more Java source files
  • Compile each source file into a class file
  • Thus an application will consist of a bunch of
    these class files. Not a single executable i.e.
    .exe
  • Send one class file to the Java system
  • It must have a method (module) called main
  • public static void main(String argv)
  • ( Get used to weird looking
    stuff! )
  • The main method controls program flow (but the OO
    orientation means that it starts a process that
    features decentralized control).

38
Sample Application (in a file called
HelloWorld.java)
  • public class HelloWorld
  • public static void main(String argv)
  • System.out.println(Hello World!)

39
Java File Names
Source code files must have the ".java"
extension. The file name should match the class
name. This naming convention is enforced by most
reasonable compilers. Thus, an improperly named
java file, saved as "myTest.java"
class test ... Compiled byte code has the
".class" extension.
Incorrect
40
Java File Names
Source code files must have the ".java"
extension. The file name should match the class
name. This naming convention is enforced by most
reasonable compilers. Thus, a properly named
java file, saved as "Test.java"
class Test ... Compiled byte code has the
".class" extension.
41
Big Picture Time
HelloWorld.java
HelloWorld.class
0xCAFEBABE ...
javac
public class HelloWorld public static void
main(String argv) System.out.println
(Hello World!)
java HelloWorld
javac HelloWorld.java
Recitation this week will cover emacs editor and
JDK
42
Java File Structure
Java Files 1. Consist of the optional package
statement, 2. followed by any necessary
import statements 3. followed by the class
name, 4. followed by any inheritance and
interface declarations. 5. Note if the file
defines more than one class or interface, only
one can be declared public, and the source file
name must match the public class name.
For cs1312, you will only follow points 2 - 4
43
An Average Java File
Thus package edu.gatech.cc.dagon.gsams-java
import java.util. import edu.gatech.cc.dagon.gs
ams-java.hashtable. import netscape.javascript.J
SObject import netscape.javascript.JSException
public class SplayTree implements TreeType,
TreeConstants ... //
SplayTree Note the globally unique package
name. Without a package specification, the
code becomes part of an unnamed default package
in the current directory.
For CS1312, youll need only work with class
names, a few import statements, and some
inheritance
44
Vocabulary
  • Structured Programming
  • A programming paradigm in which the actions (or
    verbs, or procedures) are emphasized.
  • OO Programming
  • A programming paradigm in which the actors (or
    nouns, or objects) and their interaction is
    emphasized.
  • Byte Compiler
  • A compiler which translates human-readable source
    code into byte code (transportable to various
    virtual machines) instead of object code written
    for a specific kind of machine.

45
Vocabulary (contd)
  • Byte Interpreter
  • An interpreter which translates byte code into
    object code for a particular kind of machine and
    executes them on that machine.
  • Byte Code
  • An instruction for a virtual machine.
  • Java Virtual Machine (JVM)
  • The virtual machine (software) for which all Java
    programs are compiled. A byte code interpreter
    is required to translate from the JVM byte code
    instructions into to instructions for a given
    actual machine.

46
Java Portability
Demo.java
javac Demo.java
Demo.class
java Demo
47
Built-in Data Types
  • CS1501/CS1311 (4 atomic data types String)
  • Num (number)
  • Char (character)
  • Boolean
  • Ptr (pointer)
  • String
  • Java (6 important primitives String)
  • int (integer)
  • long (long integer, 2x bits)
  • float (real number)
  • double (real number, 2x bits)
  • char (character, use single quotes b)
  • boolean
  • String (Java is case sensitive, so capitalize
    first letter here String, not string use double
    quotes a string)

Note A String is NOT a primitive
48
List of Data Types
Primitive Type Default Value boolean
false char
'\u0000' (null) byte (byte) 0 short
(short) 0 int
0 long
0L float 0f double
0d void
N/A
Note At times, the Java Language Specification
refers to void as a primitive, though other parts
(e.g., s. 14.7) say void is not a primitive as
in C/C. One cannot cast to a void type in Java.
49
Variable Declarations
  • CS1501/CS1311
  • ltidentifiergt isoftype ltdata typegt
  • Java
  • ltdatatypegt ltidentifiergt
  • or (optional initialization at declaration)
  • ltdata typegt ltidentifiergt ltinit valuegt

50
Examples
  • int counter
  • int numStudents 583
  • float gpa
  • double batAvg .406
  • char gender
  • char gender f
  • boolean isSafe
  • boolean isEmpty true
  • String personName
  • String streetName North Avenue

51
Questions?
52
Assignment
  • Java allows multiple assignment.
  • int iStart, iEnd
  • int iWidth 100, iHeight 45, iLength 12
  • This tends to complicate javadoc comments,
    however
  • /
  • Declare cylinders diameter and height
  • /
  • int iDiameter 50, iHeight 34

Javadoc comment gets repeated twice in
output, once above each listed variable!
53
Examples
  • Note that whole integers appearing in your source
    code are taken to be ints. So, you might wish
    to flag them when assigning to non-ints
  • float fMaxGrade 100f // now holds 100.0
  • double dTemp 583d // holds double precision
    583
  • float fTemp 5.5 // ERROR!
  • // Java thinks 5.5
    is a double
  • Upper and lower case letters can be used for
    float (F or f), double (D or d), and long
    (l or L, but we should prefer L)
  • float fMaxGrade 100F // now holds 100.0
  • long x 583l // holds 583, but looks
    like 5,381
  • long y 583L // Ah, much better!

54
Primitive Casting
  • Conversion of primitives is accomplished by (1)
    assignment with implicit casting or (2) explicit
    casting
  • int iTotal 100
  • float fTemp iTotal // fTemp now
    holds 100.0
  • When changing type results in a loss of
    precision, an explicit cast is needed. This is
    done by placing the new type in parens
  • float fTotal 100f
  • int iTemp fTotal // ERROR!
  • int iStart (int) fTotal
  • We will see much, much more casting with
    objects (later) . . .

55
Casting Test Your Knowledge
  • Given
  • int iStart 10
  • float fTemp 5.5
  • fTemp fTemp (float)iStart
  • What does iStart now hold?

Trick question
Remember Everythings a number at some level
56
Operators
  • Assignment
  • Arithmetic , -, , /, (mod), and others
  • int iNumLect 2
  • int iNumStudents 583
  • int iStudentsPerLect
  • iStudentsPerLect iNumStudents / iNumLect
  • // gives 291 due to integer division
  • int iNumQualPoints 30
  • int iNumCreditHours 8
  • float fGPA
  • fGPA iNumQualPoints / iNumCreditHours
  • // gives 3.0 due to integer division
  • iVar iVar fVar // gives compile-time error

57
Test Your Knowledge
SOLUTION VARIETY PACK
  • Heres the problem
  • int iVar 10
  • float fVar 23.26f
  • // gives compile-time error
  • iVar iVar fVar
  • Which solution works best?

3
230
4
1
232
232
2
Lesson write code thats easily understood.
Same Compile Error
58
Shorthand Operators
  • iCounter iCounter 1 //OR iCounter
  • iCounter iCounter - 1 //OR iCounter--
  • iCounter iCounter 2 //OR iCounter2
  • iCounter iCounter 5 //OR iCounter5Last
    two examples its op then equals (e.g.,
    2), not equals then op (e.g., isnt 2)
  • We will see examples with recursion where the
    shorthand operator potentially causes a problem.

59
Documentation Comments
  • Three ways to do it
  • // Double slashes comment out everything until
    the end of the line
  • / This syntax comments out everything between
    the / and the /.
  • (There are no nested comments as in C. /
  • /
  • This is syntax for Javadoc comments (similar
    to second style
  • of commenting, but allows for HTML formatting
    features.
  • /
  • For CS1312, use Javadoc comments

60
Some Comments on Comments
1. C-style comments with / / no nesting 2.
C style comments beginning // 3. A unique
"doc comment" starting with / .../ Fun with
comments // / // /
/////////////////// / /
Lesson Comments should be helpful dont worry
about compiler tricks with syntax.
61
Commenting Factoids
  • Watch for comments that open, but never close
  • int x, y /
  • Here, we declare the
  • the point coordinates.
  • // int z /

Lesson Java encourages clear code through the
type of operators and comments it allows!
A syntax highlighting editor is your friend!
62
Javadoc
/ ltPREgt Get the name. Returns the
name at a specified array location. lt/PREgt
_at_param i the index of the array to be
retrieved. _at_return strName the name _at_see
EmployeesisEmployed() called to verify
employment / public String getName (int
i) if (myEmpl.isEmployed()) return
myArrayi else return "Nada" //
getName(int)
63
Javadoc (Contd)
  • You may include HTML tags (but avoid structuring
    tags, like ltH1gt, etc.)
  • Javadoc comments should immediately preceed the
    declaration of the class, field or method. The
    first sentence should be a summary. Use the
    special javadoc tags--_at_. When '_at_' tags are used,
    the parsing continues until the doc compiler
    encounters the next '_at_' tag.

_at_see ltclass namegt _at_see ltfull-class namegt
_at_seeltfull-class namemethod.namegt _at_v
ersion
_at_author _at_param _at_return
_at_exception
_at_deprecated // jdk 1.1
_at_since
// jdk 1.1 _at_serial // jdk 1.2
64
Constants
  • CS1501/CS1301
  • ltCONST_IDgt is ltconstant valuegt
  • MIN_PASSING is 60
  • PI is 3.14159
  • Java
  • public final static lttypegt ltIDergt ltvaluegt
  • public final static int iMIN_PASSING 60
  • public final static float fPI (float) 3.14159
  • Details on why this syntax to come soon...

65
Printing to Screen
  • CS1501/CS1301
  • print (ltargumentsgt)
  • Java
  • System.out.println(ltargumentgt)
  • System.out.println( ) // prints blank line
  • System.out.println(5) // prints 5
  • System.out.println(Hello World) // prints
    Hello World
  • println vs. print in Java
  • println includes carriage return at end, next
    print or println on new line
  • print causes next print or println to begin at
    next location on same line

66
Printing (contd)
  • When starting Java, there are at least three
    streams created for you
  • System.in // for getting input
  • System.out // for output
  • System.err // for bad news output
  • These are InputStream and PrintStream objects
  • Note For Win95/NT System.out is "almost"
    identical to System.err they both display to the
    screen (the one exception is when using DOS
    redirect, gt, where System.out is redirected,
    while System.err is still put to the screen.)

67
Printing (contd)
System.out.println ("This line is printed
out") System.err.println ("This is the error
stream's output") These are both instances of
the PrintStream class. There are other methods
you might find useful in these classes
System.out.flush() System.out.write(int)
System.out.write(byte buf, int offset, int
length) // eek!
Dont use in 1312 (needed for autograder)
68
Summary
  • Java Basics Summary
  • Java Programs
  • JVM, applications applets
  • Entry point is main() or init()
  • Java Primitives and Operators
  • Primitive data types vs. 1501/1301 types
  • Operators straightforward except for and
  • Your new best friend
  • System.out.println( )

69
Questions
70
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com