当前位置: 首页 > news >正文

使用Java完成下面程序

第一题:

1. 创建Student类,包含如下私有属性,提供有参和无参构造器及公共的get和set方法 姓名 年龄 学历 ,

在测试类中,利用满参和无参的构造器来创建五个对象,将对象存入数组中。

小红 12 小学 小米 21 大学 小明 14 初中 小强 19 大学 小宝 30 研究生

遍历数组,将年龄小于15的人的学历改为幼儿园。

 遍历数组,把所有的年龄都增加5岁

 遍历数组,把年龄小于20的删除(删除就是把值设为null)

 遍历数组,打印所有的学生信息(小米-21-大学)

Student类代码如下

public class Student {private String name;private int age;private String title;public Student() {//无参构造}public  Student(String name, int age, String title) {//有参this.name = name;this.age = age;this.title = title;}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 getTitle() {return title;}public void setTitle(String title) {this.title = title;}public void print(){System.out.println("姓名:"+name+"年龄"+age+"学历"+title);}
}

测试类代码如下

public class TestStudent {public static void main(String[] args) {Student s1 = new Student("小红", 12, "小学");Student s2 = new Student("小米", 21, "大学");Student s3 = new Student("小明", 14, "初中");Student s4 = new Student("小强", 19, "大学");Student s5 = new Student("小宝", 30, "研究生");Student[] s = {s1, s2, s3, s4, s5};//遍历数组int i;for (i = 0; i < s.length; i++) {if (s[i].getAge() < 15) {s[i].setTitle("幼儿园");}s[i].setAge(s[i].getAge()+5);if (s[i].getAge() < 20) {s[i]=null;}else{System.out.println(s[i].getName()+s[i].getAge()+s[i].getTitle());}}}
}

结果如下

 

第二题:

1.创建Phone类,包含如下私有的属性(提供所需的构造器和公共的get和set方法) 品牌 价格 使用年限

2.在测试类中,利用满参构造创建4个对象,将对象存入数组中。 华为-1200-4 苹果-9000-1 锤子-3000-3 小米-1800-2

3.遍历数组,将使用年限小于2或价格低于2000的手机筛选出来。

4.在控制台上打印所有筛选出来的对象(格式:华为-1200-4)

Phone类代码如下

public class Phone {private String brand;private int price;private int year;public Phone() {}public Phone(String brand, int price, int year) {this.brand = brand;this.price = price;this.year = year;}public String getBrand(){return brand;}public void setBrand(String brand){this.brand = brand;}public int getPrice(){return price;}public void setPrice(int price){this.price = price;}public int getYear(){return year;}public void setYear(int year){this.year = year;}public void print(){System.out.println(brand+"--"+price+"--"+year);}
}

测试类代码如下

public class TestPhone {public static void main(String[] args) {Phone p1 = new Phone("华为",1200,4);Phone p2 = new Phone("苹果",9000,1);Phone p3 = new Phone("锤子",3000,3);Phone p4 = new Phone("小米",1800,2);Phone[] p = {p1,p2,p3,p4};int i;for(i = 0;i<p.length;i++){if(p[i].getYear()>=2&&p[i].getPrice()>=2000){p[i].print();}}}
}

结果如下

 

第三题: 定义一个火车票类(Ticket), 包含私有的属性 出发站(startStation), 到达站(stopStation), 出发时间(startTime), 身份证号(id), 票价(price), 座位号(seat), 车次(trainNumber)属性, 火车票类包含一个打印火车票信息的方法printTicketInfo

要求: 使用有参和无参的构造器和公共的get和set方法来创建三个对象并打印 

火车票类Ticket代码如下

public class Ticket {private String startStation;private String stopStation;private String startTime;private int id;private int price;private int seat;private String trainNumber;public Ticket(){}public String getStartStation() {return startStation;}public void setStartStation(String startStation) {this.startStation = startStation;}public String getStopStation() {return stopStation;}public void setStopStation(String stopStation) {this.stopStation = stopStation;}public String getStartTime() {return startTime;}public void setStartTime(String startTime) {this.startTime = startTime;}public int getId() {return id;}public void setId(int id) {this.id = id;}public int getPrice() {return price;}public void setPrice(int price) {this.price = price;}public int getSeat() {return seat;}public void setSeat(int seat) {this.seat = seat;}public String getTrainNumber() {return trainNumber;}public void setTrainNumber(String trainNumber) {this.trainNumber = trainNumber;}public void Ticket(String startStation, String stopStation, String startTime, int id, int price, int seat, String trainNumber) {this.startStation = startStation;this.stopStation = stopStation;this.startTime = startTime;this.id = id;this.price = price;this.seat = seat;this.trainNumber = trainNumber;}public void printTicketInfo(){System.out.println("出发站 "+startStation+" 到达站 "+stopStation+" 出发时间 "+startTime+" 身份证号 "+id+" 票价 "+price+" 座位号 "+seat+" 车次 "+trainNumber);}
}

测试类代码如下

public class TestTicket {public static void main(String[] args) {Ticket t = new Ticket();t.setId(1);t.setSeat(23);t.setPrice(123);t.setStartStation("郑州");t.setStartTime("九点");t.setTrainNumber("g67");t.setStopStation("新乡");t.printTicketInfo();Ticket t1 = new Ticket();t1.Ticket("新乡","郑州","八点",3,45,56,"g89");t1.printTicketInfo();Ticket t2 = new Ticket();t2.Ticket("周口","郑州","十点",3,45,56,"g69");t2.printTicketInfo();}
}

结果如下

 

 

 

 

 

http://www.dtcms.com/a/276029.html

相关文章:

  • Vue3 学习教程,从入门到精通,Vue3指令知识点及使用方法详细介绍(6)
  • 组合数学学习笔记
  • Stance Classification with Target-Specific Neural Attention Networks
  • Linux解决vim中文乱码问题
  • SE机制深度解析:从原理到实现
  • tiktok 弹幕 逆向分析
  • 缺陷特征粘贴增强流程
  • 李宏毅(Deep Learning)--(三)
  • python内置函数 —— zip
  • MyBatis实现分页查询-苍穹外卖笔记
  • 在 Android 库模块(AAR)中,BuildConfig 默认不会自动生成 VERSION_CODE 和 VERSION_NAME 字段
  • docker基础与常用命令
  • 如何让AI更高效
  • 留学真相:凌晨两点被海关拦下时,我才明白人生没有退路
  • 如何用Python编程实现一个简单的Web爬虫?
  • Echarts学习方法分享:跳过新手期,光速成为图表仙人!
  • 【Lucene/Elasticsearch】 数据类型(ES 字段类型) | 底层索引结构
  • 易混淆英语单词对比解析与记忆表
  • 股票的k线
  • BKD 树(Block KD-Tree)Lucene
  • 以太坊重放攻击
  • 特辑:Ubuntu,前世今生
  • 关于学习docker中遇到的问题
  • AI领域的黄埔军校:OpenAI是新一代的PayPal Mafia,门生故吏遍天下
  • 可以用一台伺服电机控制多台丝杆升降机联动使用吗
  • 类和对象—多态
  • C语言:20250712笔记
  • SpringBoot集合Swagger2构建可视化API文档
  • P2619 [国家集训队] Tree I
  • 【Datawhale AI夏令营】Task2 笔记:MCP Server开发的重难点