java复习八实验

1.家中的电视

编写一个Java应用程序,模拟家庭买一台电视,即家庭将电视作为自己的一个成员,通过调用一个方法将某台电视的引用传递给自己的电视成员。具体要求如下:

(1)有TV.java、Family.java和Main.java3个源文件,其中TV.java中的TV类负责创建“电视”对象,Family.java中的Family类负责创建“家庭”对象,Main.java是主类。

(2)在主类的main()方法中首先使用TV类创建一个对象haierTV,然后使用Family类再创建一个对象zhangSanFamily,并将先前TV类的实例haierTV的引用传递给zhangSanFamily对象的成员变量homeTV。代码如下:

public class Main

{

    public static void main(String[] args)

    {

        TV haierTV = new TV();

        haierTV.setChannel(5);

        System.out.println("haierTV的频道是" + haierTV.getChannel());

        Family zhangSanFamily = new Family();

        zhangSanFamily.buyTV(haierTV);

        System.out.println("zhangSanFamily开始看电视节目");

        System.out.println("体育频道");

        zhangSanFamily.seeTV();

        int m = 2;

        System.out.println("zhangSanFamily将电视更换到" + m + "频道");

        zhangSanFamily.remoteControl(m);

        System.out.println("haierTV的频道是" + haierTV.getChannel());

        System.out.println("zhangSanFamily再看电视节目");

        System.out.println("经济频道");

        zhangSanFamily.seeTV();

    }

}

class TV

{

    int channel;

    void setChannel(int m)

    {

        if(m >= 1)

        {

            channel = m;

        }

    }

    int getChannel()

    {

        return channel;

    }

    void showProgram()

    {

        switch (channel)

        {

            case 1:

                System.out.println("综合频道");

                break;

            case 2:

                System.out.println("经济频道");

                break;

            case 3:

                System.out.println("文艺频道");

                break;

            case 4:

                System.out.println("国际频道");

                break;

            case 5:

                System.out.println("体育频道");

                break;

            default:

                System.out.println("不能收看" + channel + "频道");

        }

    }

}

class Family

{

    TV homeTV;

    void buyTV(TV tv)

    {

        homeTV = tv;

    }

    void remoteControl(int m)

    {

        homeTV.setChannel(m);

    }

    void seeTV()

    {

        homeTV.getChannel();

    }

}

2.在下列字符串中将“登录网站”错写为“登陆网站”,将“惊慌失措”错写为“惊慌失错”,“忘记密码,不要惊慌失错,请登录www.yy.cn或登录www.tt.cc”。编写一个Java应用程序,输出把错别字替换为正确用字的字符串。

import java.util.regex.*;

public class Main {

   public static void main(String args[ ]) { 

      String str = "忘记密码,不要惊慌失错,请登陆www.yy.cn或登陆www.tt.cc";

      Pattern pattern;        

      Matcher matcher;         

      String regex = "登陆";

      pattern = Pattern.compile(regex); 

      matcher = pattern.matcher(str);   

      while(matcher.find()) {

         String s = matcher.group();

         System.out.print(matcher.start()+"位置出现:");

         System.out.println(s);

      } 

      System.out.println("将\"登陆\"替换为\"登录\"的字符串:");

      String result = matcher.replaceAll("登录");

      System.out.println(result);

      pattern= Pattern.compile("惊慌失错");  

      matcher = pattern.matcher(result);           

      System.out.println("将\"惊慌失错\"替换为\"惊慌失措\"的字符串:");

      result = matcher.replaceAll("惊慌失措");

      System.out.println(result);

   }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值