Java入门——day46

1.Dog类

定义一个Dog 类,包含了age,weight 等属性,以及对这些属性操作的方法,实现并测试这个类。

 1 //Dog类
 2 public class Dog {
 3     private int age;     //年龄
 4     private int weight;  //体重
 5     public Dog() {
 6     }
 7     public Dog(int age, int weight) {
 8         this.age = age;
 9         this.weight = weight;
10     }
11     public int getAge() {
12         return age;
13     }
14     public void setAge(int age) {
15         this.age = age;
16     }
17     public int getWeight() {
18         return weight;
19     }
20     public void setWeight(int weight) {
21         this.weight = weight;
22     }
23     public static void main(String[] args) {
24         Dog kiki=new Dog(5,12);
25         System.out.println("Dog name:kiki");
26         System.out.println("age:"+kiki.getAge());
27         System.out.println("weight:"+kiki.getWeight());
28     }
29 }


  2.Rectangle类

设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,根据坐标能计算矩形的面积。

 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 }


3.人员类

设计一个用于人事管理的“人员”类。由于考虑到通用性,这里只抽象出所有类型人员都具有的属性:编号、性别、出生日期、身份证号等。其中“出生日期”声明为一个“日期”类内嵌子对象。用成员函数实现对人员信息的录入和显示。

 1 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 }


4.矩形类

定义并实现一个矩形类,有厂、宽两个属性,由成员函数计算矩形的面积。

 1 import java.util.Scanner;
 2 //矩形类
 3 public class Rectangle {
 4     private int length; //矩形的长
 5     private int width;  //矩形的宽
 6     public Rectangle() {
 7     }
 8     public Rectangle(int length, int width) {
 9         this.length = length;
10         this.width = width;
11     }
12     //计算矩形的面积
13     public void area() {
14         int s;
15         s=length*width;
16         System.out.println("矩形面积:"+s);
17     }
18     public static void main(String[] args) {
19         Scanner in=new Scanner(System.in);
20         int a,b;
21         System.out.print("请输入矩形的长和宽:");
22         a=in.nextInt();
23         b=in.nextInt();
24         Rectangle r=new Rectangle(a,b);
25         r.area();
26     }
27 }


5.数据类型类

定义一个DataType(数据类型)类,能处理包含字符型、整型、浮点型3种类型的数据,给出其构造函数。

 1 //数据类型
 2 public class DataType {
 3     private char s;  //字符型
 4     private int a;   //整型
 5     private float b; //浮点型
 6     public DataType() {
 7     }
 8     public DataType(char s, int a, float b) {
 9         this.s = s;
10         this.a = a;
11         this.b = b;
12     }
13     public void display() {
14         System.out.println("字符型:"+s);
15         System.out.println("整型:"+a);
16         System.out.println("浮点型:"+b);
17     }
18     public static void main(String[] args) {
19         DataType p=new DataType('m',10,125.2f);
20         p.display();
21     }
22 }


6.Circle类

定义一个Circle 类,有数据成员radius(半径),成员函数getArea(),计算圆的面积,构造一个Circle的对象进行测试。

 1 import java.util.Scanner;
 2 //Circle类
 3 public class Circle {
 4     private double radius; //半径
 5     public Circle() {
 6     }
 7     public Circle(double radius) {
 8         this.radius = radius;
 9     }
10     public void getArea() {
11         double s;
12         s=3.1415926*radius*radius;
13         System.out.printf("面积:%-10f",s);
14     }
15     public static void main(String[] args) {
16         Scanner in=new Scanner(System.in);
17         double r;
18         System.out.print("半径:");
19         r=in.nextDouble();
20         Circle c=new Circle(r);
21         c.getArea();
22     }
23 }


7.树类

定义一个Tree(树)类,有成员ages(树龄),成员函数grow(int years)对ages加上years,age()显示Tree对象的ages的值。

 1 import java.util.Scanner;
 2 //Tree类
 3 public class Tree {
 4     private int ages; //树龄
 5     public Tree() {
 6     }
 7     public Tree(int ages) {
 8         this.ages=ages;
 9     }
10     //增加生长年数
11     public void grow(int years) {
12         ages+=years;
13     }
14     //显示树龄
15     public void age() {
16         System.out.println("树龄:"+ages);
17     }
18     public static void main(String[] args) {
19         Scanner in=new Scanner(System.in);
20         int a,b;
21         System.out.print("请输入原有树龄:");
22         a=in.nextInt();
23         System.out.print("请输入生长的年数:");
24         b=in.nextInt();
25         Tree t=new Tree(a);
26         t.grow(b);
27         t.age();
28     }
29 }


8.CPU类

编写一个名为CPU的类,描述一个CPU的以下信息:时钟频率,最大不会超过3000MHZ;字长,可以是32位或64位;核数,可以是单核、双核或四核;是否支持超线程。各项信息要求使用位域来表示。

 1 //CPU类
 2 enum Word{
 3     bit32,bit64
 4 }
 5 enum Heshu{
 6     one,two,four
 7 }
 8 enum Yes_no{
 9     yes,no
10 }
11 public class CPU {
12     private int fre;
13     private Word wd;
14     private Heshu hu;
15     private Yes_no yo;
16     public CPU() {
17     }
18     public CPU(int fre, Word wd, Heshu hu, Yes_no yo) {
19         this.fre = fre;
20         this.wd = wd;
21         this.hu = hu;
22         this.yo = yo;
23     }
24     public void show() {
25         System.out.println("时钟频率:"+fre);
26         System.out.print("字长:");
27         switch(wd) {
28         case bit32:System.out.println("32位");break;
29         case bit64:System.out.println("64位");break;
30         }
31         System.out.print("核数:");
32         switch(hu) {
33         case one:System.out.println("单核");break;
34         case two:System.out.println("双核");break;
35         case four:System.out.println("四核");break;
36         }
37         System.out.print("是否支持超线程:");
38         switch(yo) {
39         case yes:System.out.println("是");break;
40         case no:System.out.println("否");break;
41         }
42     }
43     public static void main(String[] args) {
44         CPU c=new CPU(300000,Word.bit64,Heshu.two,Yes_no.yes);
45         c.show();
46     }
47 }

原文地址:https://www.cnblogs.com/znjy/p/13537457.html