Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package week2;
- public class Day12B {
- public static void main(String[] args) {
- //gagawa muna object
- Dog callDog = new Dog();
- callDog.eat();
- System.out.println("---------------");
- callDog.useMethods();
- ______ call______ = new ______();
- call______.useMethods();
- }
- }
- //you can make multiple classes in a single file,
- // but make sure only the namesake(Main class) is the only one public
- class Animal {
- String animalName = "Animal";
- void eat() {
- System.out.println("The animal is eating");
- }
- void sleep() {
- System.out.println("The animal is sleeping");
- }
- }
- class Dog extends Animal {
- void info() {
- System.out.println("Dogs are mans best friend.");
- }
- void useMethods() {
- info();
- eat();
- sleep();
- }
- }
- class ______ extends Animal {
- void info() {
- System.out.println("______");
- }
- void useMethods() {
- info();
- eat();
- sleep();
- }
- }
- //------------------------------------
- public class Day12F {
- public static void main(String[] args) {
- EncapTest callEncap = new EncapTest();
- callEncap.setNum1(40);
- callEncap.setNum2(22);
- callEncap.setOp("-");
- callEncap.getPrintout();
- }
- }
- class EncapTest {
- private double num1, num2;
- private String op, printout;
- public void setNum1(double num1) {
- this.num1 = num1;
- }
- public void setNum2(double num2) {
- this.num2 = num2;
- }
- public void setOp(String op) {
- this.op = op;
- }
- public String getPrintout() {
- compute();
- return printout;
- }
- private void compute(){
- double result = 0;
- boolean computeSuccess = true;
- switch (op) {
- case "+":
- result = num1 + num2;
- break;
- case "-":
- result = num1 - num2;
- break;
- case "/":
- if (num2 > 0) {
- result = num1 / num2;
- } else {
- printout = "Cannot divide by zero";
- computeSuccess = false;
- }
- break;
- default:
- printout = "Operator not on list";
- computeSuccess = false;
- }
- if (computeSuccess) {
- printout = String.format("%.2f %s %.2f = %.2f"
- , num1, op, num2, result);
- }
- }
- }
Add Comment
Please, Sign In to add comment