初来乍到,承蒙关照
啦啦啦啦啦~~~~~~

题目1

为二次方程式\(ax^2+bx+c=0\)设计一个名为QuadraticEquation的类,这个类包括:

  • 代表三个系数的私有数据域a、b和c;

  • 一个参数为a、b和c的构造方法;

  • 数据域a、b和c的三个get/set方法;

  • 一个名为getDiscriminant()的方法返回判别式,\(b^2-4ac\)

  • 一个名为getRoot1()和getRoot2()的方法返回等式的两个根:

    • \(r_1=\frac{-b+\sqrt{b^2-4ac}}{2a}\)

    • \(r_1=\frac{-b-\sqrt{b^2-4ac}}{2a}\)

    这些方法只有在判别式为非负数的时候才有用,如果判别式为负,这些方法返回0。

实现这个类,编写一个测试程序,提示用户输入a、b和c的值,然后显示判别式的结果,如果判别式为正数,显示两个根,如果判别式为0,显示一个根,否则显示“The equation has no real roots”,参见下图运行实例:

注:可以使用Math.pow(x,0.5)来计算\(\sqrt{x}\)

Enter a, b, c: 1.0  3  1 
The roots are -0.381966 and -2.61803
Enter a, b, c: 1  2.0  1 
The roots is -1
Enter a, b, c: 1  2  3 
The equation has no real roots

答题区:

源码:

package com.oop.test1;


import static java.lang.Math.sqrt;

public class QuadraticEquation {
    private double a;
    private double b;
    private double c;
    //默认构造器
    public QuadraticEquation() {

    }
    //自定义有参构造
    public QuadraticEquation(double a,double b,double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }
    //获取 a 的值
    public double getA() {
        return a;
    }
    //为 a 赋值
    public void setA(double a) {
        this.a = a;
    }
    public double getB() {
        return b;
    }
    public void setB(double b) {
        this.b = b;
    }
    public double getC() {
        return c;
    }
    public void setC(double c) {
        this.c = c;
    }
    //方法getDiscriminant(),用于返回判别式的值
    public double getDiscriminant(double a,double b,double c){
        return b * b -4 * a * c;
    }
    //计算有实根的方程的第一个根
    public double getRoot1(double a,double b,double c){
        if (getDiscriminant(a,b,c)>=0){
            return (-b+sqrt(b*b-4*a*c))/(2*a);
        }
        else{
            return 0;
        }
    }
    //计算有实根的方程的第二个根
    public double getRoot2(double a,double b,double c){
        if (getDiscriminant(a,b,c)>=0){
            return (-b-sqrt(b*b-4*a*c))/(2*a);
        }
        else{
            return 0;
        }
    }
}
package com.oop.test1;

import java.text.DecimalFormat;
import java.util.Scanner;

public class Application1 {
    public static void main(String[] args) {
        System.out.print("Enter a, b, c: ");
        Scanner scanner = new Scanner(System.in);
        double a = scanner.nextDouble();//键盘获取方程系数
        double b = scanner.nextDouble();
        double c = scanner.nextDouble();
        String r1,r2;

        DecimalFormat decimalFormat = new DecimalFormat("#.######");//根小数位过多时取前六位

        QuadraticEquation q = new QuadraticEquation();//调用非静态类中方法,需先实例化
        q.setA(a);//为系数赋值
        q.setB(b);
        q.setC(c);
        if (q.getDiscriminant(a,b,c) > 0){
            r1 = decimalFormat.format(q.getRoot1(a,b,c));
            r2 = decimalFormat.format(q.getRoot2(a,b,c));
            System.out.println("The roots are "+ r1 +" and"+ r2);
        } else if (q.getDiscriminant(a,b,c) == 0) {
            r1 = decimalFormat.format(q.getRoot1(a,b,c));
            System.out.println("The roots is "+ r1 );
        }else{
            System.out.println("The equation has no real roots");
        }
        scanner.close();//关闭键盘输入

    }

}

