Java第三次实训作业

1.编写“学生”类及其测试类

  “学生”类:

类名:Student

属性:姓名、性别、年龄、学号、5门课程的成绩

方法1:在控制台输出各个属性的值

方法2:计算平均成绩

方法3:输出各个属性的值和平均成绩

  测试类

创建2个对象,调用方法,要求:对象各个属性的值,从键盘输入。

代码

 1 package Person;
 2 import java.util.*;
 3 
 4 public class Student {
 5     String name;
 6     String sex;
 7     int age;
 8     String num;
 9     static int[]score=new int[5];
10     
11     public void myprint(){
12         System.out.println("姓名:"+name+"   性别:"+sex+"   学号:"+num+"   年龄:"+age);
13         System.out.println("五门科目的成绩为:");
14         for(int i=0;i<score.length;i++){
15             System.out.println(score[i]);
16         }
17     }
18     void aver(){
19         int sum=0;
20         for(int i=0;i<score.length;i++){
21             sum=sum+score[i];
22         }
23         System.out.println("平均分为:");
24         double average=sum/score.length;
25         System.out.println(average);
26     }
27 
28     public static void main(String[] args) {
29         Scanner sc=new Scanner(System.in);
30         Student s1=new Student();
31         System.out.println("请输入姓名、性别、学号、年龄:");
32         s1.name=sc.nextLine();
33         s1.sex=sc.nextLine();
34         s1.num=sc.nextLine();
35         s1.age=sc.nextInt();
36         
37         System.out.println("请输入五门科目的成绩:");
38         for(int i=0;i<score.length;i++){
39             score[i]=sc.nextInt();
40         }
41         
42         s1.myprint();
43         s1.aver();
44         
45         
46     }
47 }

运行界面

Java第三次实训作业

 

 

2.编写“借书卡”类及其测试类

   “借书卡”类

属性:账号、持卡人姓名、身份证号码、地址、已借书数、可借书数、本次借书数、本次还书数

方法一:借书,显示本次借书数和可借书数

方法二:还书,显示本次还书数和已借书数

  测试类

本次借书数 和 本次还书数,从键盘输入

代码

 1 package Person;
 2 import java.util.*;
 3 
 4 public class Bookcard{
 5     String num;
 6     String name;
 7     String id;
 8     String address;
 9     int x,y,m,n;        //x=已借书数、y=可借书数、m=本次借书数、n=本次还书数
10     Scanner sc=new Scanner(System.in);
11     
12     public void input(){            
13         System.out.println("请依次输入您的帐号、名字、身份证号、地址");
14         name=sc.nextLine();
15         name=sc.nextLine();
16         id=sc.nextLine();
17         address=sc.nextLine();
18     }
19     public void Borrow(int all){
20         System.out.println("您的图书卡总共能借出的书数为:"+all);
21         System.out.println("请输入您本次借书的数目:");
22         m=sc.nextInt();
23         y=all-m;
24         System.out.println("您可借书数为:"+y);
25         
26     }
27     public void Return(int all){
28         System.out.println("请输入您本次还书的数目:");
29         n=sc.nextInt();
30         x=y-n;
31         System.out.println("您已借书数为:"+x);
32     }
33     
34     public static void main(String[] args) {
35         Bookcard b=new Bookcard();
36         int all=10;
37         b.input();
38         b.Borrow(all);
39         b.Return(all);
40         
41     }
42 }

运行界面

Java第三次实训作业

 

 

3.编写“用户”类及其测试类。

此程序实现了简易的“登录”功能,即验证用户名和密码是否正确。

  “用户”类

属性:用户名、密码

方法1:查找用户名是否存在(请将已注册的用户信息保存在数组中),用户存在返回true,不存在返回false

方法2:验证密码是否正确,正确返回true,不正确返回false

  测试类

从键盘输入用户名和密码

用户名、密码均正确,在控制台输出“登录成功”

用户名不存在,在控制台输出“用户名不存在”

密码不正确,在控制台输出“密码不正确”

代码

 

 

4.编写“电费管理类”及其测试类。

  “电费”类

属性:上月电表读数、本月电表读数

方法一:显示上月、本月电表读数

方法二:计算本月用电数

方法三:显示本月用电数

