Professional Documents
Culture Documents
SoftUni Team
SoftUni Global
https://softuni.org
Table of Contents
1. What is programming?
2. First program with Java and IntelliJ IDEA
3. Variables and data types
4. Read user input
5. Simple operations
▪ work with text and numbers
▪ arithmetic operations
6. Console printing
2
What is programming?
What is Programming?
▪ Computer science
▪ Uses commands to communicate with the
computer
▪ The commands are arranged and executed
one after another
▪ The series of commands form a computer
program
4
What is a Programming Language?
▪ Computer programs are written in a
programming language
▪ Example: Java, C#, JavaScript, Python, PHP,
C, C++
▪ It is used a programming environment
(development environment)
▪ Example: IntelliJ IDEA, Visual Studio,
PyCharm, Visual Studio Code, Code Blocks
5
What is a Computer Program?
▪ The program is a sequence of commands
▪ May contain calculations, checks, repetitions
▪ Programs are written in text format
▪ The text of the program is called a source code
▪ The source code is compiled to an executable file
▪ For example, Main.java is compiled to
Main.class
6
Interesting Facts About the Java Language
▪ One of the top 3 most popular programming
languages
▪ 95% of all technology corporations use Java as
their primary language
▪ There are currently more than 3 billion phones and
125 million TVs that use Java
▪ The first name of the language was Oak
▪ The creator of the language is James Gosling
7
Let’s Make Our First Console Program
Writing Programming Code
▪ The program code is stored in the src folder of the project
9
Writing Programming Code (2)
▪ Write the following code:
System.out.println("Hello SoftUni");
10
Starting the Program
▪ There are two ways to start the program:
▪ By using the keyboard shortcut : Ctrl + Shift + F10
▪ Right click -> Run (from the drop-down menu)
11
Result from Starting the Program
▪ If there are no errors, the program will run
▪ The result will be displayed on the console (below):
Result
Test your solution: https://judge.softuni.org/Contests/Compete/Index/3447#0 12
Typical Errors in Java Programs
▪ Writing outside the body of the main() method:
13
Typical Errors in Java Programs (2)
▪ Lack of ; at the end of each command
14
Numbers from 1 to 10
▪ Write a program that ▪ Solution:
prints the numbers System.out.println(1);
from 1 through 10, System.out.println(2);
15
Variables and Data Types
Variables
▪ Computers are machines that process data
▪ The data is stored in the computer memory in variables
▪ Variables have three main characteristics: type, name and value
▪ Defining a variable and assigning a value:
18
Read User Input
Working with a console
Reading Text
▪ Everything we receive from the console comes in a
text form
▪ Everything we print on the console is converted to text
▪ Console read command:
▪ Return the text, entered by the user:
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
20
Reading Text (2)
▪ Reading the input:
Scanner scanner = new Scanner(System.in);
▪ Example:
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println(name);
21
Reading Integers
▪ Reading an integer number:
String input = scanner.nextLine();
int num = Integer.parseInt(input);
25
Greetings by Name – Solution
double a = 1.5;
double b = 2.5;
String sum = "The sum is: " + a + b;
System.out.println(sum); // The sum is 1.52.5
27
Arithmetic Operations : + and -
▪ Addition of numbers (operator +):
int a = 5;
int b = 7;
int sum = a + b; // 12
28
Arithmetic Operations : * and /
▪ Multiplication of numbers (operator *):
int a = 5;
int b = 7;
int product = a * b; // 35
29
Problems with Division of Numbers
▪ When dividing integer numbers, the result is an integer:
int a = 25;
System.out.println(a / 4); // Integer result:6
System.out.println(a / 0); // Error: division by 0
30
Arithmetic operations: %
▪ Module - remainder of integer division of numbers (operator %):
int a = 7;
int b = 2;
int product = a % b; // 1
31
Printing on Console
Concatenating Text and Numbers
▪ When printing text, numbers and other data, we can combine them
using templates
▪ %s (String), %d (int), %f (double), %c (char), %n (new line),…
Scanner scanner = new Scanner(System.in);
String firstName = scanner.nextLine();
String lastName = scanner.nextLine();
int age = Integer.parseInt(scanner.nextLine());
String town = scanner.nextLine();
▪ ▪The
…computer program is a series of commands
▪ ▪Commands
… are written in the main(…) method
▪ ▪We…print with the command
System.out.println(…)
▪ Entering text and numbers
▪ Arithmetic operations with numbers:
+, -, *, /, (), %
▪ Print text by template
34
Questions?
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
License
▪ This course (presentations, examples, demonstration code,
exercises, homework, videos and other assets) is copyrighted
▪ Unauthorized copying, distribution or use is illegal
▪ © Softuni Global – https://softuni.org
36
Training in SoftUni Global
▪ Softuni Global – High-Quality Education, Profession,
and Job for Software Developers
▪ softuni.org
▪ SoftUni Global@ Facebook
▪ facebook.com/softuni.global
▪ SoftUni Global Reddit
▪ r/softuni