新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java代码求面积周长 java求周长和面积

用java求圆的面积与周长

class Circle {

创新互联公司服务项目包括耿马网站建设、耿马网站制作、耿马网页制作以及耿马网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,耿马网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到耿马省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

private int r;

public Circle(int r){

this.r=r;

}

public int getr(){

return r;

}

public double getArea(){

return Math.PI*r*r;

}

public double getCircleLength(){

return Math.PI*2*r;

}

}

public class CircleComputer{

public static void main(String []args){

Circle a=new Circle(1);

System.out.println("圆半径r= "+a.getr());

System.out.println("圆面积为:"+a.getArea());

System.out.println("圆周长为:"+a.getCircleLength());

}

}

求一个,用Java编写一个求长方形的面积和周长的程序,(面向对象).

//看看我这个程序把 比较符合面向对象的思想,告诉搂住一声,尽量把一些程序写尽方法里,而不是都写在主方法中!这样不好~~~~ 希望对你有用!!

import java.util.Scanner;

public class Ex {

public static int squ(int x,int y){ //求面积的方法

int s = x* y;

return s;

}

public static double len(int x,int y){//求周长的方法

int l = (x+y)*2;

return l;

}

public static void main(String [] aa){

System.out.println("请输入宽:"); //从命令行输入宽

Scanner in = new Scanner(System.in);

int le = in.nextInt();

System.out.println("请输入高:");//从命令行输入高

Scanner in2 = new Scanner(System.in);

int hi = in2.nextInt(); //转换为int型

int mianji = squ(le,hi); //调用方法

System.out.println("面积是:" + mianji);

/*

* 求周长同理,调用周长那个方法即可

*/

}

}

求一个计算周长和面积的java程序

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class a

{

public static void main(String []args)

{

b a=new b();

}

}

class b extends JFrame

{

private JLabel x,y,z,a;

private JTextField x1,y1,z1,a1;

private JButton bo,bo2;

public b()

{

x=new JLabel("边长: ",SwingConstants.RIGHT);

y=new JLabel("宽度: ",SwingConstants.RIGHT);

z=new JLabel("周长: ",SwingConstants.RIGHT);

a=new JLabel("面积: ",SwingConstants.RIGHT);

x1=new JTextField(10);

y1=new JTextField(10);

z1=new JTextField(10);

a1=new JTextField(10);

bo=new JButton("开始计算");

bo2=new JButton("退出程式");

setTitle("Area and Primeter of Renctangle-中软科技");

Container pane=getContentPane();

pane.setLayout(new GridLayout(5,2));

pane.add(x);

pane.add(x1);

pane.add(y);

pane.add(y1);

pane.add(z);

pane.add(z1);

pane.add(a);

pane.add(a1);

pane.add(bo);

pane.add(bo2);

setSize(400,300);

bo.addActionListener(new boHand());

bo2.addActionListener(new bo2Hand());

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

private class boHand implements ActionListener

{

private double length,width,area,primeter;

public void actionPerformed(ActionEvent e)

{

length=Double.parseDouble(x1.getText());

width=Double.parseDouble(y1.getText());

area=length*width;

primeter=2*(width+length);

z1.setText(""+primeter+"cm");

a1.setText(""+area+"cm*cm");

}

}

private class bo2Hand implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

System.exit(0);

}

}

}

用java语言如何编写计算圆周长和圆面积程序

可以通过创建一个圆的类完成计算圆周长和面积的功能。

假设这个圆的类名叫做Circle,因为根据圆的半径就可以求出圆的周长和面积,所以可以在这个类中定义一个半径属性mRadius,然后定义两个方法getLength和getArea分别实现计算圆周长和面积的功能。

java语言源代码如下:

public class Circle{

//圆的半径

private double mRadius;

public Circle(double mRadius){

this.mRadius = mRadius;

}

//获取圆的周长

public double getLength(){

return 2*Math.PI*mRadius;

}

//获取圆的面积

public double getArea(){

return Math.PI*mRadius*mRadius;

}

}

//注:由于测试类只是调用Circle类的方法,功能很简单,便没有写测试类。

Java编写一个矩形类,并计算面积和周长?

class Rectangle{

private int width = 2;

private int length = 1;

public int getWidth(){

return this.width;

}

public void setWidth(int w){

this.width = w;

}

public int getLength(){

return this.length;

}

public void setLength(int l){

this.length = l;

}

public int getArea(){

return this.length * this.width;

}

public int getCircumference(){

return (this.length + this.width) * 2;

}

public Rectangle(){}

public Rectangle(int l, int w){

this.length = l;

this.width = w;

}

}

public class demo{

public static void main(String[] args) {

Rectangle rect = new Rectangle(30, 8);

System.out.print("长方形的面积是:");

System.out.println(rect.getArea());

System.out.printf("长方形的周长是:%d\n", rect.getCircumference());

}

}

如何用java计算一个圆的面积和周长?

一、数学公式:

圆周长=2*π*半径

面积=π*半径²

二、算法分析:

周长和面积都依赖半径,所以要先输入半径值,然后套用公式,计算周长和面积。 最终输出结果即可。

三、参考代码:

代码如下

#include "stdio.h"

#define Pi 3.14

void main()

{

float r,c,area;

printf("请输入圆的半径:");

scanf("%f",r);

c=2*Pi*r;

area=Pi*r*r;

printf("该圆的周长是%.2f,面积是%.2f\n",c,area);

}


文章标题:java代码求面积周长 java求周长和面积
本文链接:http://www.wjwzjz.com/article/hgdpjo.html
在线咨询
服务热线
服务热线:028-86922220
TOP