日期:2014-05-17  浏览次数:21095 次

C#--第七周实验--任务1--创建物体类PhysicalObject,通过其私有字段来存放重量和体积,通过公有方法判断该物体是否会在水中下沉
/* (程序头部注释开始)
 * 程序的版权和版本声明部分
 * Copyright (c) 2011, 烟台大学计算机学院学生 
 * All rights reserved.
 * 文件名称:创建物体类PhysicalObject
 * 作 者: 雷恒鑫 
 * 完成日期: 2012 年10 月08 日
 * 版 本 号: V1.0 
 * 对任务及求解方法的描述部分
 * 输入描述:在其中创建物体类PhysicalObject,通过其私有字段来存放重量和体积.
 * 输入描述:通过公有属性来访问其重量、体积、密度,并通过公有方法来判断该物体是否会在水中下沉。
 * 问题描述:
 * 程序输出:
 * 程序头部的注释结束
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Five_week
{
    class Program
    {
        static void Main(string[] args)
        {
            PhysicalObject ph1 = new PhysicalObject();
            ph1.set_weight(50);
            ph1.set_volume(20);
            ph1.Calculation_Density();
            ph1.panduan();
            PhysicalObject ph2 = new PhysicalObject(100, 50);
            ph2.panduan();
            Console.ReadKey();
        }
    }
    class PhysicalObject
    {
        private double volume;
        private double weight;
        private double Density;
        public PhysicalObject()
        {
            volume = 0;
            weight = 0;
            Density = 0;
        }
        public PhysicalObject(double volume1, double weight1)
        {
            volume = volume1;
            weight = weight1;
            Density = weight / volume;
        }
        public void set_volume(double volume)
        {
            this.volume = volume;
        }
        public void set_weight(double weight)
        {
            this.weight = weight;
        }
        public void set_Density(double Density)
        {
            this.Density = Density;
        }
        public double get_volume()
        {
            return volume;
        }
        public double get_weight()
        {
            return weight;
        }
        public double get_Density()
        {
            return Density;
        }
        public void Calculation_Density()
        {
            Density = weight / volume;
        }
        public void panduan()
        {
            if (Density > 1)
            {
                Console.WriteLine("物体的重量为{0}克,体积为{1}立方厘米,密度为{2}克/立方厘米,此物体会在水中下沉。", volume, weight, Density);
            }
            else
            {
                Console.WriteLine("物体的重量为{0}克,体积为{1}立方厘米,密度为{2}克/立方厘米,此物体不会在水中下沉。", volume, weight, Density);
            }
        }

    }
}


运行结果: