「java矩形面积」java矩形面积简单
今天给各位分享java矩形面积的知识,其中也会对java矩形面积简单进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java 编写一个类,该类创建的对象可以计算矩形的面积,刚学java 希望是简单的
- 2、“Java”编程中如何求矩形的面积?
- 3、Java编程求矩形的面积
- 4、java求矩形面积 矩形的面积怎么求
- 5、如何用java计算三角形、矩形、圆的面积?
- 6、Java编写一个矩形类,并计算面积和周长?
java 编写一个类,该类创建的对象可以计算矩形的面积,刚学java 希望是简单的
import java.math.BigDecimal;
public class Rectangle {
private double width; //宽
private double length; //长
/**
* @return 获得面积
*/
public double getRectangleArea(){
double area=new BigDecimal(width).multiply(new BigDecimal(length)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();//四舍五入,保留两位小数
return area;
}
/**
*
* @return 获得周长
*/
public double getRectanglePerimeter(){
double perimeter=new BigDecimal(width).add(new BigDecimal(length)).setScale(2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(2)).doubleValue();//四舍五入,保留两位小数
return perimeter;
}
public Rectangle(int width,int length){
this.width=width;
this.length=length;
}
public Rectangle(){
}
public void setWidth(double width){
this.width=width;
}
public double getWidth(){
return width;
}
public void setLength(double length){
this.length=length;
}
public double getLength(){
return length;
}
public static void main(String [] args){
Rectangle r=new Rectangle();
r.setLength(3.265);
r.setWidth(7.275);
System.out.println("面积为"+r.getRectangleArea());
System.out.println("周长为"+r.getRectanglePerimeter());
}
}
“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是一种可以撰写跨平台应用程序的面向对象的程序设计语言。
Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。
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求矩形面积 矩形的面积怎么求
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计算三角形、矩形、圆的面积?
//图形类作父类
public class Diagram {
//计算面积
public double area(){return 0;}
}
//圆类:继承图形类
public class Crile extends Diagram{
private double r;
public Crile(double r){
this.r=r;
}
//重写area方法
public double area(){
double r=this.r;
return r*r*3.14;
}
}
//三角形类:继承图形类
public class Triangle extends Diagram{
private double high; //三角形的高
private double bottom; //三角形的底
public Triangle(double h,double b){
this.high=h;
this.bottom=b;
}
public double area(){
double h=this.high;
double b=this.bottom;
return h*b/2;
}
}
//测试
public class test {
public static void main(String[] args) {
System.out.println("请选择图形的类型:(1)三角形(2)圆");
Scanner scanner=new Scanner(System.in);
int i=scanner.nextInt();
if(i==1){
System.out.println("你选择的是三角形!请输入三角形高长(回车结束):");
double high=scanner.nextLong();
System.out.println("请输入三角形底长(回车结束):");
double bottom=scanner.nextLong();
//这里体现动态,如果选择的图形是三角形,那么创建三角形类
//调用的时候就是调用的三角形的方法
Diagram diagram=new Triangle(high, bottom);
System.out.println("三角形的面积为:"+diagram.area());
}
if(i==2){
System.out.println("你选择的是圆形!请输入圆的半径(回车结束):");
double r=scanner.nextLong();
Diagram diagram=new Crile(r);
System.out.println("三角形的面积为:"+diagram.area());
}
}
}
其他的一样了,纯手工 望采纳!
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矩形面积和java矩形面积简单的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-07,除非注明,否则均为
原创文章,转载请注明出处。