方法四:假设每度电的价格为1.2元,计算并显示本月电费

  测试类

上月电表读数、本月电表读数,从键盘输入

代码

 1 package Person;
 2 import java.util.Scanner;
 3 
 4 public class Electricityfees {
 5     private int lastmonth;
 6     private int themonth;
 7     
 8     public int getLastmonth() {
 9         return lastmonth;
10     }
11     public void setLastmonth(int lastmonth) {
12         this.lastmonth = lastmonth;
13     }
14     
15     public int getThemonth() {
16         return themonth;
17     }
18     public void setThemonth(int themonth) {
19         this.themonth = themonth;
20     }
21     
22     public void myscanner(){
23         Scanner sc = new Scanner(System.in);
24         System.out.println("请输入上月电表读数为:");
25         lastmonth = sc.nextInt();
26         System.out.println("请输入本月电表读数为:");
27         themonth = sc.nextInt();
28     }
29     public void count(){
30         double fees;
31         fees = 1.2*themonth;
32         System.out.println("本月电费为:"+fees);
33     }
34     
35 }
 1 package Person;
 2 
 3 public class TestElectricityfees {
 4     public static void main(String[] args) {
 5         Electricityfees e1 = new Electricityfees();
 6         e1.getLastmonth();
 7         e1.getThemonth();
 8         e1.myscanner();
 9         e1.count();
10         
11         Electricityfees e2 = new Electricityfees();
12         e2.setLastmonth(1200);
13         e2.myscanner();        
14         e2.setThemonth(1500);
15         e2.count();
16 
17 
18     }
19 }

运行界面

 Java第三次实训作业

 

 

5.编写“四则运算类”及其测试类。

应用场景:计算器。能实现简单的四则运算,要求:只进行一次运算。

  “四则运算”类

属性:操作数一、操作数二、操作符

方法一:对两个操作数做加运算

方法二:对两个操作数做减运算

方法三:对两个操作数做乘运算

方法四:对两个操作数做除运算

  测试类

从键盘输入两个操作数和一个操作符,计算之后,输出运算结果。

代码

 1 package Person;
 2 import java.util.*;
 3 
 4 public class Four {
 5     private double x;
 6     private double y;
 7     private String i;
 8     
 9     public double getX() {
10         return x;
11     }
12     public void setX(double x) {
13         this.x = x;
14     }
15     public double getY() {
16         return y;
17     }
18     public void setY(double y) {
19         this.y = y;
20     }
21     public String getI() {
22         return i;
23     }
24     public void setI(String i) {
25         this.i = i;
26     }
27     
28     public void myprint(){
29         System.out.println("请依次输入操作数1、操作符2、操作符:");
30         Scanner sc = new Scanner(System.in);
31         x=sc.nextDouble();
32         y=sc.nextDouble();
33         i=sc.next();
34     }
35     public int flag(){
36         int flag = 0;
37         if(i.equals("+")){
38             flag=1;
39         }
40         else if(i.equals("-")){
41             flag=2;
42         }
43         else if(i.equals("*")){
44             flag=3;
45         }
46         else if(i.equals("/")){
47             flag=4;
48         }
49         return flag;
50     }
51     public void plus(){                //
52         double sum;
53         sum=x+y;
54         System.out.println(x+i+y+"="+sum);
55     }
56     public void reduce(){            //
57         double cut;
58         cut=x-y;
59         System.out.println(x+i+y+"="+cut);
60     }
61     public void multiplication(){            //
62         double product;
63         product=x*y;
64         System.out.println(x+i+y+"="+product);
65     }
66     public void division(){            //
67         double Quotient;
68         Quotient=x/y;
69         System.out.println(x+i+y+"="+Quotient);
70     }
71     
72     
73 
74 }
 1 package Person;
 2 
 3 public class TestFour {
 4     public static void main(String[] args) {
 5         Four f = new Four();
 6         int flag;
 7         
 8         f.myprint();
 9         flag=f.flag();
10         switch(flag){
11             case 1:f.plus();break;
12             case 2:f.reduce();break;
13             case 3:f.multiplication();break;
14             case 4:f.division();break;
15         }
16 
17     }
18 }

 运行界面

Java第三次实训作业