Class 2 class 1+++
Class 2 class 1+++
Structure of basic
Java program
Main features
● automatic memory management;
● advanced exception handling;
● I / O handling;
● set of standard collections (Map, List, Array,
etc.);
● tools and API for creating network applications
(including using RMI);
● classes to execute HTTP requests and process
responses;
● built-in language tools for creating multithreaded
applications;
● unified access to databases based on JDBC and
SQL
Stages of Java program
execution on computer
Interpretability
The Java interpreter can execute Java byte
code on any computer that has the interpreter
and the execution system installed. On the
interpretation platform, the program
compilation phase is simple and performed
step-by-step, so the programming process is
optimized and simplified, there are no
traditional difficult stages of compilation, build,
testing.
Loading Java classes in runtime
Exercise 1
Linux, Unix
Commands for bash/terminal
sudo apt update
sudo apt install openjdk-11-jdk // JDK
Configuration
An environment variable that will point to our JDK installation.
In Search field print in “Environment variables”
Go to section System Variables and click "New”
Enter the variable name as JAVA_HOME.
Enter the variable value as [directory where you unpacked zip package]
Select PATH variable in System Variables and click button Edit
Append directory where you unpacked zip package with suffix “\bin” to the end of PATH
variable value.
Ex: C:\WINDOWS\system32;C:\WINDOWS;"C:\Program Files\Java\jdk-11\bin"
Click OK (Save)
Test Java is installed
Search -> print in “cmd” -> Enter the command “java -version” in opened command line
window
Exercise 1
Install Java on Linux, Unix (Mac) - via SDK Man // recommended option
Press cmd + space, then type in “Terminal”
Enter following commands into terminal:
sudo apt update
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk version
sdk list java
sdk list java <version>
Copy to buffer any 11th version
sdk install java <11th version>
For reference:
https://sdkman.io
Exercise 1
Install Java on Mac via Homebrew
Press cmd + space, then type in “Terminal”
Enter following commands into terminal:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew tap homebrew/cask-versions
brew update
brew tap caskroom/cask
brew install openjdk@11
}
5)Semicolons at the end of each line is OBLIGATORY.
Exercise 2
Simple Java program
Your first app: displays your first and last name, group number and a
few words about yourself.
Create a source file (you can use any text editor) and save it with
.java extension
Compile the source file of the class using the javac command
Ensure the bytecode of compiled .class file was created
Run the program using the java command
Java packages
1) categorize classes and interfaces so that they can
be easily maintained
2) restrict access to it
3) remove name collision
Java packages types
1) user-defined package
2) built-in package: such as java.io.*, java.lang.*
etc.
Example:
import java.util.Scanner
java is a top level package
Example:
import java.nio.charset.StandardCharsets;
String string = "Hello World";
string.length();
string.getBytes(StandardCharsets.UTF_8);
utf8Bytes.length;
Exercise 4
Install IDE for Java code
Intellij Idea (by Jetbrains)
https://www.jetbrains.com/idea/download/#section=windows
Write a program that displays the statement: "Java: Write once - use
everywhere" 10 times.
Requirements:
* The program should display text.
* The text should start with "Java".
* The text should end with "use anywhere".
* The text should consist of 10 lines.
* The output text must match the task.
Exercise 6
Program using different numeric types & operators
Uncomment one line so that the program displays the numbers 12 and 2 (first 12, then 2).
Requirements:
* The program should display the numbers 12 and 2 on the screen.
* You cannot modify rows with variable declarations.
* The program must contain only two int variables.
* You cannot modify the lines responsible for accessing the console.
* You must uncomment one line and not change the rest.
// y = x * y;
// y = x + y;
x = y - x;
y = y - x;
System.out.println(x);
System.out.println(y);
}
}
Exercise 7
Program using different numeric types, operators and casts
int result = +a + b + c + d;
System.out.println(result);
}
}
Exercise 9
Program with correct usage of comments
Comment out those variables that are not used anywhere else. The program must compile.
Requirements:
* You cannot change the type of variables.
* Variable values cannot be changed.
* You cannot add new rows to the program or delete existing rows.
* You must comment on unused variables.
* The program output cannot be changed.
public class Solution {
public static void main(String[] args) {
int a = 10;
int b = 15;
double c = b + 38;
int d = a + 12;
double e = 12.3;
String s = "s" + a;
String s1 = a + "b";
String s2 = "a";
String s3 = s1 + "a";
String s4 = s3 + "b";
System.out.println(c + s4 + s);
}
}
Exercise 10
Program using methods
Implement the public static void div (int a, int B) method.
The method should divide the first number by the second and display the result of dividing a by B.
An integer should be displayed on the screen.
Requirements:
* The div function should divide a by B.
* The div function should display the split result on the screen.
* The main function should call the div function 3 times.
* The main function should not call the on-screen text command.
* The program should write three numbers: 2 1 0. Each number in a separate row.
}
}
Exercise 11
Using packages in a program (Math package)
Write a program that calculates the square of the variable “number” and displays it
on the screen
Notes:
A square is the result of multiplying a number by itself (number * number).
Requirements:
* The variable number can only change the value.
* The program must show the output data on the screen.
* The output text must be a number.
* The output text must be the square of the variable number.
Exercise 12
Program using different numeric types, operators and methods
Display the multiplication table 10 by 10 as follows:
1 2 3 4 ...
2 4 6 8 ...
3 6 9 12 ...
4 8 12 16 ...
...
Requirements:
* The program should display text.
* The printed text must contain 10 lines.
* Each resulting row must contain 10 numbers separated by a space.
* The output numbers must be a multiplication table.
Exercise 13*
Place the brackets correctly so that the number 382 is displayed on the
screen
Requirements:
* Method main must contain only one System.out.println method call.
* You cannot change the sequence of numbers and arithmetic
operations.
* The number of parentheses should remain the same (2 opening and 2
changing).
* The main method should display 382 numbers on the screen.
Hint:
\ "- escaping double quotes;
\\ - escaping the ‘\’ symbol.
Requirements:
* The program should display text.
* Two lines should be drawn.
* First line must be: It's a Windows path: "C:\Program
Files\Java\jdk11\bin"
* Second line must be: It's a Java string: \"C:\\Program
Files\\Java\\jdk11\\bin\"
Exercise 15
Program with methods
Implement the method
public static int convertToSeconds(int hour)
which converts hours to seconds.
Call it twice in the main method with any parameters.
Display the results on the screen, each time with a new line.
Requirements:
* Add convertToSeconds method, it should be public static, return and
accept int.
* convertToSeconds(int hour) method is used to translate the transmitted
hours into seconds.
* The main method must call convertToSeconds(int hour) method twice.
* The main method should display the results of the call on the screen, each
time with a new line.
* convertToSeconds(int hour) method should not display anything on the
screen.
Exercise 16
String and its operations
Create a StringHelper class that has 2 static methods:
String multiply (String s, int count) - returns s repeated "count" times.
String multiply (String s) - returns s repeated 5 times.
Example:
multiply(Jane) should return Janejanejanejanejane
Requirements:
* Methods of the StringHelper class must return string.
* StringHelper class methods must be static.
* StringHelper class methods must be public.
* The multiply(String s, int count) method should return a string repeated
count times.
* The multiply(String s) method should return a string repeated 5 times.
Exercise 16
Program using I/O
Enter the name, number 1, number 2 separately from the keyboard.
Write a caption:
"Name" earns "number 1" for "number 2" years.
Example:
John earns 3000 after 5 years.
Requirements:
* The program should display text.
* The program must read data from the keyboard.
* The printed text must contain a typed name.
* The printed text must contain the number 1.
* The printed text must contain the number 2.
* The printed text must fully satisfy task requirements.