Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3.  
  4. public class Test {
  5.     public static void main(String[] args){
  6.         Car test = new Car();
  7.         Trailer t = new Trailer(80,60);
  8.         RoadTrain r = new RoadTrain(60,45);
  9.  
  10.         ArrayList<Car> cars = new ArrayList<>();
  11.         cars.add(t);
  12.         cars.add(r);
  13.  
  14.         System.out.println("Max speed: " + test.maxSpeed(t,r));
  15.         System.out.println("Min radius: " + test.minRadius(t,r));
  16.         try{
  17.             Test.save(cars);
  18.         } catch (IOException e) {}
  19.         try{
  20.             Test.read(cars);
  21.         } catch (IOException e){
  22.  
  23.         } catch (ClassNotFoundException e) {}
  24.     }
  25.  
  26.     public static void save(ArrayList<Car> cars) throws IOException{
  27.         FileOutputStream fos = new FileOutputStream("temp.out");
  28.         ObjectOutputStream oos = new ObjectOutputStream(fos);
  29.         for(Car car : cars){
  30.             oos.writeObject(car);
  31.             oos.flush();
  32.         }
  33.         oos.close();
  34.     }
  35.  
  36.     public static void read(ArrayList<Car> cars) throws IOException, ClassNotFoundException {
  37.         FileInputStream fis = new FileInputStream("temp.out");
  38.         ObjectInputStream oin = new ObjectInputStream(fis);
  39.         for(Car car : cars){
  40.             Car c = (Car) oin.readObject();
  41.             cars.add(c);
  42.         }
  43.     }
  44.  
  45.     public static class Trailer extends Car implements Serializable{
  46.             public Trailer(int a, int b){
  47.                 this.setSpeed(a);
  48.                 this.setRadius(b);
  49.             }
  50.         }
  51.  
  52.     public static class RoadTrain extends Car implements Serializable{
  53.             public RoadTrain(int a, int b){
  54.                 this.setSpeed(a);
  55.                 this.setRadius(b);
  56.             }
  57.         }
  58.  
  59.     public static class Car{
  60.         private int permissible_speed = 0;
  61.         private int minimum_turning_radius = 0;
  62.  
  63.         public int getSpeed(){
  64.             return permissible_speed;
  65.         }
  66.         public int getRadius(){
  67.             return minimum_turning_radius;
  68.         }
  69.         public void setSpeed(int s){
  70.             if(s > 0 ) this.permissible_speed = s;
  71.             else System.out.println("Error");
  72.         }
  73.         public void setRadius(int r){
  74.             if(r > 0 && r < 180) this.minimum_turning_radius = r;
  75.             else System.out.println("Error");
  76.         }
  77.         public int maxSpeed(Car a, Car b){
  78.             if (a.getSpeed() > b.getSpeed()) return a.getSpeed();
  79.             else return b.getSpeed();
  80.         }
  81.         public int minRadius(Car a, Car b){
  82.             if (a.getRadius() > b.getRadius()) return b.getRadius();
  83.             else return a.getRadius();
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement