QUESTION 1 Writing and using classes (guided)
In this question, you will create three classes (so you will have three .java files):
1) a class called Cuboid (in Cuboid.java) that defines a three-dimensional geometric shape
called rectangular cuboid (as shown on the next page)
2) a class called Cone (in Cone.java) that defines a three-dimensional geometric shape called
right circular cone (as shown on the next page)
3) a driver class called Driver (in Driver.java) that drives the creation and manipulation of
objects from the two classes mentioned earlier.
The instance variables in the Cuboid class should represent the cuboid's three dimensions: length,
depth, and height (all double values). The instance variables in the Cone class should represent the
cone's height and the radius of its base (all double values). In addition, the Cuboid class and the
Cone class should each include the following:
• A constructor that receives no formal parameters and initializes all instance variables to 1.0.
• A constructor that receives the dimensions of the object to be created as formal parameters
and initializes all instance variables using these values.
• Getter methods: one getter for each of the instance variables.
• Instance method volume that calculates and returns the volume of the object (a double
value).
• Instance method setDimensions that changes the values of all instance variables of the
object to new values received as formal parameters. The method should not return any value.
The method should update instance variables only if the received new values are all positive,
and it should print a warning message otherwise.
• Instance method price that receives one formal parameter: a price of a material per unit of
volume – a double value. The method should calculate and return the price of the object (a
double value) assuming that the object is made out of that material.
• A toString method of your own design, with information describing the object's state.
In the driver class Driver, have your program do the following:
• Instantiate two Cuboid objects and two Cone objects. Print these objects to the screen.
• Exercise and test all methods from each class. Print results to the screen so that you can verify
whether the methods you are testing work correctly. Include appropriate messages. Include
both “before” and “after” values if needed.
Make sure your tests are fully readable – both in your program code and in the program
printouts on the screen.• All dimensions and volumes should be printed with a precision of 1 digit after the decimal point.
All prices of objects should be printed with a precision of 2 digits after the decimal point.
The purpose of the driver class in this question is to fully exercise and test the Cuboid class and the
Cone class. Asking the user for input values is optional (and it will not receive more points).
Hints:
• The value of π can be obtained from java.lang.Math as Math.PI (see API documentation).
• The textbook includes a similar but simpler version of the problem as a programming project:
PP 5.4 in [L,DeP,Ch], or PP 4.1 in [L,L]. The author's solution to that project is included in the
“Assignment A Package”: files Sphere.java and MultiSphere.java. Treat it only as a reference!
While there are some similarities, the problem and the requirements of this assignment are
different. Also, our style of writing code and comments is different (closer to “"Code
Conventions for the Java Programming Language").
In your code, please use TAB key for indentation, follow our convention for { } alignment, and
use String.format to format numbers in strings or for printouts.

解决方案 »

  1.   

    创建3个类 
    第一个类的类名是Cuboid.java 其中定义一个三围几何图形 是矩形立方体
    第二个类的类名是Cone.java 定义的三围几何图形是圆锥体
    第三个类叫 Driver.java  是用来创建和操作之前建立的2个类中的对象呵呵 下面的 看的有点头晕了 
    得慢慢来啊
      

  2.   

    The instance variables in the Cuboid class should represent the cuboid's three dimensions: length,
    depth, and height (all double values). The instance variables in the Cone class should represent the
    cone's height and the radius of its base (all double values). In addition, the Cuboid class and the
    Cone class should each include the following:在Cuboid类中的实例变量需要说明长方体三个值 :长、宽、高。
    在Cone类中的实例变量要说明圆锥体的高和半径。
    另外 这2个类要包含以下内容:
      

  3.   


    别太懒,学习还是要注重过程,你直接把题目贴上来求答案,是不是有点太……
    good luck!
      

  4.   

    我接着翻译.........哈哈
    Cuboid(长方体)类中的实例变量包含:长,宽,高(都是double值)。Cone(圆锥体)类中的实例变量包含:高度,底面半径。此外,Cuboid类和Cone类都要满足下面的要求:
    1,一个空构造函数并且初始化所有的实例变量值为1.0;
    2,一个能通过输入参数初始化所以实例变量的构造函数;
    3,为所有的实例变量创建Getter方法
    4,实例方法volume,计算和返回对象的体积(double值)
    5,实例方法setDimensions,可以通过接受参数改变对象的所有实例变量值,该方法返回void。该方法只有当改变所有实例变量值时,才进行修改,否则抛出异常。
    6,实例方法price,接受一个参数:单位体积材料的价格-double值。这个方法计算和返回物体的价格(double值)假设该物体材料均匀。
    7,实例方法toString,你自己设计,反映该对象的一些状态信息
    在驱动类Driver中。让你的程序做如下工作:
    实例化两个Cuboid对象和两个Cone对象。打印这些对象到屏幕上。
    练习和测试前面两个类中的所有方法。打印结果到屏幕,以便你能够判断你测试的方法是否正确。包含一些适当的信息。如果需要,包含“before”和“after”字眼
    确保你的测试可读性高,包括代码和程序的输出内容。
    所有的维度和体积在打印的时候应该保留一位小数,所有的价格应该保留两位小数
    题目中,驱动类的目的是全面的测试Cuboid和Cone类。由用户输入参数为可选。提示:
    π的值可以有java.lang.Math中的Math.PI获得
    课本上包含这个问题的相似并且简单的版本:......作者的方案包含在“Assignment A Package”下面:Sphere.java文件和MultiSphere.java。仅仅把它当成一个参照!有些相同的地方,但问题和任务的要求不同。同时,我们的写代码的风格和注释不同(接近“Code Conventions for the Java Programming Language”)。在你的代码中,请用TAB键缩进,服从大括号的约定,并且使用String.format来格式化字符串中的数字。