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());
}
}
//Jcreator4.0編譯通過,寫的比較簡單 只有簡單的功能 剛剛寫的求周長時忘乘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利用方法的重載計算圓形和矩形的面積
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
方法的重載
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計算三角形、矩形、圓的面積
//圖形類作父類
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求助:由鍵盤輸入一個矩形的長和寬,求出這個矩形的周長和面積 該怎麼寫
publicstaticvoidmain(String[]args){
System.out.println("輸入長度");
Doublec=newScanner(System.in).nextDouble();
System.out.println("輸入寬度");
Doublek=newScanner(System.in).nextDouble();
System.out.println("面積:"+c*k);
System.out.println("周長:"+(c+k)*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程序,描寫一個矩形類,並輸出某個矩形的長,寬,面積。具體描述如下
// 矩形
public class RectangleDemo {
public static void main(String[] args) {
RectangleDemo demo = new RectangleDemo(12, 32);
System.out.println(demo.getPerimeter());
System.out.println(demo.getArea());
demo = new RectangleDemo();
System.out.println(demo.getArea());
System.out.println(demo.getPerimeter());
demo.setHeight(50);
demo.setWidth(30);
System.out.println(demo.getArea());
System.out.println(demo.getPerimeter());
}
// 求周
public double getPerimeter() {
return (height + width) * 2;
}
// 求面積
public double getArea() {
return height * width;
}
public RectangleDemo(double height, double width) {
this.height = height;
this.width = width;
}
public RectangleDemo() {
this.height = 10;
this.width = 10;
}
private double height;// 高度
private double width;// 寬度
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
}
編寫矩形類RectangleJava程序矩形類兩數據員別rLength寬rWidth通getLength()、getWidth()、getArea()別查看矩形、寬面積通setLength()setWidth()重新設置矩形寬
㈨ 用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));
}
}