当前位置:网站首页>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
- 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 ;
- Search element specifies the position of a character or string in another string , It can be used indexOf() Method ;
- Use the method when intercepting a substring in a string substring();
- 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 ;
- Conversion between numbers and strings , The main use of parseInt() Methods and toString() Method ;
- toBinaryString()、toHexString()、toOctalString() Method , You can separate a value in binary 、 Hexadecimal and octal forms are converted into strings ;
- Math Class Random() Method can be used to obtain random numbers .
边栏推荐
- Package bedding of components
- Matlab opens M file garbled solution
- [dark horse morning post] Shanghai Municipal Bureau of supervision responded that Zhong Xue had a high fever and did not melt; Michael admitted that two batches of pure milk were unqualified; Wechat i
- The latest tank battle 2022 - full development notes-3
- [insert, modify and delete data in the headsong educator data table]
- Why use redis
- Programme de jeu de cartes - confrontation homme - machine
- 【头歌educoder数据表中数据的插入、修改和删除】
- MySQL锁总结(全面简洁 + 图文详解)
- 杂谈0516
猜你喜欢
Callback function ----------- callback
2022 Teddy cup data mining challenge question C idea and post game summary
Relationship between hashcode() and equals()
强化学习基础记录
强化學習基礎記錄
实验六 继承和多态
QT meta object qmetaobject indexofslot and other functions to obtain class methods attention
Using spacedesk to realize any device in the LAN as a computer expansion screen
C language Getting Started Guide
Matlab opens M file garbled solution
随机推荐
Strengthen basic learning records
canvas基础1 - 画直线(通俗易懂)
7-9 制作门牌号3.0(PTA程序设计)
仿牛客技术博客项目常见问题及解答(一)
优先队列PriorityQueue (大根堆/小根堆/TopK问题)
[the Nine Yang Manual] 2020 Fudan University Applied Statistics real problem + analysis
[面试时]——我如何讲清楚TCP实现可靠传输的机制
ArrayList的自动扩容机制实现原理
Miscellaneous talk on May 14
Inaki Ading
Leetcode. 3. Longest substring without repeated characters - more than 100% solution
记一次猫舍由外到内的渗透撞库操作提取-flag
FAQs and answers to the imitation Niuke technology blog project (II)
【Numpy和Pytorch的数据处理】
5月27日杂谈
Why use redis
A piece of music composed by buzzer (Chengdu)
【数据库 三大范式】一看就懂
1. Preliminary exercises of C language (1)
ABA问题遇到过吗,详细说以下,如何避免ABA问题