Classe Astre en Java: Guia de Programació Orientada a Objectes

Geología4,02 KB

Classe Astre en Java

A continuació es presenta la implementació de la classe Astre i una classe de prova TestAstre que permet crear i comparar astres.

Classe Astre

public class Astre {
    /** Atributs */
    public static final int ESTEL = 0;
    public static final int NEBULOSA = 1;
    public static final int GALAXIA = 2;
    private String nom;
    private int tipus;
    private double brillantor;
    private double distancia;

    /** Constructors */
    public Astre(String nom, int tipus, double brillantor, double distancia) {
        this.nom = nom;
        this.tipus = tipus;
        this.brillantor = brillantor;
        this.distancia = distancia;
    }

    public Astre() {
        this("Sirius", 0, -1.42, 8.7);
    }

    /** Consultor i modificador de brillantor */
    public double getBrillantor() {
        return brillantor;
    }

    public void setBrillantor(double novaBrillantor) {
        brillantor = novaBrillantor;
    }

    /** Mètode equals */
    public boolean equals(Object o) {
        return o instanceof Astre &&
               nom.equals(((Astre) o).nom) &&
               tipus == ((Astre) o).tipus &&
               brillantor == ((Astre) o).brillantor &&
               distancia == ((Astre) o).distancia;
    }

    /** Mètode toString */
    public String toString() {
        String str = nom + ": ";
        switch (tipus) {
            case 0: str += "Estel "; break;
            case 1: str += "Nebulosa "; break;
            case 2: str += "Galàxia "; break;
        }
        str += "(" + (Math.round(brillantor * 100) / 100.0) + ", " + (Math.round(distancia * 100) / 100.0) + ")";
        return str;
    }

    /** Mètode de càlcul de la magnitud absoluta */
    public double magnitudAbsoluta() {
        return brillantor + 5 * Math.log10(distancia);
    }

    /** Compara la magnitud absoluta amb la d'un altre astre (p. ex., la Lluna) */
    public int mesBrillant(Astre Lluna) {
        double magAbAstre = this.magnitudAbsoluta();
        double magAbLluna = Lluna.magnitudAbsoluta();
        int comparacio = 0;
        if (magAbAstre > magAbLluna) {
            comparacio = 1;
        } else if (magAbAstre < magAbLluna) {
            comparacio = -1;
        }
        return comparacio;
    }

    /** Mètode visibleAmb */
    public String visibleAmb() {
        if (brillantor < 5) {
            return "a simple vista";
        } else if (brillantor >= 5 && brillantor < 7) {
            return "amb prismàtics";
        } else if (brillantor >= 7 && brillantor <= 25) {
            return "amb telescopi";
        } else {
            return "amb grans telescopis";
        }
    }
}

Classe de prova TestAstre

import java.util.*;

public class TestAstre {
    /** Crear un nou astre */
    public static void main(String[] args) {
        Astre Alfa = new Astre("Alfa Centauri", Astre.ESTEL, 4.6, 4.3);
        System.out.println(Alfa);

        Scanner teclat = new Scanner(System.in);
        System.out.println("\nIntrodueix les dades d'un astre");
        System.out.print("Nom: ");
        String n = teclat.nextLine();
        System.out.print("Tipus (0: estel, 1: nebulosa, 2: galàxia): ");
        int t = teclat.nextInt();
        System.out.print("Brillantor: ");
        double b = teclat.nextDouble();
        System.out.print("Distància: ");
        double d = teclat.nextDouble();

        Astre nou = new Astre(n, t, b, d);
        System.out.println(nou + " és observable " + nou.visibleAmb() + "\n");

        int brillaMes = Alfa.mesBrillant(nou);
        switch (brillaMes) {
            case 0:
                System.out.println(Alfa + " i " + nou + " brillen igual.");
                break;
            case 1:
                System.out.println(Alfa + " brilla més que " + nou);
                break;
            case -1:
                System.out.println(Alfa + " brilla menys que " + nou);
                break;
        }
    }
}

Descargar

PDF Word HTML