第七次作业

目录

1、修改手机默认语言

2、设置信用卡密码

3、飞速的高铁

4、计算机械钟和石英手表的时间

5、多功能参数(方法的重载)

6、输出圆形和矩形的面积

7、定义人类的介绍方式

8、编写登陆方法

9、人工包装的水果与普通水果的价格

10、模拟上课场景(接口与实现)

11、儿子喜欢做的事(接口与实现 多实现)


1、修改手机默认语言

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 In {
    public static void main(String[] args) {
        //创建两个手机对象,演示效果
        
        phone VIVO=new phone();
        //在创建手机的时刻,将手机的语言改为中文
        phone OPPO=new phone("中文");

    }
}

运行结果:

2、设置信用卡密码


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

    //设置卡号,默认密码
    public creditCard(String id) {
        this.id=id;
        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 Pu {
    public static void main(String[] args) {
        //创建信用卡,不设置初始密码
        creditCard num1=new creditCard("40137356156462146");
        //重置密码,将需要重置的对象传递过去,使用其卡号,并返回一个新对象
        num1=new creditCard(num1,"168779"); //重置密码
    }
}

运行结果:

3、飞速的高铁

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 In {
    public static void main(String[] args) {
        //创建高铁类,传入火车的速度
        high_speed_railway h=new high_speed_railway(145.8);
    }
}

运行结果:

4、计算机械钟和石英手表的时间

class clock{
    String type;
    double price;

    public static void getTime(){
        //不确定题目要求的是获取当前时间,还是只是模拟获取时间
        System.out.println("当前时间:10点10分");	
    }
    public void show(){
        System.out.printf("%s的价格为 %.2f元RMB\n",this.type,this.price);	//输出
    }
    public clock(String type, double price) {
        this.type = type;
        this.price = price;
    }

}

public class In {
    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();
    }
}

运行结果:

5、多功能参数(方法的重载)

public class In {
    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
    }
}

运行结果:

6、输出圆形和矩形的面积

//图形类
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 In {
    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));
    }
}

运行结果:

7、定义人类的介绍方式

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 Main_7 {
    public static void main(String[] args) {
        human h_1=new human("小明",18);
        System.out.println(h_1.toString());
    }
}

运行结果:

8、编写登陆方法

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

public class In {
    public static void main(String[] args) {
        try (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;
    }
}

运行结果:

9、人工包装的水果与普通水果的价格

//普通水果
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+this.price+this.weight+"0.0"+"";
    }
}
 
//精装水果
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+this.price+this.weight+this.PackingCharge+"\t\t\t"+"";
    }
}
 
public class Pu {
    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("差价:"+(difference));
    }
}

运行结果:

10、模拟上课场景(接口与实现)

package zy;
 
interface Why{//问候接口
 public void txh();
 public void lsh();
}
interface word{//工作接口
 public void sk();
 public void bj();
}
class tx{//同学类
 public void lsh(){//重写方法
  System.out.println("peter:老师好");
 }
 public void bj() {
  System.out.println("peter:同学开始记笔记");
 }
}
class ls{//老师类
 public void txh(){//重写方法
  System.out.println("mike:同学们好");}
  public void sk() {
  System.out.println("mike:老师开始上课");
  }
  }
 
public class Dad {
 public static void main(String[]args) {
  tx wh1=new tx();
  wh1.lsh();
  ls txh1=new ls();
  txh1.txh();
  txh1.sk();
  wh1.bj();
 }
 
 
}

运行结果:

11、儿子喜欢做的事(接口与实现 多实现)

package zy;
interface B{//爸爸接口
	 public void b();
	}
	interface M{//妈妈接口
	 public void m();
	}

	class YJSK  implements bb,mm{//实现接口
		 
		 public void e() {
		  System.out.println("儿子喜欢做的事:");
		 }
		 @Override
		 public void b() {//重写方法
		  
		  // TODO Auto-generated method stub
		  System.out.println("看电视");
		  System.out.println("做饭");
		 }
		 
		 @Override
		 public void m() {
		  // TODO Auto-generated method stub
		  
		  System.out.println("抽烟");
		  System.out.println("钓鱼");
		 }
		public static void main(String[]args) {
		  YJSK er=new YJSK();//创建对象
		  er.e();//调用
		  er.b();
		  er.m();
		 }
		 
		 
		}

运行结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值