STRINGS - PowerPoint PPT Presentation

About This Presentation
Title:

STRINGS

Description:

Not just numerical information. Human beings deal with 'letters' and 'words' ... Under the Hood. In the computer's memory we only have bits. 0000000001000001 ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 37
Provided by: thaddeusf
Category:
Tags: strings | hood

less

Transcript and Presenter's Notes

Title: STRINGS


1
STRINGS
  • CSC 171 FALL 2004
  • LECTURE 17

2
chars Strings
  • Computers process information
  • Not just numerical information
  • Human beings deal with letters and words
  • Java codes letters as the char type
  • Java codes words as the String type
  • ASCII one byte (8 bits) per character
  • UniCode two bytes (16 bits) per character

3
Under the Hood
  • In the computers memory we only have bits
  • 0000000001000001
  • We can interpret (semantics) the bits any way we
    like
  • The same bit pattern (syntax) can have multiple
    meanings

4
  • public class StringChar
  • public static void main(String args)
  • char c1 'A'
  • int i1 (int) c1
  • System.out.println("c1 (char) " (char) c1)
  • System.out.println("c1 (int) " (int) c1)
  • System.out.println("i1 (char) " (char) i1)
  • System.out.println("i1 (int) " (int) i1)

c1 (char) A c1 (int) 65 i1 (char) A i1
(int) 65
5
  • public static void prnt1(char c1)
  • for (int i 0 ilt10i)
  • System.out.println(c1 " " i
  • " " (c1 i ))
  • // output for prnt1(A) ??

A 0 65 A 1 66 A 2 67 A 3 68 A
4 69 A 5 70 A 6 71 A 7 72 A
8 73 A 9 74
6
  • public static void prnt1(char c1)
  • for (int i 0 ilt10i)
  • System.out.println(c1 " " i
  • " " (char) (c1 i ))
  • // output for prnt1(A) ??

A 0 A A 1 B A 2 C A 3 D A 4
E A 5 F A 6 G A 7 H A 8
I A 9 J
7
Strings groups of chars
  • String s1 Hello CSC 171
  • String s2 new String(Hello CSC 171)

8
Strings as Arrays
  • String name Teddy

9
String length
  • String name Teddy,
  • str1 Ted,
  • str2 new String(),
  • str3
  • name.length()
  • str1.length()
  • str2.length()
  • str3.length()

5
3
0
Error str3 is null
10
Strings are objects
  • String st1 // declares a reference
  • st1 new String(Teddy)
  • //constructs a new object
  • // sets st1 to refer to the new object

11
Comparing Strings
  • Is one string equal to another?
  • It is import to know what we are comparing
  • compares the reference
  • equals compares the contents of the objects

12
The reference
  • String st1 // declares a reference
  • st1 new String(Teddy)
  • //constructs a new object
  • // sets st1 to refer to the new object

Memory address
13
  • String st1
  • st1 new String(Teddy)
  • String st2
  • st2 new String(Teddy)

14
  • String st1 st1 new String(Teddy)
  • String st2 st2 new String(Teddy)
  • boolean b1 (st1 st2)
  • // false because different loc

15
  • String st1 st1 new String(Teddy)
  • String st2 st2 st1
  • boolean b1 (st1 st2)
  • // true because same loc

16
  • String st1 st1 new String(Teddy)
  • String st2 st2 new String(Teddy)
  • boolean b1 (st1.equals(st2))
  • // true because contents

17
  • String st1 st1 new String(Teddy)
  • String st2 st2 new String(Ted)
  • boolean b1 (st1.equals(st2))
  • // false because contents

18
Primitive Reference
  • Primitive
  • int
  • byte
  • char
  • short
  • long
  • float
  • double
  • boolean
  • Reference
  • Object
  • String
  • Applet
  • myRational

19
Passing Objects to Methods
  • public class myClass public int value1

20
  • public class myClassTest
  • public static void main(String args)
  • myClass mc1 new myClass()
  • mc1.value1 3

21
  • int x1 3
  • intChange(x1)
  • System.out.println("x1 " x1)
  • public static void intChange(int intVal)
  • intVal 5

22
  • myClass mc1 new myClass()
  • mc1.value1 3
  • myClassRefChange(mc1)
  • System.out.println("mc1.value1 " mc1.value1)
  • public static void myClassRefChange(myClass
    myClassRef)
  • myClassRef new myClass()
  • myClassRef.value1 7

23
  • myClassValChange(mc1)
  • System.out.println("mc1.value1 "
    mc1.value1)
  • public static void myClassValChange(myClass
    myClassRef)
  • myClassRef.value1 9

24
  • mc1 myClassRefReturn(mc1)
  • System.out.println("mc1.value1 "
    mc1.value1)
  • public static myClass myClassRefReturn(myClass
    myClassRef)
  • myClassRef new myClass()
  • myClassRef.value1 11
  • return myClassRef

25
Technical Aside
  • FORMATTING NUBMERS

26
Formatting Numbers
  • int quarters 2
  • int dollars 3
  • double total dollars quarters 0.25
  • final double TAX_RATE 8.5
  • double tax total TAX_RATE / 100
  • System.out.println("Total "total)
  • System.out.println("Tax "tax)

Total 3.5 Tax 0.2975
27
Formatting Numbers
  • NumberFormat formatter
  • NumberFormat.getNumberInstance()
  • formatter.setMaximumFractionDigits(2)
  • System.out.println("Total "total)
  • System.out.println("Tax "
  • formatter.format(tax))

Total 3.5 Tax 0.3
28
Formatting Numbers
  • NumberFormat formatter
  • NumberFormat.getNumberInstance()
  • formatter.setMaximumFractionDigits(2)
  • formatter.setMinimumFractionDigits(2)
  • System.out.println("Total "total)
  • System.out.println("Tax "
  • formatter.format(tax))

Total 3.5 Tax 0.30
29
Strings are immutable
  • They dont change
  • We simulate change by making new strings
  • StringBuffers are used to change

30
SubString creates new
  • Two ways of getting substrings
  • String letters abcdefghijklmabcdefghijklm
  • System.out.println(letters.substring(20))
  • // form index 20 to end hijklm
  • System.out.println(letters.substring(0,6))
  • // form index 0 up to 6 abcdef

31
FRAME WINDOWS
  • Applets are programs that run inside a browser.
  • Limited access to local machine
  • Good graphics/UI support
  • Applications run outside of a browser
  • Access to local machine
  • Often (so far) console/text based
  • Frame based applications
  • Access to local machine
  • Good graphics/UI

32
  • import javax.swing.ImageIcon
  • import javax.swing.JFrame
  • import javax.swing.JLabel
  • import javax.swing.JPanel
  • public class FrameTest
  • public static void main(String args)
  • JFrame frame new JFrame()
  • frame.setDefaultCloseOperation(JFrame.EXIT_
    ON_CLOSE)
  • JLabel iconLabel new JLabel(new
    ImageIcon ("sierpins.gif"))
  • JLabel textLabel new JLabel("Hello,
    World!")

33
  • JPanel panel new JPanel()
  • panel.add(iconLabel)
  • panel.add(textLabel)
  • frame.setContentPane(panel)
  • frame.pack()
  • frame.show()

34
Better Design
  • public class FrameAloneTest
  • public static void main(String args)
  • FrameAlone myFrame new FrameAlone()
  • myFrame.runFrameAlone()

35
FrameAlone
  • import javax.swing.ImageIcon
  • import javax.swing.JFrame
  • import javax.swing.JLabel
  • import javax.swing.JPanel
  • public class FrameAlone extends JFrame
  • public FrameAlone()
  • this.setDefaultCloseOperation(JFrame.EXIT_O
    N_CLOSE)
  • JLabel iconLabel new JLabel(new
    ImageIcon ("sierpins.gif"))
  • JLabel textLabel new JLabel("Hello,
    World!")

36
  • JPanel panel new JPanel()
  • panel.add(iconLabel)
  • panel.add(textLabel)
  • this.setContentPane(panel)
  • public void runFrameAlone()
  • this.pack()
  • this.show()
Write a Comment
User Comments (0)
About PowerShow.com