Skip to content
Open

Uf2 #15

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions exercicis_extres/exercici1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

class cotxe {

public string $marca;
public string $model;

public function __construct(string $marca, string $model) {
$this->marca = $marca;
$this->model = $model;
}

public function descripcio(): string {
return "La marca es " .$this->marca. " i el model es " .$this->model. ".";
}

}

$cotxe = new cotxe("Seat", "Ibiza FR");
echo $cotxe->descripcio();
echo '<br>';

?>
62 changes: 62 additions & 0 deletions exercicis_extres/exercici10.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

class producte {

public string $nom;
public string $preu;

public function __construct(string $nom, string $preu) {
$this->nom = $nom;
$this->preu = $preu;
}

public function descripcio(): string {
return "El producte es " .$this->nom. " i el preu es " .$this->preu. ".<br>";
}

public function getProducte(): string {
return $this->preu;
}
}

$productes = [

new Producte("Cocacola", "3€"),
new Producte("Fanta", "3€"),
new Producte("Pipas", "1€"),
new Producte("Pizza", "3€"),
new Producte("Kinder bueno", "2€")

];





?>

<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

<h2>Llista de Productes</h2>
<table>
<tr>
<th>Nom</th>
<th>Preu</th>
</tr>
<?php foreach ($productes as $producte): ?>
<tr>
<td><?= htmlspecialchars($producte->nom) ?></td>
<td><?= number_format($producte->preu, 2) ?>€</td>
</tr>
<?php endforeach; ?>

</table>

</body>
</html>
21 changes: 21 additions & 0 deletions exercicis_extres/exercici2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

class cotxe {

public string $marca;
public string $model;

public function __construct(string $marca = "Seat", string $model = "Ibiza FR") {
$this->marca = $marca;
$this->model = $model;
}

public function descripcio(): string {
return "La marca es " .$this->marca. " i el model es " .$this->model. ".";
}

}

$cotxe = new cotxe();
echo $cotxe->descripcio();
echo '<br>';
21 changes: 21 additions & 0 deletions exercicis_extres/exercici3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

class persona {

public string $nom;
public int $edat;

public function __construct(string $nom, int $edat) {
$this->nom = $nom;
$this->edat = $edat;
}

public function descripcio(): string {
return "Hola, sóc " .$this->nom. " i tinc " .$this->edat. " anys.";
}

}

$persona = new persona("Alejandro", 27);
echo $persona->descripcio();
echo '<br>';
27 changes: 27 additions & 0 deletions exercicis_extres/exercici4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

class persona {

public string $nom;
public int $edat;

public function __construct(string $nom, int $edat) {
$this->nom = $nom;
$this->edat = $edat;
}

public function descripcio(): string {
return "Hola, sóc " .$this->nom. " i tinc " .$this->edat. " anys.<br>";
}

}

$persona = new persona("Alejandro", 27);
$persona2 = new persona("Joan", 28);
$persona3 = new persona("Gerard", 28);

echo $persona->descripcio();
echo $persona2->descripcio();
echo $persona3->descripcio();

echo '<br>';
15 changes: 15 additions & 0 deletions exercicis_extres/exercici6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

class Calculadora {

public function sumar(float $a, float $b): float {
return $a + $b;
}

}

$calculadora = new Calculadora();

echo "El resultado de la suma es: " . $calculadora->sumar(2, 2) . "<br>";

?>
65 changes: 65 additions & 0 deletions exercicis_extres/exercici7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

class persona {

public string $nom;
public int $edat;

public function __construct(string $nom, string $edat) {
$this->nom = $nom;
$this->edat = $edat;
}

public function saludar(): string {
return "Hola, sóc " .$this->nom. " i tinc " .$this->edat. " anys.";
}

public function descripcion(): string {
return "Mi nombre es " .$this->nom. " y tengo " .$this->edat. " años.";
}

}

//inicializamos con la variable persona con null. realizamos una comprobación de la solicitud con el post y guardamos los datos enviados en $nom y $edat del formulario
$persona = null;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$nom = $_POST["nom"] ?? "";
$edat = $_POST["edat"] ?? 0;

