1.三维坐标向量类
设计一个描述三维坐标的向量类vector3D,成员如下:
数据成员:
- 三个坐标x,y,z,float类型,私有访问属性
公有函数成员:
-
三个参数均有默认值的构造函数,默认值为0,0,0;
-
完成输入输出,输出格式为(x, y, z);
-
完成加法+、减法-、数乘*运算;
在主函数中定义两个vector3D类对象v1,v2,均不带参数,之后输入数字1或2选择为v1输入赋值,还是为v1和v2输入赋值,对v1和v2进行加、减运算,对v1进行数乘运算,乘数由用户输入,最后输出三种运算结果。
1 import java.util.Scanner; 2 //三维向量坐标类 3 public class Vector3D { 4 private float x; 5 private float y; 6 private float z; 7 public Vector3D() { 8 this.x=0; 9 this.y=0; 10 this.z=0; 11 } 12 public Vector3D(float x, float y, float z) { 13 this.x = x; 14 this.y = y; 15 this.z = z; 16 } 17 public void input() { 18 Scanner in=new Scanner(System.in); 19 x=in.nextFloat(); 20 y=in.nextFloat(); 21 z=in.nextFloat(); 22 } 23 public void display() { 24 System.out.println("("+x+","+y+","+z+")"); 25 } 26 //加法 27 public Vector3D add(Vector3D v) { 28 float a=x+v.x; 29 float b=y+v.y; 30 float c=z+v.z; 31 Vector3D p=new Vector3D(a,b,c); 32 return p; 33 } 34 //减法 35 public Vector3D sub(Vector3D v) { 36 float a=x-v.x; 37 float b=y-v.y; 38 float c=z-v.z; 39 Vector3D p=new Vector3D(a,b,c); 40 return p; 41 } 42 //数乘 43 public Vector3D mul(float n) { 44 float a=x*n; 45 float b=y*n; 46 float c=z*n; 47 Vector3D p=new Vector3D(a,b,c); 48 return p; 49 } 50 public static void main(String[] args) { 51 int n; 52 Vector3D v1=new Vector3D(); 53 Vector3D v2=new Vector3D(); 54 Scanner in=new Scanner(System.in); 55 System.out.println("1.为三维向量v1赋值并进行数乘运算"); 56 System.out.println("2.为三维向量v1、v2赋值并进行加、减运算"); 57 System.out.print("请输入你要进行的操作编号:"); 58 n=in.nextInt(); 59 if(n==1) { 60 float t; 61 Vector3D v; 62 System.out.print("请输入v1的三维坐标:"); 63 v1.input(); 64 System.out.print("请输入乘数:"); 65 t=in.nextFloat(); 66 v=v1.mul(t); 67 System.out.print("数乘结果为:"); 68 v.display(); 69 } 70 if(n==2) { 71 System.out.print("请输入v1的三维坐标:"); 72 v1.input(); 73 System.out.print("请输入v2的三维坐标:"); 74 v2.input(); 75 System.out.print("相加结果为:"); 76 (v1.add(v2)).display(); 77 System.out.print("相减结果为:"); 78 (v1.sub(v2)).display(); 79 } 80 } 81 }
2.电子钟
设计一款电子钟类,用于显示时、分、秒
-
含有形参有默认值的默认构造函数;
-
前缀++ 和 后缀—用于调整时间,每次调整均对秒进行调整,若秒满60,则分加1,若分满60则时加1,时满24,则清零重新开始;
-
设置函数用于输入(设定)时间;
-
设置函数用于输出时间。
1 import java.util.Scanner; 2 //电子钟类 3 public class Time { 4 private int hour; 5 private int minute; 6 private int second; 7 public Time() { 8 this.hour = 0; 9 this.minute = 0; 10 this.second = 0; 11 } 12 public Time(int hour, int minute, int second) { 13 this.hour = hour; 14 this.minute = minute; 15 this.second = second; 16 } 17 // 正计时 18 public void zheng() { 19 ++second; 20 if (second >= 60) { 21 second = 0; 22 ++minute; 23 if (minute >= 60) { 24 minute = 0; 25 ++hour; 26 hour %= 24; 27 } 28 } 29 } 30 // 倒计时 31 public void dao() { 32 second--; 33 if (second < 0) { 34 minute--; 35 second = 59; 36 } 37 if (minute < 0) { 38 minute = 59; 39 hour--; 40 } 41 if (hour < 0) { 42 hour = 23; 43 } 44 } 45 // 输入电子钟 46 public void input() { 47 Scanner in = new Scanner(System.in); 48 hour = in.nextInt(); 49 minute = in.nextInt(); 50 second = in.nextInt(); 51 } 52 // 输出电子钟 53 public void output() { 54 System.out.println(hour + ":" + minute + ":" + second); 55 } 56 public boolean dengYu(Time t) { 57 if (hour == t.hour && minute == t.minute && second == t.second) { 58 return true; 59 } else { 60 return false; 61 } 62 } 63 public static void main(String[] args) throws InterruptedException { 64 int n; 65 Scanner in = new Scanner(System.in); 66 // 菜单打印 67 System.out.println("功能选择:"); 68 System.out.println("*********"); 69 System.out.println("1.钟表"); 70 System.out.println("2.正计时"); 71 System.out.println("3.倒计时"); 72 System.out.println("*********"); 73 System.out.print("请输入你的选择:"); 74 n = in.nextInt(); 75 if (n == 1) { 76 System.out.print("请先校准时间:"); 77 Time t = new Time(); 78 t.input(); 79 while (true) { 80 t.output(); 81 t.zheng(); 82 Thread.sleep(2000); 83 } 84 } 85 if (n == 2) { 86 Time t = new Time(); 87 while (true) { 88 t.output(); 89 t.zheng(); 90 Thread.sleep(2000); 91 } 92 } 93 if (n == 3) { 94 Time t1 = new Time(); 95 Time t2 = new Time(); 96 System.out.print("请输入时间:"); 97 t1.input(); 98 while (true) { 99 t1.output(); 100 t1.dao(); 101 Thread.sleep(2000); 102 if (t1.dengYu(t2)) { 103 break; 104 } 105 } 106 System.out.println("STOP!"); 107 } 108 } 109 }
3.分数类
1 import java.util.Scanner; 2 //分数类 3 public class Fraction { 4 private int x; 5 private int y; 6 public Fraction() { 7 } 8 public Fraction(int x, int y) { 9 this.x = x; 10 this.y = y; 11 } 12 // 分数的输入 13 public void input() { 14 Scanner in = new Scanner(System.in); 15 x = in.nextInt(); 16 y = in.nextInt(); 17 } 18 // 分数的输出 19 public void output() { 20 21 if (y < 0) { 22 x = -x; 23 y = -y; 24 } 25 System.out.println(x + "/" + y); 26 } 27 // 求分子、分母的最大公因数 28 public static int max(int a, int b) { 29 int c = 0; 30 while (a % b != 0) { 31 c = a % b; 32 a = b; 33 b = c; 34 } 35 return c; 36 } 37 // 化简分数 38 public Fraction simp() { 39 int p = max(x, y); 40 x = x / p; 41 y = y / p; 42 Fraction f = new Fraction(x, y); 43 return f; 44 } 45 // 分数的加法 46 public Fraction add(Fraction f) { 47 int p = max(x * f.y + y * f.x, y * f.y); 48 if (y == f.y) { 49 Fraction c = new Fraction(x + f.x, y); 50 return c; 51 } else { 52 Fraction c = new Fraction((x * f.y + y * f.x) / p, (y * f.y) / p); 53 return c; 54 } 55 } 56 // 分数的减法 57 public Fraction sub(Fraction f) { 58 int p = max(x * f.y - y * f.x, y * f.y); 59 if (y == f.y) { 60 Fraction c = new Fraction(x - f.x, y); 61 return c; 62 } else { 63 Fraction c = new Fraction((x * f.y - y * f.x) / p, (y * f.y) / p); 64 return c; 65 } 66 } 67 // 分数的数乘 68 public Fraction mul(int n) { 69 int p = max(x * n, y); 70 Fraction c = new Fraction((x * n) / p, y / p); 71 return c; 72 } 73 74 // 判断关系:分数1==分数2 75 public boolean dengYu(Fraction c) { 76 int p1 = max(x, y); 77 int p2 = max(c.x, c.y); 78 if (x / p1 == c.x / p2 && y / p1 == c.y / p2) { 79 return true; 80 } else { 81 return false; 82 } 83 } 84 // 判断关系:分数1!=分数2 85 public boolean buDengYu(Fraction c) { 86 int p1 = max(x, y); 87 int p2 = max(c.x, c.y); 88 if (x / p1 == c.x / p2 && y / p1 == c.y / p2) { 89 return false; 90 } else { 91 return true; 92 } 93 } 94 // 判断关系:分数1>=分数2 95 public boolean daYu(Fraction c) { 96 int n1 = x * c.y; 97 int n2 = y * c.x; 98 if (n1 >= n2) { 99 return true; 100 } else { 101 return false; 102 } 103 } 104 // 判断关系:分数1<=分数2 105 public boolean xiaoYu(Fraction c) { 106 int n1 = x * c.y; 107 int n2 = y * c.x; 108 if (n1 <= n2) { 109 return true; 110 } else { 111 return false; 112 } 113 } 114 public static void main(String[] args) { 115 int t, n; 116 Fraction f1 = new Fraction(); 117 Fraction f2 = new Fraction(); 118 // 菜单打印 119 System.out.println("功能选择:"); 120 System.out.println("**************"); 121 System.out.println("1.分数加法"); 122 System.out.println("2.分数减法"); 123 System.out.println("3.分数数乘"); 124 System.out.println("4.分数关系比较"); 125 System.out.println("**************"); 126 System.out.print("请输入你的选择:"); 127 Scanner in = new Scanner(System.in); 128 t = in.nextInt(); 129 if (t == 1) { 130 System.out.print("请输入分数1:"); 131 f1.input(); 132 System.out.print("请输入分数2:"); 133 f2.input(); 134 System.out.print("相加结果:"); 135 (f1.add(f2)).output(); 136 } 137 if (t == 2) { 138 System.out.print("请输入分数1:"); 139 f1.input(); 140 System.out.print("请输入分数2:"); 141 f2.input(); 142 System.out.print("相减结果:"); 143 (f1.sub(f2)).output(); 144 } 145 if (t == 3) { 146 System.out.print("请输入分数:"); 147 f1.input(); 148 System.out.print("请输入乘数:"); 149 n = in.nextInt(); 150 System.out.print("数乘结果:"); 151 (f1.mul(n)).output(); 152 } 153 if (t == 4) { 154 System.out.print("请输入分数1:"); 155 f1.input(); 156 System.out.print("请输入分数2:"); 157 f2.input(); 158 if (f1.daYu(f2)) { 159 System.out.print("分数1==分数2"); 160 } 161 if (f1.buDengYu(f2)) { 162 System.out.println("分数1!=分数2"); 163 } 164 if (f1.daYu(f2)) { 165 System.out.println("分数1>=分数2"); 166 } 167 if (f1.xiaoYu(f2)) { 168 System.out.println("分数1<=分数2"); 169 } 170 } 171 } 172 }