【程序代码】
public class Circle
{
//私有变量半径
private int r;
//构造方法初始化半径
Circle(int r)
{
this.r = r;
}
//获得半径的getR方法
public int getR()
{
return r;
}
//计算圆面积方法
public double area(int r)
{
return 3.14*r*r;
}
//计算圆周长方法
public double circleLength(int r)
{
return 2*3.14*r;
}
}
class CircleComputer
{
public static void main(String[] args)
{
int cr = 2;
Circle c = new Circle(cr);
int r = c.getR();
System.out.println("圆的半径为:"+r);
System.out.println("圆的面积为:"+c.area(r));
System.out.println("圆的周长为:"+c.circleLength(r));
}
}
【编译与运行】
编译:javac Circle.java
运行:java CircleComputer
Circle类:
public class Circle {
private double r;//私有成员r
public Circle(){}
public Circle(double r){//构造方法
this.r=r;
}
/**
* 计算面积
* @return
*/
public double area(){
return Math.PI*r*r;
}
/**
* 计算周长
* @return
*/
public double circlelength(){
return 2*Math.PI*r;
}
public double getR() {
return r;
}
}
CircleComputer类:
public class CircleComputer {
public static void main(String[] args) {
Circle c=new Circle(1);
System.out.println("半径:"+c.getR());
System.out.println("周长:"+c.circlelength());
System.out.println("面积:"+c.area());
}
}
这个我昨天帮别人写了一个的..一模一样..汗
我在在把代码发一次吧.方法名你自己改,我用了GUI的!
我发现这里碰到同一个问题的机率还真高!
//GUI界面 保存为MyMath 类就行了
import java.awt.Container;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class MyMath extends JFrame{
Container cont;
JButton jb;
JTextField jtf1;
JTextField jtf2;
JRadioButton jrb1;
JRadioButton jrb2;
public void init(){
this.setSize(300,300);
this.setTitle("求圆的面积和周长");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
cont=this.getContentPane();
cont.setLayout(null);
}
public void setWin(){
JLabel jl1 = new JLabel("请输入圆的半径");
jl1.setBounds(50,50,100,25);
cont.add(jl1);
jtf1 = new JTextField();
jtf1.setBounds(160,50,100,25);
cont.add(jtf1);
jrb1 = new JRadioButton("计算面积");
jrb1.setBounds(50,110,80,25);
jrb2 = new JRadioButton("计算周长");
jrb2.setBounds(160,110,80,25);
ButtonGroup bg = new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
cont.add(jrb1);
cont.add(jrb2);
jb = new JButton("开始计算");
jb.setBounds(100,180,100,25);
jb.addActionListener(new MathFunction(this));
cont.add(jb);
jtf2 = new JTextField();
jtf2.setBounds(70,230,160,25);
cont.add(jtf2);
}
public static void main(String[] args) {
MyMath mm = new MyMath();
mm.init();
mm.setWin();
mm.setVisible(true);
}
}
//math类的事件,保证为MathFunction类名就行了
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class MathFunction implements ActionListener{
MyMath m1;
public MathFunction(MyMath m1){
this.m1=m1;
}
public void actionPerformed(ActionEvent e) {
final float PAI=(float) 3.14;
String regex ="100|[1-9][0-9]|[0-9]";
if(e.getSource()==m1.jb&&m1.jtf1.getText().matches(regex)){
if(m1.jrb1.isSelected()==true){
float r = Integer.parseInt(m1.jtf1.getText());
float s =r*r*PAI;
m1.jtf2.setText("该圆的面积等于:"+s);
}
if(m1.jrb2.isSelected()==true){
float r = Integer.parseInt(m1.jtf1.getText());
float c = r*2*PAI;
m1.jtf2.setText("该圆的周长等于:"+c);
}
}
else{
JOptionPane.showMessageDialog(null,"输入有误,请重新输入");
m1.jtf1.setText("");
m1.jtf1.getText().trim();
}
}
}
以上二个的class文件一定要在同一目录下. 类名,方法名你自己改...半径的长度控制,你自己改那个正则表达式吧..很简单的!