Tasks studies - laboratory
Programming tools: Eclipse vs IntelliJ
To create a project, select the “Create New Project” option:
Then, in the “New Project” window, configure the basic settings (select Java and choose the Project SDK):
Click the “Next” button to proceed to the next view. Configure the options as shown in the image:
At this point, set the project name and its location on the disk. Complete the process by clicking “Finish”:
The main IDE view should appear. So far, all screenshots have been presented in a dark theme. Now, let’s switch to a light theme for variety:
Keywords:
package - Defines a package, a group of classes and interfaces with a similar scope. From an operating system perspective, a package is a folder, and the classes and interfaces are defined within files. Usually, classes and interfaces are created in separate files, although multiple classes and interfaces can exist in a single file.
class - Defines the characteristics and behaviors that an object created from such a class will have. Behaviors are actions that an object can perform (called methods in Java). The characteristics of an object are stored in class fields.
interface - Defines behaviors that an object created from such an interface will adopt. Unlike classes, interface methods often do not contain executable code (exceptions exist but will be covered later). These methods act as a blueprint and are implemented by classes later.
import - Used at the beginning of a code file. It specifies the location of external packages and classes that can be used in the code.
extends - Enables classes and interfaces to be extended by subclasses and subinterfaces.
implements - Enables interfaces to be implemented by classes.
public class Start {
public static void main(String[] args) {
System.out.println("Hello!");
}
}
The first noticeable elements are the keywords public, class, static, and void. The code is divided into blocks. One defines the class block, and another, nested within it, defines the method block. In our first program, a block instruction contains another block instruction, which in turn defines an instruction in the form of an expression (calling a method to print Hello!
on the console).
package com.company;
public class Main {
public static void main(String[] args) {
// write your code here
System.out.println("Hello Java");
}
}
The first line public class Start
defines a class:
The word public is an access modifier determining the visibility of the element it precedes—in this case, the class. In simple terms, public means that the class can be accessed from any part of the program.
The word class represents a template for creating objects with defined characteristics (fields) and behaviors (methods). An object created from a class is called its instance, and the process of creation is called instantiation.
The word Main is the class name.
The second line public static void main(String[] args)
defines a method:
public specifies visibility as explained above.
static indicates that the method is static and does not require an instance of the class to be called.
void specifies that the method does not return any value.
main is the method name. A public static method named main with specific parameters (String[] args) is reserved as the entry point for the program.
(String[] args) specifies the method parameter. It represents an array of arguments. String refers to a class used for string representation, args is the parameter name, and [] indicates an array.
The third line System.out.println("Hello Java")
is responsible for printing the string “Hello Java” to the console:
System refers to a class.
out is a field in the System class.
println is a method in the System class that displays data on the screen.
The program can be run via the “Run” -> “Run Main” (Shift+F10) menu option (1) or by clicking the green arrow in the upper-right corner of the project (2).
Java is a strongly object-oriented language but also includes primitive types. Primitive types can be used in class fields and as variables in methods. Primitive types include: char
, boolean
, byte
, short
, int
, long
, float
, and double
.
package com.company;
public class Main {
public static void main(String[] args) {
// write your code here
System.out.println("Hello Java");
char itemFirstCharacter = 'i';
System.out.println(itemFirstCharacter);
boolean enabled = true;
System.out.println(enabled);
byte itemsNumberByte = 7;
System.out.println(itemsNumberByte);
short itemsNumberShort = 400;
System.out.println(itemsNumberShort);
int itemsNumberInt = 53000;
System.out.println(itemsNumberInt);
long itemsNumberLong = 72036854775807L;
System.out.println(itemsNumberLong);
float itemsNumberFloat = 4.33f;
System.out.println(itemsNumberFloat);
double itemsNumberDouble = 4.99;
System.out.println(itemsNumberDouble);
}
}
public static void main(String[] args) {
// Declare variables
String name;
int age;
double salary;
String city;
// Initialize variables
name = "Jurek";
age = 22;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter an integer:");
int a = in.nextInt();
System.out.println("Entered number: " + a);
System.out.println("Enter another integer:");
a = in.nextInt();
System.out.println("Entered number: " + a);
System.out.println("Enter a float number:");
float f = in.nextFloat();
System.out.println("Entered number: " + f);
System.out.printf("Integer: %d, Float: %.2f\n", a, f);
}
}
Write a method that returns your name and current age.
Write a method that takes 2 numbers as an argument and prints their sum, difference and product. The arguments passed to the method are numbers entered by the user from the keyboard.
Write a method that takes a number as an argument and returns true if the number is even.
Write a method that takes a number as an argument and returns true if the number is divisible by 3 and 5.
Write a method that takes a number as an argument and returns it raised to the 3rd power.
Write a method that takes a number as an argument and returns its square root.
Write a method that takes three numbers as an argument. The method should return true if a right triangle can be built from the segments of the lengths passed in the arguments.
The arguments passed to the method are random numbers from the range <a,b>
a and b are read
by the user from the keyboard.