zoukankan      html  css  js  c++  java
  • 第十二周上机作业

    1、设计个类,分别是:(知识点:抽象类及抽象方法)

    1Shape表示图形类,有面积属性area、周长属性per,颜色属性color,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:getArea计算面积、getPer计算周长、showAll输出所有信息,还有一个求颜色的方法getColor

    22个子类:

    1Rectangle表示矩形类,增加两个属性,Width表示长度、height表示宽度,重写getPergetAreashowAll三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。

    2Circle表示圆类,增加1个属性,radius表示半径,重写getPergetAreashowAll三个方法,另外又增加两个构造方法(为半径、颜色赋值的)。

     3)一个测试类PolyDemo,在main方法中,声明创建每个子类的对象,并调用2个子类的showAll方法。

    package chap;
    
    public abstract class shape {
        double area;
        double per;
        String color;
        public shape(){
        }
        public shape(String color){
            this.color=color;
        }
        public abstract void getarea();
        public abstract void getper();
        public abstract void showall();
        public String getcolor(){
            return color;
        }
    }
    package chap;
    
    public class rectangle extends shape{
        double width;
        double height;
        public rectangle(){
            
        }
        public rectangle(double width,double height){
            this.width=width;
            this.height=height;
            this.color=color;
        }
        public void getarea(){
            area=width*height;
        }
        public void getper(){
            per=2*(width+height);
        }
        public void showall(){
            System.out.println("矩形面积是"+area+"周长是"+per+"颜色是"+color);
        }
    }
    package chap;
    
    public class circle extends shape{
        double radius;
        public circle(){
        }
        public circle(double radius,String color){
            this.color=color;
            this.radius=radius;
        }
        public void getarea(){
            area=2*3.14*radius;
        }
        public void gerper(){
            per=3.14*radius*radius;
        }
        public void showall(){
            System.out.println("圆的面积是"+area+"周长是"+per+"颜色是"+color);
        }
        @Override
        public void getper() {
            // TODO Auto-generated method stub
            
        }
    }
    package chap;
    
    public class polydemo {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            rectangle r=new rectangle();
            r.getarea();
            r.getper();
            r.showall();
            circle c=new circle();
            c.getarea();
            c.gerper();
            c.showall();
        }
    }

    2Cola公司的雇员分为以下若干类:(知识点:多态)

    (1) ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。

    Ÿ 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。

    (2) SalariedEmployee ColaEmployee 的子类,拿固定工资的员工。

    Ÿ 属性:月薪

    (3) HourlyEmployee ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。

    Ÿ 属性:每小时的工资、每月工作的小时数

    (4) SalesEmployee ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。

    Ÿ 属性:月销售额、提成率

    (5) 定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类TestCompany,main方法,把若干各种类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资。

    package cola;
    
    public class colaemployee {
        String name;
        int month;
        public colaemployee(){
            
        }
        public colaemployee(String name,int month){
            this.name=name;
            this.month =month;
        }
        public double getsalary(int month){
            return 0;
        }
    }
    package cola;
    
    public class salariedemployee extends colaemployee{
        double salary;
        public salariedemployee(String name,int month,double salary){
            super(name,month);
            this.salary=salary;
        }
        public double getsalary(int month){
            if (super.month==month) {
                return salary+100;
            } else {
                return salary;
            }
        }
    }
    package cola;
    
    public class hourlyemployee extends colaemployee{
        int hoursalary;
        int hournum;
        public hourlyemployee(String name,int month,int hoursalary,int hournum){
            super(name,month);
            this.hoursalary=hoursalary;
            this.hournum=hournum;
        }
            public double getsalary(int month){
                if (super.month==month) {
                    if(hournum>160){
                        return hoursalary*160+hoursalary*(hournum-160)*1.5+100;
                    }else{
                        return hoursalary*hournum+100;
                    }
            }else{
                if (hournum>160) {
                    return hoursalary*160+hoursalary*(hournum-160)*1.5;
                } else {
                    return hoursalary*hournum;
                }
            }
        }
    }
    package cola;
    
    public class hourlyemployee extends colaemployee{
        int hoursalary;
        int hournum;
        public hourlyemployee(String name,int month,int hoursalary,int hournum){
            super(name,month);
            this.hoursalary=hoursalary;
            this.hournum=hournum;
        }
            public double getsalary(int month){
                if (super.month==month) {
                    if(hournum>160){
                        return hoursalary*160+hoursalary*(hournum-160)*1.5+100;
                    }else{
                        return hoursalary*hournum+100;
                    }
            }else{
                if (hournum>160) {
                    return hoursalary*160+hoursalary*(hournum-160)*1.5;
                } else {
                    return hoursalary*hournum;
                }
            }
        }
    }
    package cola;
    
    public class company {
        public void getsalary(colaemployee c,int month){
            System.out.println(c.name+"在"+month+"月的月薪为"+c.getsalary(month)+"元");
        }
    }
    package cola;
    
    public class testcompany {
    
        public static void main(String[] args) {
            colaemployee[] cel={
                    new salariedemployee("salariedemployee",6,30000),
                    new hourlyemployee("hourlyemployee",5,100,300),
                    new salesemployee("salesemployee",3,50000,0.3)
            };
            for(int i=0;i<cel.length;i++){
                new company().getsalary(cel[i],5);
            }
        }
    }

    3利用接口实现动态的创建对象:(知识点:接口

    1)创建4个类

    1苹果

    2香蕉

    3葡萄

    4园丁

    2)在三种水果的构造方法中打印一句话.

    以苹果类为例

    class apple

    {

    public apple()

    {

    System.out.println(“创建了一个苹果类的对象”);

    }

    }

    3)类图如下:

    4)要求从控制台输入一个字符串,根据字符串的值来判断创建三种水果中哪个类的对象。

    运行结果如图:

     

    package shier;
    
    import java.util.Scanner;
    
    public interface fruit {
        
    }
        class apple implements fruit{
            public apple(){
                System.out.println("创建了一个苹果类的对象");
            }
        }
        class banana implements fruit{
            public banana(){
                System.out.println("创建了一个香蕉类的对象");
            }
        }
        class grape implements fruit{
            public grape(){
                System.out.println("创建了一个葡萄类的对象");
            }
        }
        class gardener implements fruit{
            public void create(){
                Scanner input=new Scanner(System.in);
                String name=input.next();
                fruit fruit=null;
                switch(name){
                case"apple":
                    fruit=new apple();
                    break;
                case"banana":
                    fruit=new banana();
                    break;
                case"grape":
                    fruit=new grape();
                    break;
                    default:
                        System.out.println("输入错误,请重新输入");
                }
            }
        }
    package shier;
    
    public class test {
    
        public static void main(String[] args) {
            gardener g=new gardener();
            g.create();
        }
    }
  • 相关阅读:
    cidaemon.exe过程cpu入住率和关闭cidaemon.exe加工方法
    .net 一些常用的工具来破解
    关于加密和解密的设计思路
    oncopy和onpaste
    来迁移数据管道
    使用JSP实现商场购物车模块
    2014在辛星Javascript口译科
    Lua学习 1) —— Android呼叫变量值和分配
    HttpSQS
    手机后端开发
  • 原文地址:https://www.cnblogs.com/komorebi-jane/p/12929699.html
Copyright © 2011-2022 走看看