📄 j2pp(4-7).txt
字号:
public static void main( String[] args )
{
Pinto p = new Pinto();
System.out.println( "My car: " + p );
p.start();
if( p.isRunning() == false )
{
System.out.println( "Starter failed, let's roll start it!" );
p.rollStart();
}
System.out.println( "Current speed: " + p.getCurrentSpeed() +
", Hatchback open = " + p.isHatchBackDoorOpen() );
for( int i=0; i<20; i++ )
{
p.accelerate();
System.out.println( "Current speed: " + p.getCurrentSpeed() +
", Hatchback open = " + p.isHatchBackDoorOpen() );
}
while( p.getCurrentSpeed() > 0 )
{
p.decelerate();
System.out.println( "Current speed: " + p.getCurrentSpeed() +
", Hatchback open = " + p.isHatchBackDoorOpen() );
}
p.stop();
if( p.isHatchBackDoorOpen() )
{
System.out.println( "Have to close the hatchback!" );
p.closeHatchBack();
}
}
}
7.3
public class Car
{
// Transmission types
public static final int AUTOMATIC = 0;
public static final int TIPTRONIC = 1;
public static final int MANUAL = 2;
// Attributes
protected String typeOfCar;
protected int horsePower;
protected int maximumSpeed;
protected int numberOfDoors;
protected String paint;
protected int gasCapacity;
protected int oilCapacity;
protected int transmission;
// State attributes
protected boolean running = false;
protected int currentSpeed;
protected int currentGas;
protected int currentOil;
public Car( String typeOfCar,
int horsePower,
int maximumSpeed,
int numberOfDoors,
String paint,
int gasCapacity,
int oilCapacity,
int transmission )
{
this.typeOfCar = typeOfCar;
this.horsePower = horsePower;
this.maximumSpeed = maximumSpeed;
this.numberOfDoors = numberOfDoors;
this.paint = paint;
this.gasCapacity = gasCapacity;
this.oilCapacity = oilCapacity;
this.transmission = transmission;
}
public void start()
{
running = true;
}
public void stop()
{
running = false;
}
public boolean isRunning()
{
return running;
}
public void accelerate()
{
currentSpeed += 5;
}
public void decelerate()
{
currentSpeed -= 5;
}
public int getCurrentSpeed()
{
return currentSpeed;
}
public String toString()
{
return typeOfCar;
}
}
7.4
public class Porsche2 extends Car
{
// Attributes
private int turbos = 2;
private boolean nos;
// State attributes
private boolean turbo1Engaged = false;
private boolean turbo2Engaged = false;
private boolean nosEnabled = false;
public Porsche2()
{
super( "Porsche",
450,
220,
2,
"Yellow",
15,
5,
Car.TIPTRONIC );
}
public void accelerate()
{
// Check to see if we are running or not
if( running == false )
{
return;
}
// Create a variable representing how much we are going to
// accelerate this second
int increment = 15;
// Check the turbos; they add 5mph per second acceleration
if( turbo1Engaged )
{
increment += 5;
}
if( turbo2Engaged )
{
increment += 5;
}
// Check the NOS; it represents 15mph per second
if( nos )
{
increment += 15;
}
// Increment the current speed
currentSpeed += increment;
if( currentSpeed > maximumSpeed ) currentSpeed = maximumSpeed;
}
public void decelerate()
{
currentSpeed -= 20;
if( currentSpeed < 0 ) currentSpeed = 0;
}
public void engageTurbos()
{
turbo1Engaged = true;
turbo2Engaged = true;
}
public void disengageTurbos()
{
turbo1Engaged = false;
turbo2Engaged = false;
}
public void engageNOS()
{
nos = true;
maximumSpeed += 50;
}
public void disengageNOS()
{
nos = false;
maximumSpeed -= 50;
}
public String toString()
{
return "A shiny new " + paint + " Porsche!";
}
public static void main( String[] args )
{
Porsche2 p = new Porsche2();
System.out.println( "My new car: " + p );
p.start();
System.out.println( "Current speed: " + p.getCurrentSpeed() );
for( int i=0; i<20; i++ )
{
if( i == 5 )
{
p.engageTurbos();
}
else if( i == 14 )
{
p.engageNOS();
}
p.accelerate();
System.out.println( "Current speed: " + p.getCurrentSpeed() );
}
p.disengageNOS();
p.disengageTurbos();
while( p.getCurrentSpeed() > 0 )
{
p.decelerate();
System.out.println( "Current speed: " + p.getCurrentSpeed() );
}
p.stop();
}
}
7.5
public class Pinto2 extends Car
{
// State attributes
public boolean hatchBackDoorOpen = false;
public Pinto2()
{
super( "Pinto",
50,
60,
5,
"two-tone",
15,
5,
Car.MANUAL );
}
public void accelerate()
{
// Check to see if we are running or not
if( running == false )
{
return;
}
currentSpeed += 4;
if( currentSpeed > maximumSpeed )
{
currentSpeed = maximumSpeed;
// The high speed knocked the door open!
openHatchBack();
}
}
public void decelerate()
{
currentSpeed -= 5;
if( currentSpeed < 0 ) currentSpeed = 0;
}
public void rollStart()
{
running = true;
}
public void pushUpHill()
{
System.out.println( "Ouch, this thing is heavy!" );
}
public void openHatchBack()
{
hatchBackDoorOpen = true;
}
public void closeHatchBack()
{
hatchBackDoorOpen = false;
}
public boolean isHatchBackDoorOpen()
{
return hatchBackDoorOpen;
}
public static void main( String[] args )
{
Pinto2 p = new Pinto2();
System.out.println( "My car: " + p );
p.start();
if( p.isRunning() == false )
{
System.out.println( "Starter failed, let's roll start it!" );
p.rollStart();
}
System.out.println( "Current speed: " + p.getCurrentSpeed() +
", Hatchback open = " + p.isHatchBackDoorOpen() );
for( int i=0; i<20; i++ )
{
p.accelerate();
System.out.println( "Current speed: " + p.getCurrentSpeed() +
", Hatchback open = " + p.isHatchBackDoorOpen() );
}
while( p.getCurrentSpeed() > 0 )
{
p.decelerate();
System.out.println( "Current speed: " + p.getCurrentSpeed() +
", Hatchback open = " + p.isHatchBackDoorOpen() );
}
p.stop();
if( p.isHatchBackDoorOpen() )
{
System.out.println( "Have to close the hatchback!" );
p.closeHatchBack();
}
}
}
7.6
public abstract class Car2
{
// Transmission types
public static final int AUTOMATIC = 0;
public static final int TIPTRONIC = 1;
public static final int MANUAL = 2;
// Attributes
protected String typeOfCar;
protected int horsePower;
protected int maximumSpeed;
protected int numberOfDoors;
protected String paint;
protected int gasCapacity;
protected int oilCapacity;
protected int transmission;
// State attributes
protected boolean running = false;
protected int currentSpeed;
protected int currentGas;
protected int currentOil;
public Car2( String typeOfCar,
int horsePower,
int maximumSpeed,
int numberOfDoors,
String paint,
int gasCapacity,
int oilCapacity,
int transmission )
{
this.typeOfCar = typeOfCar;
this.horsePower = horsePower;
this.maximumSpeed = maximumSpeed;
this.numberOfDoors = numberOfDoors;
this.paint = paint;
this.gasCapacity = gasCapacity;
this.oilCapacity = oilCapacity;
this.transmission = transmission;
}
public void start()
{
running = true;
}
public void stop()
{
running = false;
}
public boolean isRunning()
{
return running;
}
public abstract void accelerate();
public abstract void decelerate();
public int getCurrentSpeed()
{
return currentSpeed;
}
public String toString()
{
return typeOfCar;
}
}
7.7
public class BadCar extends Car2
{
}
7.8
public class Driver1
{
public void drivePorsche( Porsche2 p )
{
System.out.println( "Driving: " + p );
p.start();
for( int i=0; i<10; i++ )
{
p.accelerate();
System.out.println( "Current speed: " + p.getCurrentSpeed() );
}
for( int i=0; i<5; i++ )
{
p.decelerate();
System.out.println( "Current speed: " + p.getCurrentSpeed() );
}
p.stop();
}
public void drivePinto( Pinto2 p )
{
System.out.println( "Driving: " + p );
p.start();
for( int i=0; i<10; i++ )
{
p.accelerate();
p.rollStart();
System.out.println( "Current speed: " + p.getCurrentSpeed() );
}
for( int i=0; i<5; i++ )
{
p.decelerate();
System.out.println( "Current speed: " + p.getCurrentSpeed() );
}
p.stop();
}
public static void main( String[] args )
{
Driver1 d = new Driver1();
Porsche2 porsche = new Porsche2();
d.drivePorsche( porsche );
Pinto2 pinto = new Pinto2();
d.drivePinto( pinto );
}
}
7.9
public class Driver2
{
public void drive( Car c )
{
System.out.println( "Driving: " + c );
c.start();
for( int i=0; i<10; i++ )
{
c.accelerate();
System.out.println( "Current speed: " + c.getCurrentSpeed() );
}
for( int i=0; i<5; i++ )
{
c.decelerate();
System.out.println( "Current speed: " + c.getCurrentSpeed() );
}
c.stop();
}
public static void main( String[] args )
{
Driver2 d = new Driver2();
Porsche2 porsche = new Porsche2();
d.drive( porsche );
Pinto2 pinto = new Pinto2();
d.drive( pinto );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -