SlideShare a Scribd company logo
1 of 26
Java Tutorial
Part 4: Data and
Calculations
Variables
Storing and
Processing Data
in the Memory
 Computers are machines that process data
 Both program instructions and data are stored in
the computer memory
 Data is stored by using variables
How Does Computing Work?
10110
 A variable is a container for information
 A named area of the computer memory
 The data can be read and changed at any time
 Variables provide means for:
 Storing data
 Retrieving the stored data
 Modifying the stored data
Variables
 Variables are characterized by:
 name (identifier)
 type (of the data preserved)
 value (stored information)
 Defining a variable in Java:
Variables
int age = 25;
Data
type
Variable
name
Variable
value
Name Type Value
age int 25
name String "Peter"
size double 3.50
Computer memory
Data Types
Text, Numbers,
and Other
Types in Java
 Variables store value of a certain type
 Number, letter, text (string), date, color, picture, list, …
 Data types:
 int – an integer: 1, 2, 3…
 double – a floating-point number: -0.5, 3.14, …
 boolean – a boolean: true, false
 char – a symbol: 'a', 'b', '#', …
 String – text: "Hello", "World", …
Data Types
 Data types define ranges of values with similar
characteristics
 Data types are characterized by:
 Name
 Example: boolean, int, String
 Size (memory usage)
 Example: 4 bytes
 Default value
 Example: 0
Data Types (2)
Statements
Commands in the
Computer Programs
 The actions that a program takes, are expressed as statements
 Common statements (actions / commands) include:
 Declaring a variable
 Assigning a value
 Declaring + initializing
Statements
int counter;
counter = 1;
int counter = 1;
 Printing a value
System.out.println(counter);
 Modifying a value
counter++;
sum = a + b;
 If-else statement
 Method definition statement
Complex Statements
if (a == 5)
System.out.println("Five");
else
System.out.println("Not Five");
 Loop statement
int a = 5;
while (a > 0) {
a--;
}
static int sum(int a, int b) {
return a + b;
}
Arithmetic Operators
Add, Subtract, Multiply
and Divide Numbers
+ -
* /
 Adding numbers (operator + )
 Subtracting numbers (operator - )
Arithmetic Operators: + and -
int a = 5;
int b = 7;
int sum = a + b;
System.out.println(sum); // 12
int a = 15;
int b = 7;
System.out.println(a - b); // 8
 Multiplying numbers (operator *)
 Dividing numbers (operator / )
Arithmetic Operators: * and /
int a = 5;
int b = 7;
System.out.println(a * b); // 35
int a = 25;
int b = 4;
System.out.println(a / b); // 6
 When dividing integers, the result is also integer:
 When dividing floating-points, the result is also floating-point:
Division Behavior in Java
int a = 25;
System.out.println(a / 4); // Integer result: 6
System.out.println(a / 0); // Error: division by 0
double a = 15;
System.out.println(a / 2.0); // 7.5
System.out.println(a / 0.0); // Infinity
System.out.println(0 / 0.0); // NaN
 Modulo / remainder from integer division (operator % )
Arithmetic Operators: %
int a = 7;
int b = 2;
System.out.println(a % b); // 1
System.out.println(3 % 2); // 1
System.out.println(4 % 2); // 0
System.out.println(3.5 % 1); // 0.5
7 = 3 * 2 + remainder 1
3 = 1 * 2 + remainder 1
4 = 3 * 2 + remainder 0
3.5 = 3 * 1 +
remainder 0.5
Expressions
Calculations with
Values and Operators
(a+b)
*c -1
 Expressions == sequences of operators, literals and
variables which are evaluated to a value
 Consist of at least one operand
 Can have 1 or more operators
Expressions
int r = (150-20) / 2 + 5;
int y = x + 5;
String name = "John Doe";
 Write a program to convert from USD to EUR:
 Read a floating-point number: the dollars to be converted
 Convert dollars to euro (use fixed rate of dollars to euro: 0.88)
 Print the converted value in euro formatted to the 2nd digit
Problem: Currency Converter
17 14.96
87 76.56
Judge: https://judge.softuni.org/Contests/Practice/Index/3253
Creating a New Project in IntelliJ IDEA
Solution: Currency Converter
Scanner scanner = new Scanner(System.in);
double dollars = scanner.nextDouble();
double euros = dollars * 0.88;
System.out.printf("%.2f", euros);
Submission in the Judge System
https://judge.softuni.org/Contests/Practice/Index/3253
 Write a program, which:
 Reads 2 real numbers from the console
 Performs 4 arithmetic operations on the obtained
2 numbers, in the following order: +, -, *, /
 Formats and prints the results like this example:
Problem: Four Operations
5
10
5.00 + 10.00 = 15.00
5.00 - 10.00 = -5.00
5.00 * 10.00 = 50.00
5.00 / 10.00 = 0.50
Solution: Four Operations
double num1 = Double.parseDouble(sc.nextLine());
double num2 = Double.parseDouble(sc.nextLine());
double sum = num1 + num2;
System.out.printf("%.2f + %.2f = %.2fn",
num1, num2, sum);
// TODO: Implement the rest of operations
Denotes a
new line
 …
 …
 …
Next Steps
 Join the SoftUni "Learn To Code" Community
 Access the Free Coding Lessons
 Get Help from the Mentors
 Meet the Other Learners
https://softuni.org

More Related Content

What's hot

03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...Intro C# Book
 
14. Java defining classes
14. Java defining classes14. Java defining classes
14. Java defining classesIntro C# Book
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classesIntro C# Book
 
13. Java text processing
13.  Java text processing13.  Java text processing
13. Java text processingIntro C# Book
 
Ch02 primitive-data-definite-loops
Ch02 primitive-data-definite-loopsCh02 primitive-data-definite-loops
Ch02 primitive-data-definite-loopsJames Brotsos
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks QueuesIntro C# Book
 
12. Exception Handling
12. Exception Handling 12. Exception Handling
12. Exception Handling Intro C# Book
 
18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and SetIntro C# Book
 
Introduction To Algorithm [2]
Introduction To Algorithm [2]Introduction To Algorithm [2]
Introduction To Algorithm [2]ecko_disasterz
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstractionIntro C# Book
 
02. Primitive Data Types and Variables
02. Primitive Data Types and Variables02. Primitive Data Types and Variables
02. Primitive Data Types and VariablesIntro C# Book
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++Ilio Catallo
 
Bitwise Operations in Programming
Bitwise Operations in ProgrammingBitwise Operations in Programming
Bitwise Operations in ProgrammingSvetlin Nakov
 
OOP program questions with answers
OOP program questions with answersOOP program questions with answers
OOP program questions with answersQuratulain Naqvi
 

What's hot (20)

10. Recursion
10. Recursion10. Recursion
10. Recursion
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
 
14. Java defining classes
14. Java defining classes14. Java defining classes
14. Java defining classes
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classes
 
13. Java text processing
13.  Java text processing13.  Java text processing
13. Java text processing
 
Ch02 primitive-data-definite-loops
Ch02 primitive-data-definite-loopsCh02 primitive-data-definite-loops
Ch02 primitive-data-definite-loops
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues
 
09. Methods
09. Methods09. Methods
09. Methods
 
12. Exception Handling
12. Exception Handling 12. Exception Handling
12. Exception Handling
 
18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set
 
Introduction To Algorithm [2]
Introduction To Algorithm [2]Introduction To Algorithm [2]
Introduction To Algorithm [2]
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
 
02. Primitive Data Types and Variables
02. Primitive Data Types and Variables02. Primitive Data Types and Variables
02. Primitive Data Types and Variables
 
07. Arrays
07. Arrays07. Arrays
07. Arrays
 
Parameters
ParametersParameters
Parameters
 
DSA 103 Object Oriented Programming :: Week 4
DSA 103 Object Oriented Programming :: Week 4DSA 103 Object Oriented Programming :: Week 4
DSA 103 Object Oriented Programming :: Week 4
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++
 
Bitwise Operations in Programming
Bitwise Operations in ProgrammingBitwise Operations in Programming
Bitwise Operations in Programming
 
OOP program questions with answers
OOP program questions with answersOOP program questions with answers
OOP program questions with answers
 

Similar to Java Tutorial: Part 4 - Data and Calculations

Similar to Java Tutorial: Part 4 - Data and Calculations (20)

2 EPT 162 Lecture 2
2 EPT 162 Lecture 22 EPT 162 Lecture 2
2 EPT 162 Lecture 2
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02
 
Chapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory ConceptChapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory Concept
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptx
 
L03vars
L03varsL03vars
L03vars
 
keyword
keywordkeyword
keyword
 
keyword
keywordkeyword
keyword
 
C notes for exam preparation
C notes for exam preparationC notes for exam preparation
C notes for exam preparation
 
pythonQuick.pdf
pythonQuick.pdfpythonQuick.pdf
pythonQuick.pdf
 
python notes.pdf
python notes.pdfpython notes.pdf
python notes.pdf
 
python 34💭.pdf
python 34💭.pdfpython 34💭.pdf
python 34💭.pdf
 
C++
C++C++
C++
 

More from Svetlin Nakov

BG-IT-Edu: отворено учебно съдържание за ИТ учители
BG-IT-Edu: отворено учебно съдържание за ИТ учителиBG-IT-Edu: отворено учебно съдържание за ИТ учители
BG-IT-Edu: отворено учебно съдържание за ИТ учителиSvetlin Nakov
 
Programming World in 2024
Programming World in 2024Programming World in 2024
Programming World in 2024Svetlin Nakov
 
AI Tools for Business and Startups
AI Tools for Business and StartupsAI Tools for Business and Startups
AI Tools for Business and StartupsSvetlin Nakov
 
AI Tools for Scientists - Nakov (Oct 2023)
AI Tools for Scientists - Nakov (Oct 2023)AI Tools for Scientists - Nakov (Oct 2023)
AI Tools for Scientists - Nakov (Oct 2023)Svetlin Nakov
 
AI Tools for Entrepreneurs
AI Tools for EntrepreneursAI Tools for Entrepreneurs
AI Tools for EntrepreneursSvetlin Nakov
 
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023Svetlin Nakov
 
AI Tools for Business and Personal Life
AI Tools for Business and Personal LifeAI Tools for Business and Personal Life
AI Tools for Business and Personal LifeSvetlin Nakov
 
Дипломна работа: учебно съдържание по ООП - Светлин Наков
Дипломна работа: учебно съдържание по ООП - Светлин НаковДипломна работа: учебно съдържание по ООП - Светлин Наков
Дипломна работа: учебно съдържание по ООП - Светлин НаковSvetlin Nakov
 
Дипломна работа: учебно съдържание по ООП
Дипломна работа: учебно съдържание по ООПДипломна работа: учебно съдържание по ООП
Дипломна работа: учебно съдържание по ООПSvetlin Nakov
 
Свободно ИТ учебно съдържание за учители по програмиране и ИТ
Свободно ИТ учебно съдържание за учители по програмиране и ИТСвободно ИТ учебно съдържание за учители по програмиране и ИТ
Свободно ИТ учебно съдържание за учители по програмиране и ИТSvetlin Nakov
 
AI and the Professions of the Future
AI and the Professions of the FutureAI and the Professions of the Future
AI and the Professions of the FutureSvetlin Nakov
 
Programming Languages Trends for 2023
Programming Languages Trends for 2023Programming Languages Trends for 2023
Programming Languages Trends for 2023Svetlin Nakov
 
IT Professions and How to Become a Developer
IT Professions and How to Become a DeveloperIT Professions and How to Become a Developer
IT Professions and How to Become a DeveloperSvetlin Nakov
 
GitHub Actions (Nakov at RuseConf, Sept 2022)
GitHub Actions (Nakov at RuseConf, Sept 2022)GitHub Actions (Nakov at RuseConf, Sept 2022)
GitHub Actions (Nakov at RuseConf, Sept 2022)Svetlin Nakov
 
IT Professions and Their Future
IT Professions and Their FutureIT Professions and Their Future
IT Professions and Their FutureSvetlin Nakov
 
How to Become a QA Engineer and Start a Job
How to Become a QA Engineer and Start a JobHow to Become a QA Engineer and Start a Job
How to Become a QA Engineer and Start a JobSvetlin Nakov
 
Призвание и цели: моята рецепта
Призвание и цели: моята рецептаПризвание и цели: моята рецепта
Призвание и цели: моята рецептаSvetlin Nakov
 
What Mongolian IT Industry Can Learn from Bulgaria?
What Mongolian IT Industry Can Learn from Bulgaria?What Mongolian IT Industry Can Learn from Bulgaria?
What Mongolian IT Industry Can Learn from Bulgaria?Svetlin Nakov
 
How to Become a Software Developer - Nakov in Mongolia (Oct 2022)
How to Become a Software Developer - Nakov in Mongolia (Oct 2022)How to Become a Software Developer - Nakov in Mongolia (Oct 2022)
How to Become a Software Developer - Nakov in Mongolia (Oct 2022)Svetlin Nakov
 
Blockchain and DeFi Overview (Nakov, Sept 2021)
Blockchain and DeFi Overview (Nakov, Sept 2021)Blockchain and DeFi Overview (Nakov, Sept 2021)
Blockchain and DeFi Overview (Nakov, Sept 2021)Svetlin Nakov
 

More from Svetlin Nakov (20)

BG-IT-Edu: отворено учебно съдържание за ИТ учители
BG-IT-Edu: отворено учебно съдържание за ИТ учителиBG-IT-Edu: отворено учебно съдържание за ИТ учители
BG-IT-Edu: отворено учебно съдържание за ИТ учители
 
Programming World in 2024
Programming World in 2024Programming World in 2024
Programming World in 2024
 
AI Tools for Business and Startups
AI Tools for Business and StartupsAI Tools for Business and Startups
AI Tools for Business and Startups
 
AI Tools for Scientists - Nakov (Oct 2023)
AI Tools for Scientists - Nakov (Oct 2023)AI Tools for Scientists - Nakov (Oct 2023)
AI Tools for Scientists - Nakov (Oct 2023)
 
AI Tools for Entrepreneurs
AI Tools for EntrepreneursAI Tools for Entrepreneurs
AI Tools for Entrepreneurs
 
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023
 
AI Tools for Business and Personal Life
AI Tools for Business and Personal LifeAI Tools for Business and Personal Life
AI Tools for Business and Personal Life
 
Дипломна работа: учебно съдържание по ООП - Светлин Наков
Дипломна работа: учебно съдържание по ООП - Светлин НаковДипломна работа: учебно съдържание по ООП - Светлин Наков
Дипломна работа: учебно съдържание по ООП - Светлин Наков
 
Дипломна работа: учебно съдържание по ООП
Дипломна работа: учебно съдържание по ООПДипломна работа: учебно съдържание по ООП
Дипломна работа: учебно съдържание по ООП
 
Свободно ИТ учебно съдържание за учители по програмиране и ИТ
Свободно ИТ учебно съдържание за учители по програмиране и ИТСвободно ИТ учебно съдържание за учители по програмиране и ИТ
Свободно ИТ учебно съдържание за учители по програмиране и ИТ
 
AI and the Professions of the Future
AI and the Professions of the FutureAI and the Professions of the Future
AI and the Professions of the Future
 
Programming Languages Trends for 2023
Programming Languages Trends for 2023Programming Languages Trends for 2023
Programming Languages Trends for 2023
 
IT Professions and How to Become a Developer
IT Professions and How to Become a DeveloperIT Professions and How to Become a Developer
IT Professions and How to Become a Developer
 
GitHub Actions (Nakov at RuseConf, Sept 2022)
GitHub Actions (Nakov at RuseConf, Sept 2022)GitHub Actions (Nakov at RuseConf, Sept 2022)
GitHub Actions (Nakov at RuseConf, Sept 2022)
 
IT Professions and Their Future
IT Professions and Their FutureIT Professions and Their Future
IT Professions and Their Future
 
How to Become a QA Engineer and Start a Job
How to Become a QA Engineer and Start a JobHow to Become a QA Engineer and Start a Job
How to Become a QA Engineer and Start a Job
 
Призвание и цели: моята рецепта
Призвание и цели: моята рецептаПризвание и цели: моята рецепта
Призвание и цели: моята рецепта
 
