SlideShare a Scribd company logo
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";
Java Tutorial: Part 4 - Data and Calculations
 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

PPTX
Java Foundations: Data Types and Type Conversion
PPTX
Java Foundations: Basic Syntax, Conditions, Loops
PPTX
02. Data Types and variables
PPTX
Java Foundations: Strings and Text Processing
PPT
Java basic tutorial by sanjeevini india
PPTX
19. Data Structures and Algorithm Complexity
PPTX
Java Tutorial: Part 1. Getting Started
PPTX
05. Java Loops Methods and Classes
Java Foundations: Data Types and Type Conversion
Java Foundations: Basic Syntax, Conditions, Loops
02. Data Types and variables
Java Foundations: Strings and Text Processing
Java basic tutorial by sanjeevini india
19. Data Structures and Algorithm Complexity
Java Tutorial: Part 1. Getting Started
05. Java Loops Methods and Classes

What's hot (20)

PPTX
10. Recursion
PPTX
03 and 04 .Operators, Expressions, working with the console and conditional s...
PPTX
09. Java Methods
PPTX
14. Java defining classes
PPTX
11. Java Objects and classes
PPTX
13. Java text processing
PPT
Ch02 primitive-data-definite-loops
PPTX
16. Arrays Lists Stacks Queues
PPTX
09. Methods
PPTX
12. Exception Handling
PPTX
18. Dictionaries, Hash-Tables and Set
PPT
Introduction To Algorithm [2]
PPTX
20.1 Java working with abstraction
PPTX
02. Primitive Data Types and Variables
PPTX
07. Arrays
PPT
Parameters
PPTX
DSA 103 Object Oriented Programming :: Week 4
PDF
Resource wrappers in C++
PPTX
Bitwise Operations in Programming
DOCX
OOP program questions with answers
10. Recursion
03 and 04 .Operators, Expressions, working with the console and conditional s...
09. Java Methods
14. Java defining classes
11. Java Objects and classes
13. Java text processing
Ch02 primitive-data-definite-loops
16. Arrays Lists Stacks Queues
09. Methods
12. Exception Handling
18. Dictionaries, Hash-Tables and Set
Introduction To Algorithm [2]
20.1 Java working with abstraction
02. Primitive Data Types and Variables
07. Arrays
Parameters
DSA 103 Object Oriented Programming :: Week 4
Resource wrappers in C++
Bitwise Operations in Programming
OOP program questions with answers
Ad

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

PPTX
Class 2 variables, classes methods...
PPTX
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
PDF
java basics - keywords, statements data types and arrays
PPT
ch02-primitive-data-definite-loops.ppt
PPT
ch02-primitive-data-definite-loops.ppt
PPTX
Lecture 3 and 4.pptx
PPT
Java căn bản - Chapter3
PPT
9781439035665 ppt ch02
PDF
BIT211_2.pdf
PPTX
Revision of introduction in java programming.pptx
PPTX
Numerical data.
PPT
lecture2 (1).ppt variable s and operators
PPT
Ap Power Point Chpt2
PPTX
Java chapter 2
PPTX
Ch2 Elementry Programmin as per gtu oop.pptx
PPT
Java: Primitive Data Types
PPTX
05 operators
PPTX
Java fundamentals
PPTX
Chapter 3.3
PPT
M C6java2
Class 2 variables, classes methods...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
java basics - keywords, statements data types and arrays
ch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.ppt
Lecture 3 and 4.pptx
Java căn bản - Chapter3
9781439035665 ppt ch02
BIT211_2.pdf
Revision of introduction in java programming.pptx
Numerical data.
lecture2 (1).ppt variable s and operators
Ap Power Point Chpt2
Java chapter 2
Ch2 Elementry Programmin as per gtu oop.pptx
Java: Primitive Data Types
05 operators
Java fundamentals
Chapter 3.3
M C6java2
Ad

More from Svetlin Nakov (20)

PPTX
AI and the Future of Devs: Nakov @ Techniverse (Nov 2024)
PPTX
AI за ежедневието - Наков @ Techniverse (Nov 2024)
PPTX
AI инструменти за бизнеса - Наков - Nov 2024
PPTX
AI Adoption in Business - Nakov at Forbes HR Forum - Sept 2024
PPTX
Software Engineers in the AI Era - Sept 2024
PPTX
Най-търсените направления в ИТ сферата за 2024
PPTX
BG-IT-Edu: отворено учебно съдържание за ИТ учители
PPTX
Programming World in 2024
PDF
AI Tools for Business and Startups
PPTX
AI Tools for Scientists - Nakov (Oct 2023)
PPTX
AI Tools for Entrepreneurs
PPTX
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023
PPTX
AI Tools for Business and Personal Life
PDF
Дипломна работа: учебно съдържание по ООП - Светлин Наков
PPTX
Дипломна работа: учебно съдържание по ООП
PPTX
Свободно ИТ учебно съдържание за учители по програмиране и ИТ
PPTX
AI and the Professions of the Future
PPTX
Programming Languages Trends for 2023
PPTX
IT Professions and How to Become a Developer
PPTX
GitHub Actions (Nakov at RuseConf, Sept 2022)
AI and the Future of Devs: Nakov @ Techniverse (Nov 2024)
AI за ежедневието - Наков @ Techniverse (Nov 2024)
AI инструменти за бизнеса - Наков - Nov 2024
AI Adoption in Business - Nakov at Forbes HR Forum - Sept 2024
Software Engineers in the AI Era - Sept 2024
Най-търсените направления в ИТ сферата за 2024
BG-IT-Edu: отворено учебно съдържание за ИТ учители
Programming World in 2024
AI Tools for Business and Startups
AI Tools for Scientists - Nakov (Oct 2023)
AI Tools for Entrepreneurs
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023
AI Tools for Business and Personal Life
Дипломна работа: учебно съдържание по ООП - Светлин Наков
Дипломна работа: учебно съдържание по ООП
Свободно ИТ учебно съдържание за учители по програмиране и ИТ
AI and the Professions of the Future
Programming Languages Trends for 2023
IT Professions and How to Become a Developer
GitHub Actions (Nakov at RuseConf, Sept 2022)

Recently uploaded (20)

PDF
System and Network Administraation Chapter 3
PPTX
Online Work Permit System for Fast Permit Processing
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Materi_Pemrograman_Komputer-Looping.pptx
PPT
Introduction Database Management System for Course Database
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
Materi-Enum-and-Record-Data-Type (1).pptx
PPTX
FLIGHT TICKET RESERVATION SYSTEM | FLIGHT BOOKING ENGINE API
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
L1 - Introduction to python Backend.pptx
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Transform Your Business with a Software ERP System
PPTX
Presentation of Computer CLASS 2 .pptx
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
How to Confidently Manage Project Budgets
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Understanding Forklifts - TECH EHS Solution
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
System and Network Administraation Chapter 3
Online Work Permit System for Fast Permit Processing
2025 Textile ERP Trends: SAP, Odoo & Oracle
Materi_Pemrograman_Komputer-Looping.pptx
Introduction Database Management System for Course Database
ISO 45001 Occupational Health and Safety Management System
Materi-Enum-and-Record-Data-Type (1).pptx
FLIGHT TICKET RESERVATION SYSTEM | FLIGHT BOOKING ENGINE API
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
L1 - Introduction to python Backend.pptx
Which alternative to Crystal Reports is best for small or large businesses.pdf
Upgrade and Innovation Strategies for SAP ERP Customers
Transform Your Business with a Software ERP System
Presentation of Computer CLASS 2 .pptx
PTS Company Brochure 2025 (1).pdf.......
How to Confidently Manage Project Budgets
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Understanding Forklifts - TECH EHS Solution
How to Choose the Right IT Partner for Your Business in Malaysia

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";
  • 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

  • #2: 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.
  • #3: 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.
  • #7: 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.
  • #10: In the next section I will explain the concept of "statements" in programming and the different types of statements.
  • #13: 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).
  • #20: 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.
  • #27: 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