Advertisement
16225

4.2 ралица

Jun 16th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. package Kursovaa2;
  2. public class Main {
  3.     public static void main(String[] args) {
  4.  
  5.         Battery battery = new Battery("LItieva", 20, 20);
  6.         Screen screen = new Screen(4.7, 25000);
  7.         Phone nokiaN95 = new Phone("95", "Nokia", 25.5, battery, screen);
  8.         System.out.println(nokiaN95.toString());
  9.  
  10.     }
  11. }
  12. _______________________________________________
  13. package Kursovaa2;
  14. public class Battery {
  15.  
  16.     private String model;
  17.     private double idleTime;
  18.     private double hoursTalk;
  19.  
  20.     public Battery(String model, double idleTime, double hoursTalk) {
  21.         this.model = model;
  22.         this.idleTime = idleTime;
  23.         this.hoursTalk = hoursTalk;
  24.     }
  25.    @Override
  26.     public String toString() {
  27.         return String.format("Model: %s\n"
  28.                 + "Idle time: %.2f\n"
  29.                 + "Hours talk %.2f\n", this.model, this.idleTime, this.hoursTalk);
  30.     }
  31. }
  32. ______________________________________________________
  33. package Kursovaa2;
  34. public class Phone {
  35.  
  36.     private String model;
  37.     private String brand;
  38.     private double price;
  39.     private String owner;
  40.     private Battery battery;
  41.     private Screen screen;
  42.  
  43.     public Phone(String model, String brand, double price, Battery battery, Screen screen) {
  44.         this.model = model;
  45.         this.brand = brand;
  46.         this.price = price;
  47.         this.owner = null;
  48.         this.battery = battery;
  49.         this.screen = screen;
  50.     }
  51. ____________________________________________________
  52. public Phone(String model, String brand, double price, String owner, Battery battery, Screen screen) {
  53.         this.model = model;
  54.         this.brand = brand;
  55.         this.price = price;
  56.         this.owner = owner;
  57.         this.battery = battery;
  58.         this.screen = screen;
  59.     }
  60.  
  61.     @Override
  62.     public String toString() {
  63.         String result = String.format("%s %s\n"
  64.                 + "Price: %.2f\n", this.brand, this.model, this.price);
  65.         if (this.owner != null) {
  66.             result += String.format("Owner: %s", this.owner);
  67.         }
  68.         result += battery.toString();
  69.         result += screen.toString();
  70.         return result;
  71.     }
  72. }
  73. _______________________________________
  74. package Kursovaa2;
  75. public class Screen {
  76.  
  77.     private double size;
  78.     private int colors;
  79.  
  80.     public Screen(double size, int colors) {
  81.         this.size = size;
  82.         this.colors = colors;
  83.     }
  84.  
  85.     @Override
  86.     public String toString() {
  87.         return String.format("Size: %.2f\n"
  88.                 + "Colors: %d", this.size, this.colors);
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement