Java的第七次作业

//手机类
class Phone{
    private String language="英文";   //默认语言为英文

    public Phone(){
        System.out.println("智能手机的默认语言为"+this.language);
    }
    //创建通过有参构造将手机语言设置为中文
    public Phone(String language) {
        this.language = language;
        System.out.println("将智能手机的默认语言设置为"+this.language);
    }
}


public class Mobile {
	public static void main(String[] args) {
		// 创建两个手机对象,演示效果
Phone apple=new Phone();
Phone redmi=new Phone("中文");//在创建手机的时刻,将手机的语言改为中文
	}

}

运行结果:

class creditCard{
     String id; //卡号
     String password="123456";  //密码

    //设置卡号,默认密码
    public creditCard(String id) {
        this.id=id;
        System.out.println("信用卡"+this.id+"的默认密码为:"+this.password);
    }

    //设置初始密码与初始密码
    public creditCard(String id,String Password){
        this.id=id;
        this.password=Password;
        System.out.println("信用卡"+this.id+"的密码为:"+this.password);
    }
    //重置信用卡密码,
    public creditCard(creditCard data,String Password){

        this.id=data.id;
        this.password=Password;
        System.out.println("重置信用卡"+this.id+"的密码为:"+this.password);
    }

}


public class Phone {
    public static void main(String[] args) {
        //创建信用卡,不设置初始密码
        creditCard num1=new creditCard("40137356156462146");
        //重置密码,将需要重置的对象传递过去,使用其卡号,并返回一个新对象
        num1=new creditCard(num1,"168779"); //重置密码
    }
}

运行结果:

class train{
    double speed;

    public train() {

    }
    public train(double speed) {
        this.speed = speed;
        System.out.printf("火车的速度为 %.1f 公里/小时\n",speed);
    }
}

class high_speed_railway extends train{
    public high_speed_railway(double speed) {
        super(speed);   //构造父类
        this.speed=(super.speed*2); //高铁的速度是火车的二倍
        System.out.printf("高铁的速度为 %.1f 公里/小时\n",this.speed);
    }
}


public class Phone {
    public static void main(String[] args) {
        //创建高铁类,传入火车的速度
        high_speed_railway h=new high_speed_railway(145.8);
    }
}

运行结果:

 

public class Main_4 {
    public static void main(String[] args) {
        //创建两个钟表的对象,并赋值数据
        clock clockNum1=new clock("机械钟",189.99);
        clock clockNum2=new clock("石英表",69);

        clockNum1.show();
        clockNum1.getTime();

        clockNum2.show();
        clockNum2.getTime();
    }
}


public class Mobile{
    public static void main(String[] args) {
        //创建两个钟表的对象,并赋值数据
        clock clockNum1=new clock("机械钟",189.99);
        clock clockNum2=new clock("石英表",69);

        clockNum1.show();
        clockNum1.getTime();

        clockNum2.show();
        clockNum2.getTime();
    }
}

运行结果:

public class Mobile {
    static final double PI=3.141592653589793;

    public static void main(String[] args) {
        //获取PI值
        System.out.println(calculation());
        //获取圆的面积
        System.out.println(calculation(4));
        //获取矩阵的面积
        System.out.println(calculation(3, 4));

    }
    public static double calculation(double r){
        return PI*(r*r);    //计算圆的面积:pi*r方
    }
    public static double calculation(double wide,double height){
        return wide*height;    //计算矩形的面积:宽*高
    }
    public static double calculation(){
        return PI;    //无输入返回PI
    }
}

运行结果:

 

//图形类
class Shape{
    public void Calculated_area(){
    }
}
//圆形类
class circle extends Shape{
    //计算圆的面积
    public double Calculated_area(double r){
        return (3.1415926)*(r*r);
    }
}

//矩形类
class rectangle extends Shape{
    //计算矩形面积
    public double Calculated_area(double wide,double hetght){
        return wide*hetght;
    }
}



public class Phone {
    public static void main(String[] args) {
        circle c=new circle();
        System.out.println("圆的面积:"+c.Calculated_area(1.5));

        rectangle r=new rectangle();
        System.out.println("矩形的面积:"+r.Calculated_area(2, 5.5));
    }
}

运行结果:

 

class human{
    String name;
    int age;

    public human(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public human() {
    }

    @Override
    public String toString(){
    	// 判断年龄是否大于等于18岁,是则返回成年,否则返回未成年人
        return "我"+this.age+"岁,我是"+(this.age >= 18 ? "成年人" : "未成年人");
    }
}


public class Mobile {
    public static void main(String[] args) {
        human h_1=new human("小明",18);
        System.out.println(h_1.toString());
    }
}

运行结果: 

class user{
    String name;
    String password;
    public user(String id, String password) {
        this.name = id;
        this.password = password;
    }
    public user() {
    }
}


public class Phone {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);

        System.out.println("请输入用户名:");
        String name=in.next();
        System.out.println("请输入密码:");
        String passwo=in.next();

        System.out.println("------------------");

        if(true ==  logIn(name,passwo)){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }
    }
    //登录方法
    public static boolean logIn(String Name,String PassWord){
        //代表正确的用户名与密码
        user ur=new user("张三","123456");
        int nLen=Name.length();
        int pLen=PassWord.length();

        //如果用户名密码长度与正确的长度不相同
        if(nLen != ur.name.length() || pLen != ur.password.length() ){
            return false;
        }

        //验证用户名
        for (int i = 0; i < nLen; i++) {
            //判断用户名是否相同
            // charAt(index) 为获取字符串指定下标的元素
            if(Name.charAt(i) != ur.name.charAt(i)){
                return false;
            }
        }

        //验证密码
        for (int i = 0; i < pLen; i++) {
            //判断密码是否相同
            if(PassWord.charAt(i) != ur.password.charAt(i)){
                return false;
            }
        }
        //如果所以都符合的话,那么返回true,表示登录成功
        return true;
    }
}

运行结果:

 


//普通水果
class fruit{
  String name;    //水果名称
  double price;   //水果价格
  double weight;  //重量 / 千克

  public fruit(String name, double price, double weight) {
      this.name = name;
      this.price = price;
      this.weight = weight;
  }

  public fruit() {
  }
  @Override
  public String toString(){
      return this.name+"\t"+this.price+"\t\t\t"+this.weight+"\t\t\t"+"0.0\t\t"+"";
  }
}

//精装水果
class PackagedFruit extends fruit{
  double PackingCharge;   //包装费
  public PackagedFruit(String name, double price, double weight, double packingCharge) {
      super(name, price, weight);
      this.PackingCharge=packingCharge;
  }

  public PackagedFruit() {
  }
  @Override
  public String toString(){
      return this.name+"\t"+this.price+"\t\t\t"+this.weight+"\t\t\t"+this.PackingCharge+"\t\t"+"";
  }
}



public class Mobile {
  public static void main(String[] args) {
      //给普通水果赋值
      fruit apple=new fruit("苹果",1.98,5.0);
      //给精装水果赋值
      PackagedFruit PackageApple=new PackagedFruit("苹果",1.98,5.0,1);

      System.out.println("水果名称 \t 水果价格(元/千克) \t 水果重量(千克) \t 包装费(元/千克) \t 总价");
      System.out.println("-----------------------------------------------------------------------------------");

      //总价格等于 价格*重量
      double sum1=apple.price*apple.weight;
      System.out.println(apple.toString()+sum1);  //输出数据与总价

      //总价格等于 水果价格包装费*重量
      double sum2=(PackageApple.price+PackageApple.PackingCharge)*PackageApple.weight;
      System.out.println(PackageApple.toString()+sum2);

      System.out.println("-----------------------------------------------------------------------------------");

      //差价等于两个总和的差值
      double difference=Math.abs(sum2-sum1);
      System.out.println("差价\t\t\t\t\t\t\t"+(difference));
  }
}

运行结果:

//问候接口
interface greet{
    void Greet();
}
//工作接口
interface work{
    void Work();
}

//教师类
class teacher implements greet,work{
    private String name;
    //实现这两个接口
    @Override
    public void Work(){
        System.out.println(this.name+":同学们好");
    }
    @Override
    public void Greet(){
        System.out.println(this.name+":老师开始上课");
    }
    //有参构造
    public teacher(String name) {
        this.name = name;
    }
    //无参构造
    public teacher() {
    }
}

class student implements greet,work{
    private String name;

    //实现这两个接口
    @Override
    public void Greet() {
        System.out.println(this.name+":老师好");
    }
    @Override
    public void Work() {
        System.out.println(this.name+":同学开始记笔记");
    }

    //有参构造与无参构造
    public student(String name) {
        this.name = name;
    }
    public student() {
    }
}



public class Demo6{
    public static void main(String[] args) {
    	// 创建老师与学生的对象
        teacher mike=new teacher("mike");
        student peter=new student("peter");

        peter.Greet();	//模拟上课场景
        mike.Greet();
        mike.Work();
        peter.Work();
    }
}

运行结果:

 


interface DadHobby{
    //抽烟
    void smoke();
    //钓鱼
    void fishing();
}

interface MothersHobby{
    //看电视
    void watchTV();
    //做饭
    void cook();
}

class son implements DadHobby,MothersHobby{

    @Override
    public void smoke() {
        //ps:抽烟有害健康,未成年人禁止吸烟
        System.out.println("抽烟");
    }

    // 实现钓鱼方法
    @Override
    public void fishing() {
        System.out.println("钓鱼");
    }

    @Override
    public void watchTV() {
        System.out.println("看电视");
    }

    @Override
    public void cook() {
        System.out.println("做饭");
    }
}

public class Demo6 {
    public static void main(String[] args) {
        son xm=new son();

        System.out.println("儿子喜欢做的事有:");

        xm.watchTV();
        xm.cook();
        xm.smoke();
        xm.fishing();
    }
}

运行结果:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值