Paradigms Homework1 一个超小型二元一次方程组计算器

原创 2014年01月18日 08:43:03
今天累惨了,先把代码放上 后天再更
import javax.swing.*;

import java.text.DecimalFormat;
import java.awt.event.*;

public class BinaryEquation{
	
	JFrame frame;
	JLabel label1, label2, label3, label4,rightanswer1,rightanswer2,label5,label6;
	JTextField slope5 = new JTextField(10);
	JTextField slope6 = new JTextField(10);
	JButton compare;
	long userx, usery;
	String y;
	String x;
	
	public static void main (String[] args)
	{
		BinaryEquation gui = new BinaryEquation();
		//set parameters
		int a = 1, b = 2, c = 3, d = 4;
		a = (int) (Math.random()*101);
		b = (int) (Math.random()*101);
		c = (int) (Math.random()*101);
		d = (int) (Math.random()*101);
		gui.go(a,b,c,d);
	}
	
	public void go(int a,int b,int c,int d)
	{		
		QuadricEquation equa1 = new QuadricEquation();
		QuadricEquation equa2 = new QuadricEquation();
		
		equa1.SetParameters(a, b);
		equa2.SetParameters(c, d);
				
		//solve the equations to get the right answers
		DecimalFormat df = new DecimalFormat();
		x=df.format((float)(a-c)/(d-b));
		y=df.format((float)(a*d-b*c)/(c-a));
		
		//create the user interface
		JFrame frame = new JFrame();
		JLabel label1 = new JLabel("Equation1: y = "+ a + "x + "+b);
		JLabel label2 = new JLabel("Equation2: y = "+ c + "x + "+d);
		JLabel label3 = new JLabel("User's Guess: x =");
		JLabel label4 = new JLabel("User's Guess: y =");
		label5 = new JLabel();
		label6 = new JLabel();
		
		//get user's input
		
		JButton result = new JButton("Result");
				
		frame.setLayout(null);
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				
		frame.getContentPane().add(label1);
		frame.getContentPane().add(label2);
		frame.getContentPane().add(label3);
		frame.getContentPane().add(label4);
		frame.getContentPane().add(label5);
		frame.getContentPane().add(label6);
		
		frame.getContentPane().add(slope5);
		frame.getContentPane().add(slope6);
				
		frame.getContentPane().add(result);
		rightanswer1 = new JLabel();
		rightanswer2 = new JLabel();
		frame.getContentPane().add(rightanswer1);
		frame.getContentPane().add(rightanswer2);
		
		rightanswer1.setBounds(30, 180, 200, 30);
		rightanswer2.setBounds(30, 210, 200, 30);
				
		frame.setSize(300,600);
		label1.setBounds(30, 30, 200, 30);
		label2.setBounds(30, 60, 200, 30);
		label3.setBounds(30, 90, 200, 30);
		label4.setBounds(30, 120, 200, 30);
		label5.setBounds(30, 240, 200, 30);
		label6.setBounds(30, 270, 200, 30);
		
		slope5.setBounds(140, 90, 50, 30);
		slope6.setBounds(140, 120, 50, 30);
				
		result.setBounds(60, 150, 100, 30);
				
		frame.setVisible(true);
		
		result.addActionListener(new ButtonListener());
		slope5.addActionListener(new textListener1());
		slope6.addActionListener(new textListener2());
	}
	
	class textListener1 implements ActionListener
	{
		public void actionPerformed(ActionEvent e1)
		{
			if(e1.getSource() == slope5)
				userx = Long.parseLong(slope5.getText());
		}
	}
	
	class textListener2 implements ActionListener
	{
		public void actionPerformed(ActionEvent e1)
		{
			if(e1.getSource() == slope6)
				usery = Long.parseLong(slope6.getText());
		}
	}
	
	class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			rightanswer1.setText("Right Answer: x = " + x);
			rightanswer2.setText("Right Answer: y = " + y);
			label5.setText("User's Guess: x = "+userx);
			label6.setText("User's Guess: y= "+usery);
		}
	}

}



//y=k*x+b
public class QuadricEquation {
	private float Slope,Intercept;
	
	public void SetParameters(float k, float b)	//Set parameters slope and intercept
	{
		Slope = k;
		Intercept = b;
	}
	public float GetSlope()					//Get parameters slope and intercept
	{
		return Slope;
	}
	public float GetIntercept()
	{
		return Intercept;
	}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

用 JavaScript 写一个超小型编译器

前几天看到 Github 上一个非常好的编译器 Demo: thejameskyle/the-super-tiny-compiler: Possibly the smallest compile...

可用于进程间共享的超小型多读单写锁

使用起来极其简单, 任何初始值为0的Integer变量都可以作为锁变量, 通过BeginRead(), EndRead(), BeginWrite(), EndWrite()来同步共享资源的读写操作,...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

php超小型模版类

总结: $smarty的工作流程: 1:把需要显示的全局变量,赋值,塞到对象内部的属性上,一个数组内 2:编译模版,把{$标签},解析成响应的phpe echo代码 3...

超小型局域网组建的方案

超小型局域网组建的方案 生活中我们经常会遇到需要组建一个超小型局域网共享上网的问题,比如在学校组建一个小型局域网共享上网,或者组建一个家庭网络共享上网等,在此过程中会碰到很多小问题,本人故撰此文,望...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)