「java怎么求长方形周长」java编程求三角形面积和周长

博主:adminadmin 2022-12-18 17:42:07 58

本篇文章给大家谈谈java怎么求长方形周长,以及java编程求三角形面积和周长对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

怎样用java设计一个成员变量包括长和宽的长方形类并计算面积和周长?

/**

* 长方形类

*/

class Rectangle{

/**

* 宽

*/

private double width;

/**

* 高

*/

private double height;

/**

* 构造方法

* @param width 宽

* @param height 高

*/

public Rectangle(double width, double height){

this.width = width;

this.height = height;

}

/**

* 计算长方形的面积

* @return 返回长方形的面积

*/

public double getArea(){

return this.width * this.height;

}

/**

* 计算长方形的周长

* @return 返回长方形的周长

*/

public double getGirth(){

return 2 * (this.width + this.height);

}

public double getWidth(){

return this.width;

}

public void setWidth(double width){

this.width = width;

}

public double getHeight(){

return this.height;

}

public void setHeight(double height){

this.height = height;

}

}

public class Test {

public static void main(String[] args){

Rectangle rect = new Rectangle(2,3);

System.out.println("长方形的宽 :" + rect.getWidth());

System.out.println("长方形的高 :" + rect.getHeight());

System.out.println("长方形的面积:" + rect.getArea());

System.out.println("长方形的周长:" + rect.getGirth());

}

}

Java编程题:求圆,正方形,长方形的周长(只求周长)?

这个题不难啊,关键点在于求周长的公式,圆的重点是π的调用使用,正方形和长方形主要是边长参数的定义,都是很简单的题目。

求一个,用Java编写一个求长方形的面积和周长的程序,(面向对象).

//看看我这个程序把 比较符合面向对象的思想,告诉搂住一声,尽量把一些程序写尽方法里,而不是都写在主方法中!这样不好~~~~ 希望对你有用!!

import java.util.Scanner;

public class Ex {

public static int squ(int x,int y){ //求面积的方法

int s = x* y;

return s;

}

public static double len(int x,int y){//求周长的方法

int l = (x+y)*2;

return l;

}

public static void main(String [] aa){

System.out.println("请输入宽:"); //从命令行输入宽

Scanner in = new Scanner(System.in);

int le = in.nextInt();

System.out.println("请输入高:");//从命令行输入高

Scanner in2 = new Scanner(System.in);

int hi = in2.nextInt(); //转换为int型

int mianji = squ(le,hi); //调用方法

System.out.println("面积是:" + mianji);

/*

* 求周长同理,调用周长那个方法即可

*/

}

}

Java编写一个矩形类,并计算面积和周长?

class Rectangle{

private int width = 2;

private int length = 1;

public int getWidth(){

return this.width;

}

public void setWidth(int w){

this.width = w;

}

public int getLength(){

return this.length;

}

public void setLength(int l){

this.length = l;

}

public int getArea(){

return this.length * this.width;

}

public int getCircumference(){

return (this.length + this.width) * 2;

}

public Rectangle(){}

public Rectangle(int l, int w){

this.length = l;

this.width = w;

}

}

public class demo{

public static void main(String[] args) {

Rectangle rect = new Rectangle(30, 8);

System.out.print("长方形的面积是:");

System.out.println(rect.getArea());

System.out.printf("长方形的周长是:%d\n", rect.getCircumference());

}

}

关于java怎么求长方形周长和java编程求三角形面积和周长的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-18,除非注明,否则均为首码项目网原创文章,转载请注明出处。