Lecture7-2024
Lecture7-2024
SCIENCE
CSI141
PROGRAMMING
h t t p s : / / h o r s t m a n n . c o m / b j l o / i n d e x . ht m l PRINCIPLES
Programming Principles T ALLMAN NKGAU
L ECTURE 7 – F UNDAMENTAL
D ATA TYPES
to:
susbtring
keyboard
M ONEY IS AN ABSTRACT HUMAN
HAPPINESS , SO WHO IS NO
LONGER ABLE TO APPRECIATE THE
TRUE HUMAN HAPPINESS , IS
COMPLETELY DEDICATED TO IT . –
A RTHUR SCHOPENHAUE
❑ Declaration: char c;
❑ Assignment: c = ‘\u00A5’
❑ Initialization: char x = ‘u’; // Notice the single quotes
❑ Display: System.out.printf(“x = %c\n”, x);
char ch;
6
Character data type - char
char ch;
int c;
7
Character data type - char
char ch;
int c;
ch = ‘a’;
8
Character data type - char
char ch;
int c;
ch = ‘a’;
char other = ‘A’;
c = ch; // ???
Convert character in ch
to its decimal value and
store the decimal value.
9
Character data type - char
char ch;
int c;
ch = ‘a’;
char other = ‘A’;
c = ch; // ???
int d = ‘A’ – ‘a’; // ???
10
M ONEY IS AN ABSTRACT HUMAN
HAPPINESS , SO WHO IS NO
LONGER ABLE TO APPRECIATE THE
TRUE HUMAN HAPPINESS , IS
COMPLETELY DEDICATED TO IT . –
A RTHUR SCHOPENHAUE
name null
String name;
null means “No address”
Memory diagram
showing the meaning of
a string declaration 13
Meaning of declarations (Semantics) and output
name 656
String name = “Amen”;
Output
System.out.printf(“name = %s\n”, name);
14
Java Programs
Java programs are made of objects that interact with each
other – Java is an Object-Oriented Language
・ Each object is based on a class
・ A class describes a set of objects with the same
behavior – it’s a template for creating objects
Each class defines a specific set of methods to use with its
objects
・ For example, the Scanner class provides methods:
– nextInt(), nextDouble(), next(), nextLine()
15
String as a Java class
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/String.html
16
Copying a char from a string
17
Index of a char in a string
18
Copying portion of a String
A substring is a contiguous portion of a String
The substring method returns a portion of a
String as a new String object:
0 1 2 3 4 5
H e l l o !
0 1 2 3 4 5
String greeting = "Hello!"; H e l l o !
int n = greeting.length(); // n is assigned 6
String addition (concatenation)
greeting 0 1 2 3 4
H e l l o
name 0 1 2 3 4
N u r s e
0 1 2 3 4 5 6 7 8 9 10 11
both
H e l l o N u r s e !
String Operations (1)
String Operations(2)
M ONEY IS AN ABSTRACT HUMAN
HAPPINESS , SO WHO IS NO
LONGER ABLE TO APPRECIATE THE
TRUE HUMAN HAPPINESS , IS
COMPLETELY DEDICATED TO IT . –
A RTHUR SCHOPENHAUE
25
Exercise #1
1.int a;
2.String fifa98 = "Do You Mind If I
Play";
3.String zebras;
4.char ch;
5.a = fifa98.length();
6.a = a /10 % 10;
7.zebras = fifa98.substring(0,15);
8.ch = fifa98.charAt(12);
9.String w = “zebras” + “, ” + zebras +
“ “ + ch + “ “ + “Win”;
26
Exercise #2
27
Exercise #3
The following algorithm describes how to turn a string
containing a ten-digit phone number (such as "4155551212")
into a more readable string with parentheses and dashes, like
this: "(415) 555-1212".
1. Take the substring consisting of the first three
characters and surround it with "(" and ")". This is
the area code.
2. Concatenate the area code, the substring consisting
of the next three characters, a hyphen, and the
substring consisting of the last four characters. This
is the formatted number.
Translate this algorithm into a Java program that reads a
telephone number into a string variable, computes the
formatted number, and displays it.
28
Constants
❑ When a variable is defined with the reserved
word final, its value can never be changed
final double BOTTLE_VOLUME = 2;
30
Summary
Page 31