Java实验定义一个电视机类实现电视机基本功能等

🏠个人主页:黑洞晓威
🧑个人简介:大家好,我是晓威,一名普普通通的大二在校生,希望在CSDN中与大家一起成长。

🎁如果你也在正在学习Java,欢迎各位大佬来到我的博客查漏补缺呀,如果有哪里写的不对的地方也欢迎诸佬指正啊。

实验目的

1、理解面向对象的相关基本概念,掌握类的声明、类的实例化和调用。

2、掌握方法及构造方法的定义;

3、掌握方法重载的用法,理解值传递和地址传递两种参数传递方式的区别;

4、掌握this关键字和static关键字的用法;

5、掌握访问控制符的使用,以实现类的封装和信息隐蔽;

6、能够初步进行类的设计,编写基本的面向对象的程序;

7、了解对象内存分配和垃圾回收机制的原理。

实验1、定义一个电视机类

实验题目:定义一个电视机类,实现电视机的基本功能(换台,调整音量,开关),并测试其功能。

源代码:

import java.util.Scanner;

public class S3_1 {
    public static void main(String[] args) {
        while(true){//while循环保证重复操作
            Television tele = new Television();//创建对象

            System.out.println("*****我是你的小电视机******");
            System.out.println("1:出来吧电视机"+"\n"+"2:残忍离开");

                Scanner scanner = new Scanner(System.in);

                
                int a=scanner.nextInt();
                if(a==1){
                    System.out.println("嘿嘿嘿电视机已打开");
                    tele.setOn();//调用Television中的setOn方法
                    break;
                }else if(a==2){
                    tele.setOff();//调用Television中的setOff方法
                    break;
                }
                scanner.close();//关闭输入流
            


        }

    }
}

class Television {

    private int sound;//音量
    private int channel;//频道


    public void setOn() {
        System.out.println("请选择您需要的功能");
        System.out.println("1:调整音量"+"\n"+"2:更换频道"+"\n"+"3关闭电视");


        Scanner scanner = new Scanner(System.in);
        int choose = 0;
        if (scanner.hasNextInt()){
                choose=scanner.nextInt();
            }
            switch (choose){
                case 1:
                    setSound();
                    break;
                case 2:
                    setChannel();
                    break;
                case 3:

                    setOff();
                    break;
            }
        
    }

    public void setSound() {
        System.out.println("请输入调整的音量大小");
        Scanner scanner = new Scanner(System.in);
        if (scanner.hasNextInt()) {
            sound = scanner.nextInt();
        } else {
            System.out.println("请输入正确格式");
        }
        scanner.close();
        System.out.println("已调节音量大小为:" + sound);
        System.out.println("继续操作我吧");
        setOn();//回到setOn方法
    }

    public void setChannel() {
        System.out.println("请输入更换的频道");
        Scanner scanner = new Scanner(System.in);
        if (scanner.hasNextInt()) {
            channel = scanner.nextInt();
        } else {
            System.out.println("请输入正确格式");
        }
        scanner.close();
        System.out.println("已更换到第" + channel + "台");
        System.out.println("继续操作我吧");
        setOn();//回到setOn方法
    }



    public void setOff(){
        System.out.println("呜呜呜电视机已关闭,下次再见QAQ");
    }
}

实验2:设计一个分数类

(1)设计一个分数类,分数的分子和分母用两个整型数表示,类中定义方法对分数进行加、减、乘、除运算;

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

源代码:

public class S3_2 {
    public static void main(String[] args) {
        fenshu f1 = new fenshu(1,5);
        fenshu f2 = new fenshu(1, 2);
        f1.add(f2);
        f1.subtract(f2);
        f1.multiply(f2);
        f1.devide(f2);
    }
}

class fenshu {
    private int fenzi;
    private  int fenmu;


    //分数的构造器
    public fenshu(int num1, int num2) {
        this.fenzi =num1;
        this.fenmu = num2;
    }

    //公约数的方法:辗转相除法
    public int gongyue(int i,int j){
        if(i>j){
            int r;
            while (true){
                r = i%j;
                if(r==0){
                    return j;//返回j
                }else {
                    i=j;
                    j=r;
                }
            }
        }else {
            int r;
            while (true){
                r = j%i;
                if(r==0){
                    return i;//返回i
                }else {
                    j=i;
                    i=r;
                }
            }
        }
    }
    
    public void add(fenshu o1){//参数引入fenshu类的o1
        //this.fenshu为当前调用add方法的fenshu,o1.fenshu为参数的
       int b = this.fenmu*o1.fenmu;//分母乘分母得到分母
       int a = this.fenzi*o1.fenmu+this.fenmu*o1.fenzi;//分子乘分子得到分子
       int z = gongyue(a,b);//求分子分母的公约数
        System.out.println(this.fenzi+"/"+this.fenmu+"加上"+o1.fenzi+"/"
                +o1.fenmu+"="+a/z+"/"+b/z);//输出结果
    }

    public void subtract(fenshu o1){
        int a = this.fenzi*o1.fenmu-this.fenmu*o1.fenzi;
        int b = this.fenmu*o1.fenmu;
        int z = gongyue(a,b);
        System.out.println(this.fenzi+"/"+this.fenmu+"减"+o1.fenzi+"/"
                +o1.fenmu+"="+a/z+"/"+b/z);
    }

    public void multiply(fenshu o1){
        int a = this.fenzi*o1.fenzi;
        int b = this.fenmu*o1.fenmu;
        int z = gongyue(a,b);
        System.out.println(this.fenzi+"/"+this.fenmu+"乘"+o1.fenzi+"/"
                +o1.fenmu+"="+a/z+"/"+b/z);
    }

    public void devide(fenshu o1 ){
        int a = this.fenzi*o1.fenmu;
        int b = this.fenmu*o1.fenzi;
        int z = gongyue(a,b);
        System.out.println(this.fenzi+"/"+this.fenmu+"除"+o1.fenzi+"/"
                +o1.fenmu+"="+a/z+"/"+b/z);
    }
 
}

实验三:定义一个手机类

(1)定义一个手机类,定义若干属性,方法和构造方法;

(2)定义测试类,其中定义多个手机类对象,并设置不同的初始值;

(3)调用手机类的相关方法,测试该类的功能。

源代码:

public class S3_3 {
    public static void main(String[] args) {
        phone apple = new phone();
        phone xiaomi = new phone("小米", 1999);
        phone huawei = new phone("华为", 4999, "任正非");

        xiaomi.saybrand();//非静态方法用对象调用
        phone.staticmeth();//静态方法直接用类名调用
        
        xiaomi.setUser("雷军");//设置xioami的Useer私有属性
        System.out.println(xiaomi.getUser());//得到xioami的Useer私有属性
        
        String str = huawei.toString();//toString方法
        System.out.println(str);
        //System.out.println(huawei);//同样会调用toString方法
        

    }
}

class phone {
    private String brand;
    private   int price;
    private String user;
    //空参构造器
    public phone() {
    }
    //带两个参构造器
    public phone(String name, int price) {
        this.brand = name;
        this.price = price;
    }
    //带三个参数的构造器
    public phone(String name, int price, String user) {
        this.brand = name;
        this.price = price;
        this.user = user;
    }

    
    //非静态方法
    public void saybrand(){
        System.out.println("我是"+brand+"品牌的手机哦");
    }

    //静态方法
    public static void staticmeth(){
        System.out.println("我是静态方法哦!");
    }
    
    public String getUser() {
        return user;
    }


    public void setUser(String user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "phone{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                ", user='" + user + '\'' +
                '}';
    }
}

实验四:定义一个类,其中定义三个方法

要求:分别求两个整数最大值、三个整数最大值和三个小数最大值,并测试其效果。

源代码:

public class S3_4 {
    public static void main(String[] args) {
        int num1 = math.max1(1,2);
        System.out.println(num1);

        int num2 = math.max2(1,4,4);
        System.out.println(num2);

        double num3 = math.max3(1.2,4.6,23.8);
        System.out.println(num3);
    }
}

class math {
    //我这里没有考虑相等的情况,默认相等时也存在最大值即它本身.
    public static int max1(int a, int b){//静态方法可直接用类名调用
        if(a>b){
            return a;
        }else return b;
    }

    public static int max2(int a,int b,int c){

        if(a>b){
            if(a>=c){
                return a;
            }else return c;
        }if(b>c){
            return b;
        }else return c;
    }

    public static double max3(double a,double b,double c){

        if(a>b){
            if(a>=c){
                return a;
            }else return c;
        }if(b>c){
            return b;
        }else return c;
    }
}

实验五:定义一个类,包含x、y两个属性

(1)定义一个类,包含x、y两个属性,一个add方法实现对两个属性的加法运算,即x、y分别加上10和20,并定义该类的构造方法;

(2)通过定义add方法的重载,联系两种形参的传递方式(基本类型和引用类型);

(3)定义测试类,测试该类的重载方法的效果,总结两种参数传递的区别。

源代码:

public class S3_5 {
    public static void main(String[] args) {
        math2 math = new math2(0, 0);//创建math2对象,math.x=0,math.y=0
        //引用类型形参传递
        math.add();
        System.out.println("x="+math.x+"  y="+math.y);//math.x=10,math.y=20

        int a = 0;
        int b = 0;
        //基本类型形参传递
        math.add(a,b);
        System.out.println("a="+a+"  b="+b);//a=0,b=0

    }
}



class math2{
    public int x;
    public int y;

    public String str = "hollo";

    //构造器
    public math2(int x, int y) {
        this.x = x;
        this.y = y;
    }

    //引用类型
    public void add(){
        this.x+=10;
        this.y+=20;
    }

    //基本类型
    public void add(int a,int b){
        a+=10;
        b+=20;
    }

}

列出测试数据和实验结果截图:

实验六:设计一个学生类Student

(1)设计一个学生类Student,包含至少两个成员变量,其中包含一个类变量,定义构造方法和一个类方法,输出该类的成员变量的值(注意类变量和实例变量的访问方式区别);

(2)定义测试类,实例化一个Student对象,然后调用该对象的相关方法,观察并分析类方法与实例方法的区别,总结静态变量和方法使用的注意事项。

源代码:

public class S3_6 {
    public static void main(String[] args) {

        Student stu = new Student("张三");


        stu.meth();//实例方法用对象调用
        Student.staticmeth();//类方法直接用类名调用

    }
}

 class Student{
    private String name;//实例变量
    private static int age = 18;//类变量

     public Student(String name) {
         this.name = name;
     }

     //实例方法
     public void meth(){
         System.out.println(this.name);
     }
     //类方法
     public static void staticmeth(){
         //静态方法(类方法)只能调用静态变量(类变量),静态变量直接用类名
         System.out.println(Student.age);

     }
 }

实验七:设计一个雇员类

(1)设计一个雇员类,属性包括:编号、姓名、年龄、职务、部门、出勤人数;方法包括:构造方法、输出信息的方法、签到方法;

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

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

源代码:

import java.util.Scanner;

class Employee {


    public String[] number = new String[1024];  //建字符串型的数组,把数据都放数组里
    public String[] name = new String[1024];
    public String[] age = new String[1024];
    public String[] pos = new String[1024];
    public String[] dep = new String[1024];
    public int attendance = 0;//出勤人数
    public Employee() {

    }

    //签到函数,数据输入
    public void sign() {
        int i=0;
        while (true ){//循环输入信息
            System.out.println("请输入编号:");

            Scanner sc = new Scanner(System.in);
            number[i] = sc.next();
            System.out.println("请输入姓名:");
            name[i] = sc.next();
            System.out.println("请输入年龄:");
            age[i] = sc.next();
            System.out.println("请输入职务:");
            pos[i] = sc.next();
            System.out.println("请输入部门:");
            dep[i] = sc.next();
            attendance ++ ;//每输入一次出勤人数加一
            i++;
            System.out.println("你是否是最后一个签到的人,若是,则输入0,否则输入1:");
            int j = 0;
            j = sc.nextInt();
            if (j == 0) break;//判断是否结束循环
        }
    }

    //数据输出
    public void information() {
        System.out.println("出勤人数为:" + attendance);
        System.out.println("出勤人员信息:");
        for (int i = 0; i < attendance; i ++ ) {//循环打印
            System.out.println("编号:" + number[i]);
            System.out.println("姓名:" + name[i]);
            System.out.println("年龄:" + age[i]);
            System.out.println("职务:" + pos[i]);
            System.out.println("部门:" + dep[i]);
        }
    }


}

class S3_7 {
    public static void main(String[] args) {        //main函数
        Employee e = new Employee();
        System.out.println("开始签到:");
        e.sign();
        System.out.println("签到完成!");
        e.information();
    }
}

列出测试数据和实验结果截图:

实验八:仿照超市购物的例子编写一个学生借书的程序:

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

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

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

源代码:

import java.util.Scanner;

public class S3_8 {
    public static void main(String[] args) {
        String[] name =new String[]{"张三","李四"};//创建学生姓名字符串数组
        int[] num = new int[]{01,02};//创建学生借书卡编号数组
        String[] allbook = new String[]{"西游记","水浒传"};//创建书籍名称数组

        Student1 student = new Student1(name);//创建学生类对象
        Card card = new Card(student,num);//创建借书卡对象
        book book = new book(allbook);//创建书籍对象

        int i = card.register();
        int j = book.borrow();
        System.out.println(name[i]+"借到了"+allbook[j]+"这本书");

    }
}

class Student1{
private String[] name;

    public Student1(String name[]) {//构造器
        this.name=name;
    }

    public String[] getName() {
        return name;
    }
}

class Card{
private String[] name;//姓名
private int[] num;//卡号

int length=0;

    public Card(Student1 student, int[] num) {
        this.name = student.getName();
        this.num = num;
        this.length=num.length;
    }

    public int  register(){
        int i = 0;
        int j =0;//用j控制循环
        Scanner scanner = new Scanner(System.in);
        while (j==0){
            System.out.println("请输入您的借书卡号:");
            int num = scanner.nextInt();
            for ( i = 0; i < length; i++) {
                if(num == this.num[i]){
                    System.out.println("欢迎借书,"+this.name[i]+"同学!");
                    j++;
                    return  i;//i指第i个同学
                }
            }
                System.out.println("输入卡号不存在,请重新输入");
        }
        scanner.close();
        return 0;
    }

}

class book{
    private String[] name;
    int length;

    public book(String[] name) {
        this.name = name;
        this.length=name.length;
    }

    public int borrow(){
        System.out.println("请输入书籍的名称");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        int j = 0;
        while(j==0){
            for ( int i = 0; i <length ; i++) {
                if(name.equals(this.name[i])){
                    j++;
                    return i;//返回第几本书
                }
                System.out.println("不存在该书籍,请重新输入");
            }
        }return 0;
    }
}

🎉文章到这里就结束了,感谢诸佬的阅读。🎉

💕欢迎诸佬对文章加以指正,也望诸佬不吝点赞、评论、收藏加关注呀😘

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

黑洞晓威

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

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

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

打赏作者

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

抵扣说明:

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

余额充值