下面是解决这两个问题的源代码示例:
### 问题1:Java接口
我们将定义一个名为 `Human` 的接口,并定义两个实现此接口的类:`Student` 和 `Worker`。在主类中,我们将使用接口变量通过多态机制来输出信息。
// 定义接口
interface Human {
void speak();
}
// 实现Student类
class Student implements Human {
private String name;
private String studentId;
private String school;
private String major;
private String interest;
public Student(String name, String studentId, String school, String major, String interest) {
this.name = name;
this.studentId = studentId;
this.school = school;
this.major = major;
this.interest = interest;
}
@Override
public void speak() {
System.out.println("姓名: " + name);
System.out.println("学号: " + studentId);
System.out.println("学校: " + school);
System.out.println("专业: " + major);
System.out.println("兴趣: " + interest);
}
}
// 实现Worker类
class Worker implements Human {
private String name;
private String company;
private String position;
private String interest;
public Worker(String name, String company, String position, String interest) {
this.name = name;
this.company = company;
this.position = position;
this.interest = interest;
}
@Override
public void speak() {
System.out.println("姓名: " + name);
System.out.println("工作单位: " + company);
System.out.println("职务: " + position);
System.out.println("兴趣: " + interest);
}
}
// 主类
public class Main {
public static void main(String[] args) {
Human student = new Student("张三", "123456", "某某大学", "计算机科学", "编程");
Human worker = new Worker("李四", "某某公司", "工程师", "旅游");
student.speak();
System.out.println();
worker.speak();
}
}
### 问题2:评价成绩
我们定义一个名为 `Compute` 的接口,它将用于计算平均值。我们将在类 `Gymnastics` 和 `School` 中实现这个接口,并在主方法中演示如何计算并输出分数。
import java.util.Arrays;
import java.util.Scanner;
// 定义接口
interface Compute {
double computeAverage(double[] scores);
}
// 实现Gymnastics类
class Gymnastics implements Compute {
@Override
public double computeAverage(double[] scores) {
Arrays.sort(scores);
double sum = 0;
for (int i = 1; i < scores.length - 1; i++) {
sum += scores[i];
}
return sum / (scores.length - 2);
}
}
// 实现School类
class School implements Compute {
@Override
public double computeAverage(double[] scores) {
double sum = 0;
for (double score : scores) {
sum += score;
}
return sum / scores.length;
}
}
// 主类
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Gymnastics Score Calculation
System.out.println("请输入体操比赛选手的评分数量:");
int n1 = scanner.nextInt();
double[] gymnastScores = new double[n1];
System.out.println("输入每个评委的分数:");
for (int i = 0; i < n1; i++) {
gymnastScores[i] = scanner.nextDouble();
}
Compute gymnastics = new Gymnastics();
double finalGymnastScore = gymnastics.computeAverage(gymnastScores);
System.out.printf("体操选手最后得分: %6.3f\n", finalGymnastScore);
// School Score Calculation
System.out.println("请输入班级同学的人数:");
int n2 = scanner.nextInt();
double[] classScores = new double[n2];
System.out.println("输入每个同学的考试分数:");
for (int i = 0; i < n2; i++) {
classScores[i] = scanner.nextDouble();
}
Compute school = new School();
double classAverageScore = school.computeAverage(classScores);
System.out.printf("班级考试平均分数: %-6.2f\n", classAverageScore);
scanner.close();
}
}
以上代码分别解决了两个问题,并确保符合题目的要求。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |