Java程序设计---实验3

1、编一个学生类(Student),其中包含以下内容:

属性:学号studentNo,姓名studentName,性别studentGender,年龄studentAge。

方法:构造方法,显示学号方法showNo(),显示姓名方法showName(),显示性别方法showSex(),显示年龄方法showAge(),修改年龄方法modifyAge()。

主类(S3_1)包含:主方法main(),在其中创建两个学生对象s1和s2并初始化,两个对象的属性自行确定,然后分别显示这两个学生的学号、姓名、性别、年龄,然后修改s1的年龄并显示修改后的结果。

import java.util.Scanner;
public class S3_1 {
    public static void main(String[] args){
        Scanner sca=new Scanner(System.in);
        Student stu1=new Student();
        stu1.studentNo=2022001;
        stu1.studentName="张三";
        stu1.studentGender="男";
        stu1.studentAge=20;
        System.out.println("学生1的信息:");
        stu1.showNo();
        stu1.showName();
        stu1.showSex();
        stu1.showAge();
        Student stu2=new Student();
        stu2.studentNo=2022001;
        stu2.studentName="王梅";
        stu2.studentGender="女";
        stu2.studentAge=19;
        System.out.println("学生2的信息:");
        stu2.showNo();
        stu2.showName();
        stu2.showSex();
        stu2.showAge();
        System.out.println("修改学生1的年龄");
        System.out.print("请输入你要修改的年龄:");
        int n=sca.nextInt();
        stu1.modifyAge(n);
    }
}

class Student{
    int studentNo;
    String studentName;
    String studentGender;
    int studentAge;
    void showNo(){
        System.out.println("学号:"+studentNo);
    }
    void showName(){
        System.out.println("姓名:"+studentName);
    }
    void showSex(){
        System.out.println("性别:"+studentGender);
    }
    void showAge(){
        System.out.println("年龄:"+studentAge);
    }
    void modifyAge(int n){
      this.studentAge=n;
        System.out.println("学生1修改后的信息:");
        this.showNo();
        this.showName();
        this.showSex();
        this.showAge();
    }
}

2.编写一个简单计算器类,拥有加减乘除等方法,每个方法都有2个操作数,同为double类型或同为整型,方法设计为重载,在测试类里通过键盘输入操作数,显示计算结果 。

import java.util.Scanner;
public class S3_2 {
    public static void main(String[] args) {
        Scanner sca=new Scanner(System.in);
        Calculator cal=new Calculator();
        int x,y;
        double a,b;
        int i=1;
        int sum;
        System.out.println("1.加法");
        System.out.println("2.减法");
        System.out.println("3.乘法");
        System.out.println("4.除法");
        System.out.println("0.退出程序");
        while(i>0){
            int option;
            System.out.print("请选择你要进行的数学运算:");
            option=sca.nextInt();
            switch(option){
                case 1:
                    System.out.print("请输入第一个数:");
                    x=sca.nextInt();
                    System.out.print("请输入第二个数:");
                    y=sca.nextInt();
                    sum=cal.calculator1(x,y);
                    System.out.println("所求的结果是:"+sum);
                    break;
                case 2:
                    System.out.print("请输入第一个数:");
                    a=sca.nextDouble();
                    System.out.print("请输入第二个数:");
                    b=sca.nextDouble();
                    sum=(int)cal.calculator1(a,b);
                    System.out.println("所求的结果是:"+sum);
                    break;
                case 3:
                    System.out.print("请输入第一个数:");
                    x=sca.nextInt();
                    System.out.print("请输入第二个数:");
                    y=sca.nextInt();
                    sum=cal.calculator2(x,y);
                    System.out.println("所求的结果是:"+sum);
                    break;
                case 4:
                    System.out.print("请输入第一个数:");
                    a=sca.nextDouble();
                    System.out.print("请输入第二个数:");
                    b=sca.nextDouble();
                    sum=(int)cal.calculator2(a,b);
                    System.out.println("所求的结果是:"+sum);
                    break;
                case 0:
                    i=-1;
                    break;
            }
        }
    }
}
class Calculator{

    public int calculator1(int a,int b){
        return a+b;
    }
    public double calculator1(double a,double b){
        return a-b;
    }
    public int calculator2(int a,int b){
        return a*b;
    }
    public double calculator2(double a,double b){
        return a/b;
    }
}

3、设计一个日期类,定义类的构造方法对日期进行初始化,在toString()中将其输出格式定为“月/日/年”。最后,编写一个测试程序来测试所定义的日期类能否实现预定的功能。

