Advertisement
16112

Курсова Работа 2 - 6

May 14th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class LKM {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.         String name;
  8.         double maxLoad;
  9.         double litters;
  10.         double regularGasMileage = 0;
  11.         System.out.printf("Enter the brand of the car: ");
  12.         name = sc.nextLine();
  13.         System.out.printf("Enter how big is your car's fueltank(liters): ");
  14.         maxLoad = sc.nextDouble();
  15.         Car c1 = new Car(name, maxLoad);
  16.         System.out.printf("Enter how many liters you want to load: ");
  17.         litters = sc.nextDouble();
  18.         System.out.println();
  19.         Litters lit1 = new Litters(litters);
  20.         c1.add(lit1);
  21.         System.out.println("What is your car's gas mileage on regular gasoline (liters/100km) ");
  22.         regularGasMileage = sc.nextDouble();
  23.         double maxDistance = (litters / regularGasMileage) * 100;
  24.         System.out.printf("You will be able to pass " + "%.0f", maxDistance);
  25.         System.out.print(" km!");
  26.     }
  27. }
  28. --------------------------------------------------------------------
  29. import java.util.ArrayList;
  30.  
  31. public class Car {
  32.     private String name;
  33.     private double maxLoad;
  34.     private ArrayList<Litters> cargo;
  35.  
  36.     public Car(String name, double maxLoad) {
  37.         this.name = name;
  38.         this.maxLoad = maxLoad;
  39.         this.cargo = new ArrayList<Litters>();
  40.     }
  41.  
  42.     public void add(Litters litri) {
  43.         if (litri.getLiters() > this.maxLoad) {
  44.             System.out.println(this.name + " can't load " + litri.getLiters() + " liters");
  45.         } else {
  46.             System.out.println(this.name + " loaded " + litri.getLiters() + " liters");
  47.             this.cargo.add(litri);
  48.         }
  49.     }
  50. }
  51. --------------------------------------------------------------------
  52. public class Litters {
  53.     private double litters;
  54.  
  55.     public Litters(double litters) {
  56.         this.litters = litters;
  57.     }
  58.  
  59.     public double getLiters() {
  60.         return litters;
  61.     }
  62.  
  63.     public void setLiters(double litters) {
  64.         this.litters = litters;
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement