자바 6주차 과제

Written by 박민영 on Apr 21st, 2022 Views Report Post

둘다 맡겼음

import java.util.Scanner;

public class LinearEquation {
    private int a,b,c,d,e,f;

    public LinearEquation(int a, int b, int c, int d, int e, int f) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
        this.e = e;
        this.f = f;
    }

    public int getA() {
        return a;
    }

    public int getB() {
        return b;
    }

    public int getC() {
        return c;
    }

    public int getD() {
        return d;
    }

    public int getE() {
        return e;
    }

    public int getF() {
        return f;
    }

    public boolean isSolvable(){
        if (a*d - b*c !=0) return true;
        else return false;
    }

    public float getX(){
        float x;
        x = (float)(e*d - b*f)/(float)(a*d - b*c);
        return x;
    }

    public float getY(){
        float y;
        y = (float)(a*f - e*c)/(float)(a*d - b*c);
        return y;
    }

    public static void main(String[] args) {
        int a,b,c,d,e,f;
        float x,y;
        Scanner sc = new Scanner(System.in);
        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();
        d = sc.nextInt();
        e = sc.nextInt();
        f = sc.nextInt();

        LinearEquation l = new LinearEquation(a,b,c,d,e,f);
        if (!l.isSolvable())
            System.out.println("The equation has no solution.");
        else {
            x = l.getX();
            y = l.getY();
            System.out.println("x is "+x+" and y is "+y);
        }
    }
}
import java.util.Scanner;
public class IntersectingPoint {
        public static void main(String[] args) {
            Scanner keyboard=new Scanner(System.in);
            int x1,x2,y1,y2,x3,x4,y3,y4;
            float x,y;

            x1=keyboard.nextInt();
            y1=keyboard.nextInt();
            x2=keyboard.nextInt();
            y2=keyboard.nextInt();
            x3=keyboard.nextInt();
            y3=keyboard.nextInt();
            x4=keyboard.nextInt();
            y4=keyboard.nextInt();

            int a,b,c,d,e,f;

            a=(y1-y2);
            b=x2-x1;
            c=y3-y4;
            d=x4-x3;
            e=(y1-y2)*x1-(x1-x2)*y1;
            f=(y3-y4)*x3-(x3-x4)*y3;
            LinearEquation l = new LinearEquation(a,b,c,d,e,f);

            if ((y1-y2)*(x4-x3)-(x2-x1)*(y3-y4)==0)
                System.out.println("교차점이 없다");
            else {
                x=l.getX();
                y=l.getY();
                if (x<0 && y>0) {
                    x=(-1)*x;
                    x= (float) (Math.floor(x*10)/10);
                    x=(-1)*x;
                    y= (float) (Math.floor(y*10)/10);
                    System.out.printf("%.1f %.1f",x,y);
                }
                else if (x>0 && y<0) {
                    y=(-1)*y;
                    y= (float) (Math.floor(y*10)/10);
                    y=(-1)*y;
                    x= (float) (Math.floor(x*10)/10);
                    System.out.printf("%.1f %.1f",x,y);
                }
                else if (x<0 && y<0) {
                    x=(-1)*x;
                    x= (float) (Math.floor(x*10)/10);
                    x=(-1)*x;
                    y=(-1)*y;
                    y= (float) (Math.floor(y*10)/10);
                    y=(-1)*y;
                    System.out.printf("%.1f %.1f",x,y);
                }
                else {
                    x= (float) (Math.floor(x*10)/10);
                    y= (float) (Math.floor(y*10)/10);
                    System.out.printf("%.1f %.1f",x,y);
                }
            }
        }
    }

Comments (0)