You are on page 1of 37

First Steps In Coding

Working with console, arithmetic operations with numbers

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

▪ The source code of the program is written inside main(String[] args)


▪ Between the opening and closing bracket { }
▪ The program code is written in offset

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:

▪ Wrong usage of lower and uppercase letters:

13
Typical Errors in Java Programs (2)
▪ Lack of ; at the end of each command

▪ Missing quote " or bracket

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);

each on a new line System.out.println(3);



Test your solution:
https://judge.softuni.org/Contests/Compete/Index/3447#1 System.out.println(10);

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:

Type Name of variable


int count = 5;
Value
17
Data Types
▪ Variables store a value of a given type
▪ Number, letter, text (string), date, color, picture, list, …
▪ Data type – examples:
▪ int – integer: 1, 2, 3, 4, 5, …
▪ double - floating-point number: 0.5, 3.14, -1.5, …
▪ String - text (string): “Hello", "Hi", …
▪ char - single character: 'a ', '& ', ' @ ', ' B ', …

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);

▪ Example: calculate the area of a square with a side а:


Scanner scanner = new Scanner(System.in);
int a = Integer.parseInt(scanner.nextLine());
int area = a * a;
System.out.print(area); Reads an Integer on
one row
Test your solution: https://judge.softuni.org/Contests/Compete/Index/3447#2 22
Reading Double Numbers
▪ Reading a double number from the console:
String input = scanner.nextLine();
double num = Double.parseDouble(input);

▪ Example: convert from inches to centimeters:


Scanner scanner = new Scanner(System.in);
double inches = Double.parseDouble(scanner.nextLine());
double centimeters = inches * 2.54;
System.out.println(centimeters);
Read a double number
from a row
Test your solution: https://judge.softuni.org/Contests/Compete/Index/3447#3 23
Simple Operations
Working with text and numbers
Greetings by Name – Example
▪ Write a program that:
▪ Reads the person's name entered by the user from the console
▪ Prints "Hello, <name>!", where <name> is read before that
▪ Sample input and output:
John Hello, John!

Peter Hello, Peter!

25
Greetings by Name – Solution

Scanner scanner = new Scanner(System.in);


String name = scanner.nextLine();
System.out.print("Hello, "); The cursor remains
System.out.print(name); on the same line
System.out.println("!");

Scanner scanner = new Scanner(System.in);


String name = scanner.nextLine(); Concatenation
System.out.print("Hello, " + name + "!");

Test your solution: https://judge.softuni.org/Contests/Compete/Index/3447#4 26


Combine Text and Number
▪ Combine text and number (operator +):
String firstName = "John";
String lastName = "Smith"; Concatenation
int age = 19;
String str = firstName + " " + lastName + " @ " + age;
System.out.println(str); // John Smith @ 19

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

▪ Subtraction of numbers (operator -):


Scanner scanner = new Scanner(System.in);
int a = Integer.parseInt(scanner.nextLine());
int b = Integer.parseInt(scanner.nextLine());
int result = a - b;
System.out.println(result);

28
Arithmetic Operations : * and /
▪ Multiplication of numbers (operator *):
int a = 5;
int b = 7;
int product = a * b; // 35

▪ Division of numbers(operator /):


int a = 25;
int i = a / 4; // 6 – the fractional part is cut
double f = a / 4.0; // 6.25 – fractional division
int error = a / 0; // Error : division by 0

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

▪ When dividing double numbers, the result is a double:


double a = 15;
System.out.println(a / 2.0); // Double Result : 7.5
System.out.println(a / 0.0); // Result : Infinity
System.out.println(0.0 / 0.0); // Result : NaN

30
Arithmetic operations: %
▪ Module - remainder of integer division of numbers (operator %):
int a = 7;
int b = 2;
int product = a % b; // 1

int odd = 3 % 2; // 1 – number 3 is odd


int even = 4 % 2; // 0 – number 4 is even
int error = 3 % 0; // Error: Division by 0

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();

System.out.printf("You are %s %s, a %d-years old


person from %s.", firstName, lastName, age, town);
Test your solution: https://judge.softuni.org/Contests/Compete/Index/3447#5 33
What Have We Learned Today?

▪ ▪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

You might also like