Java第十一次作业

1、利用接口实现动态的创建对象[选做题] • 5.1 创建4个类: • 苹果 • 香蕉 • 葡萄 • 园丁 • 5.2 在三种水果的构造方法中打印一句话. • 以苹果类为例 • class apple • { • public apple() • { • System.out.prin

package Demo01;
public interface Fruit {
class Apple implements Fruit{
public Apple() {
System.out.println("创建了一个苹果类的对象");
}
}
class Pear implements Fruit{
public Pear() {
System.out.println("创建了一个香蕉类的对象");
}
}
class Oranges implements Fruit{
public Oranges() {
System.out.println("创建了一个葡萄类的对象");
}
}
}
package Demo01

 

import java.util.Scanner;
import Demo01.Fruit.Apple;
import Demo01.Fruit.Oranges;
import Demo01.Fruit.Pear;
public class Gardener {
public static void main(String[] args) {
// TODO Auto-generated method stub
Gardener g=new Gardener();
g.create();
}
public Fruit create() {
Scanner sc=new Scanner(System.in);
System.out.println("请输入水果名称");
String name=sc.next();
Fruit f=null;
if(name.equals("苹果")) {
f=new Apple();
}else if(name.equals("香蕉")) {
f=new Pear();
}else if(name.equals("葡萄")) {
f=new Oranges();
}
return f;
}
}

 

 

2.输入6位密码,再次输入密码,如果不够6位,提示,位数不对,如果两次不一致,提示两次密码不一致。

package zuoye10;

import java.util.Scanner;

public class one {
    //1.输入6位密码,再次输入密码,如果不够6位,提示,位数不对,如果两次不一致,提示两次密码不一致。
    public static void pw() {
        Scanner input = new Scanner(System.in);
        String pass;
        String repass;
        System.out.println("请输入6位密码");
        pass = input.next();
        testfun_1(pass);
        System.out.println("请重新输入密码");
        repass = input.next();
        testfun_1(repass);
        testfun_2(pass, repass);
    }
    public static void testfun_2(String test, String retest) {
        if (!test.equals(retest)) {
            System.out.println("密码不一致");
            System.exit(0);
        }
    }
    public static void testfun_1(String test) {
        if (test.length() != 6) {
            System.out.println("位数不对");
            System.exit(0);
        }
    }
    public static void main(String[] args) {
        pw();
    }

}

 

  3.输入一个字符串,如果开头是ok并且包含no,那么输入错误。

package zuoye10;

import java.util.Scanner;

public class three {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入字符串");
        String test = input.next();
        if (test.startsWith("ok") || test.contains("no")) {
            System.out.println("输入错误");
        }
    }
}

 

 4.输入三个单词,组合成pascal命名法的字符串。(选做)

package zuoye10;

public class four {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String s_1 = "STU";
        String string_1 = s_1.substring(0, 1);
        String string_2 = s_1.substring(1, 3).toLowerCase();
        String s_2 = "manage";
        String string_3 = s_2.substring(0, 1).toUpperCase();
        String string_4 = s_2.substring(1, 6);
        String s_3 = "system";
        String string_5 = s_3.substring(0, 1).toUpperCase();
        String string_6 = s_3.substring(1, 6);
        System.out.println(string_1 + string_2 + string_3 + string_4 + string_5 + string_6);

    }
}

 

posted @ 2023-06-23 16:27  边海(皿゚)  阅读(19)  评论(0)    收藏  举报