Java第二次作业

以下是我所提交的作业:

第一个程序:

 1 package javaApplication;
 2 import java.util.Scanner;
 3 class Person 
 4 {
 5     private String name;
 6     private char sex;
 7     private int age;
 8     private String idnum;
 9     public Person(String name,char sex,int age,String idnum)
10     {
11         this.name=name;
12         this.sex=sex;
13         this.age=age;
14         this.idnum=idnum;
15     }
16     public void OutputInformation()
17     {
18         System.out.println(name+", "+sex+", "+age+", "+idnum);
19         
20     }
21 }
22 public class TsetPerson
23 {
24     public static void main(String[] args)
25     {
26         Scanner scanner=new Scanner(System.in);
27         System.out.print("请输入录入学生信息的个数:");
28         int studentnum=scanner.nextInt();
29         Person stu[]=new Person[studentnum];
30         System.out.println("请录入学生信息");
31         for(int i=0;i<stu.length;i++)
32         {
33             System.out.println("请分别输入学生"+(i+1)+"的姓名、性别、年龄、身份证号码:");
34             String name=scanner.next();
35             
36             String ch=scanner.next();
37             char sex=ch.charAt(0);
38             
39             int age=scanner.nextInt();
40             
41             String idnum=scanner.next();
42             
43             stu[i]=new Person(name,sex,age,idnum);
44             System.out.println();
45         }
46         System.out.println();
47         for(int i=0;i<stu.length;i++)
48         {
49             System.out.println("学生"+(i+1)+"的信息:");
50             stu[i].OutputInformation();
51         }
52         scanner.close();
53     }
54 }

运行结果如下:

在这个程序中,我将对象的信息通过输入来完成,这有点与题意不符。但就个人而言,通过此次作业复习下如何输入,顺带总结输入字符串时,next()方法和nextLine()的区别,及相关字符串方法;

nextLine()方法:

    返回的是Enter键之前的所有字符,它是可以得到带空格的字符串(相当于C语言中的gets输入字符串)。结束符只是Enter键。可以通过此方法输入英语句子。

next()方法:

       会自动消去带有效字符前的空格,只返回输入的字符,不能得到带空格的字符串(相当于C语言中的scanf输入字符串)。结束符有空格键、Tab键、Enter键。此方法不能用于输入英语句子。

 个人未注意的地方:next()方法一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键、Tab键或Enter键等结束符,next()方法会自动的将其去掉,只有在输入有效字符之后,next()方法才将其后的输入的空格键、Tab键或Enter键视为分隔符或结束符。

附上自己编写程序时所遇到的错误:

 运行后是这样:

 并没有如预期的那样,输入姓名后输入性别,而是直接输入性别,输入姓名那一行被跳过了。

后通过查找资料,才发现其原因:

nextLine()自动读取了被nextInt()去掉的Enter作为他的结束符,所以没办法从键盘输入值。(即nextLine()读到了nextInt()的结束符,同时也是nextLine()它自己的结束符,得到一个空的字符串)。个人解释:nextLine()的有效字符为所有,即任何字符它都接收,而其他方法未得到其有效字符时,会将其结束符去掉。(如若有误,还请大家纠正)

解决方法:在nextInt() 后加scanner.nextLine() (scanner为此程序Scanner的对象)其他方法与nextLine()连用一样。

附上解决后的代码:

 1         System.out.print("请输入录入学生信息的个数:");
 2         int studentnum=scanner.nextInt();
 3         Person stu[]=new Person[studentnum];
 4         System.out.println("请录入学生信息");
 5         for(int i=0;i<stu.length;i++)
 6         {
 7             scanner.nextLine();
 8             System.out.print("姓名:");
 9             String name=scanner.nextLine();
10             
11             System.out.print("性别:");
12             String ch=scanner.nextLine();
13             char sex=ch.charAt(0);
14             
15             System.out.print("年龄:");
16             int age=scanner.nextInt();
17             
18             System.out.print("身份证号码:");
19             String idnum=scanner.next();
20             
21             stu[i]=new Person(name,sex,age,idnum);
22             System.out.println();
23         }

运行结果显示:

 同时在这个程序中,由于不能单个输入字符,所以通过字符串方法charAt(0)得到输入字符串的第一个字符,从而获得单个字符,

顺带记忆下字符串方法:

连接字符串:使用+运算符;

获取字符串长度:length();

字符串查找:str.indexOf(substr)和str.lastIndexOf(substr);

获取指定索引位置字符:str.charAt();

获取子字符串:str.substring(int beginIndex) 和 str.substring(int beginIndex,int endIndex);

去除空格:trim();

字符串替换:str.replace(char oldChar,char newChar);

判段字符串开始与结尾:str.startsWith(String prefix) 、str.endsWith(String suffix);

字符串比较:str.compareTo();

字符串分割:str.split(String sign),str.split(String sign,int limit);

字母大小写转换:str.toLowerCase() 和str.toUpperCase();

以及append(contet)、insert(int offset,arg)、delete(int start,int end);

第二个程序:

 1 package javaApplication;
 2 class Phone
 3 {
 4     private String brand;
 5     private String model;
 6     public Phone(String brand,String model)
 7     {
 8         this.brand=brand;
 9         this.model=model;
10     }
11     public void OutputInformation()
12     {
13         System.out.println("品牌:"+brand+"     型号:"+model);
14     }
15 }
16 public class TestPhone
17 {
18     public static void main(String args[])
19     {
20         Phone s[]= {
21                         new Phone("华为","荣耀3c"),
22                         new Phone("联想","A3600D"),
23                         new Phone("小米","note"),
24                    };
25         for(int i=0;i<s.length;i++)
26         {
27             s[i].OutputInformation();
28         }
29     }
30 
31 }

运行结果如下:

第三个程序:

 1 package javaApplication;
 2 class Book
 3 {
 4     private String bookname;
 5     private String booknum;
 6     private String bookauthor;
 7     private String publishhouse;
 8     private String booktime;
 9     private int bookpage;
10     private double bookprice;
11     
12     public Book(
13                 String bookname, String booknum,
14                 String bookauthor, String publishhouse,
15                 String booktime, int bookpage,double bookprice
16                 )
17     {
18         super();
19         this.bookname = bookname;
20         this.booknum = booknum;
21         this.bookauthor = bookauthor;
22         this.publishhouse = publishhouse;
23         this.booktime = booktime;
24         this.bookpage = bookpage;
25         this.bookprice = bookprice;
26     }
27 
28     public void OutputInformation()
29     {
30         System.out.println("书名:"+bookname+"   书号:"+booknum);
31         System.out.println("主编:"+bookauthor+"   出版社:"+publishhouse);
32         System.out.println("出版时间:"+booktime+"  页数:"+bookpage);
33         System.out.println("价格:"+bookprice);
34     }
35 }
36 public class TestBook 
37 {
38     public static void main(String args[])
39     {
40         Book b1=new Book(
41                           "Java从入门到精通","05884701",
42                           "赵洛育","清华大学出版社","2016年10月",
43                           564,69.80);
44         Book b2=new Book(
45                            "数据结构","06832301",
46                            "殷人昆","清华大学出版社","2017年5月",
47                            400,49.50);
48         b1.OutputInformation();
49         System.out.println();
50         b2.OutputInformation();
51     }
52 
53 }

运行结果如下:

 第四个程序如下:

 1 package javaApplication;
 2 
 3 class Circle
 4 {
 5     final static double PI=3.14159;
 6     private double radius;
 7     public Circle()
 8     {
 9         radius=0;
10     }
11     public Circle(double r)
12     {
13         radius=r;
14     }
15     public double getRadius()
16     {
17         return radius;
18     }
19     public double getPerimeter() //计算圆的周长
20     {
21         return radius*2*PI;
22     }
23     public double getArea() //计算圆的面积
24     {
25         return radius*radius*PI;
26     }
27     
28 }
29 class Cylinder extends Circle
30 {
31     double height;
32     public Cylinder(double r,double h)
33     {
34         super(r);
35         height=h;
36     }
37     public double getHeight() {
38         return height;
39     }
40     public double getCylinderArea() //计算圆柱体表面积
41     {
42         return super.getPerimeter()*height+getArea()*2;
43     }
44     double getVol() //计算圆柱体的体积
45     {
46         return super.getArea()*height;
47     }
48     public void dispVol()
49     {
50         System.out.println("半径为:"+getRadius());
51         System.out.println("高为:"+getHeight());
52         System.out.println("底面积为:"+getArea());
53         System.out.println("表面积为:"+getPerimeter());
54         System.out.println("体积为:"+getCylinderArea());
55     }
56     
57 }
58 public class TestCylinder 
59 {
60     public static void main(String[] args)
61     {
62         Cylinder c1=new Cylinder(10,20);
63         Cylinder c2=new Cylinder(10,40);
64         System.out.println("圆柱体c1的信息为:");
65         c1.dispVol();
66         System.out.println();
67         System.out.println("圆柱体c2的信息为:");
68         c2.dispVol();
69         
70     }
71     
72 }

运行结果为:

 以上程序较为简单,所以没有什么注释。

复习下一个知识点(也是自己经常忘记得):

将子类对象赋值给父类对象,所得到的对象具有的特征如下,

1、被声明为父类对象;

2、拥有父类的属性;

3、占用子类的内存空间;

4、子类方法覆盖父类方法时,此时对象调用的是子类方法;否则自动调用继承父类的方法;

5、无法访问子类中非覆盖的变量和方法;

(此知识点摘自某本习题集)。

猜你喜欢

转载自www.cnblogs.com/duwenze/p/10634297.html