Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileNotFoundException;
- public class Main {
- public static void podniesArrayIndexOutOfBoundException() throws ArrayIndexOutOfBoundsException {
- throw new ArrayIndexOutOfBoundsException();
- }
- public static void podniesFileNotFoundException() throws FileNotFoundException {
- throw new FileNotFoundException();
- }
- public static void podniesException() throws Exception {
- throw new Exception("Zła Jednostka");
- }
- public static int podajLiczbe(String liczba) throws NumberFormatException {
- int pomocnicza;
- if (liczba.startsWith("0b"))
- pomocnicza = 2;
- else if (liczba.startsWith("0x"))
- pomocnicza = 16;
- else if (liczba.startsWith("0"))
- pomocnicza = 8;
- else
- throw new NumberFormatException();
- if (pomocnicza == 2 || pomocnicza == 16)
- return Integer.parseInt(liczba.substring(2), pomocnicza);
- else
- return Integer.parseInt(liczba.substring(1), pomocnicza);
- }
- public static void main(String[] args) {
- //ZADANIE1
- // MOZNA ZAMKNAC W JEDNYM TRY CATCH, ALE WTEDY WYRZUCI TYLKO JEDEN BLAD!!
- /*try {
- podniesArrayIndexOutOfBoundException();
- podniesFileNotFoundException();
- podniesException();
- } catch (Exception e) {
- e.printStackTrace();
- }*/
- //ZADANIE2
- int a = podajLiczbe("0111");
- System.out.println(a);
- //ZADANIE3
- /* Rakieta rakieta = new Rakieta();
- rakieta.zatankuj();
- try {
- rakieta.start();
- } catch (Exception e) {
- e.printStackTrace();
- }*/
- }
- }
- //////////////////RAKIETA
- public class Rakieta {
- private String nazwa;
- private int wagaPaliwa;
- Rakieta() {
- this.nazwa = "Bravo";
- this.wagaPaliwa = 0;
- }
- Rakieta(String nazwa, int wagaPaliwa) {
- this.nazwa = nazwa;
- this.wagaPaliwa = wagaPaliwa;
- }
- public void zatankuj() {
- if (this.wagaPaliwa < 1000)
- this.wagaPaliwa += Math.random() * 1001;
- }
- public void start() throws Exception {
- if (this.wagaPaliwa < 1000)
- throw new Exception("start anulowany - za mało paliwa");
- else
- System.out.println("Rakieta " + this.nazwa + " wystartowała");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement