100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Java POO Resumen $7.56   Add to cart

Class notes

Java POO Resumen

 3 views  0 purchase
  • Course
  • Institution

Explicacion y codigo respecto a POO en Java, Programacion Orientada a objetos

Preview 3 out of 18  pages

  • June 17, 2024
  • 18
  • 2023/2024
  • Class notes
  • Velasco
  • All classes
avatar-seller
SWITCH CASE:


POO:
Clase Automovil:
1 public class Automovil {
2
3 // Atributos
4 private String modelo;
5 private int añoFabricacion;
6 private boolean motorEncendido;
7
8 // Constructor
9 public Automovil(String modelo, int añoFabricacion) {
10 this.modelo = modelo;
11 this.añoFabricacion = añoFabricacion;
12 this.motorEncendido = false;
13 }
14
15 // Métodos
16 public void encenderMotor() {
17 if (!motorEncendido) {
18 System.out.println("Motor encendido.");
19 motorEncendido = true;
20 } else {
21 System.out.println("El motor ya está encendido.");
22 }
23 }
24
25 public void apagarMotor() {
26 if (motorEncendido) {
27 System.out.println("Motor apagado.");
28 motorEncendido = false;
29 } else {
30 System.out.println("El motor ya está apagado.");
31 }
32 }
33
34 // Método para obtener información del automóvil
35 public void obtenerInformacion() {
36 System.out.println("Modelo: " + modelo);
37 System.out.println("Año de fabricación: " + añoFabricacion);
38 System.out.println("Estado del motor: " + (motorEncendido ?
39 "Encendido" : "Apagado"));
40 }

, }

CLASE PRINCIPAL MAIN:
1 public class ProgramaAutomovil {
2
3 public static void main(String[] args) {
4
5 // Crear objetos de la clase Automovil
6 Automovil auto1 = new Automovil("Sedán", 2022);
7 Automovil auto2 = new Automovil("SUV", 2021);
8
9 // Usar métodos de la clase Automovil
10 auto1.encenderMotor();
11 auto1.obtenerInformacion();
12
13 auto2.encenderMotor();
14 auto2.obtenerInformacion();
15
16 auto1.apagarMotor();
17 auto1.obtenerInformacion();
18 }
19 }




CLASE:
1
public class Animal {
2
String nombre;
3
int edad;
4
5
void hacerSonido() {
6
System.out.println("Haciendo algún sonido");
7
}
8



1
2
3
4
Animal perro = new Animal();
perro.nombre = "Buddy";
perro.edad = 3;
perro.hacerSonido(

, Leer y Asignar:

1 public class Main {
2 // Ejemplo de encapsulamiento con getters y setters
3
4 public static void main(String[] args) {
5 // Crear una instancia de la clase Perro con raza y edad
6 Perro miPerro = new Perro("Labrador", 3);
7
8 // Llamar al getter para obtener la raza inicial del perro
9 System.out.println("Raza inicial: " + miPerro.getRaza()); //
10 Imprime "Labrador"
11
12 // Llamar al getter para obtener la edad inicial del perro
13 System.out.println("Edad inicial: " + miPerro.getEdad()); //
14 Imprime 3
15
16 // Llamar al setter para cambiar la raza y la edad del perro
17 miPerro.setRaza("Pastor Alemán");
18 miPerro.setEdad(4);
19
20 // Llamar al getter nuevamente para obtener la nueva raza y
21 edad del perro
22 System.out.println("Nueva raza: " + miPerro.getRaza()); //
23 Imprime "Pastor Alemán"
24 System.out.println("Nueva edad: " + miPerro.getEdad()); //
25 Imprime 4
26 }
27 }
28
29 public class Perro {
30 // Clase que representa a un perro
31
32 private String raza; // Atributo privado para la raza del perro
33 private int edad; // Atributo privado para la edad del perro
34
35 // Constructor que acepta la raza y la edad del perro como
36 parámetros
37 public Perro(String raza, int edad) {
38 this.raza = raza;
39 this.edad = edad;
40 }
41
42 // Getter para obtener la raza del perro
43 public String getRaza() {
44 return raza;
45 }
46
47 // Setter para establecer la raza del perro

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller johanossaserna. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $7.56. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

80435 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$7.56
  • (0)
  Add to cart