java语言程序设计基础篇第十一章编程练习题


1

package yongheng;
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();
    }
}

2

package yongheng;
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;
    }
}


3
4

package yongheng;
import java.util.Scanner;
import java.util.ArrayList;

public class Main{

    public static void main(String args[]){
        ArrayList num = new ArrayList<>();
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        for(int i = 0; i < n; ++i){
            num.add(cin.nextInt());
        }
        int res = max(num);
        System.out.println(res);
    }

    public static Integer max(ArrayList 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;
    }
}

5
6

package yongheng;

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

public class Main {

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

}

7
8
9
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();
    }
}
 
  

                            
                        
                    
                    
                    

你可能感兴趣的:(Java学习)