import java.util.Date;

public class S3_3 {
    public static void main(String[] args){
        Date1 date1=new Date1();
        System.out.println(date1.toString());
    }
}
class Date1{
    Date date=new Date();
    @Override
    public String toString() {
        return ""+(date.getMonth()+1)+"月"+date.getDate()+"日"+(date.getYear()+1900)+"年";
    }
}

4、实验题目:

设计一个分数类,分数的分子和分母用两个整型数表示,类所拥有的方法包括对分数进行加、减、乘、除等运算,以及输出分数的方法,输出分数的格式应该是:分子/分母。

在测试类中定义分数类对象,运算并输出运算结果。

import java.util.Scanner;
public class S3_4 {
    public static void main(String[] args){
        fractions fra=new fractions();
        Scanner scan=new Scanner(System.in);
        int fson1,fson2,fmom1,fmom2;
        System.out.println("请输入第一个分数的分子:");
        fson1=scan.nextInt();
        System.out.println("请输入第一个分数的分母:");
        fmom1=scan.nextInt();
        System.out.println("请输入第二个分数的分子:");
        fson2=scan.nextInt();
        System.out.println("请输入第二个分数的分母:");
        fmom2=scan.nextInt();
        System.out.println("1.进行运算");
        System.out.println("0.退出");
        System.out.println("请选择:");
        int i=scan.nextInt();
        if(i==0){
            System.out.println("已退出");
        }else {
            while (i != 0) {
                System.out.println("1.加法");
                System.out.println("2.减法");
                System.out.println("3.乘法");
                System.out.println("4.除法");
                System.out.println("请选择:");
                int n = scan.nextInt();
                switch (n) {
                    case 1:
                        fra.fAdd(fson1,fson2,fmom1,fmom2);
                        break;
                    case 2:
                        fra.fSub(fson1,fson2,fmom1,fmom2);
                        break;
                    case 3:
                        fra.fMul(fson1,fson2,fmom1,fmom2);
                        break;
                    case 4:
                        fra.fDiv(fson1,fson2,fmom1,fmom2) ;
                        break;
                }
            }
        }
    }
}
class fractions{
    public int gcd(int a,int b){
        if(a<b){
            int c=a;
            a=b;
            b=c;
        }
        int r=a%b;
        while(r!=0){
            a=b;
            b=r;
            r=a%b;
        }
        return b;
    }

    public void fAdd(int fson1,int fmom1,int fson2,int fmom2){
        int fm=fmom1*fmom2;
        fson1=fson1*fmom2;
        fson2=fson2*fmom1;
        int fs=fson1+fson2;
        if( fs==0){
            System.out.println("结果为0");
        }
        else {

            int g = gcd(fs, fm);
            System.out.println("最大公约数:" + g);
            fs = fs / g;
            fm = fm / g;
            System.out.println("加法运算后结果中分子为:" + fs);
            System.out.println("加法运算后结果中分母为:" + fm);
            System.out.println("即分数为:" + fs + "/" + fm);
        }

    }
    public void fSub(int fson1,int fmom1,int fson2,int fmom2){
        int fm=fmom1*fmom2;
        fson1=fson1*fmom2;
        fson2=fson2*fmom1;
        int fs=fson1-fson2;
        if( fs==0){
            System.out.println("结果为0");
        }
        else {

            int g = gcd(fs, fm);
            fs = fs / g;
            fm = fm / g;
            System.out.println("减法运算后结果中分子为:" + fs);
            System.out.println("减法运算后结果中分母为:" + fm);
            System.out.println("即分数为:" + fs + "/" + fm);
        }


    }
    public void fMul(int fson1,int fmom1,int fson2,int fmom2){
        int fm=fmom1*fmom2;
        int fs=fson1*fson2;
        if( fs==0){
            System.out.println("结果为0");
        }
        else {

            int g = gcd(fs, fm);
            fs = fs / g;
            fm = fm / g;
            System.out.println("乘法运算后结果中分子为:" + fs);
            System.out.println("乘法运算后结果中分母为:" + fm);
            System.out.println("即分数为:" + fs + "/" + fm);
        }

    }
    public void fDiv(int fson1,int fmom1,int fson2,int fmom2){
        int fm=fmom1*fson2;
        int fs=fson1+fmom2;
        if( fs==0){
            System.out.println("结果为0");
        }
        else {

            int g = gcd(fs, fm);
            fs = fs / g;
            fm = fm / g;
            System.out.println("除法运算后结果中分子为:" + fs);
            System.out.println("除法运算后结果中分母为:" + fm);
            System.out.println("即分数为:" + fs + "/" + fm);
        }

    }
}

 

5、实验题目:

(1)设计一个雇员类,属性包括:编号、姓名、年龄、职务、部门,要求合理选定属性类型;该雇员类还拥有统计出勤人数的功能,可以考虑为雇员类设计一个静态属性;方法包括:构造方法、输出信息的方法、签到方法;

(2)创建雇员类对象,统计雇员的出勤人数。

注意考虑属性和方法的访问权限,方法的功能,及main方法中如何实现要求统计的信息。

import java.util.Scanner;
public class S3_5 {
    public static void main(String[] args) {
        Staff staff=new Staff();
        System.out.println("签到程序开始运行");
        staff.signIn();
        System.out.println("现在展示员工数据");
        staff.output();
    }

    public static class Staff {
        Scanner scan = new Scanner(System.in);
        public String[] num = new String[100];
        public String[] name = new String[100];
        public String[] age = new String[100];
        public String[] position = new String[100];
        public String[] department = new String[100];
        public int signNum = 0;


        public void signIn() {
            boolean b = true;
            int i = 0;
            String p;
            while (b) {
                System.out.print("请输入你的编号:"); //依次输入

                this.num[i] = scan.nextLine();
                System.out.print("请输入你的姓名:");
                this.name[i] = scan.nextLine();
                System.out.print("请输入你的年龄:");
                this.age[i] = scan.nextLine();
                System.out.print("请输入你的职务:");
                this.position[i] = scan.nextLine();
                System.out.print("请输入你的部门:");
                this.department[i] = scan.nextLine();
                this.signNum++;
                System.out.print("如果你是最后一个签到的,请输入1,反之为0:");
                p = scan.nextLine();
                System.out.println();
                i++;
                if (p.equals("1")) {
                    b = false;
                }
            }
        }

        public void output() {  //信息输出函数
            System.out.println("出勤人数合计为" + this.signNum);
            for (int i = 0; i < this.signNum; i++) {
                System.out.println("编号:" + num[i]);
                System.out.println("姓名:" + name[i]);
                System.out.println("年龄:" + age[i]);
                System.out.println("职务:" + position[i]);
                System.out.println("部门:" + department[i]);
                System.out.println();
            }
        }
    }
}

6、实验题目:

设计一个电视机类,属性包括商品编号、开关状态、音量、频道等,同时设计一些方法对电视机的状态进行控制。例如,方法应包括开/关电视机、更换频道、提高/减小音量等。要求商品编号自动生成(可以考虑为电视机类设置一个管理商品编号的静态成员变量,或者专门设置一个编号管理类)。

注意:有些成员变量需要定义为静态的(static),控制和操纵静态成员变量的方法应是静态的(static)。

import java.util.Scanner;
public class S3_6 {
    public static void main(String[] args) {
        Scanner sca=new Scanner(System.in);
        TV test=new TV();
        Random random=new Random();
        System.out.println("商品名称:"+test.name);
        System.out.println("商品编号:"+2022+random.r);
        System.out.println("1.开机");
        System.out.println("2.关机");
        System.out.println("3.调高音量");
        System.out.println("4.调低音量");
        System.out.println("5.加频道");
        System.out.println("6.减频道");
        System.out.println("7.调转频道");
        System.out.println("输入负数退出程序");
        System.out.print("请选择你有进行的操作:");

        int n=1;
        while(n>0){
            int select=sca.nextInt();
            switch(select){
                case 1:
                    test.openKey();
                    break;
                case 2:
                    test.closeKey();
                    break;
                case 3:
                    if(test.key){
                        test.volumeIncrease();
                    }else{
                        System.out.println("目前已关机");
                    }
                    break;
                case 4:
                    if(test.key){
                        test.volumeDecrease();
                    }else{
                        System.out.println("目前已关机");
                    }
                    break;
                case 5:
                    if(test.key){
                        test.channelIncrease();
                    }else{
                        System.out.println("目前已关机");
                    }
                    break;
                case 6:
                    if(test.key){
                        test.channelDecrease();
                    }else{
                        System.out.println("目前已关机");
                    }
                    break;
                case 7:
                    if(test.key){
                        test.setChannel();
                    }else{
                        System.out.println("目前已关机");
                    }
                    break;
                default:
                    if(!test.key) {
                        if (select < 0) {
                            System.out.println("退出程序成功");
                            n = -1;
                        }
                        }else{
                            System.out.println("目前处于开机状态,强行退出程序的话可能对于电视有所损伤。");
                            System.out.println("1.继续 2.退出 是否继续进行:");
                            int op;
                            op=sca.nextInt();
                            switch(op){
                                case 1:
                                    System.out.println("退出程序成功");
                                    n=-1;
                                    break;
                                case 2:
                                    System.out.println("请进行其他操作:");
                                    n=1;
                                    break;
                            }
                        }
                    }
            }
        }
}
class TV {
    static String name="小米电视";
    int channel = 0;//频道
    int volume = 0;//音量
    boolean key = false;//开关状态

    void openKey() {
        key = true;
        System.out.println("已开机");
    }

    void closeKey() {
        key = false;
        System.out.println("已关机");
    }

    void volumeIncrease() {
        volume++;
        System.out.println("目前音量为:" + volume);
    }

    void volumeDecrease() {
        volume--;
        System.out.println("目前音量为:" + volume);
    }

    void channelIncrease() {
        channel++;
        System.out.println("目前频道为:" + channel);
    }

    void channelDecrease() {
        channel--;
        System.out.println("目前频道为:" + channel);
    }

    void setChannel() {
        System.out.println("请输入你要跳转的频道:");
        Scanner scan = new Scanner(System.in);
        channel = scan.nextInt();
        System.out.println("目前频道为:" + channel);
    }
}
class Random{
    int r = (int)(Math.random()*1000);//产生三位随机数
}

 

7、实验题目(选做):

仿照超市购物的例子编写一个学生借书的程序。提示:

思考需要定义的类,例如:本程序需要用到学生、借书卡、书等对象,最后实现借书的过程,如果有指定的书,则输出“***借到了***书”,否则输出“****没有借到****书”。

还需要认真思考每个类中有哪些属性和方法,能够更好的完成这个程序。

import java.util.Scanner;
class Test08{
    public static void main(String[] args){
        Scanner sca=new Scanner(System.in);
        int card;

        StudentBorrow stu1=new StudentBorrow();
        stu1.name="张三";
        stu1.card=2022001;
        stu1.privilege=true;

        StudentBorrow stu2=new StudentBorrow();
        stu2.name="李四";
        stu2.card=2022002;
        stu2.privilege=true;

        StudentBorrow stu3=new StudentBorrow();
        stu3.name="王五";
        stu3.card=2022003;

       BorrowBooks book1=new BorrowBooks();
       book1.bookname="西游记";
       book1.author="吴承恩";
       book1.booknum=1001;
       book1.booksave=true;

        BorrowBooks book2=new BorrowBooks();
        book2.bookname="水浒传";
        book2.author="施耐庵";
        book2.booknum=1002;
        book2.booksave=true;

        BorrowBooks book3=new BorrowBooks();
        book3.bookname="红楼梦";
        book3.author="曹雪芹";
        book3.booknum=1003;
        book3.booksave=true;

        BorrowBooks book4=new BorrowBooks();
        book4.bookname="三国演义";
        book4.author="罗贯中";
        book4.booknum=1004;
        book4.booksave=true;

        System.out.println("欢迎来到图书馆");
        System.out.println("1.查询权限");
        System.out.println("2.查询书籍");
        System.out.println("3.退出程序");
        int i=1;
        while(i!=0){
            int select;
            System.out.print("请输入你要选择的功能:");
            select=sca.nextInt();
            switch(select){
                case 1:
                    System.out.println("请输入你的借书卡号:");
                    card=sca.nextInt();
                    boolean bool=false;
                    if(card==stu1.card){
//                        System.out.println("你的信息为:");
//                        System.out.println("姓名:"+stu1.name);
//                        System.out.println("借书卡号"+card);
                        stu1.show();
                        if(stu1.privilege){
                            System.out.println("你有借书权限");
                            bool=true;
                        }else{
                            System.out.println("你没有借书权限");
                        }
                    }else if(card==stu2.card){
//                        System.out.println("你的信息为:");
//                        System.out.println("姓名:"+stu2.name);
//                        System.out.println("借书卡号:"+card);
                        stu2.show();
                        if(stu2.privilege){
                            System.out.println("你有借书权限");
                            bool=true;
                        }else{
                            System.out.println("你没有借书权限");
                        }
                    }else if(card==stu3.card){
//                        System.out.println("你的信息为:");
//                        System.out.println("姓名:"+stu3.name);
//                        System.out.println("借书卡号:"+card);
                        stu3.show();
                        if(stu3.privilege){
                            System.out.println("你有借书权限");
                            bool=true;
                        }else{
                            System.out.println("你没有借书权限");
                        }
                }else{
                        System.out.println("查无此人");
                    }
                    while(bool) {
                        System.out.println("是否继续进行:");
                        System.out.println("1.借书");
                        System.out.println("2.退出程序");
                        int op;
                        op = sca.nextInt();
                        switch (op) {
                            case 1 :
                            {
                                System.out.println(book1.bookname + "," + book1.author + "," + book1.booknum);
                                System.out.println(book2.bookname + "," + book2.author + "," + book2.booknum);
                                System.out.println(book3.bookname + "," + book3.author + "," + book3.booknum);
                                System.out.println(book4.bookname + "," + book4.author + "," + book4.booknum);
                                System.out.println("请输入你选择的书籍号");
                                int num = sca.nextInt();
                                if (num == book1.booknum) {
                                    if (book1.booksave == true) {
                                        System.out.println("你已借阅了" + book1.bookname);
                                        book1.booksave = false;
                                    } else {
                                        System.out.println("本书已被借走");
                                    }
                                } else if (num == book2.booknum) {
                                    if (book2.booksave == true) {
                                        System.out.println("你已借阅了" + book2.bookname);
                                        book1.booksave = false;
                                    } else {
                                        System.out.println("本书已被借走");
                                    }
                                } else if (num == book3.booknum) {
                                    if (book3.booksave == true) {
                                        System.out.println("你已借阅了" + book3.bookname);
                                        book1.booksave = false;
                                    } else {
                                        System.out.println("本书已被借走");
                                    }
                                } else if (num == book4.booknum) {
                                    if (book4.booksave == true) {
                                        System.out.println("你已借阅了" + book4.bookname);
                                        book1.booksave = false;
                                    } else {
                                        System.out.println("本书已被借走");
                                    }
                                } else {
                                    System.out.println("无此书籍");
                                }
                            }
                            break;
                            case 2 :
                                bool = false;
                                break;
                        }
                    }
                    break;
                case 2:
                    System.out.println(book1.bookname+","+book1.author+","+book1.booknum);
                    System.out.println(book2.bookname+","+book2.author+","+book2.booknum);
                    System.out.println(book3.bookname+","+book3.author+","+book3.booknum);
                    System.out.println(book4.bookname+","+book4.author+","+book4.booknum);
                    System.out.println("请输入你查询的书籍号:");
                  int num=sca.nextInt();
                  if(num==book1.booknum){
                      if(!book1.booksave){
                          System.out.println("本书已被借走");
                      }else{
                          System.out.println("本书未被借走");
                      }

                  }else if(num==book2.booknum){
                      if(!book2.booksave){
                          System.out.println("本书已被借走");
                      }else{
                          System.out.println("本书未被借走");
                      }
                  }else if(num==book3.booknum){
                      if(book3.booksave==false){
                          System.out.println("本书已被借走");
                      }else{
                          System.out.println("本书未被借走");
                      }
                  }else if(num==book4.booknum){
                      if(book4.booksave==false){
                          System.out.println("本书已被借走");
                      }else{
                          System.out.println("本书未被借走");
                      }
                  }else {
                      System.out.println("请输入正确的书籍号");
                  }
                    break;
                case 3:
                    i=0;
                    break;
            }

        }
    }
}

class StudentBorrow{
    String name;
    int card;
    boolean privilege;
//    public void setName(String name){
//       this.name=name;
//    }
//    public String getName(){
//        return name;
//    }
//    public void setCard(int card){
//        this.card=card;
//    }
//    public int getCard(){
//        return card;
//    }public void setPrivilege(boolean privilege){
//        this.privilege=privilege;
//    }
//    public boolean getPrivilege(){
//        return privilege;
//    }
    void show(){
        System.out.println("你的信息为:");
        System.out.println("姓名:"+name);
        System.out.println("借书卡号:"+card);
    }

}
class BorrowBooks{
    String bookname;
    int booknum;
    String author;
    boolean booksave;

}

8、实验题目(选做):

模仿ATM取款过程。首先编写一个账户类,成员变量包含账号、储户姓名和存款余额以及密码,再编写一个ATM机类,属性包括账户列表,方法有登录,存款和取款。编写一个测试类来测试所定义的账户类能否实现预定的功能。

 ~~不会

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

用户1234567890

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值