Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Java Inheritance 
Write the code for a new class, Hybrid which extends the Car class we created earlier.  This new class 
should include one instance variable, percentCharged, and methods to modify (or set) and access (or 
get) the value of percentCharged. 
public class Hybrid extends Car 
{ 
  protected double percentCharged; 
 
  public Hybrid (double miles, String color, double inputCharge) 
  { 
    super(miles,color); 
    this.percentCharged = inputCharge; 
  } 
 
  public void setCharge (double inputCharge) 
  { 
    this.percentCharged = inputCharge; 
  } 
 
  public double getCharge ( ) 
  { 
    return percentCharged; 
  } 
 
  public String toString ( ) 
  { 
     return "the miles are: " + this.miles + " the color is: " + 
this.color + " and the charge i:s " + percentCharged; 
  } 
} 
Let’s create a new test driver, TDHybrid, inspired by our previous TDCar, which will test the new Hybrid 
class: 
import java.util.*;   // needed for the Scanner class 
public class TDHybrid  
// this will be saved in a file called TDHybrid.java 
{ 
  public static void main (String[] args) 
  { 
    Car toyota = new Car(22000, "red"); 
    System.out.println( toyota.getMiles() + " " + toyota.getColor() ); 
    Car ford = new Car(); 
    System.out.println( ford );  // this uses the toString method 
    ford.changeMiles(1300); 
    System.out.println( ford );  // this uses the toString method 
 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Enter the number of cars to declare and 
instantiate"); 
    int n = keyboard.nextInt(); 
    Car[] fleet = new Car[n]; 
    for (int i=0; i