一道Java题,请问怎么编程啊?
定义一个类Operation,该类提供实现加、减、乘、除的操作。测试类Sample应用Operation类创建对象,通过该对象中的方法完成加、减、乘、除运算,并输出运算结...
定义一个类Operation,该类提供实现加、减、乘、除的操作。测试类Sample应用Operation类创建对象,通过该对象中的方法完成加、减、乘、除运算,并输出运算结果。要求四种运行均通过接收参数完成计算。
展开
展开全部
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);
}
});
}
}
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);
}
});
}
}
展开全部
在Operation类中定义加、减、乘、除四个方法,方法的两个参数类型用继承Number(包装类Byte, Double, Float, Integer, Long, Short等共有的父类)的包装类类型,然后每个方法中就可以进行相应的运算了,这样就不需要进行繁杂的重载,至于返回值就自己看情况吧,建议统一double类型
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把楼上的修改了下,体现复用代码的原则,另外异常处理没有加上,如果需要把除法里加上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,'/'));
}
}
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,'/'));
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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));
}
}
//加法
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));
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Java技术交流QQ群【175380752】
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |