2021-10-25 pta set reflection annotation and common shortcut keys for Eclipse

Keywords: Java Eclipse pta

catalogue

Eclipse common shortcuts

Completion

6-6 jmu-Java-05 deletion of specified elements in set List (15 points)

Referee test procedure:

answer:

7-2 use of list (15 points)

Input format:

Output format:

Input example:

Output example:

answer:  

Eclipse common shortcuts

/*
 * Eclipse common shortcuts
 * 1. Complete code declaration: alt +/
 * 2. Quick repair: ctrl + 1
 * 3. Batch package Guide: ctrl + shift + o
 * 4. Single line note: ctrl +/
 * 5. Multiline comment: ctrl + shift +/
 * 6. Cancel multiline comment: ctrl + shift +\
 * 7. Copy the specified line: ctrl + alt + ↓ or ↑
 * 8. Delete the specified line: ctrl + d
 * 9. Move up and down code: alt + ↑ or ↓
 * 10. Switch to the next line of code space: shift + enter
 * 11. Cut flowers to the code space on the previous line: ctrl + shift + enter
 * 12. Check the source code: ctrl + shift + T or ctrl + target
 * 13. Return to the previous edit page: alt + ←
 * 14. Go to the next edit page: alt + →
 * 15. View the inheritance tree structure of the specified class: ctrl + T
 * 16. Copy code: ctrl + c
 * 17. Undo: ctrl + z
 * 18. Redo: ctrl + y
 * 19. Cut: ctrl + x
 * 20. Paste: ctrl + v
 * 21. Save: ctrl + s
 * 22. Select all: ctrl + a
 * 23. Format code: ctrl + shift + f
 * 24. Select several rows and move them back as a whole: tab
 * 25. Select several rows and move forward as a whole: shift + tab
 * 26. Share the real class structure in the current class and search the specified attributes and methods: ctrl + o
 * 27. Batch modify the specified variable name, method name and class name: alt + shift + r
 * 28. Convert to capital: ctrl + shift + x
 * 29. Convert to lowercase: ctrl + shift + y
 * 30. Automatically generate getter/setter / constructor: alt + shift + s
 * 31. Display the attribute of the currently selected resource: alt + enter
 * 32. Quick search (quickly locate the next one by referring to the selected character): ctrl + k
 * 33. Close the current window: ctrl + w
 * 34. Close all windows: ctrl + shift + w
 * 35. Check where the specified structure has been used: ctrl + alt + g
 * 36. Find and replace: ctrl + f
 * 37. Maximize the current view: ctrl + m
 * 38. Go directly to the first place in the current line: home
 * 39. Directly locate to the end of the current line: end
 */

Completion

4-1 customize an annotation annotation test, and set the default value of the parameter to an empty string.

@Target({TYPE, METHOD})
@Retention(RetentionPolicy.RUNTIME )
public AnnotationTest{
    String value() default "";
}

4-2 use reflection to assign value to the name attribute of the object

class People{ 
    private String name; 
    private int age; 
    public String toString(){ 
        return "name is" + name +",age is" + age; 
    } 
} 

import java.lang.reflect.*; 
public class Main { 
    public static void main(String[] args) { 
        People p = new People(); 
        Class cla = p.getClass(); //
        Field nameField = cla.getDeclaredField("name");  //
        nameField.setAccessible(true);         
        nameField.set(stu,"Tom") ;//Set the name property of the object to Tom 
    } 
} 

5-3 the following program uses the generic mechanism to create an array list object and add three elements to it. Use the iterator to traverse the array list. Please complete the program.

import java.util.*; 
public class Main{ 
	public static void main(String[] args) { 
		List<String> al=new ArrayList<String>(); 
		al.add("red"); 
		al.add("yellow"); 
		al.add("blue");
		ListIterator<String> listIter = al.listIterator(); 
		while(listIter.hasNext())
			System.out.print(listIter.next()+"  ");
	}
}

output

red  yellow  blue 

6-6 jmu-Java-05 deletion of specified elements in set List (15 points)

Write the following two functions

/*Take the space (single or multiple) as the separator to extract the elements in line and put them into a List*/
public static List<String> convertStringToList(String line) 
/*Remove the same elements as str contents from the list*/
public static void remove(List<String> list, String str)

Referee test procedure:

public class Main {

    /*covnertStringToList function code*/   

    /*remove function code*/

