Java第七次作业

一、修改手机默认语言

 code

class Dqczy1 {
	private String language="英文";   //默认语言为英文
 
    public Dqczy1(){
        System.out.println("智能手机的默认语言为"+this.language);
    }
    //创建通过有参构造将手机语言设置为中文
    public Dqczy1(String language) {
        this.language = language;
        System.out.println("将智能手机的默认语言设置为"+this.language);
    }
}
public class Main {
    public static void main(String[] args) {
        //创建两个手机对象,演示效果
        
    	Dqczy1 ipone=new Dqczy1();
        //在创建手机的时刻,将手机的语言改为中文
    	Dqczy1 huawei=new Dqczy1("中文");
  }
}

 运行结果:

二、设置信用卡密码 

code

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

 运行结果:

 三、飞速的高铁

code 

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

运行结果:

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

  code

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 Dqczy4 {
    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();
    }
}

 运行结果:

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

   code

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

运行结果:

 六、输出圆形和矩形的面积
 code

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

 运行结果:

 七、定义人类的介绍方式

 code

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

 运行结果:

八、编写登陆方法 

code

import java.util.Scanner;
 
class user{
    String name;
    String password;
    public user(String id, String password) {
        this.name = id;
        this.password = password;
    }
    public user() {
    }
}
 
public class Dqczy8{
    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\t"+this.price+"\t\t\t\t\t"+this.weight+"\t\t\t\t\t"+"0.0\t\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\t"+this.price+"\t\t\t\t\t"+this.weight+"\t\t\t\t\t"+this.PackingCharge+"\t\t\t"+"";
    }
}
 
public class Dqczy9 {
    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\t\t\t\t\t\t\t\t\t"+(difference));
    }
}

 运行结果:

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

code

public interface student {
 
		void answer();// 回答的方法
		void note();// 笔记的方法
}
public interface teacher {//定义一个接口
	void greeting();// 问候的方法
	void attend();// 上课的方法
} 
public class Qi implements teacher, student {// 继承IFather接口和IMather接口
 
	@Override
	public void answer() {
		System.out.println("老师好");
	}
 
	@Override
	public void note() {
		System.out.println("同学开始记笔记");
	}
 
	@Override
	public void greeting() {
		System.out.println("同学们好");
	}
 
	@Override
	public void attend() {
		System.out.println("老师开始上课");
	}
 
	public static void main(String[] args) {
		student peter = new Qi();
		System.out.print("peter:");
		peter.answer();
		teacher mike = new Qi();
		System.out.print("mike:");
		mike.greeting();
		System.out.print("mike:");
		mike.attend();
		System.out.print("peter:");
		peter.note();
	}
}

 运行结果: 

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

 

code

public interface Book {
	void fish();
	 void smoke();
}public interface Bresk {
	void cook();
	 void wath();
}public class Qi implements Book,Bresk {
 
	@Override
	public void cook() {
		// TODO Auto-generated method stub
		System.out.println("做饭");
	}
 
	@Override
	public void wath() {
		// TODO Auto-generated method stub
		System.out.println("看电视");
	}
 
	@Override
	public void fish() {
		// TODO Auto-generated method stub
		System.out.println("钓鱼");
	}
 
	@Override
	public void smoke() {
		// TODO Auto-generated method stub
		System.out.println("抽烟");
	}
	 public static void main(String[] args) {
	         Bresk fa = new Qi();// 通过子类创fa建接口对象
		  System.out.println("儿子喜欢做的事有:");
		  fa.wath();
		  fa.cook();
		  Book mo =new Qi();// 通过子类创建mo接口对象
		  mo.smoke();
		  mo.fish();
		 
		 }
}

 运行结果:

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值