当前位置:网站首页>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 .
边栏推荐
- 仿牛客技术博客项目常见问题及解答(一)
- fianl、finally、finalize三者的区别
- Mortal immortal cultivation pointer-1
- [the Nine Yang Manual] 2020 Fudan University Applied Statistics real problem + analysis
- 透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
- Canvas foundation 2 - arc - draw arc
- SRC挖掘思路及方法
- ABA问题遇到过吗,详细说以下,如何避免ABA问题
- Inaki Ading
- [during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
猜你喜欢
QT meta object qmetaobject indexofslot and other functions to obtain class methods attention
甲、乙机之间采用方式 1 双向串行通信,具体要求如下: (1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制 乙机的 LED1
强化學習基礎記錄
MATLAB打开.m文件乱码解决办法
2. First knowledge of C language (2)
Strengthen basic learning records
Strengthen basic learning records
. Net6: develop modern 3D industrial software based on WPF (2)
[hand tearing code] single case mode and producer / consumer mode
Canvas foundation 1 - draw a straight line (easy to understand)
随机推荐
FAQs and answers to the imitation Niuke technology blog project (I)
强化學習基礎記錄
hashCode()与equals()之间的关系
稻 城 亚 丁
强化学习基础记录
【黑马早报】上海市监局回应钟薛高烧不化;麦趣尔承认两批次纯牛奶不合格;微信内测一个手机可注册俩号;度小满回应存款变理财产品...
7-8 7104 约瑟夫问题(PTA程序设计)
撲克牌遊戲程序——人機對抗
Caching mechanism of leveldb
Leetcode. 3. Longest substring without repeated characters - more than 100% solution
Relationship between hashcode() and equals()
A piece of music composed by buzzer (Chengdu)
[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP
优先队列PriorityQueue (大根堆/小根堆/TopK问题)
简单理解ES6的Promise
Write a program to simulate the traffic lights in real life.
Canvas foundation 1 - draw a straight line (easy to understand)
[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
js判断对象是否是数组的几种方式
MySQL lock summary (comprehensive and concise + graphic explanation)