Advertisement
16112

Constructors

Feb 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Program {
  4.  
  5.     public static void main(String[] args) throws Exception {
  6.  
  7.         Scanner sc = new Scanner (System.in);
  8.         SmartphoneBattery Battery = new SmartphoneBattery();
  9.         System.out.print("What is the type of battery: ");
  10.         Battery.type = sc.nextLine();
  11.         System.out.print("What is the capacity of the battery (mAh): ");
  12.         Battery.capacity = Double.parseDouble(sc.nextLine());
  13.         System.out.print("Adding quantity: ");
  14.         Battery.quantity = Double.parseDouble(sc.nextLine());
  15.        
  16.         System.out.println();
  17.        
  18.         Battery.printInfo();   
  19.     }
  20. }
  21.  
  22.  
  23.  
  24.  
  25.  
  26. public class SmartphoneBattery {
  27.     String type;
  28.     double capacity;
  29.     double quantity;
  30.  
  31.    
  32.      void printInfo() { System.out.println("Battery type: " + this.type);
  33.      System.out.printf("Battery capacity: %.0f\n" ,this.capacity);
  34.      System.out.printf("Adding quantity: %.0f", this.quantity); }
  35.      
  36.  
  37.     void charge(double mAh) throws Exception {
  38.         if ((mAh + this.quantity) > this.capacity) {
  39.             throw new Exception();
  40.         } else {
  41.             this.quantity += mAh;
  42.         }
  43.     }
  44.  
  45.     public SmartphoneBattery() {
  46.         this.type = "li-ion";
  47.         this.capacity = 3500;
  48.         this.quantity = 3000;
  49.     }
  50.  
  51.     public SmartphoneBattery(String type, double capacity, double quantity) {
  52.         this.type = type;
  53.         this.capacity = capacity;
  54.         this.quantity = quantity;
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement