java面向对象编程题目

2025-01-03 07:44:00
推荐回答(5个)
回答(1):

public class Rectangle
{
private double width;
private double height;

//无参构造器
public Rectangle()
{}
//有参构造器
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
//属性的get和set方法定义
public void setWidth(double width)
{
this.width = width;
}

public double getWidth()
{
return this.width;
}

public void setHeight(double height)
{
this.height = height;
}
public double getHeight()
{
return this.height;
}
//计算周长的方法
private double getPerimeter()
{
return (width+height)*2;
}
//计算面积的方法
private double getArea()
{
return width*height;
}

public static void main(String[] args)
{
Rectangle rec = new Rectangle(3.6,5.8);
System.out.println("The perimeter of Rectangle is:"+rec.getPerimeter());
System.out.println("The area of Rectangle is:"+rec.getArea());
}
}

回答(2):

package com.baidu;

import java.util.Scanner;

public class Rectangle {
private double width=0;
private double height=0;

public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getCircumference(double width,double height){
return (width+height)*2;
}
public double getArea(double width,double height){
return width*height;
}
public static void main(String[] args) {
Rectangle rtl=new Rectangle();
System.out.println("请输入矩形的长度...");
double w=new Scanner(System.in).nextDouble();
System.out.println("请输入矩形的宽度...");
double h=new Scanner(System.in).nextDouble();
System.out.println("矩形的周长是:"+rtl.getCircumference(w, h));
System.out.println("矩形的面积是:"+rtl.getArea(w, h));

}

}

回答(3):

public class Rectangle {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getPerimeter() { // 周长
return (width + height) * 2;
}
public double getArea() { // 面积
return width * height;
}
public static void main(String[] args) {
Rectangle rec = new Rectangle(20.0, 10.0);
System.out.println("perimeter=" + rec.getPerimeter());
System.out.println("area=" + rec.getArea());
}
}

回答(4):

package com.hpcc.data;

public class Demo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle rectangle = new Rectangle(10.0,12.0);
double area = rectangle.getArea();
double cir = rectangle.getCir();

System.out.println("面积是:"+area);
System.out.println("周长是:"+cir);
}
}
class Rectangle{
//宽,高两个属性
private double width;
private double height;
//设置默认值
public Rectangle(){
this.width = 0.0;
this.height = 0.0;
}
public Rectangle(double width,double height){
this.width = width;
this.height = height;
}
//获得面积
public double getArea() {
return width*height;
}
//获得周长
public double getCir() {
return 2*width+2*height;
}
}

回答(5):

public class Rectangle{
double width,height;

public double getR(){
return 2*width+2*height;
}

public double getP(){
return width*height;
}
}