當前位置:首頁 » 編程軟體 » 編程矩形類

編程矩形類

發布時間: 2022-05-11 10:48:27

① C++編程問題:編寫矩形類,要求可以提示用戶輸入矩形的長度和寬度,並顯示該矩形的長度、寬度和面積。

按照你的要求編寫的矩形類的C++程序如下

#include<iostream>

usingnamespacestd;

classRectangle{

private:

floatlength;

floatwidth;

public:

Rectangle(floatlen,floatwid);

floatarea();

};

Rectangle::Rectangle(floatlen,floatwid){

length=len;

width=wid;

}

floatRectangle::area(){

returnlength*width;

}

intmain(){

floatlength,width;

cout<<"請輸入矩形的長度和寬度:"<<endl;

cin>>length>>width;

Rectanglerect(length,width);

cout<<"矩形的長度是"<<length<<endl;

cout<<"矩形的寬度是"<<width<<endl;

cout<<"矩形的面積是"<<rect.area()<<endl;

return0;

}

② JAVA 考試編程題如何編寫一個矩形類

用這個肯定可以
public class Rectangle {

private float width;//長
private float height;//寬

public Rectangle(float width, float height){
this.width = width;
this.height = height;
}

//計算周長= (長+寬)*2
public float calcCircle(){
return (width + height) * 2;
}

//計算面積 長*寬
public float calcArea(){
return width * height;
}

public float getHeight() {
return height;
}

public void setHeight(float height) {
this.height = height;
}

public float getWidth() {
return width;
}

public void setWidth(float width) {
this.width = width;
}

}

③ c++程序設計 設計一個矩形類(rect),

#include<iostream>
using namespace std;

class rect
{
public:
rect(int width, int length) : width(width),length(length){}
int Area()
{
return length * width;
}
int Perimeter()
{
return 2 * (length + width);
}
friend int add(rect &r1, rect &r2);
private:
int length;
int width;
};

int add(rect &r1,rect &r2)
{
return r1.Area() + r2.Area();
}

int main()
{
rect rect1(2,4), rect2(4,6);
cout<<"rect1的面積是"<<rect1.Area()<<endl;
cout<<"rect1的周長是"<<rect1.Perimeter()<<endl;
cout<<"rect2的面積是"<<rect2.Area()<<endl;
cout<<"rect2的周長是"<<rect2.Perimeter()<<endl;
cout<<"rect1 和rect2的面積之和為"<<add(rect1,rect2)<<endl;
return 0;
}

運行結果
rect1的面積是8
rect1的周長是12
rect2的面積是24
rect2的周長是20
rect1 和rect2的面積之和為32
Press any key to continue

④ C++編程:編寫一個矩形rectange類。。。

程序比較長,分開發給你,按照順序粘貼到一起就行了
類定義:
#include
#include
#include
#define
n
100
using
namespace
std;
int
num_of_students=0;
class
student
{
private:
string
name;
string
no;
string
sex;
int
age;
float
score;
int
squad;
public:
void
get_info();
void
show_info();
void
show_ord_info();
string
&get_name()
{return
name;}
string
&get_no()
{return
no;}
float
get_score()
{return
score;}
int
get_squad()
{return
squad;}
}
st[n],t;
void
student::get_info()
{
cout<<"姓名:";
cin>>name;
cout<<"性別:";
cin>>sex;
cout<<"年齡:";
cin>>age;
cout<<"學號:";
cin>>no;
cout<<"班級:";
cin>>squad;
cout<<"成績:";
cin>>score;
}
void
student::show_info()
{
cout<<"---------------"<
評論
0
0
載入更多

⑤ C++編寫程序,建立矩形圓形類。

CTest1 #include <iostream>#include <cmath>using namespace std;int main(){ while (true) { cout << "1.圓形" << endl; cout << "2.長方形" << endl; cout << "3.直角三角形" << endl; cout << "4.退出" << endl; int choice; cin >> choice; system("cls"); if (choice == 4) break; switch (choice) { case 1:{ double r = 0; cout << "請輸入圓形的半徑:"; cin >> r; cout << "圓形的面積:" << 3.14 * r * r << endl << "周長:" << 3.14 * 2 * r; }break; case 2:{ double l = 0.0, w = 0.0; cout << "請輸入長方形的長和寬"<<endl; cout << "長:"; cin >> l; cout << "寬:"; cin >> w; cout << "長方形的面積:" << l * w << endl << "周長:" << 2 * (l + w); }break; case 3:{ double b = 0.0, h = 0.0; cout << "請輸入直角三角形的長和寬" << endl; cout << "底:"; cin >> b; cout << "高:"; cin >> h; cout << "長方形的面積:" << 0.5 * b * h << endl << "周長:" << (b + h + sqrt(b*b + h*h)); }break; default:break; } getchar(); getchar(); system("cls"); } //getchar();getchar(); system("pause"); return 0;} 執行結果: CTest2 #include <iostream>#include <cmath>using namespace std;class Circle{public: Circle(double r) : radius(r){} double area(){ return 3.14*radius*radius; } double girth(){ return 3.14 * 2 * radius; }private: double radius;};class Rect{public: Rect(double l, double w) : length(l), width(w){} double area(){ return length * width;} double girth(){ return 2 * (length + width); }private: double length; double width;};class Tri{public: Tri(double b, double h) : bottom(b), height(h){} double area(){ return 0.5*bottom*height; } double girth(){ return (bottom + height + sqrt(bottom*bottom + height*height)); }private: double bottom; double height;};int main(){ while (true) { cout << "1.圓形" << endl; cout << "2.長方形" << endl; cout << "3.直角三角形" << endl; cout << "4.退出" << endl; int choice; cin >> choice; system("cls"); if (choice == 4) break; switch (choice) { case 1:{ double r = 0; cout << "請輸入圓形的半徑:"; cin >> r; Circle circle(r); cout << "圓形的面積:" << circle.area() << endl << "周長:" << circle.girth(); }break; case 2:{ double l = 0.0, w = 0.0; cout << "請輸入長方形的長和寬"<<endl; cout << "長:"; cin >> l; cout << "寬:"; cin >> w; Rect rect(l, w); cout << "長方形的面積:" << rect.area() << endl << "周長:" << rect.girth(); }break; case 3:{ double b = 0.0, h = 0.0; cout << "請輸入直角三角形的長和寬" << endl; cout << "底:"; cin >> b; cout << "高:"; cin >> h; Tri tri(b, h); cout << "長方形的面積:" << tri.area() << endl << "周長:" << tri.girth(); }break; default:break; } getchar(); getchar(); system("cls"); } //getchar();getchar(); system("pause"); return 0;} 執行結果:

⑥ 用Java編程實現矩形類,其中包括計算矩形周長和面積的方法

英語不好,就拿拼音吧。

public class Juxing {
private double a;
private double b;

// 計算周長
public double zhouchang() {
return 2 * (a + b);
}

// 計算面積
public double mianji() {
return a * b;
}

public double getA() {
return a;
}

public void setA(double a) {
this.a = a;
}

public double getB() {
return b;
}

public void setB(double b) {
this.b = b;
}
}

⑦ 編程實現矩形類,其中包括計算矩形周長和面積的方法,並測試方法的正確性,用JAVA編寫

/**
* 矩形類
*/
public class Rectangle {
private double length; //長
private double width; //寬

public double getLength() {
return length;
}

public void setLength(double length) {
this.length = length;
}

public double getWidth() {
return width;
}

public void setWidth(double width) {
this.width = width;
}

public Rectangle() {
}

public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}

/**
* 面積計算方法
* @param length 長
* @param width 寬
* @return 面積
*/
public double area(){
return length*width;
}

/**
* 周長計算方法
* @param length 長
* @param width 寬
* @return 周長
*/
public double girth(){
return 2*(length+width);
}

public static void main(String[] args) {
//定義矩形,長為4.5,寬為3
Rectangle rectangle=new Rectangle(4.5,3);

System.out.println("矩形面積是:"+rectangle.area());
System.out.println("矩形周長是:"+rectangle.girth());
}
}

熱點內容
app什麼情況下找不到伺服器 發布:2025-05-12 15:46:25 瀏覽:714
php跳過if 發布:2025-05-12 15:34:29 瀏覽:467
不定時演算法 發布:2025-05-12 15:30:16 瀏覽:131
c語言延時1ms程序 發布:2025-05-12 15:01:30 瀏覽:166
動物園靈長類動物配置什麼植物 發布:2025-05-12 14:49:59 瀏覽:735
wifi密碼設置什麼好 發布:2025-05-12 14:49:17 瀏覽:148
三位數乘兩位數速演算法 發布:2025-05-12 13:05:48 瀏覽:397
暴風影音緩存在哪裡 發布:2025-05-12 12:42:03 瀏覽:542
access資料庫exe 發布:2025-05-12 12:39:04 瀏覽:630
五開的配置是什麼 發布:2025-05-12 12:36:37 瀏覽:365