当前位置:网站首页>Experiment 7 use of common classes

Experiment 7 use of common classes

2022-07-06 13:55:00 Wen Wen likes Guo Zia

Experiment seven    Use of common classes

The experiment purpose

1.  Understand the concept of class library and API How to use .

2. Master the usage of common classes .

Experimental hours  2 Class hours

Experimental content

1. Enter two strings str1、str2, Statistics string str2 Appear in the str1 Number of times in .

Such as :str1=”aaas lkaaas” ,str2=” as” , Then output  2

Tips : Text input

import java.util.Scanner;

public class test {

public static void main(String[] args) {

 Scanner sc=new Scanner(System.in); // Define the object of scanning keyboard input

    String s = sc.nextLine();     // Read a line of text from the keyboard

...

sc.close();

}

package code71;
import java.util.Scanner;
public class code71 {
	public static void main(String[] args) {
	     Scanner in=new Scanner(System.in);
	     int sum=0;
		System.out.print(" Please enter the first string :");
       String str1=in.nextLine();  // Enter the first string 
      System.out.print(" Please enter the second string :");
        String str2=in.nextLine();  // Enter the second string 
        int a=str1.indexOf(str2);  // Search for str2 stay str1 Where in 
      while(a!=-1) {
    	  sum++;
    	  a=str1.indexOf(str2,a+1);  // Count the times 
      }
      System.out.println(" character string str2 Appear in the str1 The number of times in is :"+sum+" Time !");
	}
 
}

2. Enter a string of characters from the keyboard , How many different characters are output 、 The number of occurrences of each character .

package code72;
import java.util.Scanner;
public class code72 {
	public static void main(String[] args) {
    Scanner in=new Scanner(System.in);
    String str=in.nextLine();
    StringBuffer b=new StringBuffer(str);  // Insert string buffer StringBuffer
    int length=b.length();  // The total number of characters 
    System.out.println(" All in all "+length+" Letters !");
    String a;  // Declare a single character 
    int index,sum;
 for(int i=0;i<b.length();) {
	sum=0;
   a=b.substring(0,1);  // Intercept a single character 
    index=b.indexOf(a);  // Search for the position of a single character in the whole string 
	 while(index!=-1) {
		 sum++;
		 b.deleteCharAt(index);  // Delete character 
		 index=b.indexOf(a,index);  // Count the times 
	 } 
	 System.out.println(a+" The letters have "+sum+" individual !");
 }
	}
 
}

3. Write a Java Program , Capitalize the first letter of each word in the sentence entered by the user .

package code73;
import java.util.Scanner;
public class code73 {

	public static void main(String[] args) {
		// TODO  Automatically generated method stubs 
		System.out.println(" Please enter a sentence :");
		Scanner in = new Scanner(System.in);
		String str = in.nextLine();  // Enter the original string 
		String str1 = "";  // The last output string 
		String[] a=str.split(" ");  // Divide the string into spaces 
		for(int i=0;i<a.length;i++){  // Traversal string , Replace the first letter of each word in each string array with uppercase 
			a[i]=a[i].replace(a[i].charAt(0),a[i].toUpperCase().charAt(0));
            str1+=a[i]+" ";  // Splice the replaced string into an empty string 
		}
		System.out.println(" The changed sentence is :"+str1.trim());

	}

}

4. Enter an octal digit string ( Within the range of integer data ), Respectively by 2 Base number 、10 Base number 、16 Binary output .

Tips :Integer.parseInt("100",8); // take 8 Base number 100 Convert to decimal number

          Integer.toBinaryString(100); // Decimal integer 100 Convert to binary

package code74;
import java.util.Scanner;
public class code74 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println(" Enter an octal digit string ( Within the range of integer data ):");
String a=in.nextLine();  // Enter the original string 
int b=Integer.parseInt(a);  // Convert the original string to a number 
System.out.println("2 Into the system for :"+Integer.toBinaryString(b));  // take b Convert to 2 Hexadecimal number 
System.out.println("8 Into the system for :"+Integer.toOctalString(b));  // take b Convert to 8 Hexadecimal number 
System.out.println("16 Into the system for :"+Integer.toHexString(b));  // take b Convert to 16 Hexadecimal number 
     }

5. Write a program that can generate a random letter .

package code75;
import java.util.Arrays;  // Import Arrays
public class code75 {
	public static void main(String[] args) {
		// TODO  Automatically generated method stubs 
		char a[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
				'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
		  // Build a system that contains 26 An array of upper and lower case letters 
		char ch[]=new char[1]; {  // A letter 
		for(int i=0;i<1;i++)
		{
		int b;
		b=(int)(Math.random()*(a.length));  // Generate a random number 
		ch[i]=a[b];  // Find the corresponding letter according to the position 
		}
		System.out.println(Arrays.toString(ch));  // Convert numeric type to string 
		}
	}
}

6. Programming requirements 100 Days later is the day of the month .

package code76;
import java.time.LocalDate;  // Import LocalDate
public class code76 {
	public static void main(String[] args) {
		// TODO  Automatically generated method stubs 
		String[] weekday = {" Monday "," Tuesday "," Wednesday "," Thursday "," Friday "," Saturday "," Sunday "};  // Create week array 
		LocalDate today = LocalDate.now();  // Get the current time 
		System.out.println(" It's today :" + today + "," + weekday[today.getDayOfWeek().getValue()-1]);  // Output today's format time 
		LocalDate hundredDayLater = LocalDate.now().plusDays(100);  // obtain 100 Days later 
		System.out.println("100 The day after tomorrow is :" + hundredDayLater + "," + weekday[hundredDayLater.getDayOfWeek().getValue()-1]);  // Output 100 Format time in days 
	}

}

Summary of experiments

  1. Java There are many predefined classes in the system , It is divided into different packages according to its functions , All packages are collectively called class libraries ;
  2. Search element specifies the position of a character or string in another string , It can be used indexOf() Method ;
  3. Use the method when intercepting a substring in a string substring();
  4. StringBuffer Class represents a string object with variable content , Contains a buffer , It is mainly used to complete the dynamic addition of strings 、 Insert and replace ;
  5. Conversion between numbers and strings , The main use of parseInt() Methods and toString() Method ;
  6. toBinaryString()、toHexString()、toOctalString() Method , You can separate a value in binary 、 Hexadecimal and octal forms are converted into strings ;
  7. Math Class Random() Method can be used to obtain random numbers .

原网站

版权声明
本文为[Wen Wen likes Guo Zia]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060917072845.html