Advertisement
mikolajmki

java_lab6

Nov 23rd, 2021
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.49 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.InputMismatchException;
  4. import java.util.Scanner;
  5.  
  6. import static java.lang.Math.pow;
  7.  
  8. public class Main {
  9.  
  10.     public static void znowDzielenie(int x) throws
  11.             ArithmeticException {
  12.         Scanner skaner = new Scanner(System.in);
  13.         int y = skaner.nextInt();
  14.         if (y == 0)
  15.             throw new ArithmeticException("Podaj poprawny mianownik");
  16.         else
  17.             System.out.println("Twรณj wynik to: " + x / y);
  18.     }
  19.  
  20.     public static class Kalkulator
  21.     {
  22.         float a;
  23.         float b = 0;
  24.  
  25.         public Kalkulator(float a, float b, float c) {
  26.             this.a = a;
  27.             this.b = b;
  28.         }
  29.  
  30.         public void wprowadzDane() {
  31.             Scanner scan = new Scanner(System.in);
  32.             System.out.println("Podaj liczbe: ");
  33.             try {
  34.                 this.b = scan.nextInt();
  35.             }
  36.             catch (InputMismatchException e)
  37.             {
  38.                 System.out.println("Wprowadzono bledne dane.");
  39.             }
  40.         }
  41.  
  42.         public void dodawanie()
  43.         {
  44.             a += b;
  45.         }
  46.         public void odejmowanie()
  47.         {
  48.             a -= b;
  49.         }
  50.         public void mnozenie()
  51.         {
  52.             a *= b;
  53.         }
  54.         public void dzielenie()
  55.         {
  56.             if (b == 0)
  57.             {
  58.                 try
  59.                 {
  60.                     a = (int)a / (int) b;
  61.                 }
  62.                 catch (ArithmeticException e)
  63.                 {
  64.                     System.out.println("Nie dziel przez 0!");
  65.                 }
  66.             }
  67.             else a /= (int)b;
  68.         }
  69.  
  70.         public void potegowanie()
  71.         {
  72.             a = (int)pow(a, b);
  73.         }
  74.  
  75.         public void pierwiastkowanie()
  76.         {
  77.             try {
  78.                 a = (int) pow(a, 1 / b);
  79.             }
  80.             catch (ArithmeticException e)
  81.             {
  82.                 System.out.println("Wprowadzono bledne dane.");
  83.             }
  84.         }
  85.  
  86.         public void reset()
  87.         {
  88.             a = 0;
  89.         }
  90.  
  91.         public void wyswietlaLiczby()
  92.         {
  93.             System.out.println("a = " + a + "\n");
  94.         }
  95.  
  96.         public void menu ()
  97.         {
  98.             Scanner scan = new Scanner(System.in);
  99.  
  100.             int c = 1;
  101.             while (c != 8)
  102.             {
  103.                 System.out.println("Wybierz dzialanie: ");
  104.                 System.out.println("1 - dodawanie a + b ");
  105.                 System.out.println("2 - odejmowanie a - b ");
  106.                 System.out.println("3 - mnozenie a * b ");
  107.                 System.out.println("4 - dzielenie a / b ");
  108.                 System.out.println("5 - potegowanie a ^ b ");
  109.                 System.out.println("6 - pierwiastkowanie a ^ (1 / b) ");
  110.                 System.out.println("7 - reset");
  111.                 System.out.println("8 - wyjscie\n");
  112.  
  113.                 c = scan.nextInt();
  114.                 wprowadzDane();
  115.  
  116.                 if (c == 1) dodawanie();
  117.                 else if (c == 2) odejmowanie();
  118.                 else if (c == 3) mnozenie();
  119.                 else if (c == 4) dzielenie();
  120.                 else if (c == 5) potegowanie();
  121.                 else if (c == 6) pierwiastkowanie();
  122.                 else if (c == 7) reset();
  123.  
  124.                 wyswietlaLiczby();
  125.             }
  126.         }
  127.     }
  128.  
  129.     public static void main(String[] args)
  130.     {
  131.         Kalkulator k = new Kalkulator(0, 0, 0);
  132.         k.menu();
  133.     }
  134.  
  135.     }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement