SLC S22 Week1 || Getting Started with Java and Eclipse

Java-week1.png

Created by @kouba01 using Canva & PhotoFilter

Hello Steemians

Welcome to the first week of the Steemit Learning Challenge Season 22, where we will delve into the fascinating world of object-oriented programming (OOP) with Java. This course is designed for beginners and aims to introduce the fundamental concepts of Java, starting with its installation and the configuration of Eclipse as your development environment. Once set up, we will explore the core principles of Java, including variables, operators, control structures, and more.

Throughout this week, you will learn how to write, compile, and execute Java programs, gaining a solid foundation in one of the most versatile and widely used programming languages. You will also discover the essential tools and libraries that make Java a preferred choice for developing desktop, web, and mobile applications.

This introduction seamlessly combines theory and practical exercises, ensuring that you not only understand the concepts but also apply them effectively. By designing simple programs and experimenting with Java’s features, this interactive approach will help you develop a deeper understanding of programming and problem-solving.

By the end of this contest, you will be able to write your first Java programs, setting the stage for more complex and dynamic applications in the weeks to come. Let's begin this exciting journey together!

1. Presentation of the Java Programming Language

Java is an object-oriented programming language developed by Sun Microsystems, now owned by Oracle Corporation. It is distinguished by several main features: Java combines compilation and interpretation using the Java Virtual Machine (JVM), which gives it portability exemplified by the slogan "Write Once, Run Anywhere". Independent of platforms, it generates pseudo code that can be executed on any system with a JVM. Its rich API simplifies the work of developers by offering them predefined libraries. Java automatically manages memory through a garbage collector, avoiding the problems associated with direct memory manipulation, and adopts a syntax inspired by C/C++ for relative simplicity. Strongly typed and multitasking, it allows robust programming with threads.

Java exists in three main editions:

  • J2SE (Java Standard Edition) for desktop applications,
  • J2EE (Java Enterprise Edition) for server and web applications,
  • J2ME (Java Micro Edition) for mobile applications.

Finally, the Java Development Kit (JDK) is essential for developing with Java, including the Java Runtime Environment (JRE), which is only needed to run programs.

2. Executing a Java Program

2.1. Compilation and Execution Process

In Java, it is necessary to compile the source to transform it into Java pseudo-code (intermediate code or byte code) independent of the execution platform. This pseudo-code will then be executed by the virtual machine (JVM) installed on the machine. We recall here the motto of Java: write once, run everywhere.

The following figure re-illustrates this principle: the compiler generates a platform-independent .class extension file that will then be executed platform-dependently. This makes Java portable.

image.png

2.2. Compiling a Source File

  1. Write the source code in a file named FileName.java.
  2. Compile it with the following command:
    javac FileName.java
    
  3. This generates a FileName.class file containing the bytecode.

image.png

2.3. Executing a Program

A program can only be executed if it contains a properly defined main() method. To execute a file containing byte-code, simply invoke the java command with the name of the source file without its .class extension

image.png

3. Setting Up Our Environment

3.1. Installing JDK

Download the JDK from Oracle's official website.

14.PNG

Follow the installation instructions for your operating system.
Verify the installation by running:

java -version

3.2. Installing Eclipse IDE

Download Eclipse IDE for Java Developers from Eclipse.

0.PNG

Install the application and launch it.

1.PNG

Configure a workspace to organize your projects.

4. Writing Your First Java Program

4.1. Creating a Project in Eclipse

Open Eclipse and select File > New > Java Project.

3.png

Name your project (e.g., "test").

4.PNG

Click Finish.

4.2. Adding a Java Class

Right-click on your project, then select New > Class.

3.1.png

Enter the class name (e.g., "MyFirstProgram").
Check the box to include a main() method.

5.PNG

4.3. Writing the Code

Add the following code to your class:

image.png

4.4. Running the Program

  1. Click the green "Run" button or press Ctrl + F11.

