Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. import java.io.FileNotFoundException;
  2.  
  3. public class Main {
  4.  
  5.     public static void podniesArrayIndexOutOfBoundException() throws ArrayIndexOutOfBoundsException {
  6.         throw new ArrayIndexOutOfBoundsException();
  7.     }
  8.  
  9.     public static void podniesFileNotFoundException() throws FileNotFoundException {
  10.         throw new FileNotFoundException();
  11.     }
  12.  
  13.     public static void podniesException() throws Exception {
  14.         throw new Exception("Zła Jednostka");
  15.     }
  16.  
  17.     public static int podajLiczbe(String liczba) throws NumberFormatException {
  18.         int pomocnicza;
  19.         if (liczba.startsWith("0b"))
  20.             pomocnicza = 2;
  21.         else if (liczba.startsWith("0x"))
  22.             pomocnicza = 16;
  23.         else if (liczba.startsWith("0"))
  24.             pomocnicza = 8;
  25.         else
  26.             throw new NumberFormatException();
  27.  
  28.         if (pomocnicza == 2 || pomocnicza == 16)
  29.             return Integer.parseInt(liczba.substring(2), pomocnicza);
  30.         else
  31.             return Integer.parseInt(liczba.substring(1), pomocnicza);
  32.     }
  33.  
  34.  
  35.     public static void main(String[] args) {
  36.  
  37.         //ZADANIE1
  38.         // MOZNA ZAMKNAC W JEDNYM TRY CATCH, ALE WTEDY WYRZUCI TYLKO JEDEN BLAD!!
  39.         /*try {
  40.             podniesArrayIndexOutOfBoundException();
  41.             podniesFileNotFoundException();
  42.             podniesException();
  43.         } catch (Exception e) {
  44.             e.printStackTrace();
  45.         }*/
  46.  
  47.         //ZADANIE2
  48.  
  49.         int a = podajLiczbe("0111");
  50.         System.out.println(a);
  51.  
  52.         //ZADANIE3
  53.        /* Rakieta rakieta = new Rakieta();
  54.         rakieta.zatankuj();
  55.         try {
  56.             rakieta.start();
  57.         } catch (Exception e) {
  58.             e.printStackTrace();
  59.         }*/
  60.  
  61.  
  62.     }
  63. }
  64. //////////////////RAKIETA
  65.  
  66. public class Rakieta {
  67.     private String nazwa;
  68.     private int wagaPaliwa;
  69.  
  70.     Rakieta() {
  71.         this.nazwa = "Bravo";
  72.         this.wagaPaliwa = 0;
  73.     }
  74.  
  75.     Rakieta(String nazwa, int wagaPaliwa) {
  76.         this.nazwa = nazwa;
  77.         this.wagaPaliwa = wagaPaliwa;
  78.     }
  79.  
  80.     public void zatankuj() {
  81.         if (this.wagaPaliwa < 1000)
  82.             this.wagaPaliwa += Math.random() * 1001;
  83.     }
  84.  
  85.     public void start() throws Exception {
  86.         if (this.wagaPaliwa < 1000)
  87.             throw new Exception("start anulowany - za mało paliwa");
  88.         else
  89.             System.out.println("Rakieta " + this.nazwa + " wystartowała");
  90.     }
  91.  
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement