Last active
August 29, 2015 13:57
-
-
Save firejox/9660290 to your computer and use it in GitHub Desktop.
A4 Work
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ce1002.a4.s101201046; | |
import java.util.Scanner; | |
public class A4 { | |
public static void main(String[] args) { | |
Car[] cars; | |
int i; | |
int num; | |
Scanner scan = new Scanner(System.in); | |
do { | |
System.out.println("Please input the number of cars: "); | |
num = scan.nextInt(); | |
if (num <= 0) | |
System.out.println("Out of range!"); | |
} while(num <= 0); | |
cars = new Car[num]; | |
for (i = 0; i < num; i++) { | |
cars[i] = new Car(); | |
System.out.println("Input car's"); | |
System.out.print("Brand: "); | |
cars[i].set_brand(scan.next()); | |
System.out.print("Name: "); | |
cars[i].set_name(scan.next()); | |
System.out.print("Max Speed: "); | |
cars[i].set_max_speed(scan.nextFloat()); | |
System.out.print("Fuel consumption: "); | |
cars[i].set_fuel_consumption(scan.nextFloat()); | |
System.out.print("Price: "); | |
cars[i].set_price(scan.nextFloat()); | |
System.out.print("\n"); | |
} | |
System.out.print("Output car status\n"); | |
for (i = 0; i < num; i++) { | |
System.out.print("Car brand is " + cars[i].get_brand() + ".\n" + | |
"Car name is " + cars[i].get_name() + ".\n" + | |
"Car max speed is " + cars[i].get_max_speed() + ".\n" + | |
"Car fuel consumption is " + cars[i].get_fuel_consumption() + ".\n" + | |
"Car price is " + cars[i].get_price() + ".\n"); | |
System.out.print("StartTurbo!\n"); | |
cars[i].start_turbo(); | |
System.out.print("Turbo status is " + cars[i].isTurbo() + "\n" + | |
"Current speed is " + cars[i].CurrentSpeed(cars[i].isTurbo()) + "\n" + ((i == num-1) ? "": "\n")); | |
} | |
scan.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ce1002.a4.s101201046; | |
public class Car extends Vehicle { | |
private boolean isTurbo; | |
public void start_turbo() { | |
isTurbo = (java.lang.Math.random() > 0.5); | |
} | |
public boolean isTurbo() { | |
return isTurbo; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ce1002.a4.s101201046; | |
public class Vehicle { | |
private String brand; | |
private String name; | |
private float maxSpeed; | |
private float fuelConsumption; | |
private float price; | |
public void set_brand(String _brand) { | |
brand = _brand; | |
} | |
public void set_name(String _name) { | |
name = _name; | |
} | |
public void set_max_speed(float _maxSpeed) { | |
maxSpeed = _maxSpeed; | |
} | |
public void set_fuel_consumption(float _fuelConsumption) { | |
fuelConsumption = _fuelConsumption; | |
} | |
public void set_price(float _price) { | |
price = _price; | |
} | |
public String get_brand() { | |
return brand; | |
} | |
public String get_name() { | |
return name; | |
} | |
public float get_max_speed() { | |
return maxSpeed; | |
} | |
public float get_fuel_consumption() { | |
return fuelConsumption; | |
} | |
public float get_price() { | |
return price; | |
} | |
public float CurrentSpeed() { | |
return maxSpeed * (float)java.lang.Math.random(); | |
} | |
public float CurrentSpeed(boolean isTurbo) { | |
return (isTurbo) ? (maxSpeed) : (maxSpeed * (float)java.lang.Math.random()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
return (isTurbo) ? (maxSpeed) : (maxSpeed * (float)java.lang.Math.random());
return (isTurbo) ? (maxSpeed) : CurrentSpeed();