/**
 * Klasse Motor.
 * 
 * @author Luxson Kanagarajah 
 * @version 2022.09.22
 */
public class Motor
{
    // A1. Hubraum 
    private int hubraum;
    // A4 Zündkerze
    private boolean zuendkerze;

    /**
     * Konstruktor mit Hubraum
     */
    public Motor(int hubraum)
    {
        // Instanzvariable initialisieren
       this.hubraum = hubraum;
    }
    
    /**
     * Konstruktor mit Hubraum und Zündkerze
     */
    public Motor (int hubraum, boolean zuendkerze)
    {
        this.hubraum = hubraum;
        this.zuendkerze = zuendkerze;
    }

    /**
     * A3 Die Grösse des Hubraums abfragen
     * 
     * @return int hubraum
     */
    public int getHubraum()
    {
        return hubraum;
    }
    
    /**
     * A5 Methode um die Zündkerze zu setzen oder zu entfernen
     * 
     * @param einsetzen true/false
     */
    public void setZuendkerze(boolean einsetzen)
    {
        zuendkerze = einsetzen;
    }
    
    /**
     * A6 Abfrage um zu erfahren ob die Züendkerze eingebaut ist oder nicht
     * 
     * @return boolean zuendkerze
     */
    public boolean getZuendkerze()
    {
        return zuendkerze;
    }
}

/**
 * Klasse Auto.
 * 
 * @author Luxson Kanagarajah 
 * @version 2022.09.22
 */
public class Auto
{
    // B1 Farbe
    private String farbe;
    // B4 Motor
    private Motor motor;
    // B6 Autonummer
    private int autonummer;

    /**
     * Konstruktor für Objekte der Klasse Auto
     */
    public Auto(String farbe)
    {
        // B2
        this.farbe = farbe;
    }

    /**
     * B3 Methode um die Farbe des Autos zu verändern
     * 
     * @param string farbe
     */
    public void setFarbe (String farbe)
    {
        this.farbe = farbe;
    }
    
    /**
     * B3 Abfrage um zu erfahren welche Farbe ein Auto hat
     * 
     * @return string farbe
     */
    public String getFarbe()
    {
        return farbe;
    }
    
    /**
     * B5 Methode um den Motor im Auto einzubauen oder zu ersetzen
     * 
     * @param Motor motor
     */
    public void setMotor (Motor motor)
    {
        this.motor = motor;
    }
    
    
    /**
     * B7 Methode um die Autonummer des Autos zu verändern
     * 
     * @param int autonummer
     */
    public void setAutonummer (int autonummer)
    {
        this.autonummer = autonummer;
    }
    
    /**
     * B7 Abfrage um zu erfahren welche Autonummer ein Auto hat
     * 
     * @return int autonummer
     */
    public int getAutonummer()
    {
        return autonummer;
    }
    
    /**
     * B8 Abfrage ob das Auto fahren kann
     * 
     * @return boolean true/false
     */
    public boolean kannFahren()
    {
        if(motor != null && motor.getZuendkerze() && autonummer > 0)
        {
            System.out.println("Das Auto mit der Farbe '"+farbe+"' und der Autonummer '"
            +autonummer+"' und Hubraum '"+motor.getHubraum()+"' darf fahren!");
            return true;
        }
        else
        {
            // Motor nicht initialisiert
            if(motor == null) 
            { 
                System.out.println("Motor ist nicht initialisiert.");
            }
            else
            {
                // Wenn die Zündkerze nicht eingesetzt ist
                if(!motor.getZuendkerze())
                {
                    System.out.println("Die Zündkerze ist nicht eingesetzt.");
                }
            }
            
            // Wenn Autonummer <= 0 ist
            if( autonummer <= 0)
            {
                System.out.println("Die Autonummer ist <= 0");
            }
            return false;
        }
    }
}
Auto auto1 = new Auto("rot");
auto1.kannFahren()
# Motor ist nicht initialisiert.
Die Autonummer ist <= 0
    # returned boolean false
Motor motor1 = new Motor(3000);
auto1.setMotor(motor1);
auto1.kannFahren()
# Die Zündkerze ist nicht eingesetzt.
# Die Autonummer ist <= 0
   # returned boolean false
auto1.setAutonummer(7);
motor1.setZuendkerze(true);
auto1.kannFahren()
# Das Auto mit der Farbe 'rot' und der Autonummer '' und Hubraum '3000' darf fahren!
   # returned boolean true
Source Code im Github
Zuletzt bearbeitet: 23. September 2022

Autor

Kommentare

Schreibe eine Antwort oder einen Kommentar

Deine EMail-Adresse wird nicht veröffentlicht.