0% found this document useful (0 votes)
4 views33 pages

Lecture 4- 30-Jan-2025

Chapter 2 covers the fundamentals of data and expressions in Java, including character strings, primitive data types, variable declaration and usage, expressions, operator precedence, data conversions, and user input. It explains string manipulation, concatenation, escape sequences, and the use of variables and constants. Additionally, it provides examples of Java code to illustrate these concepts.

Uploaded by

L A A
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)
4 views33 pages

Lecture 4- 30-Jan-2025

Chapter 2 covers the fundamentals of data and expressions in Java, including character strings, primitive data types, variable declaration and usage, expressions, operator precedence, data conversions, and user input. It explains string manipulation, concatenation, escape sequences, and the use of variables and constants. Additionally, it provides examples of Java code to illustrate these concepts.

Uploaded by

L A A
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/ 33

Chapter 2

Data and Expressions


Outline
• We first need to explore the fundamentals of computer processing

• Chapter 2 focuses on:


o character strings
o primitive data
o the declaration and use of variables
o expressions and operator precedence
o data conversions
o accepting input from the user

2
Character strings
• A string of characters
• is an object in JAVA, defined by the class String

• Can be represented as a string literal


• by putting double quotes around the text

• Examples:
"This is a string
literal."
"123 Main Street"
"X"
The println Method
• In the Lincoln program from Chapter 1, we invoked
the println method to print a character string
• The System.out object represents a destination (the
monitor screen) to which we can send output
System.out.println ("Whatever you are, be a good one.");

object method
information provided to the method
name
(parameters)

4
The print Method
• The System.out object provides another service as
well

• The print method is similar to the println method,


except that it does not advance to the next line

• Therefore anything printed after a print statement


will appear on the same line

• See Countdown.java

5
//********************************************************************
// Countdown.java Author: Lewis/Loftus
//
// Demonstrates the difference between print and println.
//********************************************************************

public class Countdown


{
//-----------------------------------------------------------------
// Prints two lines of output representing a rocket countdown.
//-----------------------------------------------------------------
public static void main (String[] args)
{
System.out.print ("Three... ");
System.out.print ("Two... ");
System.out.print ("One... ");
System.out.print ("Zero... ");
System.out.println ("Liftoff!"); // appears on first output line
System.out.println ("Houston, we have a problem.");
}
}

6
Output
//********************************************************************
// Countdown.java Author: Lewis/Loftus
// Three... Two... One... Zero... Liftoff!
Houston,
// Demonstrates the we have between
difference a problem.
print and println.
//********************************************************************

public class Countdown


{
//-----------------------------------------------------------------
// Prints two lines of output representing a rocket countdown.
//-----------------------------------------------------------------
public static void main (String[] args)
{
System.out.print ("Three... ");
System.out.print ("Two... ");
System.out.print ("One... ");
System.out.print ("Zero... ");
System.out.println ("Liftoff!"); // appears on first output line
System.out.println ("Houston, we have a problem.");
}
}

7
String Concatenation
• The string concatenation operator (+) is used to append one string to
the end of another
"Peanut butter " + "and jelly"
• It can also be used to append a number to a string
• A string literal cannot be broken across two lines in a program
• See Facts.java

8
//********************************************************************
// Facts.java Author: Lewis/Loftus
//
// Demonstrates the use of the string concatenation operator and the
// automatic conversion of an integer to a string.
//********************************************************************

public class Facts


{
//-----------------------------------------------------------------
// Prints various facts.
//-----------------------------------------------------------------
public static void main (String[] args)
{
// Strings can be concatenated into one long string
System.out.println ("We present the following facts for your "
+ "extracurricular edification:");

System.out.println ();

// A string can contain numeric digits


System.out.println ("Letters in the Hawaiian alphabet: 12");

continue

9
continue

// A numeric value can be concatenated to a string


System.out.println ("Dialing code for Antarctica: " + 672);

System.out.println ("Year in which Leonardo da Vinci invented "


+ "the parachute: " + 1515);

System.out.println ("Speed of ketchup: " + 40 + " km per year");


}
}

10
Output
We present the following facts for your extracurricular edification:

Letters in the Hawaiian alphabet: 12


Dialing code for Antarctica: 672
Year in which Leonardo da Vinci invented the parachute: 1515
continue
Speed of ketchup: 40 km per year

// A numeric value can be concatenated to a string


System.out.println ("Dialing code for Antarctica: " + 672);

System.out.println ("Year in which Leonardo da Vinci invented "


+ "the parachute: " + 1515);

System.out.println ("Speed of ketchup: " + 40 + " km per year");


}
}

11
String Concatenation
• The + operator is also used for arithmetic addition
• The function that it performs depends on the type of the
information on which it operates
• If both operands are strings, or if one is a string and one is a
number, it performs string concatenation
• If both operands are numeric, it adds them
• The + operator is evaluated left to right, but parentheses can be
used to force the order
• See Addition.java

12
//********************************************************************
// Addition.java Author: Lewis/Loftus
//
// Demonstrates the difference between the addition and string
// concatenation operators.
//********************************************************************

public class Addition


{
//-----------------------------------------------------------------
// Concatenates and adds two numbers and prints the results.
//-----------------------------------------------------------------
public static void main (String[] args)
{
System.out.println ("24 and 45 concatenated: " + 24 + 45);

System.out.println ("24 and 45 added: " + (24 + 45));


}
}

13
Output
//********************************************************************
// Addition.java Author: Lewis/Loftus
// 24 and 45 concatenated: 2445
24the
// Demonstrates and 45 added:
difference between69
the addition and string
// concatenation operators.
//********************************************************************

public class Addition


{
//-----------------------------------------------------------------
// Concatenates and adds two numbers and prints the results.
//-----------------------------------------------------------------
public static void main (String[] args)
{
System.out.println ("24 and 45 concatenated: " + 24 + 45);

System.out.println ("24 and 45 added: " + (24 + 45));


}
}

14
Quick Check
What output is produced by the following?
System.out.println ("X: " + 25);
System.out.println ("Y: " + (15 + 50));
System.out.println ("Z: " + 300 + 50);

15
Quick Check
What output is produced by the following?
System.out.println ("X: " + 25);
System.out.println ("Y: " + (15 + 50));
System.out.println ("Z: " + 300 + 50);

X: 25
Y: 65
Z: 30050

16
Escape Sequences
• What if we wanted to print the quote character?
• The following line would confuse the compiler because it would
interpret the second quote as the end of the string

System.out.println ("I said "Hello" to you.");

• An escape sequence is a series of characters that represents a


special character
• An escape sequence begins with a backslash character (\)

System.out.println ("I said \"Hello\" to you.");

17
Escape Sequences
• Some Java escape sequences:
Escape Sequence Meaning
\b backspace
\t tab
\n newline
\r carriage return
\" double quote
\' single quote
\\ backslash

• See Roses.java

18
//********************************************************************
// Roses.java Author: Lewis/Loftus
//
// Demonstrates the use of escape sequences.
//********************************************************************

public class Roses


{
//-----------------------------------------------------------------
// Prints a poem (of sorts) on multiple lines.
//-----------------------------------------------------------------
public static void main (String[] args)
{
System.out.println ("Roses are red,\n\tViolets are blue,\n" +
"Sugar is sweet,\n\tBut I have \"commitment issues\",\n\t" +
"So I'd rather just be friends\n\tAt this point in our " +
"relationship.");
}
}

19
Output
//********************************************************************
Roses are Author:
// Roses.java red, Lewis/Loftus
//
Violets are blue,
// Demonstrates the use of escape sequences.
Sugar is sweet,
//********************************************************************
But I have "commitment issues",
public class Roses
{ So I'd rather just be friends
At this point in our relationship.
//-----------------------------------------------------------------
// Prints a poem (of sorts) on multiple lines.
//-----------------------------------------------------------------
public static void main (String[] args)
{
System.out.println ("Roses are red,\n\tViolets are blue,\n" +
"Sugar is sweet,\n\tBut I have \"commitment issues\",\n\t" +
"So I'd rather just be friends\n\tAt this point in our " +
"relationship.");
}
}

