PatrickSwayze

zad 1 lab 2

Feb 25th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1. /**Zadanie 1
  2. Napisz klasę opisującą równanie kwadratowe o postaci y = ax 2 + bx + c. Współczynniki a, b i c powinny
  3. byd prywatne. Zdefiniuj następujące publiczne metody:
  4.  konstruktor z parametrami a, b i c
  5.  nadający wartości współczynnikom,
  6.  obliczająca y dla podanego x,
  7.  wyznaczającą pierwiastki.
  8.  
  9.  */
  10. import java.util.Scanner;
  11.  
  12. public class NewClass
  13. {
  14.     private float a, b, c;
  15.     public NewClass(float a, float b, float c)
  16.     {
  17.         this.a=a;
  18.         this.b=b;
  19.         this.c=c;
  20.     }
  21.     public void wartosc_y(float x)
  22.     {
  23.         System.out.println("a"+a);
  24.         System.out.println("b"+b);
  25.         System.out.println("c"+c);
  26.         System.out.println("x"+x);
  27.         float wynik=a*(x*x)+b*x+c;
  28.         System.out.println("Wartosc y: "+wynik);
  29.     }
  30.     public void pierwiastki(float x)
  31.     {
  32.         float delta=(b*b)-4*a*c;
  33.         if (delta<0) System.out.println("Funkcja nie posiada pierwiastkow!");
  34.         else if (delta==0)
  35.         {
  36.             System.out.println("Funkcja posiada jeden pierwiastek= "+ (-b)/(2*a));
  37.         }
  38.         else
  39.         {
  40.             System.out.println("Funkcja posiada dwa pierwiastki= "+ (-b+Math.sqrt(delta))/2*a+" oraz= "+(-b-Math.sqrt(delta))/2*a);
  41.         }
  42.        
  43.     }
  44.     public static void main(String args[])
  45.     {
  46.         Scanner scanner=new Scanner(System.in);
  47.         float x,a,b,c;
  48.         System.out.println("Podaj a: ");
  49.         a=scanner.nextFloat();
  50.         System.out.println("Podaj b: ");
  51.         b=scanner.nextFloat();
  52.         System.out.println("Podaj c: ");
  53.         c=scanner.nextFloat();
  54.        
  55.         NewClass konstruktor=new NewClass(a,b,c);
  56.        
  57.         System.out.println("Podaj x: ");
  58.        
  59.         x=scanner.nextFloat();
  60.        
  61.         konstruktor.wartosc_y(x);
  62.        
  63.         konstruktor.pierwiastki(x);
  64.        
  65.         System.out.println("Koniec programu.");
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment