1. Teachers
Design a Teacher class Teacher, which requires:
Attributes include number (int no), name (String name), age (int age), and College (string Seminar). Set corresponding get and set methods for these attributes.
Rewrite the equals method for the Teacher class. It is required to return true when the no of two Teacher objects are the same.
Override the toString method of Teacher class, which can return a string in the form of "No:, name:, age:, seminar:".
Enter Description:
Number, name, age, College of two teacher objects
Output Description:
Teacher's information: are two teachers equal
Input example:
A set of inputs is given here. For example:
1 Linda 38 SoftwareEngineering
2 Mindy 27 ComputerScience
Output example:
The corresponding output is given here. For example:no: 1, name:Linda, age: 38, seminary: SoftwareEngineering
no: 2, name:Mindy, age: 27, seminary: ComputerScience
false
Own code:
import java.util.Scanner; class Teacher{ int no; String name; int age; String seminary; Teacher(int no,String name,int age,String seminary){ this.no=no; this.name=name; this.age=age; this.seminary=seminary; } public String toString() { return "no: "+no+", name:"+name+", age: "+age+", seminary: "+seminary; } public boolean equals(Teacher an) { if(no==an.no)return true; else return false; } } public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); Teacher t1=new Teacher(in.nextInt(),in.next(),in.nextInt(),in.next()); Teacher t2=new Teacher(in.nextInt(),in.next(),in.nextInt(),in.next()); System.out.println(t1); System.out.println(t2); System.out.println(t1.equals(t2)); } }
2. Teachers - 2
Revise the previous question
- Modify the Teacher class so that the array formed by multiple Teacher objects can be sorted (the number is sorted from low to high), and use the Arrays.sort(Object[] a) method in the main function
- Define a class TeacherManagement, including:
Teacher array, providing the method add(Teacher []) so that it can add teachers
Overloaded method search is provided. The method can return string information of teachers equal to the specified name or age according to the name or age in a given group of teachers. The information format is: "No:, name:, age:, seminar:". "no such teacher" is returned if there are no teachers who meet the conditions
Enter Description:
Number of teachers teacher information name of teacher to be found age of teacher to be found
Output Description:
Sorted information teacher information by name teacher information by age
Input example:
A set of inputs is given here. For example:
4
3 Linda 38 SoftwareEngineering
1 Mindy 27 ComputerScience
4 Cindy 28 SoftwareEngineering
2 Melody 27 ComputerScience
Cindy
27
Output example:
The corresponding output is given here. For example:no: 1, name: Mindy, age: 27, seminary: ComputerScience
no: 2, name: Melody, age: 27, seminary: ComputerScience
no: 3, name: Linda, age: 38, seminary: SoftwareEngineering
no: 4, name: Cindy, age: 28, seminary: SoftwareEngineering
search by name:
no: 4, name: Cindy, age: 28, seminary: SoftwareEngineering
search by age:
no: 1, name: Mindy, age: 27, seminary: ComputerScience
no: 2, name: Melody, age: 27, seminary: ComputerScience
Own code:
import java.util.Arrays; import java.util.Scanner; class Teacher implements Comparable<Teacher>{ int no; String name; int age; String seminary; Teacher(int no,String name,int age,String seminary){ this.no=no; this.name=name; this.age=age; this.seminary=seminary; } public String toString() { return "no: "+no+", name: "+name+", age: "+age+", seminary: "+seminary; } @Override public int compareTo(Teacher o) { if(no>o.no)return 1; else if(no==o.no)return 0; else return -1; } } class TeacherManagement { Teacher[] teacher; int len; TeacherManagement(Teacher[] teacher,int len){ this.teacher=teacher; this.len=len; } void add(Teacher t) { teacher[len]=t; len++; } void search(String n) { int flag=0; for(int i=0;i<len;i++) { if(n.equals(teacher[i].name)) { System.out.println(teacher[i].toString()); flag=1; } } if(flag==0)System.out.println("no such teacher"); } void search(int age) { int flag=0; for(int i=0;i<len;i++) { if(age==teacher[i].age) { System.out.println(teacher[i].toString()); flag=1; } } if(flag==0)System.out.println("no such teacher"); } } public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int time=in.nextInt(); Teacher[] teacher=new Teacher[time]; for(int i=0;i<time;i++) teacher[i]=new Teacher(in.nextInt(),in.next(),in.nextInt(),in.next()); Arrays.sort(teacher); for(int i=0;i<time;i++) System.out.println(teacher[i]); TeacherManagement test=new TeacherManagement(teacher,time); System.out.println("search by name:"); test.search(in.next()); System.out.println("search by age:"); test.search(in.nextInt()); } }
3. Image string
A mirrored string is a string in which two character sequences are completely opposite. Input two strings without spaces from the keyboard to judge whether the second string is the mirror string of the first. If yes, output yes, otherwise output no
Enter Description:
Two strings separated by a space entered on the keyboard
Output Description:
yes(no)
Input example:
A set of inputs is given here. For example:
abc cba
Output example:
The corresponding output is given here. For example:yes
Own code:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); StringBuffer str1=new StringBuffer(in.next()); StringBuffer str2=new StringBuffer(in.next()); boolean t=(str1.reverse().toString()).equals(str2.toString()); if(t)System.out.println("yes"); else System.out.println("no"); } }
4. The position of words in sentences
Given an English sentence, write the method void wordPositions(String sentence), in which the starting position and word length of each word in the sentence are calculated and output.
Suppose that the sentence contains only English letters and spaces, and the words are not repeated.
Enter Description:
sentence
Output Description:
The starting position and word length of each word in the sentence
Input example:
A set of inputs is given here. For example:
Why are you so crazy about java
Output example:
The corresponding output is given here. For example:Why: 0, 3
are: 4, 3
you: 8, 3
so: 12, 2
crazy: 15, 5
about: 21, 5
java: 27, 4
Own code:
import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); String str_all=in.nextLine(); int[] len=new int[100];//Record length String[] str_divide=new String[100];//Word array List<Integer> pos=new LinkedList<Integer>();//Occurrence position for(int i=0;i<str_all.length();i++) { if(i==0)pos.add(i); else if(str_all.charAt(i-1)==' ')pos.add(i); } str_divide=str_all.split(" "); for(int i=0;i<str_divide.length;i++) len[i]=str_divide[i].length(); for(int i=0;i<str_divide.length;i++) System.out.println(str_divide[i]+": "+pos.get(i)+", "+len[i]); } }
5. String
For the input string s (assuming that the string only contains words and spaces composed of letters), complete the following functions:
- Counts the number of occurrences of the letter c in the string
- Find the inverse of the string
- Output all positions of the substring str of the string (without considering the substring superposition phenomenon)
- The first letter of each word in the string is capitalized and output
Enter Description:
String s letter c substring str
Output Description:
The number of occurrences of c in s, the inverse str of S, the string after the initial capital of all words in all positions in S
Input example:
A set of inputs is given here. For example:
I scream you scream we all scream for icecream
m
eam
Output example:
The corresponding output is given here. For example:4
maerceci rof maercs lla ew maercs uoy maercs I
5 16 30 43
I Scream You Scream We All Scream For Icecream
Own code:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); String str_all=in.nextLine(); //Get character c String cc=in.nextLine(); char c=cc.charAt(0); //Substring String substr=in.nextLine(); //~~~~~~~~~~~~~~~~~~~~~Times~~~~~~~~~~~~~~~~~ int count=0; for(int i=0;i<str_all.length();i++) if(c==str_all.charAt(i))count++; System.out.println(count); //~~~~~~~~~~~~~~~~~~~~~Reverse order~~~~~~~~~~~~~~~~~ StringBuffer rev=new StringBuffer(str_all); System.out.println(rev.reverse().toString()); //~~~~~~~~~~~~~~~~~~~~~3~~~~~~~~~~~~~~~~~ int pos=str_all.indexOf(substr); System.out.print(pos); while(pos!=-1) { pos=str_all.indexOf(substr, pos+substr.length()); if(pos!=-1) System.out.print(" "+pos); } System.out.println(); //~~~~~~~~~~~~~~~~~~~~~4~~~~~~~~~~~~~~ StringBuffer upper=new StringBuffer(); for(int i=0;i<str_all.length();i++) { if(i==0)upper.append(Character.toUpperCase(str_all.charAt(i))); else { if(str_all.charAt(i-1)==' ')upper.append(Character.toUpperCase(str_all.charAt(i))); else upper.append(str_all.charAt(i)); } } System.out.println(upper); } }