     public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
            List<String> list = convertStringToList(sc.nextLine());
            System.out.println(list);
            String word = sc.nextLine();
            remove(list,word);
            System.out.println(list);
        }
        sc.close();
    }

}

Example description: four groups of test data are shown below.

###Input sample

1 2 1 2 1 1 1 2
1
11 1 11 1 11
11
2 2 2 
1
1   2 3 4 1 3 1
1

###Output sample

[1, 2, 1, 2, 1, 1, 1, 2]
[2, 2, 2]
[11, 1, 11, 1, 11]
[1, 1]
[2, 2, 2]
[2, 2, 2]
[1, 2, 3, 4, 1, 3, 1]
[2, 3, 4, 3]

answer:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {
	/*Take the space (single or multiple) as the separator to extract the elements in line and put them into a List*/
	public static List<String> convertStringToList(String line) {
		//Add spaces
		String[] ss = line.split("\\s+");  String of split Method supports regular expressions
		//Arrays:asList(T... a) returns a fixed size list supported by a specified array.
        //ArrayList(int initialCapacity) constructs an empty list with the specified initial capacity. 
		List<String> st = new ArrayList<String>(Arrays.asList(ss)); Convert array to list
		return st;
	}
	
	/*Remove the same elements as str contents from the list*/
	public static void remove(List<String> list, String str) {
		//List:boolean contains(Object o) returns true if the list contains the specified element
		if(!list.contains(str)) return;
		else {
			//List:int size() returns the number of elements in this list. 
			for(int i=0;i<list.size();i++) {
				//How to put a String array into a List? You can first convert a String array into a List
				//String: Boolean contains (charsequences) returns true if and only if the string contains the specified sequence of char values. 
				if(str.contains(list.get(i))) {
					list.remove(list.get(i));
					i--; This is the difficulty. You must add this sentence if you delete it, otherwise you may jump to delete it
				}			
			}
		}

	}
	
     public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
            List<String> list = convertStringToList(sc.nextLine());
            System.out.println(list);
            String word = sc.nextLine();
            remove(list,word);
            System.out.println(list);
        }
        sc.close();
    }
}

Take a good look at the notes.  

In eclipse, quickly type System.out.println(); Shortcut keys for:

         First enter syso, then press Alt + /, you can quickly enter.

7-2 use of list (15 points)

Use the exercise list.

  • Define the Person class
  1. Define the private properties String name,int age, and use Eclipse to generate each property setter and getter, with parameter Person(String name,int age), no parameter construction method, and toString method.
  • Define the main class in the main method
  1. Define list = new arraylist();
  2. Assign a value to the variable n with the keyboard
  3. Generate n Person objects and add them to the list. The name and age of the Person are given through the keyboard
  4. Loop the list and output the information of all Person objects in the list (call the toString method)
  5. Enter a string to represent the name. Judge whether the Person object represented by the string exists in the List. If it exists, output the Person. Otherwise, output that the Person does not exist.

Input format:

First, enter n in one line to represent the number of objects, then enter the name and age of a Person object in each line, and enter a Person's name in one line to query it

Output format:

For each object, the information of the object is output in one line. For the queried person, the information of this person can be found and output, otherwise the output person does not exist.

Input example:

A set of inputs is given here. For example:

3
zhang 23
li 44
wang 33
li
3
zhang 23
li 44
wang 33
may

Output example:

The corresponding output is given here. For example:

Person [name=zhang, age=23]
Person [name=li, age=44]
Person [name=wang, age=33]
Person [name=li, age=44]
Person [name=zhang, age=23]
Person [name=li, age=44]
Person [name=wang, age=33]
This person does not exist

answer:  

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		List<Person> list = new ArrayList<Person>();
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		for(int i=0;i<n;i++) {
			Person p=new Person(sc.next(),sc.nextInt());
			list.add(p);
			System.out.println(p.toString());
		}
		
		String na = sc.next();
		int flag = 0;
		for(int i=0;i<n;i++) {
			//
			if(list.get(i).getName().equals(na)) {
				System.out.println(list.get(i).toString());
				flag = 1;
			}
		}
		if(flag == 0)
			System.out.println("This person does not exist");
	}
}

class Person {
	private String name;
	private int age;

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int s1) {
		this.age = s1;
	}
	public Person() {
		
	}
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "Person [name=" +name+", age="+age+"]";
	}
	
}

Posted by Miri4413 on Mon, 25 Oct 2021 21:29:16 -0700