首页 > 编程语言 >JavaSE基础知识分享(三)相关练习题

JavaSE基础知识分享(三)相关练习题

时间:2024-08-07 18:49:17浏览次数:6  
标签:练习题 String int void System private 基础知识 JavaSE public

写在前面

大家前面的面向对象部分学的怎么样了,快来看看这些题你能不能快速地写出答案,面向对象在Java中是非常重要的,快来检测你的薄弱点在哪,及时查漏补缺!

使用面向对象思想编写下列题目:

1.使用面向对象的思想,编写自定义描述狗的信息。设定属性包括:品种,年龄,心情,名字;方法包括:叫,跑。
要求:
1)设置属性的私有访问权限,通过公有的 get,set 方法实现对属性的访问
2)限定心情只能有“心情好”和“心情不好”两种情况,如果无效输入进行提示, 默认设置“心情好”。
3)设置构造函数实现对属性赋值
4)叫和跑的方法,需要根据心情好坏,描述不同的行为方式。
5)编写测试类,测试狗类的对象及相关方法(测试数据信息自定义)
运行效果图:

代码参考:

class Dog {
    private String kind;
    private int age;
    private String mod;
    private String name;

    public Dog() {
    }

    public Dog(String kind, int age, String mod, String name) {
        this.kind = kind;
        this.age = age;
        this.mod = mod(mod);
        this.name = name;
    }

    public String getKind() {return kind;}

    public void setKind(String kind) {this.kind = kind;}

    public int getAge() {return age;}

    public void setAge(int age) {this.age = age;}

    public String getMod() {return mod;}

    public void setMod(String mod) {this.mod = mod;}

    public String getName() {return name;}

    public void setName(String name) {this.name = name;}

    public void show() {
        System.out.println("名字叫" + name + "年龄" + age + "岁的" + kind + "犬" + mod + ",");
    }

    private String mod(String mod) {
        if (mod == null || (!mod.equals("心情很好") && !mod.equals("心情不好"))) {
            mod = "心情很好";
            System.out.println("输入信息错误,这只狗狗今天" + mod + "!");
        }
        return mod;
    }

    public void run() {
        if (this.mod.equals("心情很好")) {
            System.out.println("名字叫" + this.name + "的" + this.kind + "犬" + this.mod + ",开心的围着主人身边转");
        } else if (this.mod.equals("心情不好")) {
            System.out.println("名字叫" + this.name + "的" + this.kind + "犬" + this.mod + ",伤心的一动不动");
        }
    }

    public void woof() {
        if (this.mod.equals("心情很好")) {
            System.out.println("名字叫" + this.name + "的" + this.kind + "犬" + this.mod + ",开心的汪汪叫");
        } else if (this.mod.equals("心情不好")) {
            System.out.println("名字叫" + this.name + "的" + this.kind + "犬" + this.mod + ",伤心的呜呜叫");
        }
    }
}

class Test1 {
    public static void main(String[] args) {
        Dog d1 = new Dog("贵宾", 1, "哈哈哈", "甜心");
        Dog d2 = new Dog("贵宾", 1, "心情很好", "甜心");
        d2.run();
        d2.woof();
        System.out.println("========================================");
        Dog d3 = new Dog("德国牧羊", 1, "心情不好", "太子");
        d3.run();
        d3.woof();
        System.out.println("========================================");
        d2.show();
        d3.show();
    }
}

2.以面向对象的思想,编写自定义类描述IT从业者。设定属性包括:姓名,年龄,技术方向,工作年限, 工作单位和职务;方法包括:工作。
要求:
1)设置属性的私有访问权限,通过公有的 get,set 方法实现对属性的访问
2)限定 IT 从业人员必须年满 15 岁,无效信息需提示,并设置默认年龄为 15。
3)限定“技术方向”是只读属性(只提供 get 方法)
4)工作方法通过输入参数,接收工作单位和职务,输出个人工作信息
5)编写测试类,测试 IT 从业者类的对象及相关方法(测试数据信息自定义)
运行效果图:

代码参考:

class ItPractitioners {
    private String name;
    private int age;
    private String technicalDirection;
    private int workYear;
    private String workplace;
    private String office;

    public ItPractitioners() {
    }

    public ItPractitioners(String name, int age, int workYear, String technicalDirection) {
        this.name = name;
        this.age = age(age);
        this.workYear = workYear;
        this.technicalDirection = technicalDirection;
    }

    public ItPractitioners(String name, int age, int workYear, String technicalDirection, String workplace, String office) {
        this.name = name;
        this.age = age(age);
        this.workYear = workYear;
        this.technicalDirection = technicalDirection;
        this.workplace = workplace;
        this.office = office;
    }

    public String getName() {return name;}

    public void setName(String name) {this.name = name;}

    public int getAge() {return age;}

    public void setAge(int age) {this.age = age;}

    public String getTechnicalDirection() {return technicalDirection;}

    private void setTechnicalDirection(String technicalDirection) {this.technicalDirection = technicalDirection;}

    public int getWorkYear() {return workYear;}

    public void setWorkYear(int workYear) {this.workYear = workYear;}

    public String getWorkplace() {return workplace;}

    public void setWorkplace(String workplace) {this.workplace = workplace;}

    public String getOffice() {return office;}

    public void setOffice(String office) {this.office = office;}

    private int age(int age) {
        if (age < 15) {
            System.out.println("年龄信息无效!已修改默认年龄为15");
            age = 15;
        }
        return age;
    }

    public void work(String workplace, String office) {
        System.out.println("姓名:" + this.name);
        System.out.println("年龄:" + this.age);
        System.out.println("技术方向:" + this.technicalDirection);
        System.out.println("工作年限:" + this.workYear);
        System.out.println("目前就职于:" + workplace);
        System.out.println("职务是:" + office);
    }
}

class Test2 {
    public static void main(String[] args) {
        ItPractitioners i1 = new ItPractitioners("马未龙", 35, 10, "数据库维护");
        i1.work("腾讯实业", "数据库维护工程师");
        System.out.println("===========================================");
        ItPractitioners i2 = new ItPractitioners("张凯", 14, 1, "Java开发");
        i2.work("鼎盛科技", "Java开发工程师");
    }
}

3.以面向对象的思想,编写自定义类描述图书信息。设定属性包括:书名,作者,出版社名,价格;方法包括:信息介绍 show()。
要求:
1)设置属性的私有访问权限,通过公有的 get,set 方法实现对属性的访问
2)限定价格必须大于 10,如果无效进行提示
3)限定作者,书名为只读属性
4)设计构造方法实现对属性赋值
5)信息介绍方法描述图书所有信息
6)编写测试类,测试图书类的对象及相关方法(测试数据信息自定)
运行效果图:

代码参考:

class Book {
    private String bookName;
    private String author;
    private String publishingHouse;
    private double price;

    public Book() {
    }

    public Book(String bookName, String author, String publishingHouse, double price) {
        this.bookName = bookName;
        this.author = author;
        this.publishingHouse = publishingHouse;
        this.price = price(price);
    }

    public String getAuthor() {return author;}

    private void setAuthor(String author) {this.author = author;}

    public String getBookName() {return bookName;}

    private void setBookName(String bookName) {this.bookName = bookName;}

    public String getPublishingHouse() {return publishingHouse;}

    public void setPublishingHouse(String publishingHouse) {this.publishingHouse = publishingHouse;}

    public double getPrice() {return price;}

    public void setPrice(double price) {this.price = price;}

    private double price(double price) {
        if (price <= 10.0) {
            System.out.println("该书的价格小于10元,是本无效的书!默认为10元");
            price = 10.0;
        }
        return price;
    }

    public void show() {
        System.out.println("书名:"+this.bookName);
        System.out.println("作者:"+this.author);
        System.out.println("出版社:"+this.publishingHouse);
        System.out.println("价格:"+this.price);
    }
}

class Test3{
    public static void main(String[] args) {
        Book b1 = new Book("鹿鼎记","金庸","人民文学出版社",120.0);
        b1.show();
        System.out.println("========================================");
        Book b2 = new Book("绝代双骄","古龙","中国长安出版社",55.5);
        b2.show();
        System.out.println("========================================");
        Book b3 = new Book("鹿鼎记","金庸","人民文学出版社",8.0);
        b3.show();
    }
}

4.某公司要开发名为”我爱购物狂”的购物网站,请使用面向对象的思想设计描述商品信息。
要求:
1)分析商品类别和商品详细信息属性和方法,设计商品类别类和商品详细信息类
2)在商品详细信息类中通过属性描述该商品所属类别
3)设置属性的私有访问权限,通过公有的 get,set 方法实现对属性的访问
4)编写测试类,测试商品类别类和商品详细信息类的对象及相关方法(测试数据 信息自定)
5)创建包 info—存放商品类别类和商品详细信息类,创建包 test—存放测试类参考分析思路:
商品类别类:
属性:类别编号,类别名称商品详细信息类:
属性:商品编号,商品名称,所属类别,商品数量(大于 0),商品价格(大于 0),
方法:盘点的方法,描述商品信息。内容包括商品名称,商品数量,商品价格,现在商品总价以及所属类别信息
运行效果图:

代码参考:

public class ProductCategory {
    private String categoryId;
    private String categoryName;

    public ProductCategory() {
    }

    public ProductCategory(String categoryName, String categoryId) {
        this.categoryName = categoryName;
        this.categoryId = categoryId;
    }

    public String getCategoryName() {return categoryName;}

    public void setCategoryName(String categoryName) {this.categoryName = categoryName;}

    public String getCategoryId() {return categoryId;}

    public void setCategoryId(String categoryId) {this.categoryId = categoryId;}

    public void show(){
        System.out.println("商品类别ID为:"+categoryId+"  对应的类别为:"+categoryName);
    }

}

public class ProductInformation {
    private String productId;
    private String productName;
    private ProductCategory category;
    private int number;
    private double price;

    public ProductInformation() {
    }

    public ProductInformation(String productName, ProductCategory category, int number, double price) {
        this.productName = productName;
        this.category = category;
        this.number = number(number);
        this.price = price(price);
    }

    public ProductInformation(String productId, String productName, ProductCategory category, int number, double price) {
        this.productId = productId;
        this.productName = productName;
        this.category = category;
        this.number = number(number);
        this.price = price(price);
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public ProductCategory getCategory() {
        return category;
    }

    public void setCategory(ProductCategory category) {
        this.category = category;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    private int number(int number) {
        if (number < 0) {
            number = 0;
            System.out.println("库存数量异常,请联系管理员");
        }
        return number;
    }

    private double price(double price) {
        if (price < 0.0) {
            price = 0.0;
            System.out.println("输入价格无效!默认为0.0");
        }
        return price;
    }

    public void show1() {
        System.out.println("商品id为:" + productId + " 商品名称为:" + productName + " 所属类别为" + category.getCategoryName() + " 商品数量为:" + number + " 商品价格为:" + price);
    }

    public void show2() {
        System.out.println("商品名称:" + productName);
        System.out.println("所属类别:" + category.getCategoryName());
        System.out.println("商品售价:" + price);
        System.out.println("库存数量:" + number);
        System.out.println("商品总价:" + price * number);
    }

}

public class Test {
    public static void main(String[] args) {
        ProductCategory pc1 = new ProductCategory("洗发水", "00001");
        pc1.show();
        System.out.println("========================================");
        ProductInformation pi1 = new ProductInformation("潘婷洗发水400ml",pc1,16,40.5);
        pi1.show1();
        System.out.println("========================================");
        pi1.show2();
        System.out.println("========================================");
        ProductInformation pi2 = new ProductInformation("蜂花洗发水250ml",pc1,-1,11.5);
        pi2.show2();
    }
}

写在最后

之前分享了两个关于测试类的题目,在这里给出参考答案:

/*
    定义一个类Demo,其中定义一个求两个数据和的方法,定义一个测试类Test,进行测试
 */
//3.0写法
class Demo {
    private int a;
    private int b;

    public Demo(int a, int b) {
        this.a = a;
        this.b = b;
    }

    public int getB() {
        return b;
    }

    public void setB(int b) {
        this.b = b;
    }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public int sum() {
        return a + b;
    }
}

class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入两个整数(以空格分隔):");
        int a = sc.nextInt();
        int b = sc.nextInt();
        Demo d1 = new Demo(a, b);
        int sum = d1.sum();
        System.out.println("您输入的两个数的和为:" + sum);
    }
}

/*
    定义一个长方形类,定义 求周长和面积的方法,然后定义一个测试类Test2,进行测试。
 */

class Demo2 {
    private int a;
    private int b;

    public Demo2(int b, int a) {
        this.b = b;
        this.a = a;
    }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public int getB() {
        return b;
    }

    public void setB(int b) {
        this.b = b;
    }

    public int zhouChang() {
        return 2 * (a + b);
    }

    public int mianJi() {
        return a * b;
    }
}

class Test2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个矩形的长和宽(长和宽都应该是正整数,以空格分隔):");
        int h = sc.nextInt();
        int w = sc.nextInt();
        Demo2 d1 = new Demo2(h, w);
        int c = d1.zhouChang();
        int s = d1.mianJi();
        System.out.println("您输入的矩阵的周长为:" + c + "\n您输入的矩阵的面积为:" + s);
    }
}

好了,今天的分享到这就结束了,面向对象思想在初学时还是比较难懂的,大家一定要多多练习,查缺补漏,才能把它学好,如果有代码中有问题,欢迎在底下评论留言,代码仅供参考,不代表最终答案!

标签:练习题,String,int,void,System,private,基础知识,JavaSE,public
From: https://www.cnblogs.com/cjybigdatablog/p/18345954

相关文章

  • 电路基础知识——常见晶振电路
    电路基础知识——常见晶振电路本文介绍了有源和无源晶振的特性,包括精度、稳定性、引脚配置以及晶振的选型参数,如工作电压、输出电平、频率精度等。此外,还讨论了晶振的类型,如SPXO、VCXO和TCXO,以及PCB设计中应注意的事项,如负载电容和热传导的影响。有源晶振有源晶振的精度则可以......
  • USB基础知识总结
    USB基础知识总结USB基本概念介绍USB(UniversalSerialBus,通用串行总线)是1995年英特尔和微软等公司联合倡导发起的一种新的**PC串行通信协议。它基于通用连接技术,实现外设的简单快速连接,达到方便用户、降低成本、扩展PC连接外设范围的目的。其最大特点是支持热插拔和即插......
  • 24-08-04 JavaSE java集合详解
    24-08-04JavaSE集合详解文章目录24-08-04JavaSE集合详解理解集合java集合的体系框架Collection类Collection接口的常用方法集合的遍历iterator迭代器增强for循环List类List类的常用方法List的三种遍历方法List的排序ArrayList类ArrayList类的注意事项Arraylist的......
  • 17:Python数据类型练习题
    #1获取c1,c2相同的元素列表c1=[11,22,33]c2=[22,33,44]foriinc1:ifiinc2:print(i)#2获取c1中有,c2没有的元素列表foriinc1:ifinotinc2:print(i)#3获取c2中有,c1没有的元素列表foriinc2:ifinotinc1:print(i)#4获......
  • 数论基础知识(下)
    目录欧拉函数n的分解质因数求欧拉函数试除法求欧拉函数值积性函数筛法朴素筛埃氏筛欧拉筛(线性筛)线性筛欧拉函数快速幂同余欧拉定理费马小定理乘法逆元欧拉函数互质:∀a,b∈N,若gcd(a,b)=1,则a,b互质。定义: :1∼n......
  • Java基础编程练习题50题(没更新完会持续更新)
    1.古典问题有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?publicclassRabbit{publicstaticvoidmain(String[]args){intmonths=12;//假设计算12个月的情......
  • FreeRTOS基础知识详细版
    RTOS概念‌‌‌‌‌‌RTOS全称是RealTimeOperatingSystem,中文名就是实时操作系统,提供了任务调度、内存管理、中断处理等功能。‌1.任务调度:裸机编程需要手动调度任务,而RTOS提供自动的任务调度器。2.硬件管理:裸机编程需要开发者手动管理硬件资源,RTOS提供了......
  • python入门(1)基础知识介绍
    print函数a=10print(a)print(10)print("您好")print(a,b,"您好")print(chr(98))#chr将98转换为ASVCII值print("你好"+"上海")#都是字符串可以用+连接输出print('您好',end='不换行')#修改结束符,不换行,否则自动视为有\nfp=open("note.txt&......
  • 5.8软件工程基础知识-项目管理
    项目管理范围管理产品范围和项目范围管理过程WBS练习题进度管理基本原则过程活动资源估算软件规模估算方法进度安排关键路径法练习题成本管理过程成本的类型练习题软件配置管理配置项配置基线配置数据库练习题质量管理过程质量模型软件评审软件容错技术练习题风险......
  • Spring基础知识学习总结(一)
    一.基础介绍Spring是一个轻量级Java开发框架,最早有RodJohnson创建,目的是为了解决企业级应用开发的业务逻辑层和其他各层的耦合问题。它是一个分层的JavaSE/JavaEEfull-stack(一站式)轻量级开源框架,为开发Java应用程序提供全面的基础架构支持。Spring负责基础架构,因此Java开发......