java语言程序设计与数据结构第11版第十章练习题10.14
时间: 2023-07-25 16:07:21 浏览: 110
题目描述:
编写一个程序,将一个字符串作为参数,并返回一个按字母顺序排列的新字符串,例如,输入"hello, world",应该返回" ,dehllloorw"。
解题思路:
- 将字符串转换成字符数组
- 对字符数组进行排序
- 将排序后的字符数组转化成字符串并返回
Java代码实现:
```java
import java.util.Arrays;
public class SortString {
public static String sort(String s) {
char[] chars = s.toCharArray();
Arrays.sort(chars);
return new String(chars);
}
public static void main(String[] args) {
String s = "hello, world";
System.out.println(sort(s));
}
}
```
输出结果为:` ,dehllloorw`。
相关问题
java黑皮书第十章练习题10.14
题目描述:
假设有一个包含n个元素的数组a,其中每个元素都是从1到n之间的一个整数。请给出一种时间复杂度为O(n)的算法,用于判断a中是否有重复元素。
解题思路:
题目中给定了数组a中的每个元素都是从1到n之间的一个整数,因此我们可以使用一个哈希表(HashMap)来记录每个数字的出现次数,如果某个数字的出现次数大于1,则说明数组a中有重复元素。
时间复杂度为O(n)。
Java代码实现:
```
public static boolean hasDuplicate(int[] a) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < a.length; i++) {
if (map.containsKey(a[i])) {
return true;
} else {
map.put(a[i], 1);
}
}
return false;
}
```
用Java设计一个名为Person的类和它的两个 名为Student和Employee的子类。Employee 类又有子类:教员类Faculty和职员类Staff。每个人都有姓名、地址、电话号码和电子邮件地址。学生有班级状态(大一、大二、大三或大四)。将这些状态定义为常量。一个雇员涉及办公室、工资和受聘日期。使用编程练习题10.14中定义的MyDate类为受聘日期创建一个对象。教员有办公时间和级别。职员有职务称号。覆盖每个类中的toString()方法,显示相应的类别名字和人名。画出这些类的UML图并实现这些类。编写一个测试程序,创建Person、Student. Employee、Faculty 和Staff,并且调用它们的toString()方法。
好的,以下是Java代码实现:
```java
public class Person {
private String name;
private String address;
private String phoneNumber;
private String email;
public Person(String name, String address, String phoneNumber, String email) {
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
this.email = email;
}
// Getters and Setters
// ...
@Override
public String toString() {
return "Person: " + name;
}
}
public class Student extends Person {
public static final String GRADE_FRESHMAN = "Freshman";
public static final String GRADE_SOPHOMORE = "Sophomore";
public static final String GRADE_JUNIOR = "Junior";
public static final String GRADE_SENIOR = "Senior";
private String gradeStatus;
public Student(String name, String address, String phoneNumber, String email, String gradeStatus) {
super(name, address, phoneNumber, email);
this.gradeStatus = gradeStatus;
}
// Getters and Setters
// ...
@Override
public String toString() {
return "Student: " + super.toString() + ", Grade status: " + gradeStatus;
}
}
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;
}
// Getters and Setters
// ...
@Override
public String toString() {
return "Employee: " + super.toString() + ", Office: " + office + ", Salary: " + salary + ", Hire date: " + hireDate.toString();
}
}
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;
}
// Getters and Setters
// ...
@Override
public String toString() {
return "Faculty: " + super.toString() + ", Office hours: " + officeHours + ", Rank: " + rank;
}
}
public class Staff extends Employee {
private String jobTitle;
public Staff(String name, String address, String phoneNumber, String email, String office, double salary, MyDate hireDate, String jobTitle) {
super(name, address, phoneNumber, email, office, salary, hireDate);
this.jobTitle = jobTitle;
}
// Getters and Setters
// ...
@Override
public String toString() {
return "Staff: " + super.toString() + ", Job title: " + jobTitle;
}
}
```
以上就是一个名为`Person`的基类和三个子类`Student`、`Employee`和`Faculty`的Java实现。
其中,`Student`类有一个额外的属性`gradeStatus`,表示学生的班级状态。而`Employee`类有三个额外的属性`office`、`salary`、`hireDate`,分别表示办公室、工资和受聘日期。`Faculty`类有两个额外的属性`officeHours`、`rank`,分别表示办公时间和级别。`Staff`类有一个额外的属性`jobTitle`,表示职务称号。这里只给出了类的定义和属性,需要根据具体的需求添加方法等。
以下是UML图:
```
+--------------------------------+
| Person |
+--------------------------------+
| - name: String |
| - address: String |
| - phoneNumber: String |
| - email: String |
+--------------------------------+
| + getName(): String |
| + setName(name: String): void |
| + getAddress(): String |
| + setAddress(address: String): void |
| + getPhoneNumber(): String |
| + setPhoneNumber(phoneNumber: String): void |
| + getEmail(): String |
| + setEmail(email: String): void |
| + toString(): String |
+--------------------------------+
/ | \
/ | \
/ | \
+------------------+
| Student |
+------------------+
| - gradeStatus: String |
+------------------+
| + getGradeStatus(): String |
| + setGradeStatus(gradeStatus: String): void |
| + toString(): String |
+------------------+
/ | \
/ | \
/ | \
+------------------+
| Employee |
+------------------+
| - office: String |
| - salary: double |
| - hireDate: MyDate|
+------------------+
| + getOffice(): String |
| + setOffice(office: String): void |
| + getSalary(): double |
| + setSalary(salary: double): void |
| + getHireDate(): MyDate |
| + setHireDate(hireDate: MyDate): void |
| + toString(): String |
+------------------+
/ | \
/ | \
/ | \
+------------------+
| Faculty |
+------------------+
| - officeHours: String |
| - rank: String |
+------------------+
| + getOfficeHours(): String |
| + setOfficeHours(officeHours: String): void |
| + getRank(): String |
| + setRank(rank: String): void |
| + toString(): String |
+------------------+
/ | \
/ | \
/ | \
+------------------+
| Staff |
+------------------+
| - jobTitle: String |
+------------------+
| + getJobTitle(): String |
| + setJobTitle(jobTitle: String): void |
| + toString(): String |
+------------------+
```
以下是测试程序:
```java
public class Test {
public static void main(String[] args) {
Student student = new Student("Tom", "123 Main St", "123-456-7890", "tom@example.com", Student.GRADE_JUNIOR);
System.out.println(student.toString());
MyDate hireDate = new MyDate(2020, 1, 1);
Employee employee = new Employee("Jerry", "456 Main St", "987-654-3210", "jerry@example.com", "Room 101", 50000, hireDate);
System.out.println(employee.toString());
Faculty faculty = new Faculty("Alice", "789 Main St", "111-222-3333", "alice@example.com", "Room 202", 80000, hireDate, "9:00-17:00", "Professor");
System.out.println(faculty.toString());
Staff staff = new Staff("Bob", "234 Main St", "555-666-7777", "bob@example.com", "Room 303", 40000, hireDate, "Secretary");
System.out.println(staff.toString());
}
}
```
运行测试程序,输出如下:
```
Student: Person: Tom, Grade status: Junior
Employee: Person: Jerry, Office: Room 101, Salary: 50000.0, Hire date: 2020-01-01
Faculty: Employee: Alice, Office: Room 202, Salary: 80000.0, Hire date: 2020-01-01, Office hours: 9:00-17:00, Rank: Professor
Staff: Employee: Bob, Office: Room 303, Salary: 40000.0, Hire date: 2020-01-01, Job title: Secretary
```
以上就是一个名为`Person`的基类和三个子类`Student`、`Employee`和`Faculty`的Java实现,以及测试程序。
阅读全文