java求面積
⑴ 用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類求N個圖形的面積和
樓主,程序如下:
測試類:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ShapeSystem {
static List<Shape> sList = new ArrayList<Shape>();
static float square = 0;
public static void main(String[] args) {
while(true)
{
show();
}
}
public static void show()
{
print("***圖形類對象的管理***", 1);
print("-------------------", 1);
print("1.創建一個圓", 1);
print("2.創建一個矩形", 1);
print("3.創建一個長方體", 1);
print("4.創建一個圓柱體", 1);
print("5.顯示已經創建的圖形", 1);
print("6.求各圖形的面積之和", 1);
print("7.退出系統", 1);
print("請選擇操作項:", 2);
Scanner scanner = new Scanner(System.in);
int action = 0;
try {
action = scanner.nextInt();
} catch (RuntimeException e) {
print("輸入必須是整數!", 1);
}
float pi = 3.14f;
String name = "";
float x;
float y;
float h;
float r;
switch (action) {
case 1:
print("請輸入圓的名稱:", 2);
name = scanner.next();
print("請輸入圓心x坐標:", 2);
x = scanner.nextInt();
print("請輸入圓心y坐標:", 2);
y = scanner.nextInt();
print("請輸入圓的半徑:", 2);
r = scanner.nextFloat();
Circle c = new Circle(name,x,y,r);
sList.add(c);
square += pi*r*r;
break;
case 2:
print("請輸入矩形的名稱:", 2);
name = scanner.next();
print("請輸入矩形的長:", 2);
x = scanner.nextInt();
print("請輸入矩形的寬:", 2);
y = scanner.nextInt();
Rectangle ra = new Rectangle(name,x,y);
sList.add(ra);
square += x*y;
break;
case 3:
print("請輸入長方體的名稱:", 2);
name = scanner.next();
print("請輸入長方體的長:", 2);
x = scanner.nextInt();
print("請輸入長方體的寬:", 2);
y = scanner.nextInt();
print("請輸入長方體的高:", 2);
h = scanner.nextInt();
Cube cb = new Cube(name,x,y,h);
sList.add(cb);
square += 2*x*y + 2*x*h +2*h*y;
break;
case 4:
print("請輸入圓柱體的名稱:", 2);
name = scanner.next();
print("請輸入圓柱體的底圓心x坐標:", 2);
x = scanner.nextInt();
print("請輸入圓柱體的底圓心y坐標:", 2);
y = scanner.nextInt();
print("請輸入圓柱體的底圓的半徑:", 2);
r = scanner.nextFloat();
print("請輸入圓柱體的高:", 2);
h = scanner.nextFloat();
Clinder ccd = new Clinder(name,x,y,r,h);
sList.add(ccd);
square += 2*pi*r*h;
break;
case 5:
for(Shape s : sList)
{
print(s.getName(), 1);
}
break;
case 6:
print("各圖形的面積和為:"+square, 1);
break;
case 7:
print("系統退出...", 1);
System.exit(0);
break;
default:
print("無效的操作項...", 1);
break;
}
}
public static void print(String info,int flag)
{
if(flag == 1)
{
System.out.println(info);
}
else
{
System.out.print(info);
}
}
}
=========================華麗的分割線=============================
圖形Shape類:
public abstract class Shape {
private String name;
public Shape(String name)
{
this.name = name;
}
public abstract void show();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
=========================華麗的分割線=============================
矩形類:
public class Rectangle extends Shape {
private float x;
private float y;
public Rectangle(String name,float x,float y) {
super(name);
this.x = x;
this.y = y;
}
@Override
public void show() {
System.out.println(this.getName());
}
public String toString()
{
return this.getName() + ":長=" + this.x + " 寬=" + this.y;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
}
}
=========================華麗的分割線=============================
長方體為:
public class Cube extends Rectangle {
private float h;
public Cube(String name,float x,float y,float h) {
super(name,x,y);
this.h = h;
}
@Override
public void show() {
System.out.println(this.getName());
}
public String toString()
{
return this.getName() + ":長=" + this.getX() + " 寬=" + this.getY() + " 高=" + this.h;
}
}
=========================華麗的分割線=============================
圓:
public class Circle extends Shape {
private float x;
private float y;
private float r;
public Circle(String name, float x, float y, float r) {
super(name);
this.x = x;
this.y = y;
this.r = r;
}
@Override
public void show() {
System.out.println(this.getName());
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public float getR() {
return r;
}
public void setR(float r) {
this.r = r;
}
public String toString()
{
return this.getName() + ":圓心x=" + this.x + " y=" + this.y + " 半徑=" + this.r;
}
}
=========================華麗的分割線=============================
圓柱體:
public class Clinder extends Circle {
private float h;
public Clinder(String name, float x, float y, float r,float h) {
super(name, x, y, r);
this.h = h;
}
@Override
public void show() {
System.out.println(this.getName());
}
public String toString()
{
return this.getName() + ":圓心x=" + this.getX() + " y=" + this.getY() + " 半徑=" + this.getR() + " 高=" + this.h;
}
}
程序運行效果圖:
具體請自行運行程序操作,細節部分需自己完善。
有問題歡迎提問,滿意請點贊!有分就加點,謝謝!
⑶ 如何用java定義方法求圓的面積
定義一個方法,參數為半徑r,返回面積。代碼如下:
publicclassTest(){
publicdoublegetArea(doubler){
doublearea=Math.PI*r*r;
returnarea;
}
publicstaticvoidmain(String[]args){
Testt=newTest();
Scannerscanner=newScanner(System.in);
System.out.print("請輸入半徑:");
Stringstr=scanner.nextLine();
doubler=Double.parseDouble(str);//將輸入的半徑轉為double類型
doublearea=t.getArea(r);
System.out.print("圓的面積為:"+area);
}
}
⑷ java中怎麼求三角形的面積
我記得數學中有個公式的,利用三條邊來求的
已知三角形三邊a,b,c,半周長p,則S= √[p(p - a)(p - b)(p - c)] (海倫公式)(p=(a+b+c)/2)
⑸ java求幾何圖形面積
代碼如下:
abstractclassGeometry{
abstractdoublegetArea();
}
//三角形
classTriangleextendsGeometry{
//邊
privatedoublea;
//邊
privatedoubleb;
//邊
privatedoublec;
publicTriangle(doublea,doubleb,doublec){
this.a=a;
this.b=b;
this.c=c;
}
@Override
doublegetArea(){
doublep=(a+b+c)/2;
returnMath.sqrt(p*(p-a)*(p-b)*(p-c));
}
}
//圓
classCircleextendsGeometry{
//半徑
privatedoubler;
publicCircle(doubler){
this.r=r;
}
@Override
doublegetArea(){
returnMath.PI*r*r;
}
}
//梯形
classLadderextendsGeometry{
//上底
privatedoublea;
//下底
privatedoubleb;
//高
privatedoubleh;
publicLadder(doublea,doubleb,doubleh){
this.a=a;
this.b=b;
this.h=h;
}
@Override
doublegetArea(){
return(a+b)*h/2;
}
}
classTest{
doublecomputeGeometryArea(Geometryg){
returng.getArea();
}
}
publicclassDemo{
publicstaticvoidmain(String[]args){
Testtest=newTest();
Triangletriangle=newTriangle(3,4,6);
System.out.println("三角形面積:"+test.computeGeometryArea(triangle));
Circlecircle=newCircle(10);
System.out.println("圓面積:"+test.computeGeometryArea(circle));
Ladderladder=newLadder(10,20,15);
System.out.println("梯形面積:"+test.computeGeometryArea(ladder));
}
}
運行結果:
⑹ Java 求面積
因為你寫了 getArea2=1/2*t2*Math.sqrt((t2*t2)-(1/4*t2*t2));
這里1 / 2兩個都是整數,計算等於0。
1 / 4也等於0。
改成
getArea2 = 0.5*t2*Math.sqrt((t2*t2)-(0.25*t2*t2));
就可以了
⑺ 用java編寫程序求三角形的面積
構成三角形的條件,要根據任意兩邊和要大於第三邊。代碼如下:
import java.util.Scanner;
public class woo {
public static void main(String args[]) {
Scanner scan =new Scanner(System.in);
System.out.println("輸入三角形的三邊");
int a = scan.nextByte();
int b = scan.nextByte();
int c = scan.nextByte();
float s =(a+b+c)/2f;
float S = (float) Math.sqrt(s*(s-a)*(s-b)*(s-c));
if (a+b>c && b+c>a && a+c>b){
System.out.println(S);
}
else{
System.out.println("不構成三角形");
}
}
}
(7)java求面積擴展閱讀:
三角形是由同一平面內不在同一直線上的三條線段『首尾』順次連接所組成的封閉圖形,在數學、建築學有應用。
常見的三角形按邊分有普通三角形(三條邊都不相等),等腰三角(腰與底不等的等腰三角形、腰與底相等的等腰三角形即等邊三角形);按角分有直角三角形、銳角三角形、鈍角三角形等,其中銳角三角形和鈍角三角形統稱斜三角形。
三角形在平面上三角形的內角和等於180°,在平面上三角形的外角和等於360° ,在平面上三角形的外角等於與其不相鄰的兩個內角之和。
⑻ 用java計算長方形的面積
// 父類Sharp 如下:public abstract class Sharp {
// 定義面積方法
public abstract double area();
} // 子類RectSharp 繼承父類:public class RectSharp extends Sharp { private double width, heigth; public RectSharp(double width, double heigth) {
this.width = width;
this.heigth = heigth;
} /**
* 求出面積
*/
@Override
public double area() {
return width * heigth;
} public static void main(String[] args) {
// 創建一個長方形類
RectSharp r = new RectSharp(100, 50.2);
// 計算面積
double area = r.area();
System.out.println("該長方形的面積為:" + area);
}
}
⑼ java面積計算
MyExam4.java
class MyExam4{
double a,b,c;
MyExam4(){}
MyExam4(double aa,double bb,double cc){}
a=aa;
b=bb;
c=cc;
double returnABC()
{
double s==(a+b+c)/2;
return (s(s-a)(s-b)(s-c))*1/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());
}
}