Solución Taller#

Primero importemos algunas librerias y definamos algunas variables de que serán usadas por las funciones de cada punto

import java.time.LocalDate;
import java.util.Scanner; 

String name = "Sebastian";
LocalDate date = java.time.LocalDate.now();

//Scanner myObj = new Scanner(System.in);  // Create a Scanner object
String number = "45"; 
System.out.println("Ingresa un número: ");
//String number = myObj.nextLine();      // Read user input
// System.out.printf("%nIngresaste el número %s.", number)
System.out.println("%nIngresaste el número %s.".formatted(number))
//int porcentaje_numero = Integer.parseInt(number);
Ingresa un número: 
Ingresaste el número 45.

Primer Punto#

Crea un Notebook en VSC con el kernel de java donde se muestren las operaciones entre caracteres, cuerdas (string), y números (int, float, double, etc). Además agrega el código de la clase «HelloWorld» que ya hemos creado pero personaliza el texto. Deben ser por lo menos 7 ejemplos.

System.out.println("  Olá, %S, como você está hoje (%s)? %n".formatted(name, date));
System.out.println("  La suma entre %d y %d es %d %n".formatted(13, 5, 13+5));
System.out.println("  El modulo de %f y %d es %d %n".formatted(13.22f, 2, 13%5));
  Olá, SEBASTIAN, como você está hoje (2024-12-04)? 
  La suma entre 13 y 5 es 18 
  El modulo de 13.220000 y 2 es 3 

Segundo Punto#

Utilizando el código del punto anterior, crea una clase (asígnale un nombre diferente) donde se imprima por lo menos 7 variables con los ejemplos del primer punto.

Tercer Punto#

Crea una clase llamada Porcentaje que dado un número entero imprima el número con signo de porcentaje seguido de la misma cantidad de símbolos #, es decir, dado el número 27 la clase debe imprimir:

27%: ###########################
77%: #############################################################################
class Porcentaje{
  public void Imprimir(String number){
    int porcentaje = Integer.parseInt(number);
    if (porcentaje>=0 && porcentaje<=100) {
      System.out.println("  %d %%: %s %n".formatted(porcentaje,   "#".repeat(porcentaje)));
      System.out.println("  %d %%: %s %n".formatted(porcentaje/2, "#".repeat(porcentaje/2)));
      System.out.println("  %d %%: %s %n".formatted(porcentaje/8, "#".repeat(porcentaje/8)));
    } 
    else{
      System.out.println("  ¡ERROR!: Intenta de nuevo, el número que ingresaste no esta en el rango de 0 a 100.");
    }    
  } 
}

Porcentaje porcentaje = new Porcentaje();
porcentaje.Imprimir(number);
  45 %: ############################################# 
  22 %: ###################### 
  5 %: ##### 

Creación del objeto y ejecución del método

Cuarto Punto#

Crea una clase llamada Impresiones donde se impriman varias variables en diferentes formatos, utilizando el método Format() de Java. Revisa las dos últimas referencias de guías y documentos. Escribe por lo menos 7 ejemplos.

String string1 = String.format("  Cinco decimales: %.5f", Math.PI);
System.out.println(string1);

// No decimales
String string2 = String.format("  No decimales: %.0f", Math.PI);
System.out.println(string2);

// Números hexadecimales
String string3 = String.format("  Hexadecimal de %d es: %x", 255, 255);
System.out.println(string3);

// Notación cientifica con 3 digitos
String string4 = String.format("  Notación cientifica con 3 digitos: %.2e", Math.E);
System.out.println(string4);

// Número con 7 espacios al inicio 
String string5 = String.format("  Padded number (agregando 0s a la izquierda): %05d", 420);
System.out.println(string5);

// Imprimiendo fechas
String string6 = String.format("  Date: %1$td-%1$tm-%1$tY", date);
System.out.println(string6);

// Imprimiendo bolean
String string7 = String.format("  ¿Hoy tendras un buen día?: %b", true);
System.out.println(string7); 
  Cinco decimales: 3.14159
  No decimales: 3
  Hexadecimal de 255 es: ff
  Notación cientifica con 3 digitos: 2.72e+00
  Padded number (agregando 0s a la izquierda): 00420
  Date: 04-12-2024
  ¿Hoy tendras un buen día?: true

Quinto Punto#

double temperatura = Double.parseDouble(number);

double celcius = (temperatura-32)*5/9;
System.out.println("  %.3f C° son %.3f °F %n".formatted(temperatura, celcius));

double fahrenheit = temperatura*9/5+32;
System.out.println("  %.3f F° son %.3f °C %n".formatted(temperatura, fahrenheit));
  45.000 C° son 7.222 °F 
  45.000 F° son 113.000 °C 

Todo en una Sola Clase#

Aca se colocaran todos los puntos en una misma clase, como ya esta definida la clase Porcentaje no es necesario volverla a escribir porque el notebook guarda esa información, sin embargo la agregaremos para que quede el archivo completo. Recuerden que en un archivo se pueden colocar varias clases, esta misma solución es guardada en el archivo Taller1.java, el cual pueden ejecutar desde cualquier IDE o desde la terminal utilizando:

javac Taller1.java   # compilar
java Taller1         # ejecutar

1. Creación de la clase:#

import java.util.Scanner; 
import java.time.LocalDate;

class Taller1{
  private String name = "Sebastian";
  private LocalDate date = java.time.LocalDate.now();
  
  public static void main(String[] args){

    Scanner myObj = new Scanner(System.in);  // Create a Scanner object
    System.out.print("Ingresa un número: ");
    String number = "25";//myObj.nextLine();        // Read user input
    
    System.out.println("1ER PUNTO:");
    new Taller1().PrimerPunto();

    System.out.println("2DO PUNTO:");

    System.out.println("3RO PUNTO:");
    new Taller1().TercerPunto(number);
    // new Taller1().TercerPunto(Integer.parseInt(args[0])); // using the args variable

    System.out.println("4TO PUNTO:");
    new Taller1().CuartoPunto();

    System.out.println("5TO PUNTO:");
    new Taller1().QuintoPunto(Double.parseDouble(number));
    //new Taller1().QuintoPunto(Double.parseDouble(args[0]));
  }

  public void PrimerPunto(){
    System.out.println("  Olá, %S, como você está hoje (%s)? %n".formatted(name, date));
    System.out.println("  La suma entre %d y %d es %d %n".formatted(13, 5, 13+5));
    System.out.println("  El modulo de %f y %d es %d %n".formatted(13.22f, 2, 13%5));
  }

  public void TercerPunto(String porcentaje){
    new Porcentaje().Imprimir(porcentaje);
  }

  public void CuartoPunto(){
    // Cinco decimales
    String string1 = String.format("  Cinco decimales: %.5f", Math.PI);
    System.out.println(string1);

    // No decimales
    String string2 = String.format("  No decimales: %.0f", Math.PI);
    System.out.println(string2);

    // Números hexadecimales
    String string3 = String.format("  Hexadecimal de %d es: %x", 255, 255);
    System.out.println(string3);

    // Notación cientifica con 3 digitos
    String string4 = String.format("  Notación cientifica con 3 digitos: %.2e", Math.E);
    System.out.println(string4);

    // Número con 7 espacios al inicio 
    String string5 = String.format("  Padded number (agregando 0s a la izquierda): %05d", 420);
    System.out.println(string5);

    // Imprimiendo fechas
    String string6 = String.format("  Date: %1$td-%1$tm-%1$tY", date);
    System.out.println(string6);

    // Imprimiendo bolean
    String string7 = String.format("  ¿Hoy tendras un buen día?: %b", true);
    System.out.println(string7); 
  }

  public void QuintoPunto(double temperatura){
    double celcius = (temperatura-32)*5/9;
    System.out.println("  %.3f C° son %.3f °F %n".formatted(temperatura, celcius));

    double fahrenheit = temperatura*9/5+32;
    System.out.println("  %.3f F° son %.3f °C %n".formatted(temperatura, fahrenheit));
  }
}

class Porcentaje{
  public void Imprimir(String number){
    int porcentaje = Integer.parseInt(number);
    if (porcentaje>=0 && porcentaje<=100) {
      System.out.println("  %d %%: %s %n".formatted(porcentaje, "#".repeat(porcentaje)));
      System.out.println("  %d %%: %s %n".formatted(porcentaje/2, "#".repeat(porcentaje/2)));
      System.out.println("  %d %%: %s %n".formatted(porcentaje/8, "#".repeat(porcentaje/8)));
    } 
    else{
      System.out.println("  ¡ERROR!: Intenta de nuevo, el número que ingresaste no esta en el rango de 0 a 100.");
    }
    
  } 
}

2. Creación del objeto y ejecución del método#

Taller1 taller1 = new Taller1();
taller1.main(new String[] {});
Ingresa un número: 
1ER PUNTO:
  Olá, SEBASTIAN, como você está hoje (2024-12-04)? 
  La suma entre 13 y 5 es 18 
  El modulo de 13.220000 y 2 es 3 
2DO PUNTO:
3RO PUNTO:
  25 %: ######################### 
  12 %: ############ 
  3 %: ### 
4TO PUNTO:
  Cinco decimales: 3.14159
  No decimales: 3
  Hexadecimal de 255 es: ff
  Notación cientifica con 3 digitos: 2.72e+00
  Padded number (agregando 0s a la izquierda): 00420
  Date: 04-12-2024
  ¿Hoy tendras un buen día?: true
5TO PUNTO:
  25.000 C° son -3.889 °F 
  25.000 F° son 77.000 °C