HarrJ

Day 12 oop 1

Nov 21st, 2023 (edited)
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. package week2;
  2.  
  3. public class Day12B {
  4.     public static void main(String[] args) {
  5.         //gagawa muna object
  6.         Dog callDog = new Dog();
  7.         callDog.eat();
  8.         System.out.println("---------------");
  9.         callDog.useMethods();
  10.        
  11.         ______ call______ = new ______();
  12.         call______.useMethods();
  13.     }
  14. }
  15.  
  16. //you can make multiple classes in a single file,
  17. // but make sure only the namesake(Main class) is the only one public
  18. class Animal {
  19.     String animalName = "Animal";
  20.    
  21.     void eat() {
  22.         System.out.println("The animal is eating");
  23.     }
  24.    
  25.     void sleep() {
  26.         System.out.println("The animal is sleeping");
  27.     }
  28. }
  29.  
  30. class Dog extends Animal {
  31.     void info() {
  32.         System.out.println("Dogs are mans best friend.");
  33.     }
  34.    
  35.     void useMethods() {
  36.         info();
  37.         eat();
  38.         sleep();
  39.     }
  40. }
  41.  
  42. class ______ extends Animal {
  43.     void info() {
  44.         System.out.println("______");
  45.     }
  46.    
  47.     void useMethods() {
  48.         info();
  49.         eat();
  50.         sleep();
  51.     }
  52. }
  53.  
  54. //------------------------------------
  55.  
  56. public class Day12F {
  57.     public static void main(String[] args) {
  58.         EncapTest callEncap = new EncapTest();
  59.         callEncap.setNum1(40);
  60.         callEncap.setNum2(22);
  61.         callEncap.setOp("-");
  62.         callEncap.getPrintout();
  63.     }
  64. }
  65.  
  66. class EncapTest {
  67.     private double num1, num2;
  68.     private String op, printout;
  69.    
  70.     public void setNum1(double num1) {
  71.         this.num1 = num1;
  72.     }
  73.  
  74.     public void setNum2(double num2) {
  75.         this.num2 = num2;
  76.     }
  77.  
  78.     public void setOp(String op) {
  79.         this.op = op;
  80.     }
  81.  
  82.     public String getPrintout() {
  83.         compute();
  84.         return printout;
  85.     }
  86.    
  87.     private void compute(){
  88.         double result = 0;
  89.         boolean computeSuccess = true;
  90.         switch (op) {
  91.             case "+":
  92.                 result = num1 + num2;
  93.                 break;
  94.             case "-":
  95.                 result = num1 - num2;
  96.                 break;
  97.             case "/":
  98.                 if (num2 > 0) {
  99.                     result = num1 / num2;
  100.                 } else {
  101.                     printout = "Cannot divide by zero";
  102.                     computeSuccess = false;
  103.                 }
  104.                 break;
  105.             default:
  106.                 printout = "Operator not on list";
  107.                 computeSuccess = false;
  108.         }
  109.         if (computeSuccess) {
  110.             printout = String.format("%.2f %s %.2f = %.2f"
  111.                 , num1, op, num2, result);
  112.         }
  113.     }
  114. }
Add Comment
Please, Sign In to add comment