zoukankan      html  css  js  c++  java
  • 5.21java作业

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

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

    (2)2个子类:

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

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

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

     1 package pro1;
     2 public abstract class Shape {
     3     protected double area;
     4     protected double per;
     5     protected String color;
     6 
     7     public Shape() {
     8     }
     9 
    10     public Shape(String color) {
    11         this.color = color;
    12     }
    13 
    14     public abstract void getArea();
    15 
    16     public abstract void getPer();
    17 
    18     public abstract void showAll();
    19 
    20 }
     1 package pro2;
     2  
     3 public class Rectangle extends Shape
     4 {
     5     private double width;
     6     private double height;
     7     public Rectangle() {};
     8     public Rectangle(double width, double height, String color)
     9     {
    10         super(color);
    11         this.width = width;
    12         this.height = height;
    13     }
    14     public double getPer()
    15     {
    16         return (width + height) * 2;
    17     }
    18     public double getArea()
    19     {
    20         return width * height;
    21     }
    22     public void showAll()
    23     {
    24         System.out.println(String.format("矩形长度%f,宽度%f,面积%f,周长%f,颜色为%s", width, height, getArea(), getPer(), getColor()));
    25     }
    26 }
     1 package pro3;
     2  
     3 public class Circle extends Shape
     4 {
     5     private double radius;
     6     public Circle() {};
     7     public Circle(double radius, String color)
     8     {
     9         super(color);
    10         this.radius = radius;
    11     }
    12     public double getPer()
    13     {
    14         return 2 * 3.14 * radius;
    15     }
    16     public double getArea()
    17     {
    18         return 3.14 * Math.pow(radius, 2);
    19     }
    20     public void showAll()
    21     {
    22         System.out.println(String.format("圆半径%f,面积%f,周长%f,颜色为%s", radius, getArea(), getPer(), getColor()));
    23     }
    24 }
    package pro4;
     
    public class PolyDemo
    {
        public static void main(String[] args)
        {
            Rectangle rect = new Rectangle(20, 10, "黄色");
            rect.showAll();
            Circle ccl = new Circle(20, "白色");
            ccl.showAll();
        }
    }

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

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

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

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

    Ÿ   属性:月薪

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

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

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

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

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

    package pro1;
     
    public class ColaEmployee
    {
        private String name;
        private int birthdayMonth;
        public ColaEmployee(String name, int birthdayMonth)
        {
            this.name = name;
            this.birthdayMonth = birthdayMonth;
        }
        public double getSalary(int month)
        {
            if (month == birthdayMonth)
                return 100;
            return 0;
        }
    }
     1 package pro1;
     2  
     3 public class SalariedEmployee extends ColaEmployee
     4 {
     5     private double monthlyFee;
     6     public SalariedEmployee(String name, int birthdayMonth, double monthlyFee)
     7     {
     8         super(name, birthdayMonth);
     9         this.monthlyFee = monthlyFee;
    10     }
    11     @Override
    12     public double getSalary(int month)
    13     {
    14         return monthlyFee + super.getSalary(month);
    15     }
    16 }
     1 package pro1;
     2  
     3 public class HourlyEmployee extends ColaEmployee
     4 {
     5     private double feePerHour;
     6     private int hours;
     7     public HourlyEmployee(String name, int birthdayMonth, double feePerHour, int hours)
     8     {
     9         super(name, birthdayMonth);
    10         this.feePerHour = feePerHour;
    11         this.hours = hours;
    12     }
    13     @Override
    14     public double getSalary(int month)
    15     {
    16         double fee = 0;
    17         if (hours > 160)
    18         {
    19             fee += feePerHour * 160 + feePerHour * (hours - 160) * 1.5;
    20         }
    21         else
    22         {
    23             fee += feePerHour * hours;
    24         }
    25         fee += super.getSalary(month);
    26         return fee;
    27     }
    28 }
     1 package pro1;
     2  
     3 public class SalesEmployee extends ColaEmployee
     4 {
     5     private double monthlySale;
     6     private double royaltyRate;
     7     public SalesEmployee(String name, int birthdayMonth, double monthlySale, double royaltyRate)
     8     {
     9         super(name, birthdayMonth);
    10         this.monthlySale = monthlySale;
    11         this.royaltyRate = royaltyRate;
    12     }
    13     @Override
    14     public double getSalary(int month)
    15     {
    16         double fee = 0;
    17         fee += monthlySale * royaltyRate;
    18         fee += super.getSalary(month);
    19         return fee;
    20     }
    21 }
    1 package pro1;
    2 
    3 public class Company
    4 {
    5     public static void printCompany(ColaEmployee emp, int month)
    6     {
    7         System.out.println(emp.getName() + "的工资额为" + emp.getSalary(month));
    8     }
    9 }
     1 package pro1;
     2  
     3 public class TestCompany
     4 {
     5     public static void main(String[] args)
     6     {
     7         ColaEmployee[] emps = new ColaEmployee[]
     8         {
     9                 new SalariedEmployee("张三", 4, 18000),
    10                 new HourlyEmployee("李四", 5, 200, 180),
    11                 new SalesEmployee("王二麻子", 6, 500000, 0.05)
    12         };
    13         for (ColaEmployee emp : emps)
    14         {
    15             Company.printCompany(emp, 5);
    16         }
    17     }
    18 }

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

    (1)创建4个类

    1苹果

    2香蕉

    3葡萄

    4园丁

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

    以苹果类为例

    class apple

    {

           public apple()

           {

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

    }

    }

    (3)类图如下:

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

    1 package pro1;
    2  
    3 public interface Fruit
    4 {
    5 }
    1 package pro1;
    2  
    3 public class Apple implements Fruit
    4 {
    5     public Apple()
    6     {
    7         System.out.println("创建了一个苹果类的对象");
    8     }
    9 }
    1 package pro1;
    2  
    3 public class Banana implements Fruit
    4 {
    5     public Banana()
    6     {
    7         System.out.println("创建了一个香蕉类的对象");
    8     }
    9 }
    1 package pro1;
    2  
    3 public class Grape implements Fruit
    4 {
    5     public Grape()
    6     {
    7         System.out.println("创建了一个葡萄类的对象");
    8     }
    9 }
     1 package pro1;
     2  
     3 import java.util.Scanner;
     4  
     5 public class Gardener
     6 {
     7     public static Fruit create(String type)
     8     {
     9         switch (type)
    10         {
    11             case "苹果":
    12                 return new Apple();
    13             case "香蕉":
    14                 return new Banana();
    15             case "葡萄":
    16                 return new Grape();
    17             default:
    18                 return null;
    19         }
    20     }
    21     public static void main(String[] args)
    22     {
    23         Scanner s = new Scanner(System.in);
    24         Fruit fruit = create(s.next());
    25         if (fruit == null)
    26         {
    27             System.out.println("不存在的类");
    28         }
    29     }
    30 }
  • 相关阅读:
    使用Fiddle修改请求数据
    Fiddle抓包应用概述
    s = -1 #作用域之外的数字是不会改的
    python list.reverse() 方法 不可以 ss = li.reverse() ,这个列表翻转方法没有返回值, ss=None
    python 两个tuple元组之间连接判断是否有一个为空, and 和 & ,只能用and 不能用&
    http 协议最大url是不限制的,但实际上不会很长,有服务器的限制
    这个居然也可以python >>>geturl()["a"]
    python的字典可以这样子 print(dic["ab"+cd]) 震惊!!!
    mysql 远程 死活连不上 阿里云搞得个什么鬼
    txt默认的是个什么格式,anex,什么的,另存为utf-8或者unicode中文就不乱了
  • 原文地址:https://www.cnblogs.com/qq1123514689/p/12935648.html
Copyright © 2011-2022 走看看