灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:4447回复:0

[系统教程]JAVA编程常见面试题及答案12

楼主#
更多 发布于:2012-09-08 08:09


23. 创建一个桌子(Table)类,该类中有桌子名称、重量、桌面宽度、长度和桌子高度属性,以及以下几个方法:
(1) 构造方法:初始化所有成员变量。
(2)  area( ):计算桌面的面积。
(3)  display( ):在屏幕上输出所有成员变量的值。
(4)  changeWeight(int w):改变桌子重量。
在测试类的main()方法中实现创建一个桌子对象,计算桌面的面积,改变桌子重量,并在屏幕上输出所有桌子属性的值。

package com.test;
public class Table {
String name;//名称
double weight;//重量
double width;//宽
double longth;//长
double height;//高
//构造方法
public Table(String name, double weight, double width, double longth,
double height) {
super();
this.name = name;
this.weight = weight;
this.width = width;
this.longth = longth;
this.height = height;
}
//计算桌面的面积
public void area(){
System.out.println(“桌子面积是“+longth*width);
}
// 在屏幕上输出所有数据成员的值
public void display(){
System.out.println(“桌子名称:”+name+”\n”+”重量:”+weight+”\n”+”宽:”+width+”\n”+”长:”+longth+”\n高:”+height);
}
//改变桌子重量的方法
public void changeWeight(int i){
this.weight=i;
System.out.println(“面积改为“+this.weight);
}
public static void main(String[] args) {
Table table=new Table(“红木桌“,100.5,3.2,2.3,1.5);
System.out.println(“创建一个桌子对象,属性如下“);
table.display();
table.area();
table.changeWeight(100);
System.out.println(“更改重量后,属性如下“);
table.display();
}
}

喜欢0 评分0
游客

返回顶部