-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuadeq.java
More file actions
78 lines (77 loc) · 2.89 KB
/
Copy pathQuadeq.java
File metadata and controls
78 lines (77 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Quadeq{
static String s[]=new String[2];
static int count;
public static String alg(int num, BufferedReader br,String ur[]) throws Exception{
Random rand=new Random(num);
double a=0,b,c,D,x1=0,x2=0;
System.out.println("###Equation "+num+"###");
System.out.println("Enter first coefficient");
while (a==0) a=rand.nextInt(11);
System.out.println("Enter second coefficient");
b=rand.nextInt(11);
System.out.println("Enter third coefficient");
c=rand.nextInt(11);
s[0]="";
s[0]=Double.toString(a)+"x²";
if (b>=0) s[0]=s[0]+"+";
s[0]=s[0]+Double.toString(b)+"x";
if (c>=0) s[0]=s[0]+"+";
s[0]=s[0]+Double.toString(c)+"=0";
System.out.println(s[0]);
D=(b*b)-(4*a*c);
if(D>=0){
x1=(-b+Math.sqrt(D))/(2*a);
x2=(-b-Math.sqrt(D))/(2*a);
s[1]=String.format("%.2g%n",x1)+"; "+String.format("%.2g%n",x2);
}
if (D==0) s[1]=String.format("%.2g%n",x1);
if(D<0) {
D=(Math.sqrt(-D))/(2*a);
x1=(-b)/(2*a);
s[1]=String.format("%.2g%n",x1)+"+j"+String.format("%.2g%n",D)+
"; "+String.format("%.2g%n",x1)+"-j"+String.format("%.2g%n",D);
}
System.out.println("######");
ur[num]=s[0];
return s[1];
}
public static void main(String args[]) throws Exception{
//BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the count of equations");
s[0]=br.readLine();
count=Integer.parseInt(s[0]);
String res[]=new String[count+1];
String ura[]=new String[count+1];
for(int i=0;i<=count;i++){
res[i]="0";
ura[i]="0";
}
String ur[]=new String[count+1];
for(int i=1;i<=count;i++){
res[i] = alg(i,br,ur);
}
JFrame jf=new JFrame("Solution of quadratic equation");
jf.setSize(jf.getMaximumSize());//(1280,768);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable jt=new JTable(count+1,2);
jt.setVisible(true);
jt.setBounds(1,1,600,16*(count+1));
jt.setValueAt("Equation",0,0);
jt.setValueAt("Solution",0,1);
for(int i=1;i<=count;i++){
jt.setValueAt(ur[i],i,0);
jt.setValueAt(res[i],i,1);
}
jt.setTableHeader(null);
JScrollPane jsp=new JScrollPane(jt,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jf.add(jsp);
jf.setSize(jt.getPreferredScrollableViewportSize());
jf.setVisible(true);
}
}