SlideShare a Scribd company logo
CSE110
Principles of Programming
with Java
Lecture 03:
Classes, methods, and variables
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 2
Previously…
public class TheClassName {
}
public void theFirstMethodName() {
}
public void secondMethodName() {
}
// instructions go here
// instructions go here
// variables go here
// variables go here
// variables go here
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 3
Previously…
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 4
main() method
• All programs should have one and only one main()
method.
• Trying to execute a class without a main() method
generates a compilation error
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 5
static
public class TheClassName {
}
public static void theFirstMethodName() {
}
public static void secondMethodName() {
}
// instructions go here
// instructions go here
// variables go here
// variables go here
// variables go here
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 6
Example without main()
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 7
Example with main()
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 8
Example main() calling methods
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 9
static
public class TheClassName {
}
public static void theFirstMethodName() {
}
public static void secondMethodName() {
}
// instructions go here
// instructions go here
// variables go here (static also)
// variables go here
// variables go here
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 10
Error
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 11
Fix the Error
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 12
12
Previously…
• The Java reserved words:
abstract
boolean
break
byte
case
catch
char
class
const
continue
default
do
double
else
extends
false
final
finally
float
for
goto
if
implements
import
instanceof
int
interface
long
native
new
null
package
private
protected
public
return
short
static
strictfp
super
switch
synchronized
this
throw
throws
transient
true
try
void
volatile
while
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 13
Identifiers
• Identifiers are the words a programmer uses in a
program
• An identifier can be made up of letters, digits, the
underscore character ( _ ), and the dollar sign
• Identifiers cannot begin with a digit
• Java is case sensitive - Total, total, and
TOTAL are different identifiers
• By convention, Java programmers use different
case styles for different types of identifiers, such as
o title case for class names - Lincoln
o upper case for constants - MAXIMUM
13
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 14
White Space
• Spaces, blank lines, and tabs are called white
space
• White space is used to separate words and symbols
in a program
• Extra white space is ignored
• A valid Java program can be formatted in many
ways
• Programs should be formatted to enhance
readability, using consistent indentation
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 15
15
Comments
• Comments in a program are called inline
documentation
• They should be included to explain the purpose of
the program and describe processing steps
• They do not affect how a program works
• Java comments can take three forms:
// this comment runs to the end of the line
/* this comment runs to the terminating
symbol, even across line breaks */
/** this is a javadoc comment */
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 16
16
Comments
public class MyProgram {
/*
*/
}
// comments about the class
class bodyComments can be placed almost anywhere
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 17
Comments
public class MyProgram {
}
public static void main (String[] args) {
}
/**
* comments about the class
*
*/
// comments about the method
method
body
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 18
Variables
• Multiple variables can be created in one
declaration
int count, temp, result;
• A variable can be given an initial value in the
declaration
int sum = 0;
int base = 32, max = 149;
• An assignment statement changes the value of a
variable
total = 55;
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 19
Data Types
§ There are exactly eight primitive data types in Java
§ Four of them represent integers:
byte, short, int, long
§ Two of them represent floating point numbers:
float, double
§ One of them represents characters:
char
§ And one of them represents boolean values:
boolean
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 20
Homework
Read Sections 2.1, 2.2, and 2.3
CSE110 - Principles of Programming
Javier Gonzalez-Sanchez
javiergs@asu.edu
Summer 2017
Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.

More Related Content

PPT
Code obfuscation
PPTX
Code obfuscation
PPTX
PPTX
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
PPT
ParaSail
PPTX
PPT
Lecture 1
PPTX
Turbo pascal
Code obfuscation
Code obfuscation
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
ParaSail
Lecture 1
Turbo pascal

What's hot (20)

PPT
Introduction To C#
PPTX
PDF
Lecture02 java
PPT
Introduction of C# BY Adarsh Singh
PDF
Evaluate And Analysis of ALGOL, ADA ,PASCAL Programming Languages
PPTX
Presentation 1st
PPTX
Delegates and events in C#
PPTX
Compiler: Programming Language= Assignments and statements
PPT
Csharp4 generics
PDF
Day02 01 Advance Feature in C# DH TDT
PPTX
Inheritance
PPTX
Abstract class in c++
PPTX
ADA programming language
PPTX
ASSIGNMENT STATEMENTS AND
PPTX
Hack programming language
PDF
Pharo: Syntax in a Nutshell
PDF
Effective PHP. Part 6
PPTX
Explain Delegates step by step.
PDF
Pharo: Objects at your Fingertips
PPTX
C# Delegates
Introduction To C#
Lecture02 java
Introduction of C# BY Adarsh Singh
Evaluate And Analysis of ALGOL, ADA ,PASCAL Programming Languages
Presentation 1st
Delegates and events in C#
Compiler: Programming Language= Assignments and statements
Csharp4 generics
Day02 01 Advance Feature in C# DH TDT
Inheritance
Abstract class in c++
ADA programming language
ASSIGNMENT STATEMENTS AND
Hack programming language
Pharo: Syntax in a Nutshell
Effective PHP. Part 6
Explain Delegates step by step.
Pharo: Objects at your Fingertips
C# Delegates
Ad

Similar to 201707 CSE110 Lecture 03 (20)

PDF
201707 CSE110 Lecture 09
PDF
201707 CSE110 Lecture 02
PDF
201707 CSE110 Lecture 15
PPT
JAVA_BASICS.ppt
PPTX
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
PPT
CSL101_Ch1.ppt Computer Science
PPT
Programming with Java by Faizan Ahmed & Team
PPT
Introduction to java programming with Fundamentals
PDF
Object Oriented Programming with Java Basic Syntax.pdf
PPT
Mobile computing for Bsc Computer Science
PPT
Programming with Java - Essentials to program
PPT
Java Concepts with object oriented programming
PPT
java programming for engineering students_Ch1.ppt
PPTX
Intro to programing with java-lecture 3
PPT
CSL101_Ch1.ppt
PPTX
CSL101_Ch1.pptx
PPTX
CSL101_Ch1.pptx
PPT
CSL101_Ch1.ppt
PPTX
Java programmingjsjdjdjdjdjdjdjdjdiidiei
PPTX
Basics java programing
201707 CSE110 Lecture 09
201707 CSE110 Lecture 02
201707 CSE110 Lecture 15
JAVA_BASICS.ppt
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
CSL101_Ch1.ppt Computer Science
Programming with Java by Faizan Ahmed & Team
Introduction to java programming with Fundamentals
Object Oriented Programming with Java Basic Syntax.pdf
Mobile computing for Bsc Computer Science
Programming with Java - Essentials to program
Java Concepts with object oriented programming
java programming for engineering students_Ch1.ppt
Intro to programing with java-lecture 3
CSL101_Ch1.ppt
CSL101_Ch1.pptx
CSL101_Ch1.pptx
CSL101_Ch1.ppt
Java programmingjsjdjdjdjdjdjdjdjdiidiei
Basics java programing
Ad

More from Javier Gonzalez-Sanchez (20)

PDF
201804 SER332 Lecture 01
PDF
201801 SER332 Lecture 03
PDF
201801 SER332 Lecture 04
PDF
201801 SER332 Lecture 02
PDF
201801 CSE240 Lecture 26
PDF
201801 CSE240 Lecture 25
PDF
201801 CSE240 Lecture 24
PDF
201801 CSE240 Lecture 23
PDF
201801 CSE240 Lecture 22
PDF
201801 CSE240 Lecture 21
PDF
201801 CSE240 Lecture 20
PDF
201801 CSE240 Lecture 19
PDF
201801 CSE240 Lecture 18
PDF
201801 CSE240 Lecture 17
PDF
201801 CSE240 Lecture 16
PDF
201801 CSE240 Lecture 15
PDF
201801 CSE240 Lecture 14
PDF
201801 CSE240 Lecture 13
PDF
201801 CSE240 Lecture 12
PDF
201801 CSE240 Lecture 11
201804 SER332 Lecture 01
201801 SER332 Lecture 03
201801 SER332 Lecture 04
201801 SER332 Lecture 02
201801 CSE240 Lecture 26
201801 CSE240 Lecture 25
201801 CSE240 Lecture 24
201801 CSE240 Lecture 23
201801 CSE240 Lecture 22
201801 CSE240 Lecture 21
201801 CSE240 Lecture 20
201801 CSE240 Lecture 19
201801 CSE240 Lecture 18
201801 CSE240 Lecture 17
201801 CSE240 Lecture 16
201801 CSE240 Lecture 15
201801 CSE240 Lecture 14
201801 CSE240 Lecture 13
201801 CSE240 Lecture 12
201801 CSE240 Lecture 11

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
cuic standard and advanced reporting.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Big Data Technologies - Introduction.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPT
Teaching material agriculture food technology
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Spectral efficient network and resource selection model in 5G networks
Per capita expenditure prediction using model stacking based on satellite ima...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
cuic standard and advanced reporting.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
sap open course for s4hana steps from ECC to s4
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Electronic commerce courselecture one. Pdf
Review of recent advances in non-invasive hemoglobin estimation
Mobile App Security Testing_ A Comprehensive Guide.pdf
The AUB Centre for AI in Media Proposal.docx
Dropbox Q2 2025 Financial Results & Investor Presentation
Big Data Technologies - Introduction.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Teaching material agriculture food technology
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Empathic Computing: Creating Shared Understanding
Spectral efficient network and resource selection model in 5G networks

201707 CSE110 Lecture 03

  • 1. CSE110 Principles of Programming with Java Lecture 03: Classes, methods, and variables Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 2 Previously… public class TheClassName { } public void theFirstMethodName() { } public void secondMethodName() { } // instructions go here // instructions go here // variables go here // variables go here // variables go here
  • 3. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 3 Previously…
  • 4. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 4 main() method • All programs should have one and only one main() method. • Trying to execute a class without a main() method generates a compilation error
  • 5. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 5 static public class TheClassName { } public static void theFirstMethodName() { } public static void secondMethodName() { } // instructions go here // instructions go here // variables go here // variables go here // variables go here
  • 6. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 6 Example without main()
  • 7. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 7 Example with main()
  • 8. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 8 Example main() calling methods
  • 9. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 9 static public class TheClassName { } public static void theFirstMethodName() { } public static void secondMethodName() { } // instructions go here // instructions go here // variables go here (static also) // variables go here // variables go here
  • 10. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 10 Error
  • 11. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 11 Fix the Error
  • 12. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 12 12 Previously… • The Java reserved words: abstract boolean break byte case catch char class const continue default do double else extends false final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while
  • 13. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 13 Identifiers • Identifiers are the words a programmer uses in a program • An identifier can be made up of letters, digits, the underscore character ( _ ), and the dollar sign • Identifiers cannot begin with a digit • Java is case sensitive - Total, total, and TOTAL are different identifiers • By convention, Java programmers use different case styles for different types of identifiers, such as o title case for class names - Lincoln o upper case for constants - MAXIMUM 13
  • 14. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 14 White Space • Spaces, blank lines, and tabs are called white space • White space is used to separate words and symbols in a program • Extra white space is ignored • A valid Java program can be formatted in many ways • Programs should be formatted to enhance readability, using consistent indentation
  • 15. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 15 15 Comments • Comments in a program are called inline documentation • They should be included to explain the purpose of the program and describe processing steps • They do not affect how a program works • Java comments can take three forms: // this comment runs to the end of the line /* this comment runs to the terminating symbol, even across line breaks */ /** this is a javadoc comment */
  • 16. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 16 16 Comments public class MyProgram { /* */ } // comments about the class class bodyComments can be placed almost anywhere
  • 17. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 17 Comments public class MyProgram { } public static void main (String[] args) { } /** * comments about the class * */ // comments about the method method body
  • 18. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 18 Variables • Multiple variables can be created in one declaration int count, temp, result; • A variable can be given an initial value in the declaration int sum = 0; int base = 32, max = 149; • An assignment statement changes the value of a variable total = 55;
  • 19. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 19 Data Types § There are exactly eight primitive data types in Java § Four of them represent integers: byte, short, int, long § Two of them represent floating point numbers: float, double § One of them represents characters: char § And one of them represents boolean values: boolean
  • 20. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 20 Homework Read Sections 2.1, 2.2, and 2.3
  • 21. CSE110 - Principles of Programming Javier Gonzalez-Sanchez javiergs@asu.edu Summer 2017 Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.