0% found this document useful (0 votes)
20 views9 pages

PSP - Tarea 01

The document discusses 5 programming tasks: 1) sorting numbers, 2) generating random numbers, 3) generating random text files, 4) running processes concurrently, 5) adding Maven plugins to build JAR files for the tasks. Code examples and instructions are provided for each task.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views9 pages

PSP - Tarea 01

The document discusses 5 programming tasks: 1) sorting numbers, 2) generating random numbers, 3) generating random text files, 4) running processes concurrently, 5) adding Maven plugins to build JAR files for the tasks. Code examples and instructions are provided for each task.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Tarea 01

1. Ordenar numbers

package com.mycompany.psptarea01_1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class PSPTarea01_1 {
public static void main(String[] args)throws IOException {
InputStreamReader instring = new InputStreamReader(System.in);
BufferedReader bf = new BufferedReader(instring);
String lineText = null;
while((lineText = bf.readLine())!=null){
String datos [] = lineText.split(" ");
int numbers [] = new int[datos.length];
for(int i=0; i<numbers.length;i++){
numbers[i]=Integer.parseInt(datos[i]);}
Arrays.sort(numbers);
for(int i=0; i<numbers.length;i++){
System.out.print(numbers[i]+" ");}
System.out.println("");
}}}

2. Aleatorios

package com.mycompany.psptarea01_2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class PSPTarea01_2 {
public static void main(String[] args)throws IOException {
int generateNumbers = 40;
for(int i=0; i<generateNumbers;i++){
System.out.print(generateNumbersRandom(0,100)+" ");}}
public static int generateNumbersRandom(int min, int max){
int number =(int)(Math.random()*(max-min+1)+(min));
return number;
}}
3.

Añadimos plugin para maven a proyecto:

Ordenar numeros

<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.mycompany.psptarea01_1.PSPTarea01_1</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Aleatorios

<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.mycompany.psptarea01_2.PSPTarea01_2</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Colocamos ficheros .jar en carpeta y cambiamos nombre(ordenarNumeros.jar y aleatorios.jar)


Ejecutamos comandos en terminal

3. Lenguajes

package com.mycompany.psptarea01_3;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PSPtarea01_3 {
public static void main(String[] args) {
String letras = "abcdefghijklmnopqrstuvwxyz";
String file;
FileLock block = null;
if (args.length == 2) {
try {int numLineas = Integer.parseInt(args[0]);
String osName = System.getProperty("os.name");
if (osName.toUpperCase().contains("WIN")) {
file = args[1].replace("\\", "\\\\");
} else {file = args[1];}
File archivo = new File(file);
if (!archivo.exists()) {
archivo.createNewFile()}
RandomAccessFile raf = new RandomAccessFile(archivo, "rwd");
block = raf.getChannel().lock();
raf.seek(archivo.length());
for (int i = 0; i < numLineas; i++) {
String linea = "";
int numCaracteres = generateNumbersRandom(1, 10);
for (int j = 0; j < numCaracteres; j++) {
linea += letras.charAt(generateNumbersRandom(0, letras.length() - 1)) }
raf.writeChars(linea + "\n")}
block.release();
block = null;
raf.close();
} catch (IOException ex) {
Logger.getLogger(PSPtarea01_3.class.getName()).log(Level.SEVERE, null, ex);}
} else {System.out.println("without parameters")}}
public static int generateNumbersRandom(int min, int max) {
int number = (int) (Math.random() * (max - min + 1) + (min));
return number;}}
4. Colaborar

package com.mycompany.psptarea01_4;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PSPTarea01_4 {
public static void main(String[] args) {
if (args.length == 1) {
try for (int i = 1; i <= 10; i++) {
System.out.println("Connect process: " + i);
String comando = "java -jar lenguaje.jar " + (i * 10) + " " + args[0];
System.out.println("Command connect " + comando);
Runtime.getRuntime().exec(comando);}
} catch(SecurityException ex){System.out.println("Problems: " + ex.getMessage());
} catch (IOException ex) {
Logger.getLogger(PSPTarea01_4.class.getName()).log(Level.SEVERE, null, ex);}
} else {System.out.println("only one parameter");
}}}
5. Añadimos plugin para maven a proyecto:

Lenguajes

<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.mycompany.psptarea01_3.PSPTarea01_3</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Colaborar

<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.mycompany.psptarea01_4.PSPTarea01_4</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Colocamos ficheros .jar en carpeta y cambiamos nombre(ordenarNumeros.jar y aleatorios.jar)


Ejecutamos en terminal
Tenemos un fichero miFichero.txt

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy