当前位置:网站首页>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 .
边栏推荐
- [the Nine Yang Manual] 2018 Fudan University Applied Statistics real problem + analysis
- 实验七 常用类的使用
- 渗透测试学习与实战阶段分析
- UGUI—Text
- 7-5 走楼梯升级版(PTA程序设计)
- [the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis
- 实验七 常用类的使用(修正帖)
- . Net6: develop modern 3D industrial software based on WPF (2)
- C language Getting Started Guide
- 优先队列PriorityQueue (大根堆/小根堆/TopK问题)
猜你喜欢

HackMyvm靶机系列(6)-videoclub

Programme de jeu de cartes - confrontation homme - machine

甲、乙机之间采用方式 1 双向串行通信,具体要求如下: (1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制 乙机的 LED1

Mixlab unbounded community white paper officially released

canvas基础2 - arc - 画弧线
![[面试时]——我如何讲清楚TCP实现可靠传输的机制](/img/d6/109042b77de2f3cfbf866b24e89a45.png)
[面试时]——我如何讲清楚TCP实现可靠传输的机制

Mortal immortal cultivation pointer-1

HackMyvm靶机系列(5)-warez

PriorityQueue (large root heap / small root heap /topk problem)

1. First knowledge of C language (1)
随机推荐
[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
Differences among fianl, finally, and finalize
7-9 制作门牌号3.0(PTA程序设计)
[graduation season · advanced technology Er] goodbye, my student days
Using spacedesk to realize any device in the LAN as a computer expansion screen
MySQL中count(*)的实现方式
Miscellaneous talk on May 27
7-5 走楼梯升级版(PTA程序设计)
Matlab opens M file garbled solution
SRC mining ideas and methods
Mixlab unbounded community white paper officially released
The difference between cookies and sessions
Canvas foundation 1 - draw a straight line (easy to understand)
5月14日杂谈
仿牛客技术博客项目常见问题及解答(一)
This time, thoroughly understand the MySQL index
Custom RPC project - frequently asked questions and explanations (Registration Center)
The difference between overloading and rewriting
实验五 类和对象
强化学习系列(一):基本原理和概念