【java】第11章 继承和多态 测试题(作业部分) 旧城等待, 2022-10-21 14:53 179阅读 0赞 ### 文章目录 ### * * 11.2 * 11.4 * 11.10 * 11.13 * 11.16 * 11.17 ## 11.2 ## ![在这里插入图片描述][20210505161352790.png] package wwr; //11.2 参考https://blog.csdn.net/superyzh/article/details/46607851?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-7.vipsorttest&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-7.vipsorttest import java.util.Scanner; class MyDate { private String year; private String month; private String day; public MyDate(String year, String month, String day) { this.year = year; this.month = month; this.day = day; } public String getDate() { return year + '.' + month + '.' + day; } } class Person { private String name; private String address; private String tel; private String email; public Person (String name, String address, String tel, String email){ this.name = name; this.address = address; this.tel = tel; this.email = email; } public String toString() { return "name: " + name + "\taddress: " + address + "\ttel: " + tel + "\temail: " + email; } } class Student extends Person { private int grade; public Student (String name, String address, String tel, String email, int grade) { super(name, address, tel, email); this.grade = grade; } public String toString() { return super.toString() + "\tgrade: " + grade; } } class Employee extends Person { String office; double salary; MyDate date; public Employee(String name, String address, String tel, String email, String office, double salary, MyDate date) { super(name, address, tel, email); this.office = office; this.salary = salary; this.date = date; } public String toString() { return super.toString() + "\toffice: " + office + "\tsalary: " + salary + "\tdate: " + date.getDate(); } } class Faculty extends Employee { String worktime; String level; public Faculty(String name, String address, String tel, String email, String office, double salary, MyDate date, String worktime, String level) { super(name, address, tel, email, office, salary, date); this.worktime = worktime; this.level = level; } public String toString() { return super.toString() + "\tworktime: " + worktime + "\tlevel: " + level; } } class Staff extends Employee { String title; public Staff(String name, String address, String tel, String email, String office, double salary, MyDate date,String title) { super(name, address, tel, email, office, salary, date); this.title = title; } public String toString() { return super.toString() + "\ttitle: " + title; } } public class Demo { public static void main(String[] args) { Scanner input = new Scanner(System.in); MyDate date = new MyDate("2021", "5", "8"); Person p1 = new Person("AAA", "aaa", "11111111111", "111111@qq.com"); Student p2 = new Student("BBB", "bbb", "22222222222", "222222@qq.com", 2); Employee p3 = new Employee("CCC", "ccc", "33333333333", "333333@qq.com", "C303", 7000.00, date); Faculty p4 = new Faculty("DDD", "ddd", "44444444444", "444444@qq.com", "D404", 8000.00, date, "4 hours", "professor"); Staff p5 = new Staff("EEE", "eee", "55555555555", "555555@qq.com", "E505", 9000.00, date, "director"); System.out.println(p1.toString()); System.out.println(p2.toString()); System.out.println(p3.toString()); System.out.println(p4.toString()); System.out.println(p5.toString()); } } ## 11.4 ## ![在这里插入图片描述][2021050516141273.png] package wwr; //11.4 参考https://blog.csdn.net/jxh1025_/article/details/109304393 import java.util.ArrayList; import java.util.Scanner; public class Demo { public static void main(String[] args) { ArrayList<Integer> num = new ArrayList<>(); Scanner input = new Scanner(System.in); System.out.print("Enter numbers end with 0: "); int number = input.nextInt(); while (number != 0){ num.add(number); number = input.nextInt(); } int res = max(num); System.out.println("The max number is " + res); } public static Integer max(ArrayList<Integer> list){ if (list.size() == 0 || list == null) return 0; int ret = list.get(0); for (int i = 1;i < list.size();++i) if (list.get(i) > ret) ret = list.get(i); return ret; } } ## 11.10 ## ![在这里插入图片描述][202105051614270.png] package wwr; //11.10 参考https://blog.csdn.net/cxj18867076391/article/details/105587824/?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-2&spm=1001.2101.3001.4242 import java.util.Scanner; import java.util.ArrayList; class MyStack extends ArrayList{ private ArrayList<Object> list=new ArrayList<>(); public boolean isEmpty() { return list.isEmpty(); } public int getSize() { return list.size(); } public Object peek() { return list.get(getSize()-1); } public Object pop() { Object o=list.get(getSize()-1); list.remove(getSize()-1); return o; } public void push(Object o) { list.add(o); } @Override public String toString() { return "stack: " + list.toString(); } } public class Demo { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("Enter 5 Strings: "); String s1=input.nextLine(); String s2=input.nextLine(); String s3=input.nextLine(); String s4=input.nextLine(); String s5=input.nextLine(); input.close(); //创建一个栈类 MyStack stack=new MyStack(); //入栈 stack.push(s1); stack.push(s2); stack.push(s3); stack.push(s4); stack.push(s5); //逆序输出 int leng=stack.getSize(); for(int i=0; i<leng; i++) { System.out.println(stack.pop()); } } } ## 11.13 ## ![在这里插入图片描述][20210505161444282.png] package wwr; //11.13 参考https://blog.csdn.net/cxj18867076391/article/details/105587824/?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-2&spm=1001.2101.3001.4242 import java.util.ArrayList; import java.util.Scanner; public class Demo { public static void removeDuplicate(ArrayList<Integer> list) { ArrayList<Integer> newList = new ArrayList<>(); for(int i = 0; i < list.size(); i++) { if( !newList.contains(list.get(i)) ) { newList.add(list.get(i)); } } System.out.print("The distinct integers are "); for(int i = 0; i < newList.size(); i++) { System.out.print(newList.get(i) + " "); } } public static void main(String[] args) { Scanner input = new Scanner(System.in); ArrayList<Integer> list = new ArrayList<>(); System.out.print("Enter ten integers: "); int n1 = input.nextInt(); int n2 = input.nextInt(); int n3 = input.nextInt(); int n4 = input.nextInt(); int n5 = input.nextInt(); int n6 = input.nextInt(); int n7 = input.nextInt(); int n8 = input.nextInt(); int n9 = input.nextInt(); int n10 = input.nextInt(); input.close(); list.add(n1); list.add(n2); list.add(n3); list.add(n4); list.add(n5); list.add(n6); list.add(n7); list.add(n8); list.add(n9); list.add(n10); removeDuplicate(list); } } ## 11.16 ## ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1ODc4Mjky_size_16_color_FFFFFF_t_70] package wwr; //11.16 参考https://blog.csdn.net/cxj18867076391/article/details/105587824/?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-2&spm=1001.2101.3001.4242 import java.util.ArrayList; import java.util.Scanner; public class Demo { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number1 = (int) (Math.random() * 10); int number2 = (int) (Math.random() * 10); ArrayList<Integer> list = new ArrayList<>(); System.out.print("What is " + number1 + " + " + number2 + "? "); int answer = input.nextInt(); while(number1 + number2 != answer) { if(!list.contains(answer)) { list.add(answer); } else { System.out.println("You already entered " + answer); } System.out.print("Wrong answer. Try again. What is " + number1 + " + " + number2 + "? "); answer = input.nextInt(); } System.out.println("You got it! "); } } ## 11.17 ## ![在这里插入图片描述][20210505161539154.png] package wwr; //11.17 import java.util.Scanner; public class Demo { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter an integer m: "); int m = input.nextInt(); int n = m; int[] a = new int[m * m + 2]; for(int i = 1; i <= m * m; i++) { a[i] = 0; } for(int i = 1; i * i <= m * m; i++) { a[i * i] = 1; } for(int i = 1; i <= m * m; i++) { if(a[i * m] == 1) { n = i; break; } } System.out.print("The smallest n makes m * n a perfect square is " + n); } } [20210505161352790.png]: /images/20221021/cb60f2e9c9a84903ae6b55d0bc525429.png [2021050516141273.png]: /images/20221021/a7ecf8e252494430bb78453db911e20a.png [202105051614270.png]: /images/20221021/29843ae015854057ac7efded408e8f8c.png [20210505161444282.png]: /images/20221021/e012626acde9441ba8a799c951ae93e3.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1ODc4Mjky_size_16_color_FFFFFF_t_70]: /images/20221021/a6c7a40253af49d3aeeaefc5bf346547.png [20210505161539154.png]: /images/20221021/b153c5d6de354b38b43b46889d814edc.png
相关 java--第6章 继承与多态 实验目的: ![f03cff38b5ad7766814c0998bc7c8f3c.png][] 1.理解类的继承,掌握变量隐藏、方法覆盖的概念。 2.理解多态概念,掌 今天药忘吃喽~/ 2023年01月02日 14:29/ 0 赞/ 172 阅读
相关 【java】第7章 数组 测试题(作业部分) 文章目录 7.5 7.17 7.18 7.19 7.22 7.23 7.3 超、凢脫俗/ 2022年11月17日 13:48/ 0 赞/ 191 阅读
相关 【java】第6章 方法 测试题(作业部分) 文章目录 6.2 6.3 6.13 6.18 6.23 6.28 6.29 ╰+攻爆jí腚メ/ 2022年11月14日 13:29/ 0 赞/ 153 阅读
相关 【java】第4章 数学函数&字符串 测试题(作业部分) 文章目录 4.6 4.11 4.16 4.21 4.22 4.24 4.2 清疚/ 2022年11月10日 14:20/ 0 赞/ 167 阅读
相关 【java】第3章 选择 测试题(作业部分) 文章目录 3.15 3.22 3.27 3.28 3.29 3.15 ![在这里插入图片描述] 淩亂°似流年/ 2022年11月07日 14:56/ 0 赞/ 173 阅读
相关 【java】第11章 继承和多态 测试题(作业部分) 文章目录 11.2 11.4 11.10 11.13 11.16 11.17 11. 旧城等待,/ 2022年10月21日 14:53/ 0 赞/ 180 阅读
相关 【java】第10章 面向对象思考 测试题(作业部分) 文章目录 10.3 10.5 10.10 10.13 10.18 10.19 爱被打了一巴掌/ 2022年10月21日 14:53/ 0 赞/ 179 阅读
相关 【java】第9章 对象和类 测试题(作业部分) 文章目录 9.1 9.4 9.5 9.9 9.12 9.13 9.1 ![在这里 落日映苍穹つ/ 2022年10月21日 14:53/ 0 赞/ 187 阅读
相关 Java面试题——继承,多态 一、面向对象的思想 Java是一门纯粹的面向对象的语言。面向对象这种程序设计模式它将现实世界中的一切事物都看作是对象,例如,一个人是一个对象,汽车、飞机、小鸟等等,都是对象 港控/mmm°/ 2022年06月17日 09:27/ 0 赞/ 223 阅读
相关 Java继承多态部分习题 1 下列程序的输出结果为: class Base { Base() { int i = 100; Sy 忘是亡心i/ 2022年05月21日 11:48/ 0 赞/ 155 阅读
还没有评论,来说两句吧...