Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**Zadanie 1
- Napisz klasę opisującą równanie kwadratowe o postaci y = ax 2 + bx + c. Współczynniki a, b i c powinny
- byd prywatne. Zdefiniuj następujące publiczne metody:
- konstruktor z parametrami a, b i c
- nadający wartości współczynnikom,
- obliczająca y dla podanego x,
- wyznaczającą pierwiastki.
- */
- import java.util.Scanner;
- public class NewClass
- {
- private float a, b, c;
- public NewClass(float a, float b, float c)
- {
- this.a=a;
- this.b=b;
- this.c=c;
- }
- public void wartosc_y(float x)
- {
- System.out.println("a"+a);
- System.out.println("b"+b);
- System.out.println("c"+c);
- System.out.println("x"+x);
- float wynik=a*(x*x)+b*x+c;
- System.out.println("Wartosc y: "+wynik);
- }
- public void pierwiastki(float x)
- {
- float delta=(b*b)-4*a*c;
- if (delta<0) System.out.println("Funkcja nie posiada pierwiastkow!");
- else if (delta==0)
- {
- System.out.println("Funkcja posiada jeden pierwiastek= "+ (-b)/(2*a));
- }
- else
- {
- System.out.println("Funkcja posiada dwa pierwiastki= "+ (-b+Math.sqrt(delta))/2*a+" oraz= "+(-b-Math.sqrt(delta))/2*a);
- }
- }
- public static void main(String args[])
- {
- Scanner scanner=new Scanner(System.in);
- float x,a,b,c;
- System.out.println("Podaj a: ");
- a=scanner.nextFloat();
- System.out.println("Podaj b: ");
- b=scanner.nextFloat();
- System.out.println("Podaj c: ");
- c=scanner.nextFloat();
- NewClass konstruktor=new NewClass(a,b,c);
- System.out.println("Podaj x: ");
- x=scanner.nextFloat();
- konstruktor.wartosc_y(x);
- konstruktor.pierwiastki(x);
- System.out.println("Koniec programu.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment