一道Java题,请问怎么编程啊?

定义一个类Operation,该类提供实现加、减、乘、除的操作。测试类Sample应用Operation类创建对象,通过该对象中的方法完成加、减、乘、除运算,并输出运算结... 定义一个类Operation,该类提供实现加、减、乘、除的操作。测试类Sample应用Operation类创建对象,通过该对象中的方法完成加、减、乘、除运算,并输出运算结果。要求四种运行均通过接收参数完成计算。 展开
 我来答
手机用户35256
2011-09-26 · TA获得超过1041个赞
知道答主
回答量:479
采纳率:0%
帮助的人:213万
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.beans.*;

public class JTree_main extends JFrame {
static JTree tree = new JTree();

public JTree_main() {
Container contentPane = getContentPane();
JScrollPane scrollPane = new JScrollPane(tree);

contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(scrollPane, BorderLayout.CENTER);

tree.addPropertyChangeListener(new PropertyListener());
}
class ControlPanel extends JPanel {
JCheckBox showRoot = new JCheckBox("show root node");

public ControlPanel() {
showRoot.setSelected(tree.isRootVisible());

setLayout(new FlowLayout());
add(showRoot);

showRoot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tree.setRootVisible(showRoot.isSelected());
}
});
}
}
class PropertyListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent e) {
String name = e.getPropertyName();

if(name.equals(JTree.ROOT_VISIBLE_PROPERTY)) {
String msg = "Root Visible Property: " +e.getNewValue().toString();

JOptionPane.showMessageDialog(JTree_main.tree,msg,"Property Change",JOptionPane.INFORMATION_MESSAGE);
}
}
}
public static void main(String args[]) {
JTree_main a=new JTree_main();
a.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
a.setBounds(300,300,450,300);
a.setVisible(true);
}
}

给你一个参考一下

下面的是增加和删除树节点
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {
JTree tree = new JTree();
DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
TreeSelectionModel selectionModel = tree.getSelectionModel();

JButton removeButton = new JButton("Remove selected node");
JButton addButton = new JButton("Add node");

public Test() {
Container contentPane = getContentPane();

selectionModel.setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);

contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(tree, BorderLayout.CENTER);

tree.addTreeSelectionListener(
new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
TreePath path = e.getNewLeadSelectionPath();
boolean nodesAreSelected = (path != null);

addButton.setEnabled(nodesAreSelected);
removeButton.setEnabled(nodesAreSelected);
}
});
model.addTreeModelListener(new TreeModelListener() {
public void treeNodesInserted(TreeModelEvent e) {
showInsertionOrRemoval(e, " added to ");
}
public void treeNodesRemoved(TreeModelEvent e) {
showInsertionOrRemoval(e, " removed from ");
}
private void showInsertionOrRemoval(TreeModelEvent e,
String s) {
Object[] parentPath = e.getPath();
int[] indexes = e.getChildIndices();
Object[] children = e.getChildren();
Object parent = parentPath[parentPath.length-1];

JOptionPane.showMessageDialog(Test.this,
"Node \"" + children[0].toString() +
"\"" + s + parent.toString() +
" at index " + indexes[0],
"Node Added or Removed",
JOptionPane.INFORMATION_MESSAGE);
}
public void treeNodesChanged(TreeModelEvent e) {}
public void treeStructureChanged(TreeModelEvent e) {}
});
}
class ControlPanel extends JPanel {
public ControlPanel() {
addButton.setEnabled(false);
removeButton.setEnabled(false);

add(addButton);
add(removeButton);

addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath path =
selectionModel.getSelectionPath();

MutableTreeNode parent, node =
(MutableTreeNode)path.getLastPathComponent();

if(path.getPathCount() > 1)
parent = (MutableTreeNode)node.getParent();
else
parent = (MutableTreeNode)node;

int index = parent.getIndex(node) + 1;

String s = JOptionPane.showInputDialog(
Test.this,
"Enter a name for the new node:",
"New Tree Node",
JOptionPane.QUESTION_MESSAGE);

MutableTreeNode newNode =
new DefaultMutableTreeNode(s);

model.insertNodeInto(newNode, parent, index);
}
});
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath path =
selectionModel.getSelectionPath();

if(path.getPathCount() == 1) {
JOptionPane.showMessageDialog(ControlPanel.this,
"Can't remove root node!");
return;
}

MutableTreeNode node =
(MutableTreeNode)path.getLastPathComponent();

model.removeNodeFromParent(node);
}
});
}
}
public static void main(String args[]) {
GraphicJavaApplication.launch(new Test(),
"Tree Model Example",300,300,450,300);
}
}
class GraphicJavaApplication extends WindowAdapter {
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h) {
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true);

f.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE);

f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
}
转眯爱j
2011-09-25 · TA获得超过283个赞
知道小有建树答主
回答量:178
采纳率:0%
帮助的人:81.3万
展开全部
在Operation类中定义加、减、乘、除四个方法,方法的两个参数类型用继承Number(包装类Byte, Double, Float, Integer, Long, Short等共有的父类)的包装类类型,然后每个方法中就可以进行相应的运算了,这样就不需要进行繁杂的重载,至于返回值就自己看情况吧,建议统一double类型
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
晒星星的猫
2011-09-25
知道答主
回答量:28
采纳率:0%
帮助的人:18.1万
展开全部
把楼上的修改了下,体现复用代码的原则,另外异常处理没有加上,如果需要把除法里加上throw,调用的时候使用try catch。

class Operation{//操作类
//加减乘除 法
public double calculate(double num1,double num2,char op){
switch(op){
case '+':
return num1+num2;
case '-':
return num1-num2;
case '+':
return num1*num2;
case '/':
if(num2==0){
System.out.println("除数不能为0");
return 0;//如果除数为0,返回0,表示异常
}else{
return num1/num2;
}
}
}

}
//测试类
public class Sample {
public static void main(String[] args){
Operation oper=new Operation();
System.out.println(oper.add(1.1, 2.2,'+'));
System.out.println(oper.minu(1.1, 2.2,'-'));
System.out.println(oper.division(1.1, 2.2,'*'));
System.out.println(oper.division(1.1, 0,'/'));
System.out.println(oper.mult(1.1, 2.2,'/'));
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
清心又谦恭丶布丁9
2011-09-25 · TA获得超过6604个赞
知道小有建树答主
回答量:1247
采纳率:100%
帮助的人:1858万
展开全部
class Operation{//操作类
//加法
public float add(float num1,float num2){
return num1+num2;
}
//减法
public float minu(float num1,float num2){
return num1-num2;
}
//乘法
public float mult(float num1,float num2){
return num1*num2;
}
//除法
public float division(float num1,float num2){
if(num2==0){
System.out.println("除数不能为0");
return 0;//如果除数为0,返回0,表示异常
}else{
return num1/num2;
}
}
}
//测试类
public class Sample {
public static void main(String[] args){
Operation oper=new Operation();
System.out.println(oper.add(1.1f, 2.2f));
System.out.println(oper.minu(1.1f, 2.2f));
System.out.println(oper.division(1.1f, 2.2f));
System.out.println(oper.division(1.1f, 0f));
System.out.println(oper.mult(1.1f, 2.2f));
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
素颜vv风之殇
2011-09-25
知道答主
回答量:27
采纳率:0%
帮助的人:8.1万
展开全部
Java技术交流QQ群【175380752】
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式