What Mongolian IT Industry Can Learn from Bulgaria?
What Mongolian IT Industry Can Learn from Bulgaria?What Mongolian IT Industry Can Learn from Bulgaria?
What Mongolian IT Industry Can Learn from Bulgaria?
 
How to Become a Software Developer - Nakov in Mongolia (Oct 2022)
How to Become a Software Developer - Nakov in Mongolia (Oct 2022)How to Become a Software Developer - Nakov in Mongolia (Oct 2022)
How to Become a Software Developer - Nakov in Mongolia (Oct 2022)
 
Blockchain and DeFi Overview (Nakov, Sept 2021)
Blockchain and DeFi Overview (Nakov, Sept 2021)Blockchain and DeFi Overview (Nakov, Sept 2021)
Blockchain and DeFi Overview (Nakov, Sept 2021)
 

Recently uploaded

Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....ShaimaaMohamedGalal
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 

Recently uploaded (20)

Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 

Java Tutorial: Part 4 - Data and Calculations

  • 1. Java Tutorial Part 4: Data and Calculations
  • 3.  Computers are machines that process data  Both program instructions and data are stored in the computer memory  Data is stored by using variables How Does Computing Work? 10110
  • 4.  A variable is a container for information  A named area of the computer memory  The data can be read and changed at any time  Variables provide means for:  Storing data  Retrieving the stored data  Modifying the stored data Variables
  • 5.  Variables are characterized by:  name (identifier)  type (of the data preserved)  value (stored information)  Defining a variable in Java: Variables int age = 25; Data type Variable name Variable value Name Type Value age int 25 name String "Peter" size double 3.50 Computer memory
  • 6. Data Types Text, Numbers, and Other Types in Java
  • 7.  Variables store value of a certain type  Number, letter, text (string), date, color, picture, list, …  Data types:  int – an integer: 1, 2, 3…  double – a floating-point number: -0.5, 3.14, …  boolean – a boolean: true, false  char – a symbol: 'a', 'b', '#', …  String – text: "Hello", "World", … Data Types
  • 8.  Data types define ranges of values with similar characteristics  Data types are characterized by:  Name  Example: boolean, int, String  Size (memory usage)  Example: 4 bytes  Default value  Example: 0 Data Types (2)
  • 10.  The actions that a program takes, are expressed as statements  Common statements (actions / commands) include:  Declaring a variable  Assigning a value  Declaring + initializing Statements int counter; counter = 1; int counter = 1;  Printing a value System.out.println(counter);  Modifying a value counter++; sum = a + b;
  • 11.  If-else statement  Method definition statement Complex Statements if (a == 5) System.out.println("Five"); else System.out.println("Not Five");  Loop statement int a = 5; while (a > 0) { a--; } static int sum(int a, int b) { return a + b; }
  • 12. Arithmetic Operators Add, Subtract, Multiply and Divide Numbers + - * /
  • 13.  Adding numbers (operator + )  Subtracting numbers (operator - ) Arithmetic Operators: + and - int a = 5; int b = 7; int sum = a + b; System.out.println(sum); // 12 int a = 15; int b = 7; System.out.println(a - b); // 8
  • 14.  Multiplying numbers (operator *)  Dividing numbers (operator / ) Arithmetic Operators: * and / int a = 5; int b = 7; System.out.println(a * b); // 35 int a = 25; int b = 4; System.out.println(a / b); // 6
  • 15.  When dividing integers, the result is also integer:  When dividing floating-points, the result is also floating-point: Division Behavior in Java int a = 25; System.out.println(a / 4); // Integer result: 6 System.out.println(a / 0); // Error: division by 0 double a = 15; System.out.println(a / 2.0); // 7.5 System.out.println(a / 0.0); // Infinity System.out.println(0 / 0.0); // NaN
  • 16.  Modulo / remainder from integer division (operator % ) Arithmetic Operators: % int a = 7; int b = 2; System.out.println(a % b); // 1 System.out.println(3 % 2); // 1 System.out.println(4 % 2); // 0 System.out.println(3.5 % 1); // 0.5 7 = 3 * 2 + remainder 1 3 = 1 * 2 + remainder 1 4 = 3 * 2 + remainder 0 3.5 = 3 * 1 + remainder 0.5
  • 18.  Expressions == sequences of operators, literals and variables which are evaluated to a value  Consist of at least one operand  Can have 1 or more operators Expressions int r = (150-20) / 2 + 5; int y = x + 5; String name = "John Doe";
  • 19.
  • 20.  Write a program to convert from USD to EUR:  Read a floating-point number: the dollars to be converted  Convert dollars to euro (use fixed rate of dollars to euro: 0.88)  Print the converted value in euro formatted to the 2nd digit Problem: Currency Converter 17 14.96 87 76.56 Judge: https://judge.softuni.org/Contests/Practice/Index/3253
  • 21. Creating a New Project in IntelliJ IDEA
  • 22. Solution: Currency Converter Scanner scanner = new Scanner(System.in); double dollars = scanner.nextDouble(); double euros = dollars * 0.88; System.out.printf("%.2f", euros);
  • 23. Submission in the Judge System https://judge.softuni.org/Contests/Practice/Index/3253
  • 24.  Write a program, which:  Reads 2 real numbers from the console  Performs 4 arithmetic operations on the obtained 2 numbers, in the following order: +, -, *, /  Formats and prints the results like this example: Problem: Four Operations 5 10 5.00 + 10.00 = 15.00 5.00 - 10.00 = -5.00 5.00 * 10.00 = 50.00 5.00 / 10.00 = 0.50
  • 25. Solution: Four Operations double num1 = Double.parseDouble(sc.nextLine()); double num2 = Double.parseDouble(sc.nextLine()); double sum = num1 + num2; System.out.printf("%.2f + %.2f = %.2fn", num1, num2, sum); // TODO: Implement the rest of operations Denotes a new line
  • 26.  …  …  … Next Steps  Join the SoftUni "Learn To Code" Community  Access the Free Coding Lessons  Get Help from the Mentors  Meet the Other Learners https://softuni.org

Editor's Notes

  1. Hello, I am Svetlin Nakov from SoftUni (the Software University). I am here for the fourth part of my free Java coding tutorial – a series of video lessons with hands-on coding exercises. Today I continue with the fourth part of my free Java coding tutorial for absolute beginners. If you missed the previous parts, please review them first, to catch up. In this lesson, I will talk about variables and data types (such as string, integer number, floating-point number, boolean and others), statements (which define the commands in the programs), the most used arithmetic operators (plus, minus, multiply, divide and remainder) and the expressions in Java (or how to combine operators with values to implement a calculation). As usually, I will show you how to solve several coding problems and how to submit your solutions in the judge system for automated grading. Don't skip the coding exercises at the end of this lesson! They give you skills and coding experience. To learn coding, you should code! That's it! Let's start! Let's learn how to use data and calculations in Java.
  2. Let's start with variables. In programming variables are used to store and process data in the in the computer memory. Variables are named memory areas, which hold data of certain type, like number of text. Let's learn more about them.
  3. In the next section, I will give you a brief explanation of data types in programming and how they work in Java. I will mention the number types (int and double), the text type (String), the character type (char) and the Boolean type. And of course, I will demonstrate you these data types in a live coding demo.
  4. In the next section I will explain the concept of "statements" in programming and the different types of statements.
  5. Let's review the most important arithmetic operators, used to perform calculations with data in Java. I will explain the operators plus (for adding numbers), minus (for subtracting numbers), asterisk (for multiplying number), slash (for dividing numbers) and percent (for calculating the reminder of а division).
  6. Now it's time for the hands-on exercises, because we want to learn skills, not just talk or watch video lessons. Follow the exercises: solve the practical problems and send your solutions to the judge system for grading. Learn by doing! Write code, run the code, test the code, make mistakes, fix them, run and test again, finally submit your code in the judge. This is how you learn coding: by practice. You will find the problem descriptions and the link to the judge system at softuni.org. Let's do the exercises. Let's code. Let's solve a few practical problems.
  7. Did you like this code lesson? Do you want more? Subscribe to my YouTube channel to get more free video tutorials on computer programming and software engineering. Join the learners' community at softuni.org. Get free access to the practical exercises and the automated judge system for this code lesson. Get free help from mentors and meet other learners. Join now! SOFTUNI.ORG