【java】第11章 继承和多态 测试题(作业部分)

文章目录

    • 11.2
    • 11.4
    • 11.10
    • 11.13
    • 11.16
    • 11.17

11.2

【java】第11章 继承和多态 测试题(作业部分)_第1张图片

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", "[email protected]");
		Student p2 = new Student("BBB", "bbb", "22222222222", "[email protected]", 2);
		Employee p3 = new Employee("CCC", "ccc", "33333333333", "[email protected]", "C303", 7000.00, date);
		Faculty p4 = new Faculty("DDD", "ddd", "44444444444", "[email protected]", "D404", 8000.00, date, "4 hours", "professor");
		Staff p5 = new Staff("EEE", "eee", "55555555555", "[email protected]", "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

【java】第11章 继承和多态 测试题(作业部分)_第2张图片

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

在这里插入图片描述

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

【java】第11章 继承和多态 测试题(作业部分)_第3张图片

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

【java】第11章 继承和多态 测试题(作业部分)_第4张图片

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

在这里插入图片描述

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);
	}
}

你可能感兴趣的:(java)