Task01
问题
Equals方法:
定义Student类,该类包含姓名,年龄,性别三个属性,
在Student类中覆盖继承自Object类的equals方法。
实现根据Student对象属性值,判断两个Student对象是否相等的功能,并测试。
class Student {
String name;
int age;
boolean isMale;
@Override
public boolean equals(Object obj) {
// 实现equals方法
}
}
代码
package day09.Task01;
import java.util.Objects;
import java.util.Scanner;
public class Task01 {
public static void main(String[] args) {
Student student1 = new Student("张三", 18, true);
Student student2 = new Student("张三", 18, true);
Student student3 = new Student("李四", 18, true);
System.out.println(student1.equals(student2));
System.out.println(student1.equals(student3));
}
}
class Student {
String name;
int age;
boolean isMale;
@Override
public boolean equals(Object obj) {
if (obj == this){
return true;
}
if (obj == null || this.getClass() != obj.getClass()){
return false;
}
Student student = (Student)obj;
return student.name.compareTo(this.name) == 0 && student.age == this.age && student.isMale == this.isMale;
}
public Student(String name, int age, boolean isMale) {
this.name = name;
this.age = age;
this.isMale = isMale;
}
}
结果
Task02
问题
判断一个字符串是否是回文串
代码
package day09.Task02;
import java.util.Scanner;
public class Task02 {
public static void main(String[] args) {
System.out.println("请输入一个字符串");
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
int length = string.length();
boolean flag = true;
for(int i = 0; i < length/2; i++){
if (string.charAt(i) != string.charAt(length - i - 1)){
flag = false;
break;
}
}
if (flag){
System.out.println("这是一个回文串");
}else{
System.out.println("这不是一个回文串");
}
}
}
结果
Task03
问题
统计大串中小串出现的次数。
代码
package day09.Task03;
import java.util.Scanner;
public class Task03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个大串");
String string1 = scanner.nextLine();
System.out.println("请输入一个小串");
String string2 = scanner.nextLine();
int length1 = string1.length();
int length2 = string2.length();
int ans = 0;
for (int i = 0; i < length1; i++) {
if(string1.indexOf(string2,i) != -1){
ans++;
i = string1.indexOf(string2,i);
}
}
System.out.println("小串在大串中出现了" + ans +"次");
}
}
结果
附加题
题目
给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。 注意:如果有多个最长回文串,返回其中一个即可
代码
package day09.附加题;
import java.util.Scanner;
/*
也可利用动态规划+二维数组的思想解决。
*/
public class 附加题 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个字符串");
String string = scanner.nextLine();
int length = string.length();
int max = 1;
int start = 0;
int end = 0;
for (int i = 0; i < length; i++) {
for (int j = 1; j < length; j++) {
if (judgePrim(i, j, string) && j - i + 1 > max){
max = j - i + 1;
start = i;
end = j;
}
}
}
System.out.println("最长的回文子串的长度为:" + max);
System.out.println(string.substring(start, end + 1));
}
public static boolean judgePrim (int start, int end, String string){
int length = end - start + 1;
for(int i = start; i < start + length/2 ; i++){
if (string.charAt(i) != string.charAt(end - (i - start) )){
return false;
}
}
return true;
}
}