创建一个计算抽象类中数字的方法。 [英] Creating a method to count numbers in an abstract class.

查看:64
本文介绍了创建一个计算抽象类中数字的方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1。)我创建了一个抽象类,如下所示。属于该类的所有方法也按以下方式添加。

1.) I have created an abstract class as the following. All the methods that belongs to the class is also added as at the following.

public abstract class Vehicles {
    
    public String vRegNo;
    public String vModel;
    public String vBrand;
    public boolean hired; // This hired attribute is used to store the hire           availability of the vehicle.
    public int vLength;
    public int vHeight;
    public byte vWheels;

    public String getvRegNo() {
        return vRegNo;
    }

    public void setvRegNo(String vRegNo) {
        this.vRegNo = vRegNo;
    }

    public String getvModel() {
        return vModel;
    }

    public void setvModel(String vModel) {
        this.vModel = vModel;
    }

    public String getvBrand() {
        return vBrand;
    }

    public void setvBrand(String vBrand) {
        this.vBrand = vBrand;
    }

    public boolean isHired() {
        return hired;
    }

    public void setHired(boolean hired) {
        this.hired = hired;
    }

    public int getvLength() {
        return vLength;
    }

    public void setvLength(int vLength) {
        this.vLength = vLength;
    }

    public int getvHeight() {
        return vHeight;
    }

    public void setvHeight(int vHeight) {
        this.vHeight = vHeight;
    }

    public byte getvWheels() {
        return vWheels;
    }

    public void setvWheels(byte vWheels) {
        this.vWheels = vWheels;
    }
    
    
}







2 。)为了使用那个类,我在下面创建了另一个子类作为van。






2.) To use that class I have created another sub class as van at the below.

public class Van extends Vehicles {
    
    public int vLoadCapacity;

    public int getvLoadCapacity() {
        return vLoadCapacity;
    }

    public void setvLoadCapacity(int vLoadCapacity) {
        this.vLoadCapacity = vLoadCapacity;
    }
    
    
    
    
    
    public static void main(String[] args) {
        
    Van oVan1 = new Van();
    oVan1.setvRegNo("PA-6589");
    oVan1.setvBrand("Toyota");
    oVan1.setvModel("Regius Ace 167");
    oVan1.setvLoadCapacity(16);
    oVan1.setvHeight(6);
    oVan1.setvLength(14);
    oVan1.setHired(true);
    
        System.out.println("Van 01 Vehicle Registration No :" + oVan1.getvRegNo());
        System.out.println("Van 01 Brand :" + oVan1.getvBrand());
        System.out.println("Van 01 Model :" + oVan1.getvModel());
        oVan1.getvLoadCapacity();
        oVan1.getvHeight();
        oVan1.getvLength();
        System.out.println("Van 01 Hire Availability :" + oVan1.hireVan());
        System.out.println("");
       
    Van oVan2 = new Van();
    oVan2.setvRegNo("PB-6139");
    oVan2.setvBrand("Toyota");
    oVan2.setvModel("Hiace 113");
    oVan2.setvLoadCapacity(12);
    oVan2.setvHeight(6);
    oVan2.setvLength(13);
    oVan2.setHired(true);
    
    
        System.out.println("Van 02 Vehicle Registration No :" + oVan2.getvRegNo());
        System.out.println("Van 02 Brand :" + oVan2.getvBrand());
        System.out.println("Van 02 Model :" + oVan2.getvModel());
        oVan2.getvLoadCapacity();
        oVan2.getvHeight();
        oVan2.getvLength();
        System.out.println("Van 02 Hire Availability :" + oVan2.hireVan());
        System.out.println("");

    
    Van oVan3 =  new Van();
    oVan3.setvRegNo("NA-9999");
    oVan3.setvBrand("Toyota");
    oVan3.setvModel("Hiace KDH110");
    oVan3.setvLoadCapacity(18);
    oVan3.setvHeight(7);
    oVan3.setvLength(18);
    oVan3.setHired(false);
    
        System.out.println("Van 03 Vehicle Registration No :" + oVan3.getvRegNo());
        System.out.println("Van 03 Brand :" + oVan3.getvBrand());
        System.out.println("Van 03 Model :" + oVan3.getvModel());
        oVan3.getvLoadCapacity();
        oVan3.getvHeight();
        oVan3.getvLength();
        System.out.println("Van 03 Hire Availability :" + oVan3.hireVan());
        System.out.println("");

    
    Van oVan4 = new Van();
    oVan4.setvRegNo("NB-0005");
    oVan4.setvBrand("Nissan");
    oVan4.setvModel("Caravan 2010");
    oVan4.setvLoadCapacity(17);
    oVan4.setvHeight(7);
    oVan4.setvLength(15);
    oVan4.setHired(false);
    
        System.out.println("Van 04 Vehicle Registration No :" + oVan4.getvRegNo());
        System.out.println("Van 04 Brand :" + oVan4.getvBrand());
        System.out.println("Van 04 Model :" + oVan4.getvModel());
        oVan4.getvLoadCapacity();
        oVan4.getvHeight();
        oVan4.getvLength();
        System.out.println("Van 04 Hire Availability :" + oVan4.hireVan());
        System.out.println("");

    
    Van oVan5 = new Van();
    oVan5.setvRegNo("NP-2314");
    oVan5.setvBrand("Mitsubishi");
    oVan5.setvModel("Delica L200");
    oVan5.setvLoadCapacity(10);
    oVan5.setvHeight(7);
    oVan5.setvLength(10);
    oVan5.setHired(true);
    oVan5.hireVan();
    
        System.out.println("Van 05 Vehicle Registration No :" + oVan5.getvRegNo());
        System.out.println("Van 05 Brand :" + oVan5.getvBrand());
        System.out.println("Van 05 Model :" + oVan5.getvModel());
        oVan5.getvLoadCapacity();
        oVan5.getvHeight();
        oVan5.getvLength();
        System.out.println("Van 05 Hire Availability :" + oVan5.hireVan());
        System.out.println("");

        
     
        
    }
    
    //This hireVan() method is used to find out the vans that can be hired! 
    public String hireVan(){
      String hStatus;  
        if(hired==true){
           hStatus = "Hired !";   
        }else{
            hStatus ="Not Hired !";
        }
        
        return hStatus;
        
    }
    
 
}





但我现在的问题是我怎么能够计算出可以完全雇用的货车的数量。我需要在



But my problem is now how am I able to calculate the no of vans that can be hired totally. I need to create a method in side the

public class van extends vehicles 





有静态方法的解决方案吗?请帮我!



谢谢!



############### ################################################## ###

on 12-17-2012提问者:



解决方案很难理解。但是我已经创建了下面的全部类,并且引导了我创建了2个构造函数。



类01

.

Is there any solution with static methods ? Please help me!

Thank You!

####################################################################
on 12-17-2012 questioner addeed:

The solutions are rather difficult to understand. However I have created the whole classes as the below and created 2 constructors as you have guided me.

Class 01

public abstract class Vehicle {
    
    private String strRegNumber;
    private boolean bolHired;
    private String strMake;
    private String strModel;
    private int intVLength;
    private int intVHeight;
}
//After this setters & getters have set correctly.



Class 02


Class 02

public class Van extends Vehicle{
    
    public int intPassengers;
    public boolean bolFullAC;
    public int intWheels;
    
    public Van(){
        
        this.setStrRegNumber("");
        this.setStrMake("");
        this.setStrModel("");
        this.setBolHired(false);
        this.setIntVHeight(0);
        this.setIntVLength(0);
        
    }
    
    public Van(String strRegNumber, String strMake, String strModel, boolean bolHired, int intVHeight, int intVLength){
        
        this();
        this.setStrRegNumber(strRegNumber);
        this.setStrMake(strMake);
        this.setStrModel(strModel);
        this.setBolHired(bolHired);
        this.setIntVHeight(intVHeight);
        this.setIntVLength(intVLength);
    }
           
}







但我的问题是朋友那么我如何能够使用方法向属性添加具有相关值的新货车并计算可用于租用的货车的数量。还有朋友,如果你能给我一些好书的建议,请参考实际场景和例子,请试着找我一个!我真的很感激。



谢谢



Chiransj




But my question is pal then how am I able to add a new van with relevant values to the attributes using a method and calculate the no of vans that available to hire. And also pal if you can give me some advice on a good book to refer with practical scenarios and examples please try to find me one! I really appreciate it.

Thank You

Chiransj

推荐答案

你需要一个类似于车辆的课程 - 一些VehicleFacade:



You need a class above the Vehicles - some VehicleFacade:

public calls VehicleFacade {

Vector<vehicle> vehicles = new Vector<vehicles>();

  public VehicleFacade(){ /* doany(); */}

  public void addVehicle(Vehicle vehicle){
    if(this.validateVehicle(vehicle)){
      vehicles.add(vehicle);
    }
  }

  private boolean validateVehicle(Vehicle vehicle){
    //validate vehicle

    // additional for special versions
    if(vehicle instanceof Van){
      return false;// in case vehicle is not valid 
    }
    return true; // all ok
  }

  // additional methods for deleting vehicles, counting vehicles, selecting vehicles, validation

  public Vector<vehicle> getVehicles(){
    return vehicles;
  }

}





您可以在主处持有,使用和询问分类:





This you can hold, work with and ask in your Main-Class:

public class VehicleTest{
   
  private final VehicleFacade facade;
  
  public VehicleTest(){
    facade = new VehicleFacade();
    
    this.initTest();
  }

  private void initTest(){
    // code of your former main goes here - with small changes:
    Van van = new Van();
    facade.addVehicle(van); // vehicle van is stored in facade

    // more fun

  
    System.out.println("Vehicles available for hiring: " + facade.getNumberOfAvailableVehicles()); 
    // you'll figure the method   "getNumberOfAvailableVehicles()" out...
  }
  
  public static void main(String[] args) {
    VehicleTest test = new VehicleTest(); // break out of static main function
  }

}





(我希望代码有效,我在答案框中写了它并没有验证它。简单的故障应该很容易由你来修复;))



这是 GoF [ ^ ],称为Facade Pattern [ ^ ]。这根本没什么大不了的,更多的是一个结构在一个类(通常也是一个接口)上提供想法来收集所有对象并将它们存储在一个目的地。



我可以推荐尽可能多地阅读GoF,这就是让你成为开发人员而不仅仅是程序员的原因。


玩得开心。



编辑:我希望你弄清楚如何打破静态主功能 - 我迈步转到构造函数并在构造函数之后执行其余的操作(根据定义,它应该不执行代码,因此我将测试推送到函数initTest()并让那里的乐趣发生)。



(I hope the code is valid, i wrote it in the answer box and did not validate it. Simple malfunctions should be easy to fix by you ;) )

This is one of the GoF[^], called the Facade Pattern[^]. It''s no big deal at all, more a structure giving idea on one class (often also an Interface) to gather all the objects and store them in one destination.

I can recommend to read as much about the GoF as possible, this is what makes you a developer and not only a programmer.

Have fun.

I hope you figure how one breaks the static main function - I step over to the constructor and do the rest after the constructor (which should by definition not execute code, so I pushed the test into the function initTest() and let the fun happen there).


简单的解决方案:



您的主要方法,即应用程序的入口点,需要搬出班级货车:

Simple Solution:

Your main method, which is the entry point for your app, needs to move out of the class van:
public class VehicleManagement(){

  public VehicleManagement(){
    this.ignition();
  }

  private void ignition(){
    // code of former main goes here
  }

  public static void main(String[] args) {
     VehicleManagement oManagement = new VehicleManagement(); // break out of static main function
  }
}





这样就留下了 Van - Vehilce ,而不是更多。

请按照我的建议为你的Van添加一些构造函数,因为这是常识:





This leaves the Van as what it is - a Vehilce, not more.
Please follow my advice and add some constructors to your Van, as this is common sense:

// 0 args constr. for basic init
public van(){
  this.setvRegNo("");
  this.setvBrand("");
}

//and one constructor with full set of values:
public van(String strRegNo, String strVBRand, /*add other values*/){
  this(); // call 0 args constr. always first
  this.setvRegNo(strRegNo);
  this.setvBrand(strVBrand);
  // rest of the fun
}







返回 VehicleManagement



要存放车辆,您需要一个像列表 [ ^ ]:




Back to VehicleManagement:

To store the Vehicles you''ll need a place like a List[^]:

private final List<vehicle> oList = new ArrayList<vehicle>();
private void ignition(){
  oList.add(new Van("REgNo1", "GM", /* more values*/);
}</vehicle></vehicle>





好​​的,现在所有的Vans和其他车辆都存储在该列表中。我们现在可以要求提供一些信息:



OK, now all Vans and other Vehicles are stored in that List. We can now ask for some information:

private int getNumberOfAvailableVehicles(){
  int iReturn = 0;
  for (Vehicle oVehicle: oList){ // loop through list
    try{ if(!oVehicle.getHired()) iReturn++; } // count
    catch(Exception oException){ return -1;} // something went wrong
  }
  return iReturn;
}





最后一些重构:

- 为什么所有变量都是以v开头?这个想法很好,你应该使用类型相关的前缀:str表示String,i表示整数。这就是所谓的匈牙利语通知 [ ^ ]。这有点过时了,但在大/复杂的应用程序中仍然有意义。

- 车辆的长度和高度应该是双倍值,而不是int。这更有意义。 (不是漂浮,我知道老师喜欢漂浮,但双人是最好的!)。



玩得开心。



Finally some refactoring:
- Why are all your variables starting with a "v" ? The idea is good, you should use a Type related prefix: "str" for String, "i" for integer. That''s called Hungarian Notiation[^]. It''s a bit out of style, but still makes sense in big/complicated apps.
- length and height of the Vehicle should be double values, not int. That makes more sense. (not float, I know that teachers like float, but double is the one to go for!).

have fun.


好的,这是圣诞节的时候 - 所以我们在这里用完整的解决方案



1.拿你现在的班级车辆和范 - 现在看起来很好。

新的构造器是好的(除了你错过了int intWheels设置在范,但这不会伤害应用程序,直到它被要求)



2.删除Van中的主要功能

Van将是我们正在使用的对象,但它不会执行代码。



3.在车辆和范旁添加另一个班级

车辆管理课程现在将成为我们乐趣的中心:

< br $> b $ b

OK, it''s Christmas time - so here we go with the complete solution:

1. Take your current classes "Vehicle" and "Van" - those are looking quite fine now.
the new constructors are good (beside of that you missed the "int intWheels" to be set in "Van", but that does not harm the app until it is requested)

2. Delete the main function in "Van"
"Van" will be an object we are working with, but it will not execute code.

3. add another class beside of "Vehicle" and "Van"
The class "VehicleManagement" will now be the center of our fun:


/**
 *  This will be your Center class.
 *  "Vehicle" and "Van" are just objects that you are working with. 
 *  The fun is in here! 
 */
public class VehicleManagement(){
  // List to hold the Vehicles (Vehicle is the abstract "mother". The List will hold any object that extends "Vehicle")
  ArrayList<vehicle> oVehicles = new ArrayList<vehicles>();

  // constructor - not much more to do than to pass on the ball.
  public VehicleManagement(){
    // most would use "init()". But that is often taken and as one most times does not want to 
    // override methods, I decided a long time ago to go with ignition();
    this.ignition(); 
  }
 
  private void ignition(){
    // Both "jobs" where outsourced into their own functions. Those functions return the result of their work.

    // load List with Vehicles
    // ArrayList can "swallow" a complete List of Objects - so let's just throw them in there:
    oVehicles.addAll(createVehicles());

    // now let's count the hired vehicles.
    // the function "getHiredVehicles()" will count for us and return the result:
    System.out.println("Number of hired Vehicles: " + getHiredVehicles());

   
  }

  private List<vehicles> createVehicles(){
    ArrayList<vehicles> oList = new ArrayList<vehicles>(); // this List only exists  locally in this method!!

    oList.add(new Van("PA-1000", "1968", "Chevy", false, 6, 20)); // some van, not hired
    oList.add(new Van("PA-1001", "1969", "Chevy", false, 6, 20)); // ...
    oList.add(new Van("PA-1002", "1970", "Chevy", false, 6, 20)); // ...
    oList.add(new Van("PA-1003", "1971", "Chevy", false, 6, 20)); // ...
    oList.add(new Van("PA-1004", "1972", "Chevy", false, 6, 20)); // ...
    oList.add(new Van("PA-1005", "1973", "Chevy", true,  6, 20)); // some van HIRED!
    oList.add(new Van("PA-1006", "1974", "Chevy", true,  6, 20)); // ...
    oList.add(new Van("PA-1007", "1975", "Chevy", true,  6, 20)); // ...
    oList.add(new Van("PA-1008", "1976", "Chevy", true,  6, 20)); // ...
    
    // ok, enough vans to test the code. Let's return the answer:
    return oList; 
  }

  private int getHiredVehicles(){
    // cause all beings beside of humans count from 0: 
    int iCountHired = 0;

    // to write the following if-statement like that is common.
    // easy to see what is expected for the if statement to work.

    // foreach-loop rushing through the List of Vehicles
    for(Vehicle oVehicle: oVehicles){ // check every Vehicle...
      if(true == oVehicle.getHired()){ // ...if it is hired...
        iCountHired++; // ...if so, then count...
      }
    } // done! 
    return iCountHired; // ...now return the result!
  }
  

  // It is common to place the main at the last position.
  // it's the static entry point for the JVM to run the application
  // one breaks out of the main as fast as possible
  // there is NEVER functional code in the main!

  public static void main(String[] args) {
     VehicleManagement oManagement = new VehicleManagement(); // break out of static main function

     // "new VehicleManagement();" would be enough in this example, 
     // cause there is nothing to do with a variable "oManagement"
  }
}





You should understand this basics, as they are common used throughout programming.



Good books do not need to be expensive.

This: Learning Java (Java Series) from o’’reilly[^] @ amazon.com would do the job just fine.

It’’s not up to date - and does not need to.

As you can see within the ratings it is well written and covers all topics for a beginning Java developer.

Get a new copy and do not just place it on the book shelf (every one owns such programming books that are hardly read...)



Books from o’’Reilly are always worth a recommendation.



You should understand this basics, as they are common used throughout programming.

Good books do not need to be expensive.
This: Learning Java (Java Series) from o''reilly[^] @ amazon.com would do the job just fine.
It''s not up to date - and does not need to.
As you can see within the ratings it is well written and covers all topics for a beginning Java developer.
Get a new copy and do not just place it on the book shelf (every one owns such programming books that are hardly read...)

Books from o''Reilly are always worth a recommendation.


这篇关于创建一个计算抽象类中数字的方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