当前位置:编程学习 > C/C++ >>

用JAVA编写一个计算立方体、球体和圆柱的体积

答案:有测试数据!

/*
* Geometry.java
*/

public class Geometry {
public static int instance = 0;
public Geometry () {
instance++;
}
public static double calculateRectangle(double width, double height) {
return width * height;
}

public static double calculateCube(double width, double height, double length) {
return width * height * length;
}

public static double calculateSphere (double radius) {
return (4 * Math.PI * radius * radius * radius) /3;
}

public static void main(String[] args) {
System.out.println(" 1 " + calculateRectangle(4, 5));
System.out.println(" Instance = " + instance);
System.out.println(" 2 " + calculateCube(4, 5, 6));
System.out.println(" Instance = " + instance);
System.out.println(" 3 " + calculateSphere(3));
System.out.println(" Instance = " + instance);
}
}

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Volume extends JFrame implements ActionListener,ItemListener{
 JPanel p1,p2,p3;
 JRadioButton rb1,rb2,rb3;
 ButtonGroup bg;
 JButton b1,b2;
 JLabel l1,l2,l3,l4;
 JTextField tf1,tf2,tf3,tf4;
 public Volume(){
  init();
  this.add(p1,BorderLayout.NORTH);
  this.add(p2,BorderLayout.CENTER);
  this.add(p3,BorderLayout.SOUTH);
  this.setTitle("Volume Calculator");
  this.pack();
  this.setLocationRelativeTo(null);//窗口居中
  this.setVisible(true);
  this.setResizable(false);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 public void init(){
  rb1 = new JRadioButton("正方体",true);
  rb2 = new JRadioButton("球  体");
  rb3 = new JRadioButton("圆柱体");
  rb1.addItemListener(this);
  rb2.addItemListener(this);
  rb3.addItemListener(this);
  
  rb1.addActionListener(this);
  rb2.addActionListener(this);
  rb3.addActionListener(this);
  
  bg = new ButtonGroup();
  bg.add(rb1);
  bg.add(rb2);
  bg.add(rb3);
  
  p1 = new JPanel();
  p1.setBorder(BorderFactory.createTitledBorder("选择对象:"));
  p1.add(rb1);
  p1.add(rb2);
  p1.add(rb3);
  
  l1 = new JLabel("边长:");
  l2 = new JLabel("半径:");
  l3 = new JLabel("高:"); 
  
  tf1 = new JTextField(4);
  tf2 = new JTextField(4);
  tf3 = new JTextField(4);
  tf1.setEditable(true); 
  tf2.setEditable(false); 
  tf3.setEditable(false); 
   
  p2 = new JPanel();
  p2.setBorder(BorderFactory.createTitledBorder("输入参数:"));
  p2.add(l1);
  p2.add(tf1);
  p2.add(l2);
  p2.add(tf2);
  p2.add(l3);
  p2.add(tf3);
  
  l4 = new JLabel("体积:");
  tf4 = new JTextField(12);
  tf4.setEditable(false);
  b1 = new JButton("GO");
  b1.addActionListener(this);
  b2 = new JButton("Help");
  b2.addActionListener(this);
  
  p3 = new JPanel();
  p3.setBorder(BorderFactory.createTitledBorder("计算结果:"));
  p3.add(b1);
  p3.add(l4);
  p3.add(tf4);
  p3.add(b2);
 }
 public static void main(String[] args){
  JFrame.setDefaultLookAndFeelDecorated(true);
  JDialog.setDefaultLookAndFeelDecorated(true);
  new Volume();
 }
 
 public void clear(){
  tf1.setText("");
  tf2.setText("");
  tf3.setText("");
  tf4.setText("");
 }
 public String getText(JTextField tf){
  return tf.getText().trim();
 }
 
 public void shouDialog(){
  JOptionPane.showMessageDialog(this, "参数只能为正数,请不要输入非法字符!", "警告", JOptionPane.WARNING_MESSAGE);
  clear();
 }
 public  void itemStateChanged(ItemEvent ie){
  
  if(ie.getItem()==rb1){
   if(ie.getStateChange()==ItemEvent.SELECTED){
    clear();
    tf1.setEditable(true);
    tf2.setEditable(false);
    tf3.setEditable(false);
   }
  }else if(ie.getItem()==rb2){
   if(ie.getStateChange()==ItemEvent.SELECTED){
    clear();
    tf2.setEditable(true);
    tf1.setEditable(false);
    tf3.setEditable(false);
   }
  }else if(ie.getItem()==rb3){
   if(ie.getStateChange()==ItemEvent.SELECTED){
    clear();
    tf2.setEditable(true);
    tf3.setEditable(true);
    tf1.setEditable(false);
   }
  }
 
 }
 
 public void actionPerformed(ActionEvent ae){
  if(ae.getSource()==b2){//"Help"
   new Dialog(this);
   
  }else if(ae.getSource()==b1){//"GO"
   double volume = 0;
   try{
    if(rb1.isSelected()){//正方体
     if(!getText(tf1).equals("")){
      double size = Double.parseDouble(getText(tf1));
      if(size>0){
       volume = size*size*size;
       tf4.setText((double)((int)Math.round(volume*1000)/1000.000)+"");
      }else{
       shouDialog();
      }
     }
     
    }else if(rb2.isSelected()){//球体
     if(!getText(tf2).equals("")){
      double radius = Double.parseDouble(getText(tf2));
      if(radius>0){      
       volume = 4*Math.PI* radius*radius*radius/3;
       tf4.setText((double)((int)Math.round(volume*1000)/1000.000)+"");
      }else{
       shouDialog();
      }   
     }   
     
    }else if(rb3.isSelected()){//圆柱体
     if(!getText(tf2).equals("")&&!getText(tf3).equals("")){
      double radius = Double.parseDouble(getText(tf2));
      double height = Double.parseDouble(getText(tf3));
      if(radius>0&&height>0){
       volume = Math.PI*radius*radius*height;
       tf4.setText((double)((int)Math.round(volume*1000)/1000.000)+"");
      }else{
       shouDialog();
&n

上一个:北大青鸟也有java课程吗?我想学java编程!
下一个:要做Java程序员 需要知道那些技术 重点有那些

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,