interface HumanMemoryI { int MINIMUM_SIZE = 1000; String fetch (int address); void store (int address, String information); }A human memory must contain at least 1000 locations; the fetch and store operations are guaranteed to work correctly when invoked with these locations. We know that humans have much bigger memories. Hence, it is possible to invoke the fetch and store operations with locations greater than or equal to 1000. We also know that humans generally pretend to know more than they actually do. Hence, a human memory will never admit to failure by throwing an exception. Instead, when invoked with a location greater than or equal to 1000 or even with a location less than 0, a human memory might work correctly or it might make something up. Write a class HumanMemory that implements the interface HumanMemoryI following the guidelines in the above paragraph.
class HumanMemory implements HumanMemoryI { private String[] info; HumanMemory () { info = new String[MINIMUM_SIZE]; for (int i=0; i < MINIMUM_SIZE; i++) info[i] = ""; } public String fetch (int address) { try { return info[address]; } catch (ArrayIndexOutOfBoundsException e) { return "abc"; } } public void store (int address, String s) { try { info[address] = s; } catch (ArrayIndexOutOfBoundsException e) { } } }
interface Investor { // Returns the amount of money that the investor has. double getAssets (); // Increases the amount of money by (getAssets() * apr / 100). void invest (double apr); // Decreases the amount of money by (getAssets() * rate / 100). void paytaxes (double rate); } interface Charitable { // Returns the amount of money that the charitable person has. double getAssets (); // Decreases the amount of money by (amount). void donate (int amount); // Decreases the amount of money by (getAssets() * rate / 100). void paytaxes (double rate); }The first interface describes persons that are investors; the second interface describes persons that are charitable.
interface CharitableInvestor extends Charitable, Investor {}or
interface CharitableInvestor { int getAssets (); void invest (double apr); void donate (int amount); void paytaxes (double rate); }or
interface CharitableInvestor extends Charitable { void invest (double apr); }or
interface CharitableInvestor extends Investor { void donate (int amount); }
class Person implements CharitableInvestor { private int assets; Person () { assets = 1000; } public int getAssets () { return assets; } private void checkAssets () { if (assets < 0) assets = 0; } public void invest (double apr) { assets += assets * apr / 100; checkAssets(); } public void donate (int amount) { assets -= amount; checkAssets(); } public void paytaxes (double rate) { assets -= assets * rate / 100; checkAssets(); } }
double getMax (StreamTokenizer st) throws IOException { double current_max = 0.0; while (st.nextToken () != st.TT_EOF) { if (st.ttype == st.TT_NUMBER) { double newnum = st.nval; current_max = current_max > newnum ? current_max : newnum; } } return current_max; }
abstract class A {} class B extends A {} class C extends A {} class D extends Exception {} class E extends D {} class F extends D {} class G extends Exception {} class Simple { public static void main (String[] args) throws G { A x = new B(); m1 (x); } static void m1 (A x) throws G { try { m2(x); m3(0); m4(); } catch (D y) { m5(y); } } static void m2 (A x) { if (x instanceof B) System.out.println("B"); else if (x instanceof C) System.out.println("C"); else System.out.println("A"); } static void m3 (int i) throws D { if (i > 0) System.out.println("Positive"); else if (i < 0) System.out.println("Negative"); else throw new E(); } static void m4 () { System.out.println ("M4"); } static void m5 (D y) throws G { if (y instanceof E) System.out.println("E"); else if (y instanceof F) System.out.println("F"); else throw new G(); } }
B E
class A { int x, y; A () { x=1; y=2; } int getx () { return x; } } class B extends A { int x, z; B () { super(); x=3; y=4; z=5; } int getx () { return x; } int getz () { return z; } } class Prob1 { public static void main (String[] args) { A o1 = new A(); A o2 = new B(); B o3 = new B(); System.out.println(o1.x); // ------------------------------ System.out.println(o1.getx()); // ------------------------------ System.out.println(o1.y); // ------------------------------ System.out.println(o1.getz()); // ------------------------------ System.out.println(o2.x); // ------------------------------ System.out.println(o2.getx()); // ------------------------------ System.out.println(o2.y); // ------------------------------ System.out.println(o2.getz()); // ------------------------------ System.out.println(o3.x); // ------------------------------ System.out.println(o3.getx()); // ------------------------------ System.out.println(o3.y); // ------------------------------ System.out.println(o3.getz()); // ------------------------------ } }
class A { int x, y; A () { x=1; y=2; } int getx () { return x; } } class B extends A { int x, z; B () { super(); x=3; y=4; z=5; } int getx () { return x; } int getz () { return z; } } class Prob1 { public static void main (String[] args) { A o1 = new A(); A o2 = new B(); B o3 = new B(); System.out.println(o1.x); // ---1 System.out.println(o1.getx()); // ---1 System.out.println(o1.y); // ---2 System.out.println(o1.getz()); // ---Does not compile System.out.println(o2.x); // ---1 System.out.println(o2.getx()); // ---3 System.out.println(o2.y); // ---4 System.out.println(o2.getz()); // ---Does not compile System.out.println(o3.x); // ---3 System.out.println(o3.getx()); // ---3 System.out.println(o3.y); // ---4 System.out.println(o3.getz()); // ---5 } }
sabry@cs.uoregon.edu