ics 201 introduction to computer science problem solving #2

Click here to load reader

Upload: jordan-merritt

Post on 02-Jan-2016

216 views

Category:

Documents


2 download

TRANSCRIPT

Slide 1

ICS 201Introduction to Computer ScienceProblem Solving #2Consider 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) { }}

Which method overrides a method in the superclass?Answer:methodTwoQuestion 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 BoxWeightBoxWeight(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.0Weight of mybox1 is 34.3Volume of mybox2 is 24.0Weight of mybox2 is 0.076Define 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 Documentclass Document{private String text;public Document(){ text = ""; }public Document(String text){ this.text = text; }public String toString(){return text;}}class Emailclass 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(); }}

Fileclass 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");}} // Question2DocumentsSearches for the first occurrence of a character or substring.11Abstract ClassesDesign 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 VolumeSolutionabstract class SolidObject{public abstract double getSurfaceArea();public abstract double getVolume();public String toString() { return "Surface Area " + getSurfaceArea() + " Volume " + getVolume();}}The Cube ClassDesign 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*sideVolume: side*side*sideSolutionclass 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(); }}