大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
class Tixing //梯形类
成都创新互联是一家专注于网站设计、成都网站设计与策划设计,临泉网站建设哪家好?成都创新互联做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:临泉等地区。临泉做网站价格咨询:18982081108
{
private float Height; //高
private float upBotton;//上底
private float downBotton;//下底
public Tixing(float Height,float upBotton,float downBotton)//构造方法
{
this.Height=Height;
this.upBotton=upBotton;
this.downBotton=downBotton;
}
public float getTixingArea() //计算梯形面积
{
return (upBotton+downBotton)*height/2;
}
}
public class Start
{
public static void main(String[] args)
{
Tixing t=new Tixing(30,20,50);//构造
System.out.println("梯形的面积是:"+t.getTixingArea());//打印输出
}
}
public static void main(String[] args) {
try {
double top = Double.valueOf(args[0]);
double bottom = Double.valueOf(args[1]);
double height = Double.valueOf(args[2]);
double area = (top + bottom) * height / 2;
System.out.println("梯形面积是:" + area);
} catch (Exception e) {
System.out.println("命令行参数错误");
}
}
javac xxx.java
java xxx 2 4 5
import java.util.Scanner;
public class Sum{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入上底长度");
int a=sc.nextInt();
System.out.println("请输入下底长度");
int b=sc.nextInt();
System.out.println("请输入高");
System.out.println("梯形面积为"+((a+b)*sc.nextInt()));
}
}
public class Test{
//梯形面积的方法
//a为梯形上底,b为梯形下底,h为梯形高,area为面积
public double area(double a,double b,double h){
double area;
//梯形面积公式:(上底+下底)*高/2
area=(a+b)*h/2;
return area;
}
//使用mian()方法实现
public static void main(String [] args){
Test t=new Test();
System.out.print("梯形面积为"+t.area(8.9,12.1,16));
}
}
因为这3个图形的面积计算,需要的参数分别是1个,2个,3个。
所以你可以写3个方法,方法名字都一样,但是第一个方法有1个参数
第二个方法有2个参数,但三个方法有3个参数。这样完全不会混淆
package test;
public class Test{
public static void main(String[] args) {
Lader l=new Lader(4, 8, 3);
System.out.println("体形面积是"+l.computerArea());
}
}
class Lader {
private float above; //梯形的上底(变量声明)
private float bottom; //梯形的下底(变量声明)
private float height; //梯形的高(变量声明)
private float area; //梯形的面积(变量声明)
public float computerArea() { //计算面积(方法)
area = (above+bottom)*height/2.0f;
return area;
}
public Lader(float above,float bottom,float height){
this.above=above;
this.bottom=bottom;
this.height=height;
}
void setHeight(float h) { //修改高(方法)
height = h;
}
public float getAbove() {
return above;
}
public void setAbove(float above) {
this.above = above;
}
public float getBottom() {
return bottom;
}
public void setBottom(float bottom) {
this.bottom = bottom;
}
public float getArea() {
return area;
}
public void setArea(float area) {
this.area = area;
}
public float getHeight() {
return height;
}
}