7.PNG

  1. The output "Hello steemians," will appear in the console.

15.PNG

5. Basic Syntax and Rules

  1. Case Sensitivity: Identifiers like HelloWorld and helloworld are distinct.
  2. Semicolon: Each statement must end with a ;.
  3. Block Structure: Use {} to define blocks of code.
  4. Comments:
    • Single-line: //
    • Multi-line: /* ... */
    • Documentation: /** ... */

6. Variables and Data Types

6.1. Primitive Data Types

TypeSizeExample
int4 bytesint x = 10;
double8 bytesdouble pi = 3.14;
char2 byteschar c = 'A';
boolean1 bitboolean isTrue = true;

6.2. Declaration and Initialization

  • Declaration: int x;
  • Initialization: x = 10;
  • Combined: int x = 10;

6.3. Type Conversion

  • Implicit Conversion:
    int x = 10;
    double y = x; // Automatic conversion
    
  • Explicit Conversion (Casting):
    double z = 9.8;
    int w = (int) z; // Result: w = 9
    

7. Operators

7.1. Arithmetic Operators

+, -, *, /, % (modulo).

7.2. Comparison Operators

==, !=, <, >, <=, >=.

7.3. Logical Operators

&& (AND), || (OR), ! (NOT).


8. Control Structures

8.1. Conditional Statements

  • if...else:

image.png

  • switch:

image.png

8.2. Loops

  • for:

image.png

  • while:

image.png

  • do...while:

image.png

9. Arrays in Java

Arrays in Java are data structures that allow you to store multiple values of the same data type. They are fixed in size, meaning the number of elements in an array is defined when it is created and cannot be changed later.


9.1. Declaration and Initialization

  1. Declaration Only:

image.png

  1. Declaration and Allocation:

image.png

  1. Declaration with Initialization:

image.png


9.2. Accessing Array Elements

Array elements are accessed using their index, which starts at 0.

Example:

image.png

To modify an element:

image.png


9.3. Iterating Through an Array

You can iterate over an array using loops like for or for-each.

  1. Using a For Loop:

image.png

  1. Using a For-Each Loop:

image.png


9.4. Example Program: Summing Array Elements

Problem: Write a program to calculate the sum of all elements in an array.

image.png

Homework :

Task1: (2points)

Write a detailed explanation of the differences between JDK, JRE, and JVM by including their roles, functionalities, and interdependence. Add graphic illustrations which cover this points :

  • JDK: Development tools, includes JRE and compiler.
  • JRE: Execution environment for Java programs.
  • JVM: Virtual Machine responsible for running Java bytecode.

Task2: (2points)


Install and Set Up Your Java Development Environment and provide step-by-step screenshots of the installation process, Eclipse configuration.

Task3: (1points)

Write a program that calculates and displays the sum of the first 100 integers. Define the main method in a class named Sum to handle the entire calculation.

  1. Save this program in a file named Sum.java, compile it, and execute it.
  2. Create a new program named Sum2.java, where the calculation of the sum of the first 100 integers is performed in a separate function called calculateSum. This function should then be called from the main method.

Task4: (1point)


Translate, Complete, and Debug the Following Program Named Tax.java:**

public class Tax {
    public static double calculateTax(double income) {
        // result = value of the tax calculated based on the income
    }

    public static void main(String[] args) {
        System.out.print("Tax: ");
        System.out.println(calculateTax(57000));
    }
}

To calculate the tax, the income is divided into brackets, with each bracket defined and taxed according to the following table:

Income BracketT1 ≤ 2000020000 < T2 ≤ 4000040000 < T3 ≤ 6000060000 < T4
Tax Rate5%10%15%30%

For an income of 57,000, the tax calculation would be as follows:

  • T1: 20000 * 0.05 = 1000
  • T2: (40000 – 20000) * 0.10 = 2000
  • T3: (57000 – 40000) * 0.15 = 2550

