> For the complete documentation index, see [llms.txt](https://educacion.gitbook.io/programacion/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://educacion.gitbook.io/programacion/exercicios-java/ud3-solucions/constructores-e-visibilidade.md).

# Constructores e visibilidade

```
// Some code
```

1. Dirección

```java
public class Direccion {
    private String rua;
    private String cidade;
    
 
    public Direccion(String rua, String cidade) {
        this.rua = rua;
        this.cidade = cidade;
    }
    
 
    public Direccion(String rua) {
        this(rua, "A Coruña"); 
    }
    

    public void mostrarDireccion() {
        System.out.println("Rúa: " + this.rua);
        System.out.println("Cidade: " + this.cidade);
    }
    

    public void cambiarRua(String novaRua) {
        this.rua = novaRua;
    }
    
 
    public void cambiarCidade(String novaCidade) {
        this.cidade = novaCidade;
    }
}
```

2. Persona

```java
public class Persoa {
    private String nome;
    private int idade;
    private Direccion direccion;
    
    public Persoa(String nome, int idade, Direccion direccion) {
        this.nome = nome;
        this.idade = idade;
        this.direccion = direccion;
    }
    
    public Persoa(Direccion direccion) {
        this("Xoan", 23, direccion); // Chama ao primeiro constructor
    }
    
    public void mostrarInfo() {
        System.out.println("Nome: " + this.nome);
        System.out.println("Idade: " + this.idade);
        System.out.println("Dirección:");
        this.direccion.mostrarDireccion();
    }
    
  
    public void cambiarNome(String novoNome) {
        this.nome = novoNome;
    }
    
    public void cambiarIdade(int novaIdade) {
        this.idade = novaIdade;
    }
    
    public void cambiarDireccion(Direccion novaDireccion) {
        this.direccion = novaDireccion;
    }
}
```

```java
public class AppPersoa {
    public static void main(String[] args) {
        
        Direccion dir1 = new Direccion("Rúa Nova", "Vigo");
        Direccion dir2 = new Direccion("Praza Maior");
        
       
        Persoa persoa1 = new Persoa("María", 30, dir1);
        Persoa persoa2 = new Persoa(dir2);
        
        System.out.println("=== Información orixinal ===");
        System.out.println("Persoa 1:");
        persoa1.mostrarInfo();
        System.out.println("\nPersoa 2:");
        persoa2.mostrarInfo();
        
        
        persoa1.cambiarNome("María López");
        persoa1.cambiarIdade(31);
        Direccion novaDir1 = new Direccion("Rúa Urzaiz", "Vigo");
        persoa1.cambiarDireccion(novaDir1);
        
        persoa2.cambiarNome("Xoán Pérez");
        persoa2.cambiarIdade(24);
        Direccion novaDir2 = new Direccion("Rúa da Franxa", "A Coruña");
        persoa2.cambiarDireccion(novaDir2);
        
        System.out.println("\n=== Información modificada ===");
        System.out.println("Persoa 1:");
        persoa1.mostrarInfo();
        System.out.println("\nPersoa 2:");
        persoa2.mostrarInfo();
    }
}
```

3. Telefono móvil

```java
public class TelefonoMovil {

        private String numeroMovil;
        private double costeChamada; // coste por segundo
        private double costeConsumoMB; // coste por MB
        private double saldo;

    public TelefonoMovil(String numeroMovil, double costeChamada, double costeConsumoMB, double saldo) {
        this.numeroMovil = numeroMovil;
        this.costeChamada = costeChamada;
        this.costeConsumoMB = costeConsumoMB;
        this.saldo = saldo;
    }

        // Constructor vacío
        public TelefonoMovil() {
            this("",0.0,0.0,0.0);
        }


        public TelefonoMovil(String numeroMovil, double saldo) {
            this(numeroMovil, 0.05, 0.10, saldo);
        }


 
        public void chamar(int segundos) {
         
            double costeTotal = segundos * costeChamada;

            if (costeTotal > saldo) {
                // Calcular cuántos segundos se pueden hablar con el saldo disponible
                int segundosPosibles = (int)(saldo / costeChamada);
                double costeReal = segundosPosibles * costeChamada;
                saldo -= costeReal;
                System.out.println("Llamada finalizada. Solo se pudieron hablar " + segundosPosibles +
                        " segundos. Coste: " + costeReal + "€");
            } else {
                saldo -= costeTotal;
                System.out.println("Llamada realizada: " + segundos + " segundos. Coste: " + costeTotal + "€");
            }
        }

        // Método para navegar por internet
        public void navegar(int mb) {
            double costeTotal = mb * costeConsumoMB;

            if (costeTotal > saldo) {
                // Calcular cuántos MB se pueden consumir con el saldo disponible
                int mbPosibles = (int)(saldo / costeConsumoMB);
                double costeReal = mbPosibles * costeConsumoMB;
                saldo -= costeReal;
                System.out.println("Navegación finalizada. Solo se pudieron consumir " + mbPosibles +
                        " MB. Coste: " + costeReal + "€");
            } else {
                saldo -= costeTotal;
                System.out.println("Navegación realizada: " + mb + " MB. Coste: " + costeTotal + "€");
            }
        }

        public void recargar(double importe) {
            if (importe > 0) {
                saldo += importe;
                System.out.println("Recarga realizada: " + importe + "€. Saldo actual: " + saldo + "€");
            } else {
                System.out.println("El importe de recarga debe ser positivo.");
            }
        }

        public void consultarInfo() {
            System.out.println("=== INFORMACIÓN DEL TELÉFONO ===");
            System.out.println("Número: " + numeroMovil);
            System.out.println("Coste llamada por segundo: " + costeChamada + "€");
            System.out.println("Coste consumo por MB: " + costeConsumoMB + "€");
            System.out.println("Saldo disponible: " + saldo + "€");
            System.out.println("================================");
        }
}

```

```java
package TelefonoMovil;

public class App {
    public static void main(String[] args) {
        // Crear tres objetos TelefonoMovil usando diferentes constructores
        TelefonoMovil telefono1 = new TelefonoMovil();
        TelefonoMovil telefono2 = new TelefonoMovil("+34 666111222", 10.0);
        TelefonoMovil telefono3 = new TelefonoMovil("+34 666333444", 0.03, 0.08, 15.0);

        System.out.println("=== ESTADO INICIAL DE LOS TELÉFONOS ===");
        telefono1.consultarInfo();
        telefono2.consultarInfo();
        telefono3.consultarInfo();

        System.out.println("\n=== REALIZANDO OPERACIONES ===");

        // Operaciones con teléfono1
        System.out.println("\n--- Teléfono 1 ---");
        telefono1.chamar(120); // Llamada de 2 minutos
        telefono1.navegar(50); // Navegación de 50 MB
        telefono1.recargar(10.0); // Recarga de 10€

        // Operaciones con teléfono2
        System.out.println("\n--- Teléfono 2 ---");
        telefono2.chamar(300); // Llamada de 5 minutos
        telefono2.navegar(100); // Navegación de 100 MB
        telefono2.chamar(600); // Intentar llamada larga

        // Operaciones con teléfono3
        System.out.println("\n--- Teléfono 3 ---");
        telefono3.navegar(200); // Navegación de 200 MB
        telefono3.chamar(180); // Llamada de 3 minutos
        telefono3.recargar(5.0); // Recarga de 5€

        System.out.println("\n=== ESTADO FINAL DE LOS TELÉFONOS ===");
        telefono1.consultarInfo();
        telefono2.consultarInfo();
        telefono3.consultarInfo();
    }
}

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://educacion.gitbook.io/programacion/exercicios-java/ud3-solucions/constructores-e-visibilidade.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
