7-4 Count the letters in a string (10分)
(Count the letters in a string) Write a method that counts the number of letters in a string using the following header: public static int countLetters(String s)
Write a test program that prompts the user to enter a string and displays the number of letters in the string.
input style :
Enter a string
output style:
Displays the number of the letters in the string.
input sample:
qwe&&&,,;#@23qer
output sample:
The number of letters inside the string is: 6
import java.util.*;publicclassMain{publicstaticvoidmain(String[] args){// TODO Auto-generated method stubint counter=0;//定义变量counter
Scanner in =newScanner(System.in);
String word= in.next();//定义字符串//将字符串转换为小写字母后,再转换为字符数组char[] charArry=word.toLowerCase().toCharArray();for(int i=0;i<charArry.length;i++){//判断字符是否为a~zif(charArry[i]>='a'&&charArry[i]<='z'){
counter++;}}
System.out.printf("The number of letters inside the string is: %d",counter);}}
7-5 Remove duplicates (10分)
(Remove duplicates) Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList list) Write a test program that prompts the user to enter n integers to a list ,After sort the distinct intergers and displays the distinct integers separated by exactly one space.
input style :
Input the number n for the length of the array in the first line, and the next line input n integers for the array..
output style:
Displays the distinct integers separated by exactly one space
input sample:
5
32 43 32 22 22
output sample:
32 43 22
package LearningTest;import java.util.*;publicclassMain{publicstatic Integer[]threeClear(int[] arr){returngetIntegers(arr);//}static Integer[]getIntegers(int[] arr){
List list =newArrayList();for(int i=0;i<arr.length;i++){if(!list.contains(arr[i])){
list.add(arr[i]);}}return(Integer[]) list.toArray(newInteger[arr.length]);}publicstatic Integer[]toIntegerArray(int[] arr){int n=arr.length;
Integer[] iarr=newInteger[n];for(int i=0;i<n;i++){
arr[i]=newInteger(arr[i]);}return iarr;}publicstaticvoidmain(String[] args){
Main arr =newMain();
Scanner in =newScanner(System.in);int length = in.nextInt();int[] num =newint[length];for(int i =0; i < num.length; i++){
num[i]= in.nextInt();}
Integer[] total =newInteger[length];
total =threeClear(num);for(int i =0; i<total.length ; i++){if(total[i]!= null){
System.out.print(total[i]+" ");}}
in.close();}}
7-6 Combine two lists then sorted it (10分)
(Combine two lists then sorted it.)
Write a method that returns the union of two array lists of integers using the following header:
public static ArrayList<Integer> union( ArrayList<Integer> list1, ArrayList<Integer> list2)
For example, the union of two array lists {2,3,1,5} and {3,4,6} is {2, 3, 1, 5, 3, 4, 6}.
Write a test program that prompts the user to enter two lists, then sorted the union list, finally displays their sorted union. The numbers are separated by exactly one space in the output.
Write a test program that prompts the user to enter the two number m,n for the length of two arrays in the first line, and the next two line input m integer and n intergers for the two array. After sort theunion intergers and displays the sorted union list separated by exactly one space.
input style :
Input the two number m,n for the length of two arrays in the first line, and the next two line input m integer and n intergers .
output style:
Displays the sorted union list separated by exactly one space..
input sample:
3 4
23 44 32
12 43 32 44
output sample:
12 23 32 32 43 44 44
7-7 Geometry: intersecting point (II) (10分)
(Geometry: intersecting point) Suppose two line segments intersect. The two end-points for the first line segment are (x1, y1) and (x2, y2) and for the second line segment are (x3, y3) and (x4, y4).Write a program that prompts the user to enter these four endpoints and displays the intersecting point. the intersecting point can be found by solving a linear equation. Write the LinearEquation class in Programming to solve this equation. You need to keep the last three decimal places. (你需要保留小数点后三位)
input style :
Enter eight double number
output style:
Displays the intersecting point or The two lines are parallel .
input sample:
2.3 4.5 6.7 -3.4 4 5.6 6.7 9.1
output sample:
The intersecting point is at (2.657,3.859)
1 2 3 4 5 6 7 8
output sample:
The two lines are parallel