JAVA编程题

2. (1)编写一个圆类Circle,该类拥有:
①一个成员变量
Radius(私有,浮点型); // 存放圆的半径;
②两个构造方法
Circle( ) // 将半径设为0
Circle(double r ) //创建Circle对象时将半径初始化为r
③ 三个成员方法
double getArea( ) //获取圆的面积
double getPerimeter( ) //获取圆的周长
void show( ) //将圆的半径、周长、面积输出到屏幕
(2)编写一个圆柱体类Cylinder,它继承于上面的Circle类。还拥有:
①一个成员变量
double hight(私有,浮点型); // 圆柱体的高;
②构造方法
Cylinder (double r, double h ) //创建Circle对象时将半径初始化为r
③ 成员方法
double getVolume( ) //获取圆柱体的体积
void showVolume( ) //将圆柱体的体积输出到屏幕
编写应用程序,创建类的对象,分别设置圆的半径、圆柱体的高,计算并分别显示圆半径、圆面积、圆周长,圆柱体的体积。

//圆类Circle
import java.util.Scanner;
public class Circle {
private double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
//无参构造函数
public Circle(){
this.radius=0;
}
//带参构造函数
public Circle(double r ){
this.radius=r;
}
//获取面积
public double getArea(){
double r=this.radius;
double area=r*r*3.14;
return area;
}
//获取周长
public double getPerimeter(){
double perimeter=this.radius*2*3.14;
return perimeter;
}
//打印圆的信息
public void show(){
System.out.println("请输入圆的半径");
Scanner sc=new Scanner(System.in);
this.setRadius(sc.nextInt());
System.out.println("圆的半径"+this.getRadius());
System.out.println("圆的面积"+this.getArea());
System.out.println("圆的周长"+this.getPerimeter());
}
}
//圆柱类
public class Cylinder extends Circle {
//圆柱高
private double height;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
//构造方法
public Cylinder (double r, double h){
super();
this.height=h;
this.setRadius(r);
}
public double getVolume( ) {
double volume=this.getArea()*this.height;
return volume;
}
//求体积
public void showVolume(){
System.out.println("圆柱的体积"+this.getVolume());
}
}
//主程序入口
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Circle circle=new Circle();
circle.show();
Cylinder cylinder=new Cylinder(2.0,8.5);
cylinder.showVolume();
}
}
PS:注释写的很详尽了,求采纳
温馨提示:内容为网友见解,仅供参考
第1个回答  2018-07-06

使用notepad++就会产生这个问题。

方案一、指定编码

javac -encoding utf-8 xxx.java

方案二、修改文件编码

第2个回答  2012-12-16
有JAVA方面的课程视频,不知是否对你有用
希望可以帮到你

记得采纳我呀!
第3个回答  2018-07-27

一整段代码是:对数组升序排序

for (i = 0; i < a.length - 1; i++) {

i的值小于(数组长度-1)的时候执行for循环体。在此程序中是指:需要排序的次数是(数组长度-1)次。

第4个回答  2018-05-09
//创建圆形Circle类
public class Circle {
private double radius; //私有化成员

public double getRadius() { //方便show方法里面直接this(表示当前对象)调用获取半径
return radius;
}
public Circle() { //创建Cylinder的时候需要访问,不能省去

super();
}
public Circle(double radius) { //直接利用带参构造函数进行赋值,方便后面this的使用

super();
this.radius = radius;
}
double getArea() { //这里的radius相当于this.radius,上一步赋值后可以直接使用
double s = Math.PI * radius * radius;
return s;
}
double getPerimeter() {

double c = Math.PI * 2 * radius;
return c;
}
void show() { //this是当前对象,利用有参构造创建对象后相当于给变量赋值了;this直接调用

double s = Math.PI*this.getRadius()*this.getRadius();
double c = Math.PI*this.getRadius()*2;
System.out.println("圆的面积是" + s + ".." + "圆的周长是" + c);
}
}
//创建圆柱体Cylinder类
public class Cylinder extends Circle{
private double hight;
private Circle circle;
public Cylinder() {
super();
}
public Cylinder(double hight, Circle circle) {
super();
this.hight = hight;
this.circle = circle;
}
public double getHight() {
return hight;
}
public Circle getCircle() {
return circle;
}
double getVolume( ){
//circle对象可以调用自己的方法
double v =this.circle.getArea()*this.hight;
return v;
}
void showVolume( ){
System.out.println("圆柱体的体积是:"+this.getVolume());
}

double getArea(){
Double s = this.circle.getArea()*2+this.circle.getPerimeter()*this.hight;
return s;
}
void showArea(){
System.out.println("圆柱体表面积:"+this.getArea());
}
}

//主函数(主函数很简洁,因为需要的操纵已经在Circle和Cylinder中完成了)
public class Demo005 {
public static void main(String[] args){
Circle c1 = new Circle(5);
System.out.println("圆的半径是:"+c1.getRadius());
c1.show();
Cylinder cy = new Cylinder(10, c1);
cy.showVolume();
cy.showArea();
}
}

//我也是小白一枚,仅仅是对java有点兴趣,以上操作不喜欢轻喷
相似回答