//se verifica que el dato que pongamos en $nom no esté vacío y que la edad sea un numero mayor a 0. si los datos son correctos se crea una instancia de la clase persona con los valores que hemos rellenado en el formulario
if (!empty($nom) && is_numeric($edat) && $edat > 0) {
$persona = new persona($nom, (int)$edat);
} else {
echo "<p style='color: red;'>Si us plau, introdueix dades vàlides.</p>";
}
}
echo '<br>';
echo '<br>';
?>

<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post">
<label for="nom">Nom:</label>
<input type="text" id="nom" name="nom" required>
<br><br>
<label for="edat">Edat:</label>
<input type="number" id="edat" name="edat" required>
<br><br>
<button type="submit">Enviar</button>
</form>

<?php

//si pasamos los valores de nom y edat a $persona donde anteriormente se inicializa con un null, pintará el nombre y la edad de la persona
if ($persona) {
echo "<p>" . $persona->descripcion() . "</p>";
}
?>
</body>
</html>
22 changes: 22 additions & 0 deletions exercicis_extres/exercici8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

class animal {

public string $nom;
public string $tipus;

public function __construct(string $nom = "Yuna", string $tipus = "gos") {
$this->nom = $nom;
$this->tipus = $tipus;
}

public function descriure(): string {
return "El animal es " .$this->nom. " y es un " .$this->tipus. " .";
}

}

$animal = new animal();
echo $animal->descriure();

?>
22 changes: 22 additions & 0 deletions exercicis_extres/exercici9.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

class animal {

public string $nom;
public string $tipus;

public function __construct(string $nom = "Yuna", string $tipus = "gos") {
$this->nom = $nom;
$this->tipus = $tipus;
}

public function saludar(): string {
return "Hola, sóc un " .$this->tipus. " i em dic " .$this->nom. " .";
}

}

$animal = new animal();
echo $animal->saludar();

?>
61 changes: 61 additions & 0 deletions exercicis_practics/exercicianimal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

class animal {

public string $nom;
public string $tipus;

public function __construct(string $nom, string $tipus) {
$this->nom = $nom;
$this->tipus = $tipus;
}

public function descriure(): string {
return "El animal es " .$this->nom. " y es un " .$this->tipus. " .";
}

}

//inicializamos con la variable persona con null. realizamos una comprobación de la solicitud con el post y guardamos los datos enviados en $nom y $tipus del formulario
$animal = null;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$nom = $_POST["nom"] ?? "";
$tipus = $_POST["tipus"] ?? "";

//se verifica que el dato que pongamos en $nom no esté vacío y que el tipus igual. si los datos son correctos se crea una instancia de la clase animal con los valores que hemos rellenado en el formulario
if (!empty($nom) && !empty($tipus)) {
$animal = new animal($nom, $tipus);
} else {
echo "<p style='color: red;'>Si us plau, introdueix dades vàlides.</p>";
}
}
echo '<br>';
echo '<br>';
?>

<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post">
<label for="nom">Nom:</label>
<input type="text" id="nom" name="nom" required>
<br><br>
<label for="edat">Tipus:</label>
<input type="text" id="tipus" name="tipus" required>
<br><br>
<button type="submit">Enviar</button>
</form>

<?php

//si pasamos los valores de nom y edat a $persona donde anteriormente se inicializa con un null, pintará el nombre y la edad de la persona
if ($animal) {
echo "<p>" . $animal->descriure() . "</p>";
}
?>
</body>
</html>
34 changes: 34 additions & 0 deletions exercicis_practics/exercicicalculadora.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

class Calculadora {

public function sumar(float $a, float $b): float {
return $a + $b;
}

public function restar(float $a, float $b): float {
return $a - $b;
}

public function multiplicar(float $a, float $b): float {
return $a * $b;
}

// Método para dividir (con validación para evitar división por cero)
public function dividir(float $a, float $b): float {
if ($b == 0) {
echo "<p> Error: No es pot dividir entre zero </p>";
}
return $a / $b;
}
}

$calculadora = new Calculadora();

echo "El resultado de la suma es: " . $calculadora->sumar(10, 5) . "<br>";
echo "El resultado de la resta es: " . $calculadora->restar(10, 5) . "<br>";
echo "El resultado de la multiplicacion es: " . $calculadora->multiplicar(10, 5) . "<br>";
echo "El resultado de la division es: " . $calculadora->dividir(10, 5) . "<br>";


?>
Loading