0% found this document useful (0 votes)
8 views

Lecture7-2024

The document outlines fundamental data types in Java, focusing on character and string data types, their declarations, and operations. It explains how to use the String class methods and the Scanner class for input. Additionally, it includes exercises to reinforce the concepts taught in the lecture.

Uploaded by

HAMO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture7-2024

The document outlines fundamental data types in Java, focusing on character and string data types, their declarations, and operations. It explains how to use the String class methods and the Scanner class for input. Additionally, it includes exercises to reinforce the concepts taught in the lecture.

Uploaded by

HAMO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

COMPUTER

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

‣ Character data type, variables,


& assignment, output
‣ String data type, variables, &
assignment, string methods,
https://horstmann.com/bjlo/index.html output
‣ Keyboard/Console input
L EARNING OUTCOMES

At the end of the lecture, you should be able

to:

‣ Declare character and string variables

‣ Draw memory model diagrams involving character

and string variables

‣ Use the String class methods: indexOf, charAt, length,

susbtring

‣ Use the Scanner class to read strings from the


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

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

‣ Character data type, variables,


& assignment, output
‣ String data type, variables, &
assignment, string methods,
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 output
‣ Keyboard/Console input
Character data type - char
❑ Java provides a means to work with individual characters.

❑ Declaration: char c;
❑ Assignment: c = ‘\u00A5’
❑ Initialization: char x = ‘u’; // Notice the single quotes
❑ Display: System.out.printf(“x = %c\n”, x);

Type Size in Set of Operations minimum maximum


bytes values

char 2 Unsigned +, - 0 65535


16 bit
integers
5
Character data type - char

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

‣ Character data type, variables,


& assignment, output
‣ String data type, variables, &
assignment, string methods,
h t t p : / / ho r st m a n n . c o m / b j l o 1/ i n de x . h t m l output
‣ Keyboard/Console input
String data type, declaration
❑ Strings are sequences of characters
・Characters have their own type: char
・Characters have numeric values
– See the ASCII code chart in Appendix A
– For example, the letter ‘H’ has a value of 72 if it were a number

❑ String is a Java class – starts with upper case

❑ Declaration: String st;


❑ Use single quotes around a char
char initial = ‘B’;

❑ Use double quotes around a String


String initials = “BRL”;
String initial = “B”;
String emptyString = “”; // no characters!
12
Meaning of declarations (Semantics)

❑ Remember, a variable is a name that refers to a memory location where


a value could be stored. However,
— String variables DO NOT store the string value itself, but the address
of the string object.
For example, given the following declarations

name null
String name;
null means “No address”

Memory diagram
showing the meaning of
a string declaration 13
Meaning of declarations (Semantics) and output

❑ Remember, a variable is a name that refers to a memory location where


a value could be stored. However,
— String variables DO NOT store the string value itself, but the address
of the string object.
For example, given the following declarations

name 656
String name = “Amen”;

Memory diagram showing the


656
meaning of a string assignment.
656 is the address of the actual
string object in memory.
“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

❑ For each string object created, we can call methods on it to perform


some action for us.

https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/String.html

16
Copying a char from a string

❑ Each char inside a String has an index number:

❑ The first char is index zero (0)


❑ The charAt method returns a char at a given index inside a String:

String greeting = "Harry";

char start = greeting.charAt(0);


char last = greeting.charAt(4);

17
Index of a char in a string

❑ Each char inside a String has an index number:

❑ The first char is index zero (0)


❑ The indexOf method returns the position of a given char in a String
or -1 if it does not exist:

String greeting = "Harry";

int pos = greeting.indexOf(‘r’);


int p = greeting.indexOf(‘A’);

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 !

String greeting = "Hello!"; 0 1


String sub = greeting.substring(0, 2); H e
(start at 0 and end at 2-1 = 1 position)

String sub2 = greeting.substring(3, 5);


string
The length method returns the number of
characters in a string – spaces are also counted.

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)

❑ Putting strings together aka adding strings


String greeting = "Hello";
String name = “Nurse”;
String both = greeting + “ ” + name + “!”;

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

‣ Character data type, variables,


& assignment, output
‣ String data type, variables, &
assignment, string methods,
h t t p : / / ho r st m a n n . c o m / b j l o 1/ i n de x . h t m l output
‣ Keyboard/Console input
Reading input
This is a three step process in Java

1. Import the Scanner class from its ‘package’java.util


import java.util.Scanner;

2. Setup an object of the Scanner class


Scanner in = new Scanner(System.in);

3. Use methods of the new Scanner object to get input


String name = in.next(); // reading a string
String line = in.nextLine(); // line of text

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

Write a Java program to read a string from the


keyboard and display the first, the middle,
and the last characters of the string.

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;

❑ It is good style to use named constants to


explain numerical values to be used in
calculations
・Which is clearer?
double totalVolume = bottles * 2;
or
double totalVolume = bottles * BOTTLE_VOLUME;

❑ A programmer reading the first statement may not


understand the significance of the 2
❑ Also, if the constant is used in multiple places and needs to
be changed, only the initialization changes
29
Constant declaration

❑It is customary (not required) to use all UPPER_CASE


letters for constants – but is required for this course

30
Summary

• Read strings (and lines) from the keyboard


• Use the Java String class methods on string objects

Page 31

You might also like