20
Quick Check
Write a single println statement that produces the
following output:
"Thank you all for coming to my home
tonight," he said mysteriously.

21
Quick Check
Write a single println statement that produces the
following output:
"Thank you all for coming to my home
tonight," he said mysteriously.

System.out.println ("\"Thank you all for " +


"coming to my home\ntonight,\" he said " +
"mysteriously.");

22
Outline
Character Strings
Variables and Assignment
Primitive Data Types
Expressions
Data Conversion
Interactive Programs
Graphics
Applets
Drawing Shapes

23
Variables
• A variable is a name for a location in memory that holds a value
• A variable declaration specifies the variable's name and the type of
information that it will hold

data type variable name

int total;
int count, temp, result;

Multiple variables can be created in one declaration

24
Variable Initialization
• A variable can be given an initial value in the
declaration
int sum = 0;
int base = 32, max = 149;

• When a variable is referenced in a program, its


current value is used
• See PianoKeys.java

25
//********************************************************************
// PianoKeys.java Author: Lewis/Loftus
//
// Demonstrates the declaration, initialization, and use of an
// integer variable.
//********************************************************************

public class PianoKeys


{
//-----------------------------------------------------------------
// Prints the number of keys on a piano.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int keys = 88;
System.out.println ("A piano has " + keys + " keys.");
}
}

26
Output
//********************************************************************
// PianoKeys.java Author: Lewis/Loftus
//
A piano has 88 keys.
// Demonstrates the declaration, initialization, and use of an
// integer variable.
//********************************************************************

public class PianoKeys


{
//-----------------------------------------------------------------
// Prints the number of keys on a piano.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int keys = 88;
System.out.println ("A piano has " + keys + " keys.");
}
}

27
int total;

double num1, num2=4.356, num3;

char letter =‘A’, digit=‘7’;

final int MAX=45;

28
Assignment
• An assignment statement changes the value of a variable
• The assignment operator is the = sign

total = 55;

• The value that was in total is overwritten


• You can only assign a value to a variable that is
consistent with the variable's declared type
• See Geometry.java
29
//********************************************************************
// Geometry.java Author: Lewis/Loftus
//
// Demonstrates the use of an assignment statement to change the
// value stored in a variable.
//********************************************************************

public class Geometry


{
//-----------------------------------------------------------------
// Prints the number of sides of several geometric shapes.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int sides = 7; // declaration with initialization
System.out.println ("A heptagon has " + sides + " sides.");

sides = 10; // assignment statement


System.out.println ("A decagon has " + sides + " sides.");

sides = 12;
System.out.println ("A dodecagon has " + sides + " sides.");
}
}

30
Output
//********************************************************************
// Geometry.javaA heptagon has 7 sides.
Author: Lewis/Loftus
//
A decagon has 10 sides.
// Demonstrates the use of an assignment statement to change the
// value stored ain dodecagon
a variable. has 12 sides.
//********************************************************************

public class Geometry


{
//-----------------------------------------------------------------
// Prints the number of sides of several geometric shapes.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int sides = 7; // declaration with initialization
System.out.println ("A heptagon has " + sides + " sides.");

sides = 10; // assignment statement


System.out.println ("A decagon has " + sides + " sides.");

sides = 12;
System.out.println ("A dodecagon has " + sides + " sides.");
}
}

31
Constants
• A constant is an identifier that is similar to a variable except that it
holds the same value during its entire existence
• As the name implies, it is constant, not variable
• The compiler will issue an error if you try to change the value of a
constant
• In Java, we use the final modifier to declare a constant
final int MIN_HEIGHT = 69;
• They facilitate program maintenance
• If a constant is used in multiple places, its value need only be updated in one
place
32
final int MAX_LOAD = 100,
MAX_LOAD = 120;

This is not allowed because it is a constant


variable

33

You might also like