当前位置:网站首页>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 .
边栏推荐
- (original) make an electronic clock with LCD1602 display to display the current time on the LCD. The display format is "hour: minute: Second: second". There are four function keys K1 ~ K4, and the fun
- 实验八 异常处理
- ABA问题遇到过吗,详细说以下,如何避免ABA问题
- This time, thoroughly understand the MySQL index
- Safe driving skills on ice and snow roads
- 1. Preliminary exercises of C language (1)
- 自定义RPC项目——常见问题及详解(注册中心)
- 稻 城 亚 丁
- Mixlab unbounded community white paper officially released
- TypeScript快速入门
猜你喜欢
canvas基础2 - arc - 画弧线
Programme de jeu de cartes - confrontation homme - machine
透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
2022泰迪杯数据挖掘挑战赛C题思路及赛后总结
Poker game program - man machine confrontation
仿牛客技术博客项目常见问题及解答(三)
[面试时]——我如何讲清楚TCP实现可靠传输的机制
FAQs and answers to the imitation Niuke technology blog project (II)
MySQL事务及实现原理全面总结,再也不用担心面试
About the parental delegation mechanism and the process of class loading
随机推荐
强化學習基礎記錄
Miscellaneous talk on May 27
7-14 错误票据(PTA程序设计)
【MySQL-表结构与完整性约束的修改(ALTER)】
Nuxtjs quick start (nuxt2)
QT meta object qmetaobject indexofslot and other functions to obtain class methods attention
Mixlab unbounded community white paper officially released
This time, thoroughly understand the MySQL index
7-8 7104 约瑟夫问题(PTA程序设计)
[the Nine Yang Manual] 2019 Fudan University Applied Statistics real problem + analysis
7-15 h0161. 求最大公约数和最小公倍数(PTA程序设计)
PriorityQueue (large root heap / small root heap /topk problem)
渗透测试学习与实战阶段分析
Thoroughly understand LRU algorithm - explain 146 questions in detail and eliminate LRU cache in redis
[experiment index of educator database]
Poker game program - man machine confrontation
[graduation season · advanced technology Er] goodbye, my student days
Detailed explanation of redis' distributed lock principle
C language Getting Started Guide
【数据库 三大范式】一看就懂