0% found this document useful (0 votes)
9 views

Java Basics Tutorial Part 1 Getting Started

Uploaded by

Leng Hongmeng
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Java Basics Tutorial Part 1 Getting Started

Uploaded by

Leng Hongmeng
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Java Basics Tutorial

Part 1: Getting Started


with Java Coding

L esso ns
Cod e
Free
Welcome
to Coding
Writing
and Running
Java Code
What is Coding?
 Coding means to give commands to tell the computer what to do
 Sample command:
System.out.println("Hey, I am coding");

 A computer program is a sequence of commands (lines of code)


System.out.println("First command");
System.out.println("Second command");
System.out.println("Third command");
Repl.it: An Online Coding Environment

Register at:
https://repl.it
Write, compile
and run code in
Java, JS, Python
and others
Repl.it: An Online Coding Environment
Commands in Java – Examples
 Calculate an expression and print its value:
System.out.println(5 + 5);

 Check if certain word contains another word


System.out.println("softuni".contains("uni"));

 Print the numbers from 1 to 100


for (int i = 1; i <= 100; i++)
System.out.println(i);
Coding
Concepts
Programming,
Commands, Code,
Algorithms, IDEs
Programming and Algorithms
 Programming means writing computer programs (commands)
 Using certain programming language, such as Java or Python
 Algorithm == a sequence of commands that achieves certain result
 Programming (coding) is performed by programmers (developers)
 Programmers use IDE (like IntelliJ IDEA or Eclipse or REPL.it) to:
 Write the code
 Run and test the code
 Find a fix bugs (debug the code)
Computer Program – Example
 Sample Java program (sequence of Java commands):
int size = 5;
System.out.println("Size = " + size);
System.out.println("Area = " + size * size);
Complete Computer Program
 Sample complete Java program (class + method + commands):
public class Main {
public static void main(String[] args) {
int size = 5;
System.out.println("Size = " + size);
System.out.println(
"Area = " + size * size);
}
}
Console-Based Java Program – Example
 Java program, which converts from USD to EUR (at fixed rate)
Scanner scanner = new Scanner(System.in);
int dollars = scanner.nextInt(); Put this line of code:
double euro = dollars * 0.883795087; import java.util.Scanner;
System.out.println("Euro: " + euro); before the class definition
The Judge System

Sending your Solutions


for Automated Evaluation
Testing the Program in the Judge System
 Test your code online in the SoftUni Judge system:
https://judge.softuni.org/Contests/3250
C od in g E xe rci s es

Writing
Sim p l e J ava
Programs
Learn by Doing
 To learn coding, you need to write code!
 Watching videos gives you only knowledge
 Solving the exercises, gives you experience and
practical skills

Write and submit the


coding exercises!
Problem: Print "Hello Java"
 Write a Java program, which:
 Prints "Hello Java" on the console

 Submit your solution in the SoftUni judge:


https://judge.softuni.org/Contests/Practice/Index/3250
Solution: Print "Hello Java"

public class Main {


public static void main(String[] args) {
System.out.println("Hello Java");
}
}
Submission in the Judge System

https://judge.softuni.org/Contests/Practice/Index/3250
Problem: Calculate and Print 5 * 5
 Write a Java program, which:
 Calculates the value of 5 * 5
 Prints the result at the console

 Submit your solution in the SoftUni judge:


https://judge.softuni.org/Contests/Practice/Index/3250
Solution: Calculate and Print 5 * 5

public class Main {


public static void main(String[] args) {
System.out.println(5 * 5);
}
}
Problem: Name and Expression
 Write a program to print your name at the first line and
calculate and print the expression 5 + 3 at the second line
 The expected output from your program might look like this:
Maria
8

 Another example of valid output:


Peter
8
Problem: Calculations
 Write a program, which calculates and prints the value of the
following expressions:
 5+3*2
 4 * (2 + 3)
 (2 + 5) * (8 - 2) / 7
Problem: Square of 7 * 7 Stars
 Write a program, which:
 Prints a square of 7 * 7 stars like this:

* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
Solution: Square of 7 * 7 Stars
 First solution:
System.out.println("* * * * * * *");
System.out.println("* * * * * * *");
System.out.println("* * * * * * *");
System.out.println("* * * * * * *");
System.out.println("* * * * * * *");
System.out.println("* * * * * * *");
System.out.println("* * * * * * *");

 Second solution:
for (int i = 0; i < 7; i++)
System.out.println("* * * * * * *");
Problem: EUR to USD Converter
 Write a Java program, which converts from USD to EUR
 Assume the EUR/USD rate is fixed: 1.17 USD for 1 EUR
 Sample input: 5
 Sample output: USD = 5.85
Solution: EUR to USD Converter

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double eur = scan.nextDouble();
double usd = eur * 1.17;
System.out.println("USD = " + usd);
}
}
Learn By Doing!

Now
Do Your
Exerc i s es !
Learn by Doing
 The only way you can learn coding is by practice
 By writing code, a lot of code, every day

Write and submit the coding


exercises to gain experience!
Next Steps

 Join
… the SoftUni "Learn To Code" Community
 …
 …https://softuni.org
 Access the Free Coding Lessons
 Get Help from the Mentors
 Meet the Other Learners

You might also like