日期:2014-05-17 浏览次数:20950 次
/* * 程序头部注释开始 * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称:判断该物体是否会在水中下沉 * 作 者:薛广晨 * 完成日期:2011 年 10 月 14 日 * 版 本号:x1.0 * 对任务及求解方法的描述部分 * 输入描述: * 问题描述:编写C#控制台应用程序,在其中创建物体类PhysicalObject, * 通过其私有字段来存放重量和体积,通过公有属性来访问其重量、体积、密度, * 并通过公有方法来判断该物体是否会在水中下沉。 * 程序输出: * 程序头部的注释结束 */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace xue1 { class Program { static void Main(string[] args) { PhysicalObject pl1 = new PhysicalObject(); pl1.setWeight(10); pl1.setVolume(8); pl1.calculateDensity(); pl1.isSink(); PhysicalObject pl2 = new PhysicalObject(20, 25); pl2.isSink(); Console.ReadKey(); } } class PhysicalObject { private double weight;//重量 private double volume;//体积 private double density;//密度 public PhysicalObject() { this.weight = 0; this.volume = 0; this.weight = 0; } public PhysicalObject(double weight, double volume) { this.weight = weight; this.volume = volume; this.density = weight / volume; } public double getWeight() { return this.weight; } public double getVolume() { return this.volume; } public double getDensity() { return this.density = this.weight / this.volume; } public void setWeight(double weight) { this.weight = weight; } public void setVolume(double volume) { this.volume = volume; } public void calculateDensity() { this.density = this.weight / this.volume; } public void isSink() { if (this.density > 1) { Console.WriteLine("物体的重量为{0}克,体积为{1}立方厘米,密度为{2}克/立方厘米,此物体会在水中下沉。", this.weight, this.volume, this.density); } else { Console.WriteLine("物体的重量为{0}克,体积为{1}立方厘米,密度为{2}克/立方厘米,此物体不会在水中下沉。", this.weight, this.volume, this.density); } } } }
运行结果: