1 import java.util.Scanner; 2 //点类 3 class Point{ 4 private float x; //横坐标 5 private float y; //纵坐标 6 public Point(){ 7 } 8 public Point(float x,float y) { 9 this.x=x; 10 this.y=y; 11 } 12 public float getX() { 13 return x; 14 } 15 public float getY() { 16 return y; 17 } 18 } 19 //矩形类 20 public class Rectangle { 21 private Point p1=new Point(); //矩形左下角点 22 private Point p2=new Point(); //矩形右上角点 23 public Rectangle() { 24 } 25 public Rectangle(Point p1, Point p2) { 26 this.p1 = p1; 27 this.p2 = p2; 28 } 29 //计算矩形面积 30 public void Area() { 31 double s; 32 s=(double)Math.abs((p2.getX()-p1.getX())*(p2.getY()-p1.getY())); 33 System.out.println(s); 34 } 35 public static void main(String[] args) { 36 Scanner in=new Scanner(System.in); 37 float a,b,c,d; 38 System.out.print("请输入矩形左下角点的坐标:"); 39 a=in.nextFloat(); 40 b=in.nextFloat(); 41 System.out.print("请输入矩形右上角点的坐标:"); 42 c=in.nextFloat(); 43 d=in.nextFloat(); 44 Point p1=new Point(a,b); 45 Point p2=new Point(c,d); 46 Rectangle r=new Rectangle(p1,p2); 47 System.out.print("此矩形面积:"); 48 r.Area(); 49 } 50 }
import java.util.Scanner; 2 //日期类 3 class Date{ 4 private int year; 5 private int month; 6 private int day; 7 public Date() { 8 } 9 public Date(int year,int month,int day) { 10 this.year=year; 11 this.month=month; 12 this.day=day; 13 } 14 public void set() { 15 Scanner in=new Scanner(System.in); 16 year=in.nextInt(); 17 month=in.nextInt(); 18 day=in.nextInt(); 19 } 20 public void display() { 21 System.out.println(year+"."+month+"."+day); 22 } 23 } 24 //人员类 25 public class People { 26 private int num; //编号 27 private String sex; //性别 28 private Date date=new Date(); //出生日期 29 private String ID; //身份证号 30 public People() { 31 } 32 public People(int num, String sex, Date date, String iD) { 33 super(); 34 this.num = num; 35 this.sex = sex; 36 this.date = date; 37 ID = iD; 38 } 39 public void input() { 40 Scanner in=new Scanner(System.in); 41 System.out.println("请输入人员信息:"); 42 System.out.print("编号:"); 43 num=in.nextInt(); 44 System.out.print("性别:"); 45 sex=in.next(); 46 System.out.print("出生日期:"); 47 date.set(); 48 System.out.print("身份证号:"); 49 ID=in.next(); 50 } 51 public void output() { 52 System.out.println("编号:"+num); 53 System.out.println("性别:"+sex); 54 System.out.println("出生日期:"); 55 date.display(); 56 System.out.println("身份证号:"+ID); 57 } 58 public static void main(String[] args) { 59 People p=new People(); 60 p.input(); 61 p.output(); 62 } 63 }
二
三.例题