Untitled
unknown
java
2 years ago
2.6 kB
2
Indexable
Never
package com.company; import javax.sound.midi.Soundbank; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Scanner; class Monster{ int weight; int scareCount=0; Monster() { } int eat(){ System.out.println("weight is increased"); return 1; } void scare(Human human1){ System.out.println("Booo"); } } class CookieMonster extends Monster{ int weight; int sc; CookieMonster(int weight) { this.weight=weight; sc=super.scareCount; } int eat() { return weight+1; } void scare(Class Human) { Field obj[] = Human.getDeclaredFields(); if(obj[3].equals("not brave\n")) { System.out.println("scream"); sc++; } else System.out.println("Can't scream to Brave\n"); } } class ScaryMonster extends Monster{ int weight; int sc; ScaryMonster(int weight) { sc=super.scareCount; this.weight=weight; } int eat() { return weight+1; } void scare(Class Human) throws NoSuchMethodException { Method obj = Human.getDeclaredMethod("isIntelligent", null); System.out.println("scream"); sc++; } } interface Animal{ boolean isIntelligent(); void expectedHeight(); } class Human implements Animal{ String intelligence = null; String bravery = null; String name ; int weight; Human(String name, int weight, String inte, String bravery) { this.name = name; this.weight = weight; this.intelligence=inte; this.bravery=bravery; } @Override public boolean isIntelligent() { if(intelligence=="high") return true; else return false; } @Override public void expectedHeight() { System.out.println("Expected Height: "+ (weight/2)); } } public class Main { public static void main(String []args) { Human human1 = new Human("jan",70, "medium", "brave"); Human human2 = new Human("san",100, "high", "not brave"); CookieMonster coo = new CookieMonster(100); coo.scare(human1); coo.scare(human2); coo.eat(); System.out.println(coo.weight+" "+coo.sc); ScaryMonster doggo= new ScaryMonster(500); doggo.eat(); doggo.eat(); System.out.println(doggo.weight +" "+ doggo.sc); } }