小明家住农村,家里有几块形状不同的地,请帮助小明计算下他家地的总面积和人均面积。系统包括家庭类,Shape接口,园类以及长方形类。其中园类和长方形类实现Shape接口。
Family类包含属性numPeople描述家庭人数,数组shapes描述小明家所拥有的几块土地,方法 getTotalArea(), getAvgArea()分别求出家庭总土地面积和人均土地面积。
属性numPepole 和shapes值在运行时通过键盘给出。
请完成下列代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//输入土地数 input area number
int areanum = input.nextInt();
//输入家庭人数 peoplenum
int peoplenum = input.nextInt();
//创建土地数组 根据土地数 new Shape Array based on areanum
Shape []shapes = 【】;
for(int i=0;i<shapes.length;i++){
String str = input.next();
if(str.equals("cir")){ //判断是否是圆形土地
double r = input.nextDouble(); //输入圆形土地的半径
【】 //创建圆形土地对象,并放入shapes数组
}
else
if(str.equals("rec")){
double height = input.nextDouble();
double width = input.nextDouble();
【】//创建长方形土地对象,并放入shapes数组
}
}//for 结束
//创建家庭对象小明,将小明家的人数和土地传入
Family xiaoming = new Family(peoplenum,shapes);
//求小明家总土地面积
double totalArea =【】;
//求小明家人均土地面积
double avgArea =【】;
System.out.printf("total Area is:%.2f\n",totalArea);
System.out.printf("average Area is:%.2f\n",avgArea);
}
}
//定义接口Shape
【】 Shape {
//定义方法double getArea();
【】;
}
class Circle [ ]Shape {
private double radius;
@Override //重写方法getArea()
[]
public Circle() {
this(1);
}
public Circle(double radius) {
setRadius(radius);
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
class Rectangle[] Shape{
private double height,width;
//方法重写
【】
public Rectangle(double height, double width) {
super();
this.height = height;
this.width = width;
}
public Rectangle() {
this.width = height =1;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
}
class Family{
private int numPeople; //人口数
private Shape[] shapes;//家庭拥有土地
public Family(int numPeople, Shape[] shapes) {
this.numPeople = numPeople;
this.shapes = shapes;
}
public double getTotalArea(){
double sum =0;
//求家庭土地总面积,将shapes数组中每一个土地求面积相加求和
【】
return sum;
}
//求家庭人均土地面积
public double getAvgArea(){
【】
}
public int getNumPeople() {
return numPeople;
}
public void setNumPeople(int numPeople) {
this.numPeople = numPeople;
}
public Shape[] getShapes() {
return shapes;
}
public void setShapes(Shape[] shapes) {
this.shapes = shapes;
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//输入土地数 input area number
int areanum = input.nextInt();
//输入家庭人数 peoplenum
int peoplenum = input.nextInt();
//创建土地数组 根据土地数 new Shape Array based on areanum
Shape []shapes = new Shape[areanum];
for(int i=0;i<shapes.length;i++){
String str = input.next();
if(str.equals("cir")){ //判断是否是圆形土地
double r = input.nextDouble(); //输入圆形土地的半径
shapes[i]=new Circle(r); //创建圆形土地对象,并放入shapes数组
}
else
if(str.equals("rec")){
double height = input.nextDouble();
double width = input.nextDouble();
shapes[i]=new Rectangle(height,width) ;//创建长方形土地对象,并放入shapes数组
}
}//for 结束
//创建家庭对象小明,将小明家的人数和土地传入
Family xiaoming = new Family(peoplenum,shapes);
//求小明家总土地面积
double totalArea = xiaoming.getTotalArea();
//求小明家人均土地面积
double avgArea = xiaoming.getAvgArea();
System.out.printf("total Area is:%.2f\n",totalArea);
System.out.printf("average Area is:%.2f\n",avgArea);
}
}
//定义接口Shape
interface Shape {
//定义方法double getArea();
double getArea();
}
class Circle implements Shape {
private double radius;
@Override //重写方法getArea()
public double getArea()
{
return radius*radius*Math.PI;
}
public Circle() {
this(1);
}
public Circle(double radius) {
setRadius(radius);
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
class Rectangle implements Shape{
private double height,width;
//方法重写
@Override
public double getArea() {
return (height*width);
}
public Rectangle(double height, double width) {
super();
this.height = height;
this.width = width;
}
public Rectangle() {
this.width = height =1;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
}
class Family{
private int numPeople; //人口数
private Shape[] shapes;//家庭拥有土地
public Family(int numPeople, Shape[] shapes) {
this.numPeople = numPeople;
this.shapes = shapes;
}
public double getTotalArea(){
double sum =0;
//求家庭土地总面积,将shapes数组中每一个土地求面积相加求和
for(int i=0;i< shapes.length;i++)
{
sum=sum+shapes[i].getArea();
}
return sum;
}
//求家庭人均土地面积
public double getAvgArea(){
double renjun;
renjun=getTotalArea()/getNumPeople();
return renjun;
}
public int getNumPeople() {
return numPeople;
}
public void setNumPeople(int numPeople) {
this.numPeople = numPeople;
}
public Shape[] getShapes() {
return shapes;
}
public void setShapes(Shape[] shapes) {
this.shapes = shapes;
}
}
我正在使用i18n从头开始构建一个多语言网络应用程序,虽然我自己可以处理一大堆yml文件,但我说的语言(非常)有限,最终我想寻求外部帮助帮助。我想知道这里是否有人在使用UI插件/gem(与django上的django-rosetta不同)来处理多个翻译器,其中一些翻译器不愿意或无法处理存储库中的100多个文件,处理语言数据。谢谢&问候,安德拉斯(如果您已经在rubyonrails-talk上遇到了这个问题,我们深表歉意) 最佳答案 有一个rails3branchofthetolkgem在github上。您可以通过在Gemfi
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
是否有简单的方法来更改默认ISO格式(yyyy-mm-dd)的ActiveAdmin日期过滤器显示格式? 最佳答案 您可以像这样为日期选择器提供额外的选项,而不是覆盖js:=f.input:my_date,as::datepicker,datepicker_options:{dateFormat:"mm/dd/yy"} 关于ruby-on-rails-事件管理员日期过滤器日期格式自定义,我们在StackOverflow上找到一个类似的问题: https://s
我想用这两种语言中的任何一种(最好是ruby)制作一个窗口管理器。老实说,除了我需要加载某种X模块外,我不知道从哪里开始。因此,如果有人有线索,如果您能指出正确的方向,那就太好了。谢谢 最佳答案 XCB,X的下一代API使用XML格式定义X协议(protocol),并使用脚本生成特定语言绑定(bind)。它在概念上与SWIG类似,只是它描述的不是CAPI,而是X协议(protocol)。目前,C和Python存在绑定(bind)。理论上,Ruby端口只是编写一个从XML协议(protocol)定义语言到Ruby的翻译器的问题。生
这是我在ActiveAdmin中的自定义页面ActiveAdmin.register_page"Settings"doaction_itemdolink_to('Importprojects','settings/importprojects')endcontentdopara"Text"endcontrollerdodefimportprojectssystem"rakedataspider:import_projects_ninja"para"OK"endendend我想做的是,当我单击“导入项目”按钮时,我想在Controller中执行rake任务。但是我无法访问该方法。可能是什
我正在寻找用于Rails的优质管理插件。似乎大多数现有的插件/gem(例如“restful_authentication”、“acts_as_authenticated”)都围绕着self注册等展开。但是,我正在寻找一种功能齐全的基于管理/管理角色的解决方案——但不是简单地附加到另一个非基于角色的解决方案。如果我找不到,我想我会自己动手......只是不想重新发明轮子。 最佳答案 RyanBates最近做了两个关于授权的railscast(注意身份验证和授权之间的区别;身份验证检查用户是否如她所说的那样,授权检查用户是否有权访问资源
文章目录一基础定义二创建逻辑卷2-1准备物理设备2-2创建物理卷2-3创建卷组2-4创建逻辑卷2-5创建文件系统并挂载文件三扩展卷组和缩减卷组3-1准备物理设备3-2创建物理卷3-3扩展卷组3-4查看卷组的详细信息以验证3-5缩减卷组四扩展逻辑卷4-1检查卷组是否有可用的空间4-2扩展逻辑卷4-3扩展文件系统五删除逻辑卷5-1备份数据5-2卸载文件系统5-3删除逻辑卷5-4删除卷组5-5删除物理卷六LVM逻辑卷缩容6-1缩容注意事项6-2标准缩容步骤一基础定义LVM,LogicalVolumeManger,逻辑卷管理,Linux磁盘分区管理的一种机制,建立在硬盘和分区上的一个逻辑层,提高磁盘分
我为Devise用户和管理员提供了不同的模型。我也在使用Basecamp风格的子域。除了我需要能够以用户或管理员身份进行身份验证的一些Controller和操作外,一切都运行良好。目前我有authenticate_user!在我的application_controller.rb中设置,对于那些只有管理员才能访问的Controller和操作,我使用skip_before_filter跳过它。不幸的是,我不能简单地指定每个Controller的身份验证要求,因为我仍然需要一些Controller和操作才能被用户或管理员访问。我尝试了一些方法都无济于事。看来,如果我移动authentica
我正在根据Rails指南的建议开发Rails应用程序,以创建包含翻译的文件夹树和文件。我的文件夹树与此类似:|-defaults|---es.rb|---en.rb|-models|---book|-----es.rb|-----en.rb|-views|---defaults|-----es.rb|-----en.rb|---books|-----es.rb|-----en.rb|---users|-----es.rb|-----en.rb|---navigation|-----es.rb|-----en.rbconfig/locales/views/books/en.yml中的内容
我在ruby(2.0.0p39474)中执行非常快速的文件访问,并不断收到异常Toomanyopenfiles看过thisthread,here,以及各种其他来源,我很清楚操作系统限制(在我的系统上设置为1024)。我执行此文件访问的代码部分是互斥的,并采用以下形式:File.open(filename,'w'){|f|Marshal.dump(value,f)}其中filename会根据调用该部分的线程快速变化。据我了解,此表单在block后放弃其文件句柄。我可以使用ObjectSpace.each_object(File)验证打开的File对象的数量.这报告最多有100个常驻内