Java编写一个矩形类,并计算面积和周长?
class Rectangle{
中站网站建设公司创新互联,中站网站设计制作,有大型网站制作公司丰富经验。已为中站1000多家提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的中站做网站的公司定做!
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计算一个矩形的面积?
//计算矩形的面积
public class RectArea {
public static double getArea(double width, double higth) {
double area = 0.0;// 矩形面积
// 判断输入是否合理
if (!(width = 0 || higth = 0)) {
area = width * higth;
return area;// 返回面积
} else {
System.out.println("请输入合理的长宽");
return -1;
}
}
public static void main(String[] args) {
//测试 宽:10.0 高:20.0
System.out.println("矩形面积" + RectArea.getArea(10.0, 20.0));
}
}
Java利用方法的重载计算圆形和矩形的面积
public class Test {
public static void main(String[] args) {
System.out.println("圆的面积:" + area(2));
System.out.println("三角形的面积:" + area(3,4,5));
System.out.println("矩形的面积:" + area(3,4));
}
//求圆的面积
public static double area(double radius){
return Math.PI * radius * radius;
}
//求三角形的面积
public static double area(double a, double b, double c){
double s = (a + b + c) / 2;
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
//求矩形的面积
public static double area(double width, double height){
return width * height;
}
//求椭圆的面积
/* public static double area(double a, double b){
return Math.PI * a * b;
}*/
}
求椭圆面积需要2个参数,求矩形面积也需要2个参数,这些参数的数据类型、顺序和数量完全一致,无法实现重载,除非改名。
java求矩形面积 矩形的面积怎么求
import java.util.*;
public class Rectangle {
private float length; //定义长变量
private float width; // 宽变量
public Rectangle(float length,float width){
this.length=length;
this.width=width;
}
public float getGirth(){
return (length+width)*2;
} //求周长方法
public float getArea(){
return length*width;
} //求面积方法
public static void main (String[] args) {
Scanner in=new Scanner(System.in);//调用输入方法
System.out.println ("请输入矩形的长:");
float a=in.nextFloat();
System.out.println ("请输入矩形的宽:");
float b=in.nextFloat();
System.out.println ("矩形周长为:"+new Rectangle(a,b).getGirth());
System.out.println ("矩形面积为:"+new Rectangle(a,b).getArea());
}
}
计算矩形面积 用java
方法的重载
class Circle
{
private float a;
private float b;
public float area;
public void setA(float a)
{
this.a=a;
}
public float getA()
{
return this.a;
}
public void setArea(float b)
{
this.b=b;
}
public float getB()
{
return this.b;
}
public void into(float a)
{
area=a*a;
System.out.println("这是一个正方形...面积为: "+area);
}
public void into(float a,float b)
{
area=a*b;
System.out.println("这是一个正方形...面积为: "+area);
}
}
public class Java
{
public static void main(String args[])
{
Circle c=new Circle();
c.into(2);
c.into(2,3);
}
}
本文标题:java代码求矩形的面积 c语言求矩形面积代码
分享网址:http://scyingshan.cn/article/hpcdgc.html