Topics for today 2499 - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Topics for today 2499

Description:

Assign values to them. Invoke methods on them. Access their data (if allowed) More on objects ... Assigning a value (constructing or instantiating) to a class variable ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 13
Provided by: arijits
Category:
Tags: assign | today | topics

less

Transcript and Presenter's Notes

Title: Topics for today 2499


1
Topics for today (2/4/99)
  • Todays buzzwords
  • Objects and methods (from prev. lecture)
  • Exercise - playing with Strings
  • Intro to Applets - the ccj way
  • Syntax summary

2
Todays buzzwords
  • Objects and Classes
  • Methods
  • Abstraction
  • Keeping the internals hidden from outside users
  • Escape characters
  • Special characters that cannot be used otherwise
  • Debugging
  • Getting rid of those darn bugs!

3
Objects
  • Like strings, there are many other types (or
    classes) of objects in Java.
  • These classes can be built-in, or they can be in
    libraries that you can use.
  • With classes, you can
  • You can define variables of these class types
  • Assign values to them
  • Invoke methods on them
  • Access their data (if allowed)

4
More on objects
We use a fictitious class Automobile for these
examples.
  • Declaring a variable of type a class
  • Automobile myCar
  • Assigning a value (constructing or instantiating)
    to a class variable
  • myCar new Automobile(Toyota)
  • myCar new Automobile(Toyota, 1992)
  • Invoking a method on a class variable
  • miles myCar.odometerReading()

5
Back in Time()
  • Declaring a Time object
  • Time flies
  • Creating a new Time object
  • Time now new Time()
  • Time yesterday new Time(1999,2,3,0,0,0)
  • Running methods on the Time object
  • int hourNow now.getHours()
  • int secondElapsed
  • now.secondsFrom(yesterday)

6
Why do we use objects?
  • Users dont need to worry about how something is
    implemented (abstraction)
  • How does the Time object find out what time it
    is?
  • How does it calculate the number of seconds
    between two times?
  • As long as the methods stay the same, someone can
    change object implementations without problems.
  • Keeps the code clean and easy to understand

7
A little more on String
  • What if you want to have a newline inside a
    string?
  • String weird "I really need two lines to write
    this long sentence!"
  • What if you need to place quotes inside a string?
  • String talk
  • "I said "hello" to him." / right? /
  • Answer Escape characters
  • Characters with a backslash ( \ )in front of them

8
Special Characters
  • The following can be used in a string
  • \n newline
  • \t tab
  • \" a double quote inside a string
  • \\ the backslash itself
  • "to type a long string without using another\
  • string, use a \\ before the new line

9
Class exercise
  • Given a one word name written in any way,
    generate the name in a proper way.
  • e.g., generate "Shakespeare"
  • for all the following inputs
  • Shakespeare
  • shakespeare
  • sHAkeSPeaRE
  • SHAKESPEARE

10
Debugging programs
  • Using a debugger
  • system dependent
  • often confusing and takes time to learn
  • Staring at the program
  • you may need to stare quite long!
  • Using temporary printlns
  • If you suspect a problem somewhere, put a println
    before that to see if it comes out okay.

11
Other debugging techniques
  • Trying out the program using constants instead of
    variables
  • Commenting out problem areas
  • Writing the program in stages that you know works
  • Trying a different approach

12
Syntax summary
  • Creating new objects
  • myCar new Automobile("Toyota")
  • Running methods on objects
  • myEff myCar.fuelEfficiency()
  • Escaping special characters
  • \n \t \ \\
  • \ followed by end of line, continued in next line
Write a Comment
User Comments (0)
About PowerShow.com