Programming Java

Tasks studies - laboratory


Project maintained by dawidolko Hosted on GitHub Pages — Theme by dawidolko

OBJECT-ORIENTED PROGRAMMING JAVA – LABORATORY

Programming tools: Eclipse vs IntelliJ

lab1

First Project

To create a project, select the “Create New Project” option:

lab1

Then, in the “New Project” window, configure the basic settings (select Java and choose the Project SDK):

lab1
Click the “Next” button to proceed to the next view. Configure the options as shown in the image:

lab1
At this point, set the project name and its location on the disk. Complete the process by clicking “Finish”:

lab1

Running the Program

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:

lab1

Introduction to Java

Keywords:

Java Basics

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 second line public static void main(String[] args) defines a method:

The third line System.out.println("Hello Java") is responsible for printing the string “Hello Java” to the console:

Running the Program

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

lab1

Primitive Types

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.

Example:

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

Data Display Instructions

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

Keyboard Input Instructions

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

Tasks:

Task 1.

Write a method that returns your name and current age.

Task 2.

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.

Task 3.

Write a method that takes a number as an argument and returns true if the number is even.

Task 4.

Write a method that takes a number as an argument and returns true if the number is divisible by 3 and 5.

Task 5.

Write a method that takes a number as an argument and returns it raised to the 3rd power.

Task 6.

Write a method that takes a number as an argument and returns its square root.

Task 7.

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.