Java语言程序设计基础篇第十一章课后答案

11.1

package hgh;
import java.util.Scanner;

public class Main {
    public static void main(String agrs[]){
        Triangle test = new Triangle();
        System.out.println(test.toString());
    }
}  

class SimpleGeometricObject{
    private String color = "white";
    private boolean filled;
    private java.util.Date dateCreated;

    public SimpleGeometricObject(){
        dateCreated = new java.util.Date();
    }

    public SimpleGeometricObject(String color, boolean filled){
        dateCreated = new java.util.Date();
        this.color = color;
        this.filled = filled;
    }

    public String getColor(){
        return color;
    }

    public void setColor(String color){
        this.color = color;
    }

    public boolean isFilled(){
        return filled;
    }

    public void setFilled(boolean filled){
        this.filled = filled;
    }

    public java.util.Date getDateCreated(){
        return dateCreated;
    }

    public String toString(){
        return "created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled; 
    }
}

class Triangle extends SimpleGeometricObject{
    private double side1 = 1.0;
    private double side2 = 1.0;
    private double side3 = 1.0;

    public Triangle(){
        super();
        side1 = side2 = side3 = 1.0;
    }

    public Triangle(double side1, double side2, double side3){
        super();
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }

    public double getSide1(){
        return side1;
    }

    public double getSide2(){
        return side2;
    }

    public double getSide3(){
        return side3;
    }

    public double getArea(){
        double p = (side1 + side2+ side3) / 2;
        return Math.sqrt(p * (p-side1) * (p-side2) * (p-side3));
    }

    public double getPerimeter(){
        return side1 + side2 + side3;
    }

    @Override
    public String toString(){
        return "created on " + super.getDateCreated() + "\ncolor: " + super.getColor() + "\nfilled: " + super.isFilled()
                + "\nArea: " + this.getArea() + "\nPerimeter: " + this.getPerimeter();
    }
}

11.2:

package hgh;
import java.util.Scanner;

public class Main {
    public static void main(String agrs[]){
        Staff staff = new Staff();
        System.out.println(staff.toString());
    }
}

class Person{
    protected String name;
    private double height;
    private double weight;
    private String sex;

    public Person(){
        name = "default";
        height = weight = 0;
        sex = "female";
    }

    public Person(String name, double height, double weight, String sex){
        this.name = name;
        this.height = height;
        this.weight = weight;
        this.sex = sex;
    }

    public String getName(){
        return name;
    }

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

    public String getSex(){
        return sex;
    }

    public void setSex(String sex){
        this.sex = sex;
    }

    public double getHeight(){
        return height;
    }

    public void setHeight(double height){
        this.height = height;
    }

    public double getWeight(){
        return weight;
    }

    public void setWeight(double weight){
        this.weight = weight;
    }

    public String toString(){
        return "Name: " + name + "\nSex: " + sex + "\nHeight: " + height + "\nWeight: " + weight;
    }
}

class Student extends Person{
    public static int ONE = 1;
    public static int TWO = 2;
    public static int THREE = 3;
    public static int FOUR = 4;

    private int grade = 0;

    public Student(){
        super();
        grade = 0;
    }

    public Student(String name, double height, double weight, String sex, int grade){
        super(name,height,weight,sex);
        this.grade = grade;
    }

    public int getGrade(){
        return grade;
    }

    public void setGrade(int grade){
        this.grade = grade;
    }

    @Override
    public String toString(){
        return super.toString() + "\nGrade: " + grade;
    }
}

class Employee extends Person{
    private String address;
    private int tel;
    private String email;

    public Employee(){
        super();
        address = "default";
        tel = 0;
        email = "default";
    }

    public Employee(String name,double height, double weight, String sex,
            String address, int tel, String email){
        super(name,height,weight,sex);
        this.address = address;
        this.tel = tel;
        this.email = email;
    }

    public String getAddress(){
        return address;
    }

