Problem Solving #2 ICS 201 Introduction to Computer Science Consider the following two classes: public class ClassA { public void methodOne(int i) { } public void methodTwo(int i) { } public static void methodThree(int i) { } } public class ClassB extends ClassA { public static void methodOne(int i) { } public void methodTwo(int i) { } public void methodThree(int i) { } } Answer: methodTwo Which method overrides a method in the superclass? Question 2 Assume, we have a Box class. Write a new class BoxWeight, which includes a fourth component called weight. Thus, the new class will contain a Box's width, height, depth, and weight. In your answer, include accessor method getWeight that returns the weight value. // class Box class Box { private double width; private double height; private double depth; // constructor used when all dimensions are specified Box(double w, double h, double d) { width = w; height = h; depth = d; } double volume() { return width * height * depth; } } // class BoxWeight // Here, WeightedBox is extended to include weight. class BoxWeight extends Box { private double weight; // weight of box // constructor for BoxWeight BoxWeight(double w, double h, double d, double m) { super(w, h, d); weight = m; } public double getWeight(){ return weight; } } b) What is the output of the following program? class DemoBoxWeight { public static void main(String args[]) { BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3); BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076); double vol; vol = mybox1.volume(); System.out.println("Volume of mybox1 is " + vol); System.out.println("Weight of mybox1 is " + mybox1.getWeight()); System.out.println(); vol = mybox2.volume(); System.out.println("Volume of mybox2 is " + vol); System.out.println("Weight of mybox2 is " + mybox2.getWeight()); } Volume of mybox1 is 3000.0 } Weight of mybox1 is 34.3 Volume of mybox2 is 24.0 Weight of mybox2 is 0.076 Define a class named Document that contains a member variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field. Next, define a class for Email that is derived from Document and includes member variables for the sender, recipient, and title of an email message. Implement appropriate accessor and mutator methods. The body of the email message should be stored in the inherited variable text. Redefine the toString method to concatenate all text fields. Similarly, define a class for File that is derived from Document and includes a member variable for the pathname. The textual contents of the file should be stored in the inherited variable text. Redefine the toString method to concatenate all text fields. Finally, create several sample objects of type Email and File in your main method. Test your objects by passing them to the following subroutine that returns true if the object contains the specified keyword in the text property. public static boolean ContainsKeyword(Document docObject, String keyword) { if (docObject.toString().indexOf(keyword,0) >= 0) return true; return false; } class Document class Document { private String text; public Document() { text = ""; } public Document(String text) { this.text = text; } public String toString() { return text; } } class Email class Email extends Document { private String sender; private String recipient; private String title; public Email(String body, String sender, String recipient, String title) { super(body); this.sender = sender; this.recipient = recipient; this.title = title; } public String getSender() { return sender; } public void setSender(String sender) { this.sender = sender; } public String getRecipient() { return recipient; } public void setRecipient(String recipient) { this.recipient = recipient; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String toString() { return "Sender " + sender + " Recipient " + recipient + " Title " + title + " " + super.toString(); } } File class File extends Document { private String pathname; public File() { super(); pathname = " "; } public File(String body, String pathname) { super(body); this.pathname = pathname; } public void setPathname(String s) { pathname = s; } public String getPathname() { return pathname; } public String toString() { return "Pathname " + pathname + " Body " + super.toString(); } } // File class Question2Documents { public static boolean ContainsKeyword(Document docObject, String keyword) { if (docObject.toString().indexOf(keyword,0) >= 0) return true; return false; } public static void main(String[] args) { Email email1= new Email("Rrogramming in Java", "Hmad","Saad","Programming"); Email email2 = new Email(" Running marathons", "Speedy", "Gonzales", "races"); File file1 = new File("Contents about some Java file", "file.txt"); File file2 = new File("Contents about marathon races", "run.txt"); System.out.println("Which contains Java?"); if (ContainsKeyword(email1,"Java")) System.out.println(" Email1"); if (ContainsKeyword(email2,"Java")) System.out.println(" Email2"); if (ContainsKeyword(file1,"Java")) System.out.println(" File1"); if (ContainsKeyword(file2,"Java")) System.out.println(" File2"); } } // Question2Documents Abstract Classes Design and implement an abstract class called SolidObject. A SolidObject has a surface area and volume which can be calculated. Provide the following methods in your class: public abstract double getVolume() public abstract double getSurfaceArea() public String toString() //prints the Surface Area and the Volume Solution abstract class SolidObject { public abstract double getSurfaceArea(); public abstract double getVolume(); public String toString() { return "Surface Area " + getSurfaceArea() + " Volume " + getVolume(); } } The Cube Class Design and implement a class called Cube which is a subclass of SolidObject. A cube has only one instance variable side representing its side and has the following formulae: Surface Area: 6*side*side Volume: side*side*side Solution class Cube extends SolidObject implements Usable { private double x; public Cube(double side) { x = side; } public double getSurfaceArea(){return 6 * x * x; } public double getVolume() { return x * x * x; } public String toString() { return "Cube " + super.toString(); } }