Java语言程序设计-基础篇-第八版-编程练习题-第九章

时间:2021-01-02 11:45:42
package 编程练习题chapter9;
import java.util.Scanner;
public class Exercise9_1 {
public static void main(String[] args) {
Scanner input
= new Scanner(System.in);
System.out.println(
"Enter a string for SSN: ");
String s
= input.nextLine();

if (isValidSSN(s)) {
System.out.println(
"Valid SSN");
}

else {
System.out.println(
"Invalid SSN");
}
}
public static boolean isValidSSN(String ssn) {
return ssn.length() == 11 &&
Character.isDigit(ssn.charAt(
0)) &&
Character.isDigit(ssn.charAt(
1)) &&
Character.isDigit(ssn.charAt(
2)) &&
ssn.charAt(
3) == '-' &&
Character.isDigit(ssn.charAt(
4)) &&
Character.isDigit(ssn.charAt(
5)) &&
ssn.charAt(
6) == '-' &&
Character.isDigit(ssn.charAt(
7)) &&
Character.isDigit(ssn.charAt(
8)) &&
Character.isDigit(ssn.charAt(
9)) &&
Character.isDigit(ssn.charAt(
10));
}
}
package 编程练习题chapter9;

import java.util.Scanner;

public class Exercise9_2 {
public static void main(String[] args) {
Scanner input
= new Scanner(System.in);
System.out.print(
"Enter the first string: ");
String first
= input.nextLine();

System.out.print(
"Enter the second string: ");
String second
= input.nextLine();

if (isSubstring(first, second)) {
System.out.println(first
+ " is a substring of " + second);
}
else {
System.out.println(first
+ " is not a substring of " + second);
}
}

public static boolean isSubstring(String first, String second) {
int remainingLength = second.length();
int startingIndex = 0;
toWhile:
while (first.length() <= remainingLength) {
for (int i = 0; i < first.length(); i++) {
if (first.charAt(i) != second.charAt(startingIndex + i)) {
startingIndex
++;
remainingLength
--;
continue toWhile;
}
}
return true;
}
return false;
}

}
package 编程练习题chapter9;

import java.util.Scanner;

public class Exercise9_3 {
public static void main(String[] args) {
Scanner input
= new Scanner(System.in);
System.out.print(
"Enter a string for password: ");
String s
= input.nextLine();

if (isValidPassword(s)) {
System.out.println(
"Valid password");
}
else {
System.out.println(
"Invalid passsword");
}
}

public static boolean isValidPassword(String s) {
for (int i = 0; i < s.length(); i++) {
if (!Character.isLetter(s.charAt(i))
&& !Character.isDigit(s.charAt(i)))
return false;
}
if (s.length() < 8)
return false;

int count = 0;
for (int i = 0; i < s.length(); i++) {
if (Character.isDigit(s.charAt(i)))
count
++;
}

if (count >= 2)
return true;
else
return false;
}
}
package 编程练习题chapter9;

import java.util.Scanner;

public class Exercise9_11 {
public static void main(String[] args) {
Scanner input
= new Scanner(System.in);
System.out.print(
"Enter a string: ");
String s
= input.nextLine();
System.out.println(
"Sorted string is " + sort(s));
}

public static String sort(String s) {
StringBuilder buffer
= new StringBuilder(s);
char currentMax;
int currentMaxIndex;
for (int i = buffer.length() - 1; i >= 1; i--) {
currentMax
= buffer.charAt(i);
currentMaxIndex
= i;
for (int j = 1; j <= i; j++) {
if (currentMax < buffer.charAt(j)) {
currentMax
= buffer.charAt(j);
currentMaxIndex
= j;
}
}
if (currentMaxIndex != i) {
buffer.setCharAt(currentMaxIndex, buffer.charAt(i));
buffer.setCharAt(i, currentMax);
}
}
return buffer.toString();
}
}