1.根据需求,用面向对象的方式定义一个计算器类(Calculator);
2.由于计算器能对两个数字进行各种运算,所以定义两个成员变量表示两个运算数;
3.编写4个成员方法,分别实现加(add)、减(minus)、乘(multiple)、除(divide)四类运算。
4.定义构造函数对成员变量赋初值;
import java.util.Scanner; public class CalculatorDemo { public static double a; public static double b; public static boolean flag=true; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Calculator ca = new Calculator();//构造方法 do{ System.out.print("请输入要执行的运算法则:"); int n=scanner.nextInt(); System.out.println(); if(n==1){ System.out.println("请输入两个数字:"); System.out.println(ca.MyAdd(scanner.nextDouble(),scanner.nextDouble())); } if(n==2){ System.out.println("请输入两个数字:"); System.out.println(ca.MySub(scanner.nextDouble(),scanner.nextDouble())); } if(n==3){ System.out.println("请输入两个数字::"); System.out.println(ca.MyMult(scanner.nextDouble(),scanner.nextDouble())); } if(n==4){ System.out.println("请输入两个数字:"); System.out.println(ca.MyDiv(scanner.nextDouble(),scanner.nextDouble())); } if(n!=1&&n!=2&&n!=3&&n!=4){ System.out.println("确定退出吗?(Y)"); String t=scanner.next(); if(t.equals("Y")) { flag = false; } else { System.out.println("输入错误。。。"); } } }while(flag);//控制结束 } }
public class Calculator { public static double MyAdd(double a,double b){ return a+b; } public static double MySub(double a,double b){ return a-b; } public static double MyMult(double a,double b){ return a*b; } public static double MyDiv(double a,double b){ return a/b; } }
.编写一个 Java 应用程序,该程序中有 3 个类:Triangle、T rapezium 和 Circle,分别用来 表示三角形、梯形和圆形。具体要求如下。
Triangle 类具有类型为 double 的三个边、周长以及面积属性,Triangle 类具有构造方法初始化属性和返回周长、面积以及修改三个边的功能。另外,Triangle 类还具有一个 boolean 型的属性,该属性用来判断三个数能否构成一个三角形。
Trapezium 类具有类型为 double 的上底、下底、高以及面积属性,具有构造方法初始化属性和返回面积的功能。
Circle 类具有类型为double的半径、周长以及面积属性,具有构造方法初始化属性和返回周长、面积的功能。
生成实例测试该类。
import java.util.Scanner; public class Demo01 { public static double a; public static double b; public static double c; public static boolean flag = true; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Triangle triangle = new Triangle(); Trapezium trapezium = new Trapezium(); Circle circle = new Circle();//构造方法 do { System.out.println("请输入要选择的服务:"); int n = scanner.nextInt(); if (n == 1) { System.out.println("请输入三角形三边"); a = scanner.nextDouble(); b = scanner.nextDouble(); c = scanner.nextDouble(); if (triangle.TriangleF(a, b, c)) { System.out.println("该三角形面积为:" + triangle.TriangleS(a, b, c)); System.out.println("该三角形周长为:" + triangle.TriangleC(a, b, c)); } else { System.out.println("构不成三角形!!!!"); } } if (n == 2) { System.out.println("请输入梯形上底和下底和高:"); a = scanner.nextDouble(); b = scanner.nextDouble(); c = scanner.nextDouble(); System.out.println("梯形面积为:" + trapezium.TrapeziumS(a, b, c)); } if (n == 3) { System.out.println("请输入圆的半径:"); a = scanner.nextDouble(); System.out.println("圆的面积为:" + circle.CircleS(a)); System.out.println("元的周长为:" + circle.CircleC(a)); } } while (flag); } }
public class Triangle { public static boolean TriangleF(double a,double b,double c) { if(a+b>c&&a+c>b&&c+b>a) return true; else return false; } public static double TriangleC(double a,double b,double c){ return a+b+c; } public static double TriangleS(double a,double b,double c) { double p=(a+b+c)/2; double S=Math.sqrt(p*(p-a)*(p-b)*(p-c)); return S; } }
public class Trapezium { public static double TrapeziumS(double a,double b,double h) { double S=((a+b)*h)/2; return S; } }
public class Circle { public static double CircleC(double R){ return 2*Math.PI*R; } public static double CircleS(double R) { return Math.PI*R*R; } }