The assignments for this Chapter provide some practice with interfaces and inheritance. A third design option, abstract classes, are not covered. Java only supports single inheritance of classes but supports multiple inheritance of interfaces.
An interface can define only constants, method signatures, default methods, and static methods. Method bodies can exist only for default methods and static methods, which were features added in Java 8. Default and static interface methods provide the ability to share some implementation details across multiple implementing classes. Further, default and static methods can be utilized to enhance an interface without "breaking" its contract with existing users.
Class inheritance typically implements an "is-a" relationship. For example, a Carrot class would implement a Nutrition interface because a carrot is not nutrition. On the other hand, a Carrot is-a RootVegetable and so class inheritance would be the design choice.
The java.awt.Rectangle class of the standard Java library does not supply a method to compute the area or perimeter of a rectangle. Provide a subclass BetterRectangle of the Rectangle class that has getPerimeter() and getArea() methods. Do not add any instance variables. Code a separate program that tests the two methods that you supplied.
Implement a class Item that stores items represented as a string and an integer quantity. Include compareTo, add a count, and toString methods.
public void add(String itemName) public void add(String itemName, int count)//searches for duplicate item//return null if not foundpublic Item find(String name) public String toString()
Implement a Bag class that stores Items in an ArrayList<Item>. Items can be added repeatedly but should only be stored once. Supply methods for adding to a Bag, and to search for a duplicate item.The toString method of the Bag class should generate a sorted list of item-name/count pairs. Test your code by creating and adding seven Items with several duplicates.
Example:
Bag b = new Bag();
b.add(“candy”); b.add(“apple”); b.add(“pizza”); b.add(“candy”, 4);
b.add(“bread”); b.add(“apple”); b.add(“cola”);
System.out.println(b);
-----------------------------
[apple/2, bread/1, candy/5, cola/1, pizza/1]
In this problem, you will model a circuit consisting of a combination of resistors in series or parallel. Implement an interface Circuit with an method double getResistance(). Provide a Resistor class representing a single resistor. Implement Serial and Parallel classes, each of which contains an ArrayList of Circuits. A Serial circuit models a series of Circuits, each of which can be multiple resistors and/or other circuits. Similarly, a Parallel circuit models a set of circuits in parallel. Use Ohm’s law to compute the combined resistance. Include a test program and output. The equivalent resistance for a series of resistors is the sum of the resistances, i.e. 5 + 5 = 10 ohms. For a parallel circuit, the equivalent resistance is the reciprocal of the sum of the reciprocals of the resistances, i.e. 5 + 5 = 1/( 1/5 + 1/5) = 5/2 = 2.5 ohms.
Resistor resist = new Resistor(50);
System.out.println(resist);
System.out.println(resist.getResistance());
Serial serialResist = new Serial();
serialResist.add(resist);
serialResist.add(resist);
System.out.println(serialResist);
Parallel parallelResist = new Parallel();
parallelResist.add(serialResist);
parallelResist.add(resist);
System.out.println(parallelResist);
Output
50
50
100
33.33333333
Implement and test a Fraction class with the following methods:
int num, denom; //0/n is zero
//a 0 denom freezes any fraction it touches
public Fraction(int numerator, int denominator)
public Fraction add(Fraction f)
public Fraction add(int whole, int num, int denom)
public Fraction multiply(Fraction f)
public Fraction multiply(int whole, int num, int denom)
public Fraction reciprocal()
public double toDouble()
public int compareTo(Fraction f)
public Fraction reduce()
public static int gcd(int a, int b)
public String toString()
-----------------------------
Fraction f = new Fraction(3,4);
f.add(2,4,5).add(0,5,20);
System.out.println(f);
System.out.println(f.toDouble());
System.out.println(f.reciprocal());
System.out.println(f.reduce());
f = new Fraction(3,4);
f.multiply(1,1,2);
System.out.println(f);
f = new Fraction(-9,-4);
f.multiply(0,40,40);
System.out.println(f);
System.out.println(new Fraction(3,4).compareTo(new Fraction(15,20)));
System.out.println(new Fraction(3,4).compareTo(new Fraction(14,20)));
System.out.println(new Fraction(24,32).compareTo(new Fraction(16,20)));
OUTPUT
3 4/5
3.8
5/19
5/19
1 1/8
2 1/4
0
1
-1
The next assignment lists the implementation of a generic, or parameterized, class. In numerical analysis, there are many uses of matrices that only have non-zero values in certain areas. Some examples are diagonal, upper triangular, and lower triangular matrices. By not storing the zero elements, we realize a significant space savings for large matrices. The example illustrates a diagonal (0,0 1,1 2,2 etc.) matrix implementation. The class is parameterized with the type of the matrix elements. Everything worked fine until I tried to return a zero for the off-diagonal elements in the "get" routine. As far as the compiler knew, "Type" could be a String or a Pattern, neither of which have a numeric value. The solution was to "bind" the "Type" parameter to indicate that it was restricted to a sub-class of Number, which happens to be the exact intended use of a diagonal matrix. The use of a bounded parameter type reaps the additional benefit of a compiler error message if the "Number sub-class" invariant is violated.
public static class MatrixDiagonal<Type extends Number> {
public final int N;
ArrayList<Type> m;
public MatrixDiagonal(int N)
public Type get(int i, int j)
public void set(int i, int j, Type value)
------------------------------
MatrixDiagonal<Integer> m=new MatrixDiagonal<Integer>(5);
for (int i=0; i<5; i++) m.set(i, i, i+1);
System.out.println(m.get(4, 4)+m.get(3, 3));
MatrixDiagonal<Double> d=new MatrixDiagonal<Double>(5);
for (int i=0; i<5; i++) d.set(i, i, (double)i+1);
d.set(4,4,22.0);
System.out.println(d.get(4, 4)+d.get(3, 3));
jhgjg