2020年8月6日Java学习日记
学习内容:
1、复数类
public class Complex { private double real,imag; public void setvalue(double a,double b) { real=a; imag=b; } public double getReal() { return real; } public double getImag() { return imag; } public void display() { System.out.print(real); if(imag>0) System.out.println("+"+imag+"i"); else System.out.println(imag+"i"); } public Complex conjugate() { Complex c=new Complex(); c.real=real; c.imag=-1*imag; return c; } public static void main(String[] args) { Complex con=new Complex(); Complex con1=new Complex(); con.setvalue(3.2, 5.6); System.out.println("复数的实部为:"+con.getReal()); System.out.println("复数的虚部为:"+con.getImag()); System.out.print("复数为:"); con.display(); con1=con.conjugate(); System.out.print("该复数的共轭复数为:"); con1.display(); } }
2、学生类
import java.util.Scanner; public class Student1 { private int age; private String name; public void Input(int a,String n) { age=a; name=n; } public void Output() { System.out.println("姓名:"+name+"\t"+"年龄:"+age); } public static void main(String[] args) { Student1[] stu=new Student1[3]; Scanner con=new Scanner(System.in); for(int i=0;i<3;i++) { stu[i]=new Student1(); System.out.println("请输入第"+(i+1)+"位同学信息(姓名、年龄):"); stu[i].name=con.next(); stu[i].age=con.nextInt(); stu[i].Input(stu[i].age, stu[i].name); } for(int i=0;i<3;i++) { System.out.print("第"+(i+1)+"位同学:"); stu[i].Output(); } } }
3、点类
import java.util.Scanner; public class Point1 { private float x,y,z; public void Input() { Scanner con=new Scanner(System.in); System.out.println("请输入坐标(x、y、z):"); x=con.nextFloat(); y=con.nextFloat(); z=con.nextFloat(); } public float getX() {return x;} public float getY() {return y;} public float getZ() {return z;} public void Output() { System.out.println("坐标为:("+x+","+y+","+z+")"); } public static void main(String[] args) { Point1 p=new Point1(); p.Input(); p.Output(); System.out.println("横坐标为:"+p.getX()); System.out.println("纵坐标为:"+p.getY()); System.out.println("竖坐标为:"+p.getZ()); } }
4、线段类
import java.lang.Math; public class Line1 { private Point1 p1=new Point1(); private Point1 p2=new Point1(); public void Input() { System.out.println("请输入线段的起点和终点坐标:"); p1.Input(); p2.Input(); } public void display() { double d; 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())); System.out.printf("线段的长度为:%-9.4f",d); } public static void main(String[] args) { Line1 l=new Line1(); l.Input(); l.display(); } }