首页 热点资讯 义务教育 高等教育 出国留学 考研考公

用java语言

发布网友 发布时间:2022-04-23 14:49

我来回答

3个回答

热心网友 时间:2023-10-19 10:11

代码追求的是简单整洁 上面两个答案写了太多无用代码

public class A {
private double a;
private double b;
//定义方法
public double juxing() {
double c=a*b;
return c;
}
//构造函数
public ddd(double a, double b) {
super();
this.a = a;
this.b = b;
}
测试类
public class B {
public static void main(String[] args) {

A a=new A(1.0, 2.0);//后面两个值是自己传的参数,随便填的
System.out.println(a.juxing());

}
}

热心网友 时间:2023-10-19 10:11

/**
* 矩形类
* @author Administrator
*
*/
public class Rectangle {

// 矩形长
private double lenght;
// 矩形款
private double width;
// 矩形面积
private double area;

// 含参数构造方法
public Rectangle(double lenght, double width) {
this.lenght = lenght;
this.width = width;
}

// 计算矩形面积
public void calculateArea() {

area = lenght * width;
System.out.println("矩形面积是:" + area);
}

public double getLenght() {
return lenght;
}

public void setLenght(double lenght) {
this.lenght = lenght;
}

public double getWidth() {
return width;
}

public void setWidth(double width) {
this.width = width;
}

public double getArea() {
return area;
}

public void setArea(double area) {
this.area = area;
}

}

/**
* 测试类(主类)
* @author Administrator
*
*/
public class Test {

public static void main(String[] args) {

// 创建矩形类对象
Rectangle rt = new Rectangle(6,4);

// 调用计算矩形面积的方法
rt.calculateArea();
}
}

效果:

热心网友 时间:2023-10-19 10:12

public class Rectangle {
private double width;

private double height;
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 getArea(){
return width * height;
}
}
public class RectTest {
public static void main(String[] args) {
Rectangle rect = new Rectangle();
rect.setWidth(20);
rect.setHeight(10.5);
double area = rect.getArea();
System.out.println(area);
}
}

热心网友 时间:2023-10-19 10:11

代码追求的是简单整洁 上面两个答案写了太多无用代码

public class A {
private double a;
private double b;
//定义方法
public double juxing() {
double c=a*b;
return c;
}
//构造函数
public ddd(double a, double b) {
super();
this.a = a;
this.b = b;
}
测试类
public class B {
public static void main(String[] args) {

A a=new A(1.0, 2.0);//后面两个值是自己传的参数,随便填的
System.out.println(a.juxing());

}
}

热心网友 时间:2023-10-19 10:11

/**
* 矩形类
* @author Administrator
*
*/
public class Rectangle {

// 矩形长
private double lenght;
// 矩形款
private double width;
// 矩形面积
private double area;

// 含参数构造方法
public Rectangle(double lenght, double width) {
this.lenght = lenght;
this.width = width;
}

// 计算矩形面积
public void calculateArea() {

area = lenght * width;
System.out.println("矩形面积是:" + area);
}

public double getLenght() {
return lenght;
}

public void setLenght(double lenght) {
this.lenght = lenght;
}

public double getWidth() {
return width;
}

public void setWidth(double width) {
this.width = width;
}

public double getArea() {
return area;
}

public void setArea(double area) {
this.area = area;
}

}

/**
* 测试类(主类)
* @author Administrator
*
*/
public class Test {

public static void main(String[] args) {

// 创建矩形类对象
Rectangle rt = new Rectangle(6,4);

// 调用计算矩形面积的方法
rt.calculateArea();
}
}

效果:

热心网友 时间:2023-10-19 10:12

public class Rectangle {
private double width;

private double height;
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 getArea(){
return width * height;
}
}
public class RectTest {
public static void main(String[] args) {
Rectangle rect = new Rectangle();
rect.setWidth(20);
rect.setHeight(10.5);
double area = rect.getArea();
System.out.println(area);
}
}

热心网友 时间:2023-10-19 10:11

代码追求的是简单整洁 上面两个答案写了太多无用代码

public class A {
private double a;
private double b;
//定义方法
public double juxing() {
double c=a*b;
return c;
}
//构造函数
public ddd(double a, double b) {
super();
this.a = a;
this.b = b;
}
测试类
public class B {
public static void main(String[] args) {

A a=new A(1.0, 2.0);//后面两个值是自己传的参数,随便填的
System.out.println(a.juxing());

}
}

热心网友 时间:2023-10-19 10:11

/**
* 矩形类
* @author Administrator
*
*/
public class Rectangle {

// 矩形长
private double lenght;
// 矩形款
private double width;
// 矩形面积
private double area;

// 含参数构造方法
public Rectangle(double lenght, double width) {
this.lenght = lenght;
this.width = width;
}

// 计算矩形面积
public void calculateArea() {

area = lenght * width;
System.out.println("矩形面积是:" + area);
}

public double getLenght() {
return lenght;
}

public void setLenght(double lenght) {
this.lenght = lenght;
}

public double getWidth() {
return width;
}

public void setWidth(double width) {
this.width = width;
}

public double getArea() {
return area;
}

public void setArea(double area) {
this.area = area;
}

}

/**
* 测试类(主类)
* @author Administrator
*
*/
public class Test {

public static void main(String[] args) {

// 创建矩形类对象
Rectangle rt = new Rectangle(6,4);

// 调用计算矩形面积的方法
rt.calculateArea();
}
}

效果:

热心网友 时间:2023-10-19 10:12

public class Rectangle {
private double width;

private double height;
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 getArea(){
return width * height;
}
}
public class RectTest {
public static void main(String[] args) {
Rectangle rect = new Rectangle();
rect.setWidth(20);
rect.setHeight(10.5);
double area = rect.getArea();
System.out.println(area);
}
}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com