Total Tax: 5550

Task5: (2points)

Using the same program structure as in the previous exercise, write a program named Conversion.java that takes a character c as a parameter and:

  • If the character is lowercase, convert it to uppercase and display the following message:
    "The uppercase equivalent of c is ..."
  • If the character is uppercase, convert it to lowercase and display the following message:
    "The lowercase equivalent of c is ..."
  • If the character is not a letter, display the following message:
    "c is not a letter."

Task6: (2points)

Using a similar structure as in the previous exercises, write a program named ArrayOperations.java that performs operations on an integer array. The program should:

  1. Take an array of integers as input.

  2. Provide the following options for operations:

    • Find the Largest Element: Identify and display the largest element in the array.
      Example Message:
      "The largest element in the array is ..."
    • Calculate the Sum of All Elements: Compute and display the sum of all integers in the array.
      Example Message:
      "The sum of all elements in the array is ..."
    • Sort the Array in Ascending Order: Sort the elements and display the sorted array.
      Example Message:
      "The sorted array is: [ ... ]"
  3. If the array is empty, display the following message:
    "The array is empty. No operations can be performed."

Contest Guidelines

Post can be written in any community or in your own blog.

Post must be #steemexclusive.

Use the following title: SLC S22 Week1 || Getting Started with Java and Eclipse

Participants must be verified and active users on the platform.

The images used must be the author's own or free of copyright. (Don't forget to include the source.)

Participants should not use any bot voting services, do not engage in vote buying.

The participation schedule is between Monday, December 16, 2024 at 00:00 UTC to Sunday, - December 22, 2024 at 23:59 UTC.

Community moderators would leave quality ratings of your articles and likely upvotes.

The publication can be in any language.

Plagiarism and use of AI is prohibited.

Use the tags #dynamicdevs-s22w1 , #country (example- #tunisia) #steemexclusive.

Use the #burnsteem25 tag only if you have set the 25% payee to @null.

Post the link to your entry in the comments section of this contest post. (very important).

Invite at least 3 friends to participate in this contest.

Strive to leave valuable feedback on other people's entries.

Share your post on Twitter and drop the link as a comment on your post.

Rewards

SC01/SC02 would be checking on the entire 15 participating Teaching Teams and Challengers and upvoting outstanding content. Upvote is not guaranteed for all articles. Kindly take note.

At the end of the week, we would nominate the top 4 users who had performed well in the contest and would be eligible for votes from SC01/SC02.

Important Notice: The selection of the four would be based solely on the quality of their post. Number of comments is no longer a factor to be considered.


Best Regards,
Dynamic Devs Team

Sort:  

I haven't learned Java in a long time, about 3 years ago once tried it. Perhaps with the material and homework given by prof @kouba01 it will be a repeat for me in learning java lessons.

It's great that you're revisiting Java! With the materials and homework I will provide, you'll have an excellent opportunity to refresh and deepen your knowledge. Good luck!

Greetings Sir,
Can we use other applications like VS Code for java programming by downloading it's extension there or we will just follow the software you recommend.
I don't have any knowledge about java but it looks similar to C++.

You should use Eclipse as your IDE because you will discover specific packages that make learning and developing in Java easier. While VS Code allows you to write Java code using extensions, Eclipse is more complete for Java development. It offers built-in tools for project management, advanced autocompletion, debugging, and standard Java packages.

Additionally, Eclipse is often recommended for Java beginners because it simplifies access to essential features such as creating classes, organizing packages, and integrating additional tools for advanced development. This will allow you to learn best practices from the beginning.

Even if you have some knowledge of C++, Java has important differences in syntax and concepts such as the Java Virtual Machine (JVM), memory management, and packages. Using Eclipse will give you a better environment to explore these aspects.

Coin Marketplace

STEEM 0.20
TRX 0.25
JST 0.038
BTC 97128.97
ETH 3358.96
USDT 1.00
SBD 3.18