    public void setAddress(String address){
        this.address = address;
    }

    public int getTel(){
        return tel;
    }

    public void setTel(int tel){
        this.tel = tel;
    }

    public String getEmail(){
        return email;
    }

    public void setEmail(String email){
        this.email = email;
    }

    @Override
    public String toString(){
        return super.toString() + "\nAddreaa: " + address +
                "\nTel: " + tel + "\nEmail: " + email;
    }
}

class Faculty extends Employee{
    private double salary;
    private String office;
    private String startTime;
    private String endTime;
    private int grade;
    private int workTime;

    public Faculty(){
        super();
        salary = 0;
        office = "default";
        startTime = "timeless";
        endTime = "timeless";
        grade = 0;
        workTime = 0;
    }

    public Faculty(String name,double height, double weight, String sex,
            String address, int tel, String email, double salary, String office,
            String startTime, String endTime, int grade, int workTime){
        super(name,height,weight,sex,address,tel,email);
        this.salary = salary;
        this.office = office;
        this.startTime = startTime;
        this.endTime = endTime;
        this.grade = grade;
        this.workTime = workTime;
    }

    public double getSalary(){
        return salary;
    }

    public void setSalary(int salary){
        this.salary = salary;
    }

    public String getOffice(){
        return office;
    }

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

    public String getTime(){
        return startTime + "-" + endTime;
    }

    public void setTime(String startTime, String endTime){
        this.startTime = startTime;
        this.endTime = endTime;
    }

    public int getGrade(){
        return grade;
    }

    public void setGrade(int grade){
        this.grade = grade;
    }

    public int getWorkTime(){
        return workTime;
    }

    public void setWorkTime(int workTime){
        this.workTime = workTime;
    }

    @Override
    public String toString(){
        return super.toString() + "\nSalary: " + salary + "\nOffice: " + office + 
                "\nTime: " + startTime + "-" + endTime + "\nGrade: " + grade + "\nWorkTime: " + workTime;
    }

}

class Staff extends Employee{
    private double salary;
    private String office;
    private String startTime;
    private String endTime;
    private String duty;

    public Staff(){
        super();
        salary = 0;
        office = "default";
        startTime = "timeless";
        endTime = "timeless";
        duty = "default";
    }

    public Staff(String name,double height, double weight, String sex,
            String address, int tel, String email, double salary, String office,
            String startTime, String endTime, String duty){
        super(name,height,weight,sex,address,tel,email);
        this.salary = salary;
        this.office = office;
        this.startTime = startTime;
        this.endTime = endTime;
        this.duty = duty;
    }

    public double getSalary(){
        return salary;
    }

    public void setSalary(int salary){
        this.salary = salary;
    }

    public String getOffice(){
        return office;
    }

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

    public String getTime(){
        return startTime + "-" + endTime;
    }

    public void setTime(String startTime, String endTime){
        this.startTime = startTime;
        this.endTime = endTime;
    }

    public String getDuty(){
        return duty;
    }

    public void setDuty(String duty){
        this.duty = duty;
    }

    @Override
    public String toString(){
        return super.toString() + "\nSalary: " + salary + "\nOffice: " + office + 
                "\nTime: " + startTime + "-" + endTime + "\nDuty: " + duty;
    }
}

11.4:

package shap1;

import java.util.ArrayList;
import java.util.Scanner;

public class class11_4 {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter integers (input ends with 0): ");
        int value;
        do{
            value = sc.nextInt();
            if(!list.contains(value)&&value!=0){
                list.add(value);
            }
        }while(value!=0);
        System.out.println("The max Integer is : "+max(list));
    }
    
    
    public static Integer max(ArrayList<Integer> list){
        if(list.size()==0){
            return null;
        }
        int i=list.get(0);
        for (Integer integer : list) {
            i=i>integer?i:integer;
        }
        return i;
    }
    
}


11.6

package hgh;

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Date;

public class Main {

