Experiment 3: Abstract classes, interfaces and strings


Purpose

1) Familiar with the definition and use of interfaces
2) Learn the methods of string processing

Experiment content

1)
Write a program: define a door abstract class and define an interface alarm. Customize the methods in the abstract class and door; define a subclass of door and implement the alarm interface. Instantiate this subclass and call its method (just output the relevant information in the method).


2)
Separate a character string "I love java program" into four character strings "I", "love", "java", and "program". (Use StringTokenizer class);


3)
Given string array Strings[]={"string","starting","strong","street","stir","studeng","soft","sting"}; write an application (using Methods provided by the String class):

  1. Count how many strings start with st;
  2. Count how many strings end with ng;
  3. Give the position of the first occurrence of the character'r' in the string, respectively.

Experimental analysis, code and running results

1)

public class Work1 {
    
    
	public static void main(String[] args) {
    
    
		HomeDoor h1 = new HomeDoor();
		h1.close();
		h1.open();
		h1.ring();
	}
}

abstract class door{
    
    
	public abstract void open(); 
	public abstract void close();
}

interface alarm{
    
    
	void ring();
}

class HomeDoor extends door implements alarm{
    
    

	@Override
	public void ring() {
    
    
		// TODO Auto-generated method stub
		System.out.println("ding lin lin....");
	}

	@Override
	public void open() {
    
    
		// TODO Auto-generated method stub
		System.out.println("open door");
	}

	@Override
	public void close() {
    
    
		// TODO Auto-generated method stub
		System.out.println("close door");
	}
}

operation result:

Insert picture description here

Analysis: The
subclass must implement all the abstract methods in the abstract parent class (unless the subclass is also an abstract class), and must implement all the methods in the interface.

2)

import java.util.StringTokenizer;

public class Work2 {
    
    
	public static void main(String[] args) {
    
    
		StringTokenizer str = new StringTokenizer("I love java program");
		
		while(str.hasMoreElements()) {
    
    
			System.out.println(str.nextToken());
		}
	}
}

operation result:

Insert picture description here

Analysis:
When the construction method in the StringTokenizer class does not pass the second parameter: the default is to use space, line feed, and carriage return as the dividing symbol

public StringTokenizer(String str) {
    
    
        this(str, " \t\n\r\f", false);
}
public boolean hasMoreTokens(): 判断分割的字符串的集合是否还有下一个
public String nextToken(): 返回分割的下一个字符串

3)

public class Work3 {
    
    
	public static void main(String[] args) {
    
    
	int st = 0;
	int ng = 0;
	
	String Strings[]={
    
    "string","starting","strong","street","stir","studeng","soft","sting"};
	
	for(String s:Strings) {
    
    
		String r;
		int a = s.indexOf('r');
		if(s.startsWith("st")) {
    
    
			st++;
		}
		if(s.endsWith("ng")) {
    
    
			ng++;
		}
		System.out.println("***********************");
		if(a==-1) {
    
    
			System.out.println(s+"字符串中无r");
		}else {
    
    
			System.out.println(s+"字符串中r第一次出现的位置为: " + (a + 1));
		}
	}
	System.out.println("============================");
	System.out.println("以st开头的字符串有"+ st + "个");
	System.out.println("以ng结尾的字符串有"+ ng + "个");
	
	}
}

operation result:

Insert picture description here

Analysis: The
indexOf() method returns the index of the first occurrence of a character in a string, and the
stratsWith() method determines whether a string starts with a specified character or a string. The
endsWith() method determines whether a string starts with a specified character or End of string

to sum up:

Abstract class:

1) In addition to the inability of an abstract class to instantiate objects, other functions of the class still exist. The access methods of member variables, member methods and construction methods are the same as those of ordinary classes.
2) Since abstract classes cannot instantiate objects, abstract classes must be inherited before they can be used.
3) The subclass must implement all abstract methods in the abstract parent class (unless the subclass is also an abstract class)

interface:

Unless the class implementing the interface is an abstract class, the class must define all methods in the interface.
Interfaces cannot be used to instantiate objects.
The interface has no constructor.
All methods in the interface must be abstract methods.
Interfaces cannot contain member variables, except for static and final variables.
The interface is not inherited by the class, but must be implemented by the class.
The interface supports multiple inheritance.

StringTokenizer:

StringTokenizer is an application class used to separate Strings, which is equivalent to the split function.

1. The constructor
public StringTokenizer(String str)
public StringTokenizer(String str, String delim)
public StringTokenizer(String str, String delim, boolean returnDelims) The
first parameter is the String to be separated, and the second is the set of delimiting characters. The three parameters indicate whether the delimiter is returned as a mark. If the delimiter is not specified, the default is: "\t\n\r\f"

2. Commonly used methods
public boolean hasMoreTokens(): Determine whether the set of divided strings has the next
public String nextToken(): Return the next divided string

Third question:

  • indexOf(): Returns the index of the first occurrence of a character in the string
  • startsWith(): Determine whether a string starts with a specified character or string
  • endsWith(): Determine whether a string ends with a specified character or string

Guess you like

Origin blog.csdn.net/qq_46456049/article/details/108976587