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

本文介绍了一种实现超小型二元一次方程组计算器的方法,适用于学术和教育环境。作业1的内容涉及如何设计和实现这样一个计算器,讨论了基本的代数原理和算法应用。
今天累惨了,先把代码放上 后天再更
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;
	}
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值