import java.util.Scanner; public class Density { /** * Folgende Formel soll berechnet werden: Dichte = Masse /Volumen * Das Volumen darf nicht 0 sein, sonst kommt es zu einem Fehler * Fange in diesem Fall den Fehler ab und gib eine Rückmeldung * Konnte die Formel berechnet werden, soll das Ergebnis ausgegeben werden * Erweitere die Aufgabe um folgende Punkte: * Das Volumen soll nun nicht negativ und nicht über 1000 sein * hierfür soll ein Fehler ausgeworfen werden * Dieser soll wiederum abgefangen werden */ public static void main(String[] args) { try { Scanner scanner = new Scanner(System.in); System.out.println("input volume"); int volume = scanner.nextInt(); if (volume == 0) { throw new ArithmeticException("volume must not be 0"); } else if (volume < 0) { throw new ArithmeticException("volume must not be positive"); } else if (volume >= 1000) { throw new ArithmeticException("volume must not be more than 1000"); } System.out.println("input mass"); int mass = scanner.nextInt(); // does not throw an division by 0 exception for float datatypes, instead says that density is 'Infinity' float density = (float) mass / volume; System.out.printf("density: %f%n", density); } catch (ArithmeticException e) { System.out.println(e.getMessage()); } } }