a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 112|回复: 0

[考试试题] 2012JAVA常见试题及答案汇总(19)

[复制链接]
发表于 2012-8-4 12:33:16 | 显示全部楼层 |阅读模式
创建一个桌子(Table)类,该类中有桌子名称、重量、桌面宽度、长度和桌子高度属性,以及以下几个方法:4 r- W- S* S; ~
(1) 构造方法:初始化所有成员变量。1 f. M; @0 ?5 [
(2)  area( ):计算桌面的面积。' {7 D3 Z) O/ M
(3)  display( ):在屏幕上输出所有成员变量的值。5 o- m2 y; {1 `9 |, W1 L. H* U
(4)  changeWeight(int w):改变桌子重量。' X3 F% v: m. m. l( _
在测试类的main()方法中实现创建一个桌子对象,计算桌面的面积,改变桌子重量,并在屏幕上输出所有桌子属性的值。
& n% z4 y9 H/ i- r; p1 x
; t& W1 p; b5 g& f6 ?" x+ {package com.test;
2 U1 b  \' S( ^% C+ ]7 opublic class Table {$ G3 x5 w! l  ?2 G& o
String name;//名称
( N! P$ V. G) Qdouble weight;//重量
  f+ g, f) k) D1 R0 B! zdouble width;//宽" o3 T. ?5 x$ }0 W
double longth;//长
* p( u0 @. p/ q- Z3 R7 ~$ @2 N6 ydouble height;//高; O) e9 z. Z3 n9 k
//构造方法7 Z; c+ M3 }' V( v3 D5 a6 @. I* q- X
public Table(String name, double weight, double width, double longth,3 g1 \0 y- m# L2 V/ p+ j' Z; Z
double height) {
, t5 y& t! e3 ?/ Nsuper();* y$ S; D8 L8 B% {1 m3 o' u0 t: x
this.name = name;
: y$ w' n4 }2 G% rthis.weight = weight;
6 H( ^# u/ a8 D$ B$ A' s- n3 mthis.width = width;5 P$ g- F* x! c
this.longth = longth;
* S' {2 g1 V  @# hthis.height = height;) q0 M1 f* m; u* F7 p0 H( c
}8 _& a5 w5 w7 m* @$ s/ h) x
//计算桌面的面积
: s# p4 T! I- E  A* W4 opublic void area(){
( B, s" Q$ U- l2 f7 _System.out.println(“桌子面积是“+longth*width);
" a4 J9 _. s# p( d0 k}
* W1 q0 P7 K6 |7 X5 Z: F5 V// 在屏幕上输出所有数据成员的值* Y/ C# [) b. \* `& m1 @' l1 V' K' `
public void display(){
3 ^3 y3 [- l/ _System.out.println(“桌子名称:”+name+”\n”+”重量:”+weight+”\n”+”宽:”+width+”\n”+”长:”+longth+”\n高:”+height);, E5 P* Z" C4 i3 Y( B/ M
}
) h  n/ v, K' E# O6 A//改变桌子重量的方法
8 c! P+ C% V8 f+ a: qpublic void changeWeight(int i){# f2 u' F: r+ J. H' y+ `* E2 F
this.weight=i;, S* G! _( ]# A6 I2 }6 |# p
System.out.println(“面积改为“+this.weight);8 c1 M; K4 p4 y, k  V9 j0 k
}
3 m8 L3 ^, ]5 i  A3 f% _- Gpublic static void main(String[] args) {. I0 K! i2 ^1 N4 N. R
Table table=new Table(“红木桌“,100.5,3.2,2.3,1.5);
6 Q  G" b9 O& M1 m0 j: e; C' X, ASystem.out.println(“创建一个桌子对象,属性如下“);* W2 f( R0 p. e0 M' L
table.display();
+ i. _1 P; _' qtable.area();
4 M* i7 i/ z) a" y6 K; Z) Ptable.changeWeight(100);
1 {4 K' s; q( BSystem.out.println(“更改重量后,属性如下“);
( x$ i+ \2 ^# i: P. O6 G% Ltable.display();
* w8 t9 A8 Q# W$ m; ~- w& \}
- Y1 P, U+ }: W; d}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-5-6 02:40 , Processed in 0.242356 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表