1、定義長方形類,含:
屬性:寬、高(整型);
方法:求周長、面積;
構造方法3個:(1)無參——寬、高默認值為1;(2)1個參數——寬、高均為參數值;(3)2個參數——寬、高各為參數值。
要求:進行測試。
代碼如下:
長方形的類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package
Test1;
public
class
Rectangle {
//定義長寬屬性
private
int
iWidth;
private
int
iHeight;
//構造器1
public
Rectangle(){
this
.iHeight =
1
;
this
.iWidth =
1
;
}
//構造器2
public
Rectangle(
int
iIndex){
this
.iWidth = iIndex;
this
.iHeight = iIndex;
}
//構造器3
public
Rectangle(
int
iWidth,
int
iHeight){
this
.iHeight = iHeight;
this
.iWidth = iWidth;
}
//求周長
public
int
getLength(){
return
2
*(
this
.iHeight+
this
.iWidth);
}
//求面積
public
int
getSquare(){
return
this
.iHeight*
this
.iWidth;
}
}
測試類:
package
Test1;
public
class
Test {
/**
* @param args
*/
public
static
void
main(String[] args) {
// TODO Auto-generated method stub
Rectangle oRec1 =
new
Rectangle();
System.out.println(
"默認長方形的周長為:"
+oRec1.getLength());
System.out.println(
"默認長方形的面積為:"
+oRec1.getSquare());
Rectangle oRec2 =
new
Rectangle(
2
);
System.out.println(
"一個參數長方形的周長為:"
+oRec2.getLength());
System.out.println(
"一個參數長方形的面積為:"
+oRec2.getSquare());
Rectangle oRec3 =
new
Rectangle(
2
,
3
);
System.out.println(
"兩個參數長方形的周長為:"
+oRec3.getLength());
System.out.println(
"兩個參數長方形的面積為:"
+oRec3.getSquare());
}
}
|
運行結果:
默認長方形的周長為:4
默認長方形的面積為:1
一個參數長方形的周長為:8
一個參數長方形的面積為:4
兩個參數長方形的周長為:10
兩個參數長方形的面積為:6
2、定義圓類,它有一個變量radius(半徑)。從鍵盤輸入數據,通過構造方法傳遞給radius,編程計算並輸出圓的周長和面積(確保輸入的數據不為負數)。
要求:進行測試。
代碼如下:
圓的類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package
Test2;
public
class
Circle {
private
double
radius;
public
Circle(
double
dRadius){
this
.radius = dRadius;
}
public
double
getLength(){
return
2
*Math.PI*
this
.radius;
}
public
double
getArea()
{
return
Math.PI*
this
.radius*
this
.radius;
}
}
測試類:
package
Test2;
import
java.util.Scanner;
public
class
Test {
/**
* @param args
*/
public
static
void
main(String[] args) {
// TODO Auto-generated method stub
double
iRadius;
Scanner sc =
new
Scanner(System.in);
System.out.print(
"請輸入圓的半徑:"
);
iRadius = sc.nextDouble();
if
(iRadius <
0
){
System.out.println(
"你輸入的半徑有誤!"
);
}
else
{
Circle oCir =
new
Circle(iRadius);
System.out.println(
"圓的周長為:"
+oCir.getLength());
System.out.println(
"圓的面積為:"
+oCir.getArea());
}
}
}
|
運行結果:
正數時:
請輸入圓的半徑:1
圓的周長為:6.283185307179586
圓的面積為:3.141592653589793
負數時:
請輸入圓的半徑:-1
你輸入的半徑有誤!
3、定義長方體類,定義求底面積、體積的方法。(可利用上述定義的長方形類)
要求:進行測試。
代碼如下:
長方體類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
package
Test3;
import
Test1.Rectangle;
public
class
RecV
extends
Rectangle{
int
iLong;
public
RecV(
int
iWidth,
int
iHeight,
int
iLong){
super
(iWidth,iHeight);
this
.iLong = iLong;
}
public
int
getBtmArea(){
return
this
.getSquare();
}
public
int
getVolume(){
return
this
.getSquare()*
this
.iLong;
}
}
測試類:
package
Test3;
public
class
Test {
/**
* @param args
*/
public
static
void
main(String[] args) {
// TODO Auto-generated method stub
RecV oRecV =
new
RecV(
1
,
2
,
3
);
System.out.println(
"長方體的底面積為:"
+oRecV.getBtmArea());
System.out.println(
"長方體的體積為:"
+oRecV.getVolume());
}
}
|
運行結果:
長方體的底面積為:2
長方體的體積為:6
4、定義球類,定義求體積的方法。(可利用上述定義的圓類)
要求:進行測試。
代碼如下:
球的類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package
Test4;
import
Test2.Circle;
public
class
Bool
extends
Circle{
public
Bool(
double
dRadius) {
super
(dRadius);
// TODO Auto-generated constructor stub
}
public
double
getBoolVolume(){
return
(
4
/
3
)*Math.PI*Math.pow(
this
.radius,
3.0
);
}
}
測試類:
package
Test4;
public
class
Test {
/**
* @param args
*/
public
static
void
main(String[] args) {
// TODO Auto-generated method stub
Bool oBool =
new
Bool(
1
);
System.out.println(
"球的體積為:"
+oBool.getBoolVolume());
}
}
|
運行結果:
球的體積為:3.141592653589793
5、定義一個計算器類,包括加、減、乘、除運算。
要求:進行測試。
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package
Test5;
public
class
Calculator {
private
int
iFirstNum;
private
int
iSecondNum;
public
Calculator(
int
iFirst,
int
iSecond){
this
.iFirstNum = iFirst;
this
.iSecondNum = iSecond;
}
public
int
getAdd(){
return
this
.iFirstNum +
this
.iSecondNum;
}
public
int
getSub(){
return
this
.iFirstNum -
this
.iSecondNum;
}
public
int
getMul(){
return
this
.iFirstNum *
this
.iSecondNum;
}
public
void
getDev(){
if
(
this
.iSecondNum ==
0
){
System.out.print(
"分子不能為零!"
);
}
else
{
System.out.print(
this
.iFirstNum/
this
.iSecondNum);
}
}
}
測試類:
package
Test5;
public
class
Test {
public
static
void
main(String[] args) {
// TODO Auto-generated method stub
Calculator oCal =
new
Calculator(
4
,
2
);
System.out.println(
"兩數之和為 :"
+oCal.getAdd());
System.out.println(
"兩數之差為:"
+oCal.getSub());
System.out.println(
"兩數之積為:"
+oCal.getMul());
System.out.print(
"兩數之商為:"
);
oCal.getDev();
}
}
|
運行結果:
兩數之和為 :6
兩數之差為:2
兩數之積為:8
兩數之商為:2
7、編寫程序使用復數類Complex驗證兩個復數 2+2i 和3+3i 相加產生一個新的復數5+5i 。復數類Complex滿足如下要求:
(1)屬性:RealPart : int型,代表復數的實數部分;ImaginPart : int型,代表復數的虛數部分
(2)方法:
Complex( ) : 構造方法,將復數的實部和虛部都置0
Complex( int r , int i ) : 構造方法,形參 r 為實部的初值,i為虛部的初值。
Complex complexAdd(Complex a) : 將當前復數對象與形參復數對象相加,所得的結果仍是一個復數值,返回給此方法的調用者。
String toString( ) : 把當前復數對象的實部、虛部組合成 a+bi 的字符串形式,其中a 和 b分別為實部和虛部的數據。
(3)進行測試。
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
package
Test7;
public
class
Complex {
private
int
iRealPart;
private
int
iImaginPart;
public
Complex(){
iRealPart =
0
;
iImaginPart =
0
;
}
public
Complex(
int
r,
int
i){
iRealPart = r;
iImaginPart = i;
}
public
Complex complexAdd( Complex a ){
this
.iRealPart += a.iRealPart;
this
.iImaginPart += a.iImaginPart;
return
this
;
}
public
String toString(){
return
this
.iRealPart+
"+"
+
this
.iImaginPart+
"i"
;
}
}
測試類:
package
Test7;
public
class
Test {
/**
* @param args
*/
public
static
void
main(String[] args) {
// TODO Auto-generated method stub
//復數實例化
Complex oFirstNum =
new
Complex();
Complex oFirstNum1 =
new
Complex(
2
,
3
);
Complex oSecondNum =
new
Complex(
3
,
4
);
oFirstNum.complexAdd(oSecondNum);
System.out.println(
"1。復數之和為:"
+oFirstNum.toString());
oFirstNum1.complexAdd(oSecondNum);
System.out.println(
"2.復數之和為:"
+oFirstNum1.toString());
}
}
|
運行結果:
1。復數之和為:3+4i
2.復數之和為:5+7i
8、試編寫Java代碼實現一個計數器類Computer其中包括:
域value :用來保存計數器的當前值;
方法increment(): 計數器加一;
方法decrement() :計數器減一;
方法reset():計數器清零。
請編寫並調試程序。
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
package
Test8;
public
class
Computer {
private
int
value;
public
Computer(){
this
.value =
0
;
}
public
void
increment(){
this
.value++;
}
public
void
decrement(){
this
.value--;
}
public
void
reset(){
this
.value =
0
;
}
public
int
getValue(){
return
this
.value;
}
}
測試類:
package
Test8;
public
class
Test {
/**
* @param args
*/
public
static
void
main(String[] args) {
// TODO Auto-generated method stub
Computer oCount =
new
Computer();
System.out.println(
"計數器的初始值為:"
+ oCount.getValue());
oCount.increment();
oCount.increment();
oCount.increment();
System.out.println(
"加三后的值為:"
+ oCount.getValue());
oCount.decrement();
System.out.println(
"減一后的值為:"
+ oCount.getValue());
oCount.reset();
System.out.println(
"初始化后的值為:"
+ oCount.getValue());
}
}
|
運行結果:
計數器的初始值為:0
加三后的值為:3
減一后的值為:2
初始化后的值為:0