forked from mouredev/hello-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassesExercises.java
More file actions
66 lines (52 loc) · 2.54 KB
/
Copy pathClassesExercises.java
File metadata and controls
66 lines (52 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package basic.c08_oop;
/*
Clase 58 - Ejercicios: Clases y objetos
Vídeo: https://youtu.be/JOAqpdM36wI?t=21434
*/
import java.util.ArrayList;
public class ClassesExercises {
public static void main(String[] args) {
// 1. Crea una clase Book con atributos title y author. Crea un objeto y muestra sus datos.
Book myBook = new Book("Cien años de soledad", "Gabriel García Márquez");
myBook.showInfo();
// 2. Crea una clase Dog con un método bark() que imprima su sonido.
Dog myDog = new Dog();
myDog.bark();
// 3. Añade un constructor a la clase Book que reciba title y author.
// 4. Crea una clase Car con atributos brand y model y un método showData().
Car myCar = new Car("Kia","Cerato");
myCar.showData();
// 5. Crea una clase Student con atributo score y un método que diga si aprobó (mayor o igual a 60).
Student student1 = new Student("Ana", 20, "S001", 75);
student1.result();
// 6. Crea una clase BankAccount con atributo balance y un método deposit() que sume el saldo.
BankAccount myBankAccount = new BankAccount(100);
myBankAccount.deposit(50);
// 7. Crea una clase Rectangle con métodos para calcular el área y el perímetro.
Rectangle myRectangle = new Rectangle(5, 3);
System.out.println("Área: " + myRectangle.calculateArea());
System.out.println("Perímetro: " + myRectangle.calculatePerimeter());
// 8. Crea una clase Worker que reciba nombre y salario, y un método para mostrar su salario.
Worker myWorker = new Worker("Sergio", 1500.50);
myWorker.showSalary();
// 9. Crea varios objetos Person y guárdalos en un ArrayList.
ArrayList<Person> people = new ArrayList<>();
// Crear objetos Person
Person p1 = new Person("Ana", 25, "ID001");
Person p2 = new Person("Luis", 30, "ID002");
Person p3 = new Person("María", 22, "ID003");
// Agregarlos al ArrayList
people.add(p1);
people.add(p2);
people.add(p3);
// Recorrer la lista y usar sayHello()
for (Person person : people) {
person.sayHello();
}
// 10. Crea una clase Product y un método que aplique un descuento sobre su precio.
Product product1 = new Product("Laptop", 1200);
product1.showInfo(); // Precio inicial
product1.applyDiscount(15); // Aplica 15% de descuento
product1.showInfo(); // Precio después del descuento
}
}