创建一个名为Branch的类,它具有的属性分别为字符串型subName,整形的salary和整形的totalBooks.在Branch类中定义calculateHrs()方法,用以执行计算.另外定义一个接受subName和totalBooks参数的参数化构造方法.totalHrs = totalBooks*2再定义两个类Science和Arts,并通过它们调用Branch的calculateHrs()方法.
假定科学类和艺术类书籍的总数分别为10和14,输出结果应该为:科学学科的总时数是:20
艺术学科的总时数是:28实在是没什么概念,不知道如何调用方法!小弟把自己写的代码贴出来给各位大大看一下!别笑我!我不会再定义Science和Arts这两个类,所以不知道该如何写!public class Branch {
    private String subName;
    private int salary;
    private int totalBooks;
    private int totalHrs;
    public Branch(String subName, int totalBooks) {
        this.subName = subName;
        this.totalBooks = totalBooks;
    }    void calculate() {
        totalHrs = totalBooks * 2;
    }    void print() {
        System.out.println("科学学科的总时数是:" + totalHrs);
        System.out.println("艺术学科的总时数是:" + totalHrs);
    }    public static void main(String[] args) {
        Branch Science = new Branch(10);
        Science.totalHrs();
        Science.print();
        Branch Arts = new Branch(14);
        Arts.totalHrs();
        Arts.print();
    }    }还望各位大大帮我解答!不胜感激!

解决方案 »

  1.   

    你定义的构造器要传两个参数,你在建对象(new)的时候,参数只有一个,肯定报错!calculate()和print()两个方法都是无参的,你这样写可以,没问题。
      

  2.   

    谢谢各位 这个问题我昨天自己回家想了后 自己解决了 呵呵 代码如下public class Branch {
        private String subName;
        private int salary;
        private int totalBooks;
        private int totalHrs;
        public Branch(String subName, int totalBooks) {
            this.subName = subName;
            this.totalBooks = totalBooks;
        }    void calculate() {
            totalHrs = totalBooks * 2;
        }    void print() {
            System.out.println("科学学科的总时数是:" + totalHrs);
            System.out.println("艺术学科的总时数是:" + totalHrs);
        }    public static void main(String[] args) {
           new Branch();
        }    }
    class Science{
      private int totalHrs;  public Science(int totalHrs){
          this.totalHrs=totalHrs;  }
      public int getTotalHrs(){
           return totalHrs;
      }
    }
    class Arts{
      private int totalHrs;
      public  Arts(int totalHrs){
         this.totalHrs=totalHrs;
      }
      public int getTotalHrs(){
        return totalHrs;
      }}