    public static void main(String[] args) {
        ArrayList<Object> obj = new ArrayList<>();
        obj.add(new Date());
        obj.add("this is a string");
        for(Object tmp : obj){
            System.out.println(tmp.toString());
        }
    }

}

11.7

public class seven {

public static void main(String[] args) {

      ArrayList<Integer> list = new ArrayList<>();
    Scanner input = new Scanner(System.in);
    while(true){
        int num = input.nextInt();
        if(num == 0) break;//输入0结束输入
        else list.add(num);
    }

    shuffle(list);

    for(Integer jkl :list) {
        System.out.print(jkl + " ");
    }

}
public static void shuffle(ArrayList<Integer> list) {
    Integer xiabiao = 0; //用来存取随机的下标
    int temp = 0;      //存取未换之前的值
    Random rand = new Random();
    for(int i = 0;i<list.size();i++) {
        xiabiao = rand.nextInt(list.size());
        temp = list.get(i);
        //交换
        list.set(i, list.get(xiabiao));
        list.set(xiabiao, temp);

    }

}

}


11.9

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

    ArrayList<Integer> list1 = new ArrayList<>(); //存行
    ArrayList<Integer> list2 = new ArrayList<>();  //存列
    Scanner input = new Scanner(System.in);
    System.out.println("请输入您想创建的矩阵大小");
    int num = input.nextInt();
    Integer row = 0;   
    Integer column = 0;
    Integer row_Max = 0;  //行里面最多有几个1
    Integer column_Max = 0; //列里面最多有几个1
    int[][] array = new int[num][num];
    Random rand = new Random();

    //完成初始化并且用Max记录行、列里面各自最多有几个1
    for(int i = 0;i < array.length;i++) {
        for(int j = 0;j < array[0].length;j++) {
            int randInt = rand.nextInt(2); //0-1之间的随机数
            array[i][j] = randInt;
            System.out.print(array[i][j] + " ");
            if(randInt == 1) row++; //‘1’ 的行累加器
        }
        if(row > row_Max) row_Max = row;  //存储行里面最多有几个1
        //归零
        row = 0;
        System.out.println();
    }

    for(int i = 0;i < array.length;i++) {
        for(int j = 0;j < array[0].length;j++) {
            if(array[j][i] == 1) column++; //‘1’的列累加器
        }
        if(column > column_Max) column_Max = column;  //存储列里面最多有几个1
        column = 0;
    }

    //遍历数组将最多1的行、列下标存到list1、list2里面
    for(int i = 0;i < array.length;i++) {
        for(int j = 0;j < array[0].length;j++) {
            if(array[i][j] == 1) row++;
            if(array[j][i] == 1) column++;
        }
        if(row == row_Max) list1.add(i);
        if(column == column_Max) list2.add(i);
        row = 0;
        column = 0;
    }

    //打印list1,list2的结果
    System.out.println("具有最多1的行为");
    for(Integer a:list1)
        System.out.print(a + " ");
    System.out.println('\n'+"具有最多1的列为");
    for(Integer b:list2)
        System.out.print(b + " ");
}

}

11.10

package yongheng;

import java.util.Scanner;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
        MyStack stack = new MyStack();
        stack.push(5);
        stack.push(10);
        System.out.println(stack.toString() + "\n" + stack.pop() + " " + stack.pop() + "\n" + stack.toString());
    }

}

class MyStack extends ArrayList{

    @Override
    public boolean isEmpty(){
        return super.isEmpty();
    }

    public int getSize(){
        return super.size();
    }

    public Object peek(){
        return super.get(super.size()-1);
    }

    public Object pop(){
        Object o = super.get(super.size()-1);
        super.remove(super.size()-1);
        return o;
    }

    public void push(Object o){
        super.add(o);
    }

    @Override
    public String toString(){
        return "stack: " + super.toString();
    }
}







展开阅读全文

没有更多推荐了,返回首页