【作业】第六章 面向对象基础

//计算器简单的加减乘除
import java.util.Scanner;

public class Calculator {
    public static void main(String[] args){
        int jieShu=1;
        do {
            Scanner input = new Scanner(System.in);
            JiSuan jiSuan = new JiSuan();
            System.out.print("请输入你要作的运算(+-*/):");
            jiSuan.fuHao = input.next();
            System.out.print("请输入您要运算的第一个数:");
            jiSuan.num1 = input.nextDouble();
            System.out.print("请输入您要运算的第二个数:");
            jiSuan.num2 = input.nextDouble();
            System.out.println("结果为:" + jiSuan.num1 + jiSuan.fuHao + jiSuan.num2 + "=" + jiSuan.cound());
            System.out.print("输入0继续计算:");
            jieShu = input.nextInt();
        }while(jieShu==0);
        System.out.println("谢谢使用");

    }
}
class JiSuan{
    double sum,num1,num2;
    String fuHao;
    public double cound(){
        switch(fuHao){
            case "+":
                sum = num1 + num2;
                break;
            case "-":
                sum = num1 - num2;
                break;
            case "*":
                sum = num1 * num2;
                break;
            case "/":
                sum = num1 / num2;
                break;
        }
        return sum;
    }
}
---------------------------------------------------------------------------------
两个整数、三个浮点数的加法运算
public class JiaFaYunSuan {
    int num1,num2;
    double num,num3,num4,num5;
    public  JiaFaYunSuan(int num1, int num2){
        num=num1+num2;
        System.out.println(num);
    }
    public JiaFaYunSuan(double num3,double num4,double num5){
        num=num3+num4+num5;
        System.out.println(num);
    }
}
class JieGuo{
    public static void main(String[] args){
        JiaFaYunSuan yunSuan=new JiaFaYunSuan(11.96,12.88,12);
        JiaFaYunSuan yunSuan0=new JiaFaYunSuan(11,12);
    }
}
---------------------------------------------------------------------
如下JAVA代码,编译运行后,输出结果是什么?
public class MobilPhoneTest {
    public static void main(String[] args){     //main方法程序入口
        MobilPhone mp=new MobilPhone();     //创建购买手机类的对象
        mp.brand="苹果";      //“"苹果"赋值给对象mp里的brand
        String detail=mp.buy("发工资啦");   //使用买手机对象带参的有钱方法赋值给detail
        System.out.println(detail);     //输出结果
    }
}
//购买手机类
class MobilPhone{
    public String brand;    //手机品牌
    public MobilPhone(){    //无参构造方法
        this.brand="诺基亚";   //”"诺基亚"赋值刚给成员变量brand
    }
    public MobilPhone(String bra){  //带参构造方法
        this.brand=bra;     //bra赋值给成员变量brand
    }
    public String buy(){    //没发工资,购买手机方法
        return "没发工资,买一个"+brand+"牌子的手机吧!";
    }
    public String buy(String reason){   //带参的有钱方法
        return reason+",快买一个"+brand+"牌子的手机吧!";
    }
}
//结果
//发工资啦,快买一个苹果牌子的手机吧!
---------------------------------------------------------------------------------

 

智能手机和手机,使用接口实现
public interface NetWorking {
    void netWork();
}
---------------------------------
public interface Playing {
    void playVideo(String name);
}
---------------------------------
public interface TakePhoto {
    void takePhotos();
}
-----------------------------------
public abstract class HandSet {
    private String brand;
    private String type;

    public HandSet() {
    }

    public HandSet(String brand, String type) {
        this.brand = brand;
        this.type = type;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
    public void showInfo(){
        System.out.println("这是一款序号为"+type+"的"+brand+"手机");
    }
    public abstract void sendMess();
    public abstract void call();
}
public class Test {
    public static void main(String[] args){
        CommonPhone common = new CommonPhone("索尼爱立信","G502c");
        common.showInfo();
        common.call();
        common.sendMess();
        common.playVideo("热雪");
        SmartPhone smartPhone = new SmartPhone("HTC","I9100");
        smartPhone.showInfo();
        smartPhone.call();
        smartPhone.sendMess();
        smartPhone.playVideo("海贼王");
        smartPhone.netWork();
        smartPhone.takePhotos();
    }
}

//结果

//这是一款序号为G502c的索尼爱立信手机
//开始语音通话
//发送文字短信
//播放音频:《“热雪》
//这是一款序号为I9100的HTC手机
//开始视频通话
//发送文字与带图片的信息
//播放视频《”海贼王》
//上网
//拍照

 

 

接口编程思想,组装一个计算机

public interface CPU {
    String getCPUBrand();
    String getFrequency();
}
---------------------------------
public interface HardDisk {
    int getHardDiskSize();
}
---------------------------------
public interface RAM {
    String getRamBrand();
    int getRamSize();
}
----------------------------------
public class InterCPU implements CPU {
    @Override
    public String getCPUBrand() {
        return "inter";
    }

    @Override
    public String getFrequency() {
        return "4.5";
    }
}
------------------------------------------
public class JSD_RAM implements RAM {

    public String getRamBrand() {
        return "DDR4";
    }

    public int getRamSize() {
        return 16;
    }
}
-----------------------------------------------
public class XJ_HardDisk implements HardDisk {
    @Override
    public int getHardDiskSize() {
        return 1000;
    }
}
-------------------------------------------------------
public class Computer {
    CPU cpu;
    HardDisk hardDisk;
    RAM ram;
    public Computer(){}
    public Computer(CPU cpu, HardDisk hardDisk, RAM ram){
        this.cpu = cpu;
        this.hardDisk = hardDisk;
        this.ram = ram;
    }
    public void showInfo(){
        System.out.println("计算机的信息如下:");
        System.out.println("CPU品牌:"+cpu.getCPUBrand()+"  主频:"+cpu.getFrequency());
        System.out.println("内存大小:"+ram.getRamBrand()+" "+ram.getRamSize()+"GB");
        System.out.println("硬盘容量:"+hardDisk.getHardDiskSize()+"GB");
    }
}
--------------------------------------------------------------------
public class ComputerTest {
    public static void main(String[] args) {
        CPU cpu = new InterCPU();
        HardDisk hardDisk = new XJ_HardDisk();
        RAM ram = new JSD_RAM();
        Computer computer = new Computer(cpu,hardDisk,ram);
        computer.showInfo();
    }
}
//结果
//计算机的信息如下:
//CPU品牌:inter  主频:4.5
//内存大小:DDR4 16GB
//硬盘容量:1000GB

 

posted @ 2019-02-20 19:35  XiaoZheJun  阅读(451)  评论(0编辑  收藏  举报