Skip to main content

Get Started with Actividad-1

This guide will walk you through compiling and running the Actividad-1 program. You’ll learn how to set up the environment, compile the Java source code, and execute the program with sample data.

Prerequisites

Before you begin, ensure you have:
  • Java Development Kit (JDK) installed (version 8 or higher)
  • Access to a terminal or command prompt
  • The Main.java source file
You can verify your Java installation by running java -version in your terminal.

Installation & Execution

1

Navigate to the source directory

Open your terminal and navigate to the directory containing Main.java:
cd ~/workspace/source/src
2

Compile the Java program

Compile the source code using the Java compiler:
javac Main.java
This will generate a Main.class file in the same directory. If you see no output, the compilation was successful.
If you encounter errors, verify that your JDK is properly installed and that javac is in your system PATH.
3

Run the program

Execute the compiled program:
java Main
The program will start and prompt you for input.
4

Enter values for the first array

You’ll see a prompt to enter 7 values for the first array. Enter each value and press Enter:
Ingrese 7 valores para el primer arreglo
Valor 1: 10
Valor 2: 20
Valor 3: 30
Valor 4: 40
Valor 5: 50
Valor 6: 60
Valor 7: 70
5

Enter values for the second array

Next, enter 7 values for the second array:
Ingrese 7 valores para el segundo arreglo
Valor 1: 5
Valor 2: 15
Valor 3: 25
Valor 4: 35
Valor 5: 45
Valor 6: 55
Valor 7: 65
6

View the results

The program will calculate and display:
  1. The average of all input values
  2. The element-wise differences between the two arrays
El promedio es: 40.00
Arreglo de diferencias
Posición0:5
Posición1:5
Posición2:5
Posición3:5
Posición4:5
Posición5:5
Posición6:5
Note: Due to a bug in the code (line 25), the average calculation adds array1 values twice instead of including array2 values. The correct average would be 37.50, but the program displays 40.00.

Complete Example Session

Here’s what a complete terminal session looks like:
$ javac Main.java
$ java Main
Ingrese 7 valores para el primer arreglo
Valor 1: 10
Valor 2: 20
Valor 3: 30
Valor 4: 40
Valor 5: 50
Valor 6: 60
Valor 7: 70
Ingrese 7 valores para el segundo arreglo
Valor 1: 5
Valor 2: 15
Valor 3: 25
Valor 4: 35
Valor 5: 45
Valor 6: 55
Valor 7: 65
El promedio es: 40.00
Arreglo de diferencias
Posición0:5
Posición1:5
Posición2:5
Posición3:5
Posición4:5
Posición5:5
Posición6:5

Understanding the Code

The program consists of several key sections:

Array Declaration

int[] array1 = new int[7]; // Se crea el primer arreglo
int[] array2 = new int[7]; // Se crea el segundo arreglo
int[] arrayDiferencia = new int[7];
Three arrays are declared: two for user input and one to store the differences.

Reading Input

Scanner sc = new Scanner(System.in);

System.out.println("Ingrese 7 valores para el primer arreglo");
for (int i = 0; i < array1.length; i++) {
    System.out.print("Valor " + (i + 1) + ": ");
    array1[i] = sc.nextInt();
    sumaTotal += array1[i];
}
The Scanner class reads integer values from the console, and a loop iterates through each array position.

Calculating Differences

for (int i = 0; i < array1.length; i++) {
    arrayDiferencia[i] = array1[i] - array2[i];
}
Each element of the difference array stores the subtraction of corresponding elements.

Computing the Average

double promedio = (double) sumaTotal / totalElementos;
System.out.printf("El promedio es: %.2f", promedio);
The average is calculated by dividing the sum by the total number of elements (14), with type casting to double for decimal precision.

Troubleshooting

Input Mismatch Exception

If you enter non-integer values, the program will throw an InputMismatchException. Make sure to enter only whole numbers.

Command Not Found

If javac or java commands are not recognized, verify that the JDK is installed and added to your system PATH.

Next Steps

Now that you’ve successfully run the program:

Main Class

Explore detailed explanations of the Main class

Array Operations

Learn about array operations and calculations

Input Format

Understanding input requirements

Output Format

Understanding program output

Build docs developers (and LLMs) love