Using Java, object-oriented encapsulation + inheritance method computation

Source: Internet
Author: User

1. First : Given a line of characters, reverse output this string (space.), such as "AB," (4 CD "Output" DC (, Ba "). ( Requirement: Use object-oriented encapsulation + inheritance )

classbu{PrivateString str;  PublicBu () {} PublicBu (String str) { This. str =str; }     PublicString getstr () {returnstr; }     Public voidsetstr (String str) { This. str =str; }     PublicString GetInfo () {returnstr; }}classRvsextendsBu//responsible for flipping { PublicRvs () {} PublicRvs (String str) {Super(str); }     Public voidm () {Given string, reverse output string
String Temp= ""A new string variable is responsible for receiving the string after the reverse orderChar[] CHS =Super. Getstr (). ToCharArray (); for(inti = Chs.length-1;i >= 0;i--) {Temp+=Chs[i]; } Super. SETSTR (temp); }}classOthextendsRvs//remove numbers and spaces { PublicOth () {} PublicOth (String str) {Super(str); } Public voidm2 () {String temp= ""; Char[] CHS =Super. Getstr (). ToCharArray ();//Remove numbers First for(inti = 0;i < chs.length;i++){ if(Chs[i] >= ' 0 ' && chs[i] <= ' 9 '){ Continue; } temp+=Chs[i]; } temp= Temp.replace ("", "" "); Then go to the space, using the Replace () methodSuper. SETSTR (temp); }}classStatt { Public Static voidMain (string[] args) {//Rvs r = new Rvs ("4m 897ou// "); //r.m (); //System.out.println (R.getinfo ());Oth Oth =NewOth ("4m 897ou//"); OTH.M (); The inverse of the string oth.m2 (); After reverse order, the string is stripped of space and number System.out.println (Oth.getinfo ()); Last output is GetInfo () content}}

2. The second type: Enter the number n, is the odd n becomes 3n+1, is an even number is N/2, after several times such a transformation, will make N into 1, the number of output transformation, and the number of requests is

Remainder by 3 ( Requirements: Using object-oriented encapsulation + inheritance )

Package Com.oracle.acm.prac;class x{private int n;private int count;//Times Public X () {count=0;} public X (int n) {count=0;this.n=n;} public void Changenum () {//if n is odd, then n becomes 3n+1, otherwise it becomes half of N if (n%2!=0) {n=3*n+1;} ELSE{N=N/2;}} public int Getchangecount () {//After several such transformations, it is bound to make N 1. The number of output transformations while (n!=1) {this.changenum (); count++;} return count%3; The required number of times to 3}}public class Demo10 {public static void main (string[] args) {X x=new x (2); System.out.println (X.getchangecount ());}}

/**given a single line of characters, this string is output in reverse order (spaces. Number not output),such as "AB," (4 CD "Output" DC (, Ba "). Requirements: Using object-oriented encapsulation + inheritance */
class Bu{private String str;Public Bu () {}Public Bu (String str) {this.str = str;}Public String Getstr () {return str;}Public void Setstr (String str) {this.str = str;}Public String GetInfo () {return str;}}class Rvs extends Bu{Public Rvs () {}Public Rvs (String str) {super (str);}Public void M () {String temp = "";char[] CHS = SUPER.GETSTR (). ToCharArray ();for (int i = Chs.length-1;i >= 0;i--) {temp + = chs[i];}super.setstr (temp);}}class Oth extends Rvs{Public Oth () {}Public Oth (String str) {super (str);}Public void m2 () {String temp = "";char[] CHS = SUPER.GETSTR (). ToCharArray ();for (int i = 0;i < chs.length;i++) {if (chs[i] >= ' 0 ' && chs[i] <= ' 9 ') {continue;}temp + = chs[i];}temp = Temp.replace ("", "");super.setstr (temp);}}class Statt{Public static void Main (string[] args){//rvs r = new Rvs ("4m 897ou//");//r.m ();//system.out.println (R.getinfo ());Oth Oth = new Oth ("4m 897ou//");oth.m ();oth.m2 ();System.out.println (Oth.getinfo ());
} }

Using Java, object-oriented encapsulation + inheritance method computation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.