使用java中,物件導向封裝+繼承的方法算題

來源:互聯網
上載者:User

標籤:ati   []   replace   getc   n+1   字元   rac   toc   private   

1.第一種:給定一行字元,逆序輸出此字串(空格.數字不輸出),如“ab 23,(4 cd”輸出“dc(,ba”。(要求:使用物件導向封裝+繼承)

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(" ",""); //再去空格,用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()); //最後輸出是getInfo()內容 } }

2.第二種:輸入數字n,是奇數n就變為3n+1,是偶數就是n/2,經過若干次這樣的變換,一定會使n變為1,求輸出變換的次數,並要求次數是

除以3的餘數 (要求:使用物件導向封裝+繼承)

package com.oracle.acm.prac;class X{private int n;private int count; //次數public X(){count=0;}public X(int n){count=0;this.n=n;}public void changeNum(){//若n為奇數,則將n變為3n+1,否則變為n的一半if(n%2!=0){n=3*n+1;}else{n=n/2;}}public int getChangeCount(){//經過若干次這樣的變換,一定會使n變為1.求輸出變換的次數while(n!=1){this.changeNum();count++;}return count%3; //要求次數要對3取餘}}public class Demo10 {public static void main(String[] args) {X x=new X(2);System.out.println(x.getChangeCount());}}

 

 

/**給定一行字元,逆序輸出此字串(空格.數字不輸出),如“ab 23,(4 cd”輸出“dc(,ba”。要求:使用物件導向封裝+繼承 */
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());
} }

 

使用java中,物件導向封裝+繼承的方法算題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.