运行结果:

Enter a, b, c: 1.0 3 1
The roots are -0.381966 and-2.618034

进程已结束,退出代码0
Enter a, b, c: 1 2.0 1
The roots is -1

进程已结束,退出代码0
Enter a, b, c: 1 2 3
The equation has no real roots

进程已结束,退出代码0
Enter a, b, c: 1 -7 6
The roots are 6 and1

进程已结束,退出代码0

题目2

设计一个名为Person的类和它的两个名为Student学生子类和Employee雇员子类。雇员Employee类又有子类:教员类Faculty和职员类Staff。

其中:

  • 学生有班级状态(大一、大二、大三、大四、研一、研二…),将其定义为常量。
  • 一个雇员有办公室、工资和受聘日期等信息。
  • 教员有办公时间和级别,职员有职务称号。
  • 每个人都有姓名、地址、电话号码和电子邮件地址
  • 定义一个名为MyDate的类,包含数据域year(年)、month (月)和day (日) ,用于显示上述类中出现的日期数据域。在类中定义一个getDate方法, 用于展示年、月、日信息,展示格式为:xxxx-xx-xx;
  • 覆盖每个类中的toString方法,显示相应的类中的所有信息,比如学生类,需要将其姓名、地址、电话号码、电子邮件地址、班级信息显示出来。

实现这些类。编写一个测试程序,创建Person、Student、Employee、Faculty和Staff,并且调用它们的toString()方法。

答题区:

源码:

package com.oop.test2;

public class Person {
    private String name;
    private String address;
    private String phoneNumber;
    private String email;
    //无参构造
    public Person() {
    }

    //有参构造
    public Person(String name, String address, String phoneNumber, String email) {
        this.name = name;
        this.address = address;
        this.phoneNumber = phoneNumber;
        this.email = email;
    }
    //父类方法
    public String toString() {
        return "Name: " + name +
                "\nAddress: " + address +
                "\nPhone Number: " + phoneNumber +
                "\nEmail: " + email;
    }

}
package com.oop.test2;

public class Student extends Person{
    //定义班级常量
    private final String classStatus;
    //构造器
    public Student(String name, String address, String phoneNumber, String email, String classStatus) {
        super(name, address, phoneNumber, email);
        this.classStatus = classStatus;
    }
    //重写父类Person的方法
    @Override
    public String toString() {
        return super.toString()+
                "\nClass Status: " + classStatus;
    }
}

package com.oop.test2;

public class Employee extends Person{
    private String office;
    private double salary;
    private MyDate hireDate;
    //构造器
    public Employee(String name, String address, String phoneNumber, String email, String office, double salary, MyDate hireDate) {
        super(name, address, phoneNumber, email);
        this.office = office;
        this.salary = salary;
        this.hireDate = hireDate;
    }
    //重写父类Person的方法
    @Override
    public String toString() {
        return super.toString() +
                "\nOffice: " + office +
                "\nSalary: " + salary +
                "\nHire Date: " + hireDate.getDate();
    }
}
package com.oop.test2;

public class Faculty extends Employee{
    private String officeHours;
    private String rank;
    //构造器
    public Faculty(String name, String address, String phoneNumber, String email, String office, double salary, MyDate hireDate, String officeHours, String rank) {
        super(name, address, phoneNumber, email, office, salary, hireDate);
        this.officeHours = officeHours;
        this.rank = rank;
    }
    //重写父类Employee的方法
    @Override
    public String toString() {
        return super.toString() +
                "\nOffice Hours: " + officeHours +
                "\nRank: " + rank;
    }

}
package com.oop.test2;

public class Staff extends Employee{
    private String post;
    //构造器
    public Staff(String name, String address, String phoneNumber, String email, String office, double salary, MyDate hireDate, String post) {
        super(name, address, phoneNumber, email, office, salary, hireDate);
        this.post = post;
    }
    //重写父类Employee的方法
    @Override
    public String toString() {
        return super.toString() +
                "\nPost: " + post;
    }
}

package com.oop.test2;

public class MyDate {
    private int year;
    private int month;
    private int day;
    //构造器,初始化入职时间
    public MyDate(int year, int month, int day) {
            this.year = year;
            this.month = month;
            this.day = day;
        }
     
     public String getDate() {
         return year + "-" + month + "-" + day;
        }
}

package com.oop.test2;

public class Application2 {
    public static void main(String[] args) {
        // 创建一个Person对象并调用toString方法
        Person person = new Person("张三", "安徽省合肥市庐阳区", "13333765589", "868381097@qq.com");
        System.out.println(person.toString());

        // 创建一个Student对象并调用toString方法
        Student student = new Student("李四", "河南省信阳市商城县", "16678945637", "867751097@qq.com", "大三");
        System.out.println("====================================");
        System.out.println(student.toString());

        // 创建一个Employee对象并调用toString方法
        MyDate hireDate1 = new MyDate(2020, 5, 10);
        Employee employee = new Employee("王五", "安徽省淮南市xxx县", "15567345908", "65427907533@163.com", "格物楼305", 50000, hireDate1);
        System.out.println("====================================");
        System.out.println(employee.toString());

        // 创建一个Faculty对象并调用toString方法
        MyDate hireDate2 = new MyDate(2010, 5, 20);
        Faculty faculty = new Faculty("柳六", "安徽省合肥市包河区屯溪路193号合肥工业大学", "17735830987", "liuliu@163.com", "机械楼503", 60000, hireDate2, "9 AM - 5 PM", "Professor");
        System.out.println("====================================");
        System.out.println(faculty.toString());

        // 创建一个Staff对象并调用toString方法
        MyDate hireDate3 = new MyDate(2019, 6, 04);
        Staff staff = new Staff("戚琪", "安徽省合肥市蜀山区丹霞路485号合肥工业大学(翡翠湖校区)", "13809762850", "qiqi@163.com", "综合楼308", 40000, hireDate3, "teacher");
        System.out.println("====================================");
        System.out.println(staff.toString());

    }
}

运行结果:

Name: 张三
Address: 安徽省合肥市庐阳区
Phone Number: 13333765589
Email: 868381097@qq.com
====================================
Name: 李四
Address: 河南省信阳市商城县
Phone Number: 16678945637
Email: 867751097@qq.com
Class Status: 大三
====================================
Name: 王五
Address: 安徽省淮南市xxx县
Phone Number: 15567345908
Email: 65427907533@163.com
Office: 格物楼305
Salary: 50000.0
Hire Date: 2020-5-10
====================================
Name: 柳六
Address: 安徽省合肥市包河区屯溪路193号合肥工业大学
Phone Number: 17735830987
Email: liuliu@163.com
Office: 机械楼503
Salary: 60000.0
Hire Date: 2010-5-20
Office Hours: 9 AM - 5 PM
Rank: Professor
====================================
Name: 戚琪
Address: 安徽省合肥市蜀山区丹霞路485号合肥工业大学(翡翠湖校区)
Phone Number: 13809762850
Email: qiqi@163.com
Office: 综合楼308
Salary: 40000.0
Hire Date: 2019-6-4
Post: teacher

进程已结束,退出代码0

题目3

public class Test {
    public static void main(String[] args) {
        Object fruit = new Fruit();
        Object apple = (Apple)fruit;
    }
}
class Fruit {}
class Apple extends Fruit {}

上述代码的错误是什么?

答题区:

错误:

public class test3 {

        public static void main(String[] args) {
            Object fruit = new Fruit();//声明了一个 Object 类型的的变量 fruit, fruit 实际类型为Fruit
            Object apple = (Apple)fruit;/*父转子的强转换,只有在实际对象的类型确实是子类类型时才会成功,
                                          否则将引发ClassCastException异常。
                                          这里的fruit的实际类型并非Apple,所以会报错*/
        }
        
}

修改后:

public class Test {
    public static void main(String[] args) {
        Fruit fruit = new Apple();//声明了一个Fruit类型的的变量 fruit, fruit 实际类型为Apple
        Apple apple = (Apple) fruit;//fruit的实际类型为Apple,所以可就行向下的强制转换
    }
}
class Fruit {}
class Apple extends Fruit {}

题目4

创建名为Circle的类,包含私有数据域r(半径),包含一个参数为r的构造方法,包含公共方法getArea()用于获取其面积,重写toString方法,要求能够显示该圆的信息;创建名为ComparableCircle的类,它扩展Circle类并实现Comparable接口。实现compareTo方法,使其根据面积比较两个圆。编写一个测试程序求出ComparableCircle对象的两个实例中的较大者。

提示:圆周率πMath.PI

答题区:

源码:

package com.oop.test4;

import static java.lang.Math.PI;

public class Circle {
    private double r;
    //无参构造
    public Circle() {
    }
    //参数为r的有参构造
    public Circle(double r) {
        this.r = r;
    }
    //用于获取半径
    public double getR() {
        return r;
    }

    //计算面积
    public double getArea(double r){
        return r * r * PI;
    }
    public String toString(){
        return "圆的半径r=: " + r +
                "\n圆的面积S:" + getArea(r) + "\n";
    }
}
package com.oop.test4;

public interface Comparable {
   void compareTo(double r1, double r2);
}
package com.oop.test4;


public class ComparableCircle extends Circle implements Comparable {
    public ComparableCircle() {
    }
    public ComparableCircle(double r) {
        super(r);
    }

    @Override
    public void compareTo(double r1, double r2) {

        if (getArea(r1) > getArea(r2)){
            System.out.println("半径为:" + r1 + "的圆面积更大");
        }
        else if (getArea(r1) < getArea(r2)){
            System.out.println("半径为:" + r2 + "的圆面积更大");
        }
        else{
            System.out.println("两个圆的面积相同");
        }

    }
}
package com.oop.test4;

import java.util.Scanner;

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

        Scanner scanner = new Scanner(System.in);

        System.out.print("请分别输入两个圆的半径: ");

        double r1 = scanner.nextDouble();
        double r2 = scanner.nextDouble();

        // 创建一个ComparableCircle对象并调用toString方法
        ComparableCircle comparableCircle1 = new ComparableCircle(r1);
        System.out.print(comparableCircle1.toString());
        System.out.println("--------------------------");

        // 创建一个ComparableCircle2对象并调用toString方法
        ComparableCircle comparableCircle2 = new ComparableCircle(r2);
        System.out.print(comparableCircle2.toString());
        System.out.println("--------------------------");

        Comparable comparable = new ComparableCircle();
        comparable.compareTo(comparableCircle1.getR(),comparableCircle2.getR());

        scanner.close();
        
    }
}

运行结果:

请分别输入两个圆的半径: 5 3.4
圆的半径r=: 5.0
圆的面积S:78.53981633974483
--------------------------
圆的半径r=: 3.4
圆的面积S:36.316811075498
--------------------------
半径为:5.0的圆面积更大

进程已结束,退出代码0
请分别输入两个圆的半径: 6 9.2
圆的半径r=: 6.0
圆的面积S:113.09733552923255
--------------------------
圆的半径r=: 9.2
圆的面积S:265.90440219984004
--------------------------
半径为:9.2的圆面积更大

进程已结束,退出代码0
请分别输入两个圆的半径: 9 9
圆的半径r=: 9.0
圆的面积S:254.46900494077323
--------------------------
圆的半径r=: 9.0
圆的面积S:254.46900494077323
--------------------------
两个圆的面积相同

进程已结束,退出代码0
posted on 2023-07-20 14:59  绿萝佛裳  阅读(24)  评论(0编辑  收藏  举报