2020.8.11
一、今日学习内容
1、复数类
1 public class Complex { 2 private double real,imag; 3 public void setvalue(double a,double b) { 4 real=a; 5 imag=b; 6 } 7 public double getReal() { 8 return real; 9 } 10 public double getImag() { 11 return imag; 12 } 13 public void display() { 14 System.out.print(real); 15 if(imag>0) 16 System.out.println("+"+imag+"i"); 17 else 18 System.out.println(imag+"i"); 19 } 20 public Complex conjugate() { 21 Complex c=new Complex(); 22 c.real=real; 23 c.imag=-1*imag; 24 return c; 25 } 26 public static void main(String[] args) { 27 Complex con=new Complex(); 28 Complex con1=new Complex(); 29 con.setvalue(3.2, 5.6); 30 System.out.println("复数的实部为:"+con.getReal()); 31 System.out.println("复数的虚部为:"+con.getImag()); 32 System.out.print("复数为:"); 33 con.display(); 34 con1=con.conjugate(); 35 System.out.print("该复数的共轭复数为:"); 36 con1.display(); 37 } 38 }
2、学生类
1 import java.util.Scanner; 2 public class Student1 { 3 private int age; 4 private String name; 5 public void Input(int a,String n) { 6 age=a; 7 name=n; 8 } 9 public void Output() { 10 System.out.println("姓名:"+name+"\t"+"年龄:"+age); 11 } 12 public static void main(String[] args) { 13 Student1[] stu=new Student1[3]; 14 Scanner con=new Scanner(System.in); 15 for(int i=0;i<3;i++) { 16 stu[i]=new Student1(); 17 System.out.println("请输入第"+(i+1)+"位同学信息(姓名、年龄):"); 18 stu[i].name=con.next(); 19 stu[i].age=con.nextInt(); 20 stu[i].Input(stu[i].age, stu[i].name); 21 } 22 for(int i=0;i<3;i++) { 23 System.out.print("第"+(i+1)+"位同学:"); 24 stu[i].Output(); 25 } 26 } 27 }
3、点类
1 import java.util.Scanner; 2 public class Point1 { 3 private float x,y,z; 4 public void Input() { 5 Scanner con=new Scanner(System.in); 6 System.out.println("请输入坐标(x、y、z):"); 7 x=con.nextFloat(); 8 y=con.nextFloat(); 9 z=con.nextFloat(); 10 } 11 public float getX() {return x;} 12 public float getY() {return y;} 13 public float getZ() {return z;} 14 public void Output() { 15 System.out.println("坐标为:("+x+","+y+","+z+")"); 16 } 17 public static void main(String[] args) { 18 Point1 p=new Point1(); 19 p.Input(); 20 p.Output(); 21 System.out.println("横坐标为:"+p.getX()); 22 System.out.println("纵坐标为:"+p.getY()); 23 System.out.println("竖坐标为:"+p.getZ()); 24 } 25 }
4、线段类
1 import java.lang.Math; 2 public class Line1 { 3 private Point1 p1=new Point1(); 4 private Point1 p2=new Point1(); 5 public void Input() { 6 System.out.println("请输入线段的起点和终点坐标:"); 7 p1.Input(); 8 p2.Input(); 9 } 10 public void display() { 11 double d; 12 d=Math.sqrt((p1.getX()-p2.getX())*(p1.getX()-p2.getX())+(p1.getY()-p2.getY())*(p1.getY()-p2.getY())+(p1.getZ()-p2.getZ())*(p1.getZ()-p2.getZ())); 13 System.out.printf("线段的长度为:%-9.4f",d); 14 } 15 16 public static void main(String[] args) { 17 Line1 l=new Line1(); 18 l.Input(); 19 l.display(); 20 } 21 }
二、遇到的问题
在做例题时,有时会报错,不知道原因,数组中每一个对象都需要初始化。
三、明日计划
明天开始PPT第四讲的验证