Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.ArrayList;
- public class Test {
- public static void main(String[] args){
- Car test = new Car();
- Trailer t = new Trailer(80,60);
- RoadTrain r = new RoadTrain(60,45);
- ArrayList<Car> cars = new ArrayList<>();
- cars.add(t);
- cars.add(r);
- System.out.println("Max speed: " + test.maxSpeed(t,r));
- System.out.println("Min radius: " + test.minRadius(t,r));
- try{
- Test.save(cars);
- } catch (IOException e) {}
- try{
- Test.read(cars);
- } catch (IOException e){
- } catch (ClassNotFoundException e) {}
- }
- public static void save(ArrayList<Car> cars) throws IOException{
- FileOutputStream fos = new FileOutputStream("temp.out");
- ObjectOutputStream oos = new ObjectOutputStream(fos);
- for(Car car : cars){
- oos.writeObject(car);
- oos.flush();
- }
- oos.close();
- }
- public static void read(ArrayList<Car> cars) throws IOException, ClassNotFoundException {
- FileInputStream fis = new FileInputStream("temp.out");
- ObjectInputStream oin = new ObjectInputStream(fis);
- for(Car car : cars){
- Car c = (Car) oin.readObject();
- cars.add(c);
- }
- }
- public static class Trailer extends Car implements Serializable{
- public Trailer(int a, int b){
- this.setSpeed(a);
- this.setRadius(b);
- }
- }
- public static class RoadTrain extends Car implements Serializable{
- public RoadTrain(int a, int b){
- this.setSpeed(a);
- this.setRadius(b);
- }
- }
- public static class Car{
- private int permissible_speed = 0;
- private int minimum_turning_radius = 0;
- public int getSpeed(){
- return permissible_speed;
- }
- public int getRadius(){
- return minimum_turning_radius;
- }
- public void setSpeed(int s){
- if(s > 0 ) this.permissible_speed = s;
- else System.out.println("Error");
- }
- public void setRadius(int r){
- if(r > 0 && r < 180) this.minimum_turning_radius = r;
- else System.out.println("Error");
- }
- public int maxSpeed(Car a, Car b){
- if (a.getSpeed() > b.getSpeed()) return a.getSpeed();
- else return b.getSpeed();
- }
- public int minRadius(Car a, Car b){
- if (a.getRadius() > b.getRadius()) return b.getRadius();
- else return a.getRadius();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement