Created
October 11, 2014 22:49
-
-
Save OlgaKulikova/3cad47b5f7d422ea240f to your computer and use it in GitHub Desktop.
Придумать исключение
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package MyExceptions; | |
// Придумать свое исключение и написать соответствующий класс и использующий его код. | |
import java.util.InputMismatchException; | |
import java.util.Scanner; | |
public class MainClass { | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
System.out.println("Введите количество пройденных шагов"); | |
try { | |
int steps = scan.nextInt(); | |
StepCounter st = new StepCounter(); | |
double m = st.count(steps); | |
System.out.println("Вы прошли " + m + " метров"); | |
} catch (StepException e) { | |
System.out.println(e.getMessage()); | |
} catch (InputMismatchException ex) { | |
System.out.println("Не верный формат ввода!"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package MyExceptions; | |
public class StepCounter { | |
private static final double LENGTH_OF_ONE_STEP = 0.6; | |
public double count(int steps) throws StepException { | |
if (steps < 0) { | |
throw new StepException("wrong steps"); | |
} | |
return steps * LENGTH_OF_ONE_STEP; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package MyExceptions; | |
public class StepException extends Exception { | |
public StepException(String message) { | |
super(message); | |
} | |
@Override | |
public String getMessage() { | |
return "StepException: " + super.getMessage(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment