1 / 15

Problem Solving #2

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 ) { } }

bonnie
Télécharger la présentation

Problem Solving #2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Problem Solving #2 ICS 201 Introduction to Computer Science

  2. Consider the following two classes: public class ClassA { public void methodOne(inti) { } public void methodTwo(inti) { } public static void methodThree(inti) { } } public class ClassB extends ClassA { public static void methodOne(inti) { } public void methodTwo(inti) { } public void methodThree(inti) { } } Which method overrides a method in the superclass? Answer: methodTwo

  3. 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.

  4. // 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; } }

  5. // 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; } }

  6. 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

  7. 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 booleanContainsKeyword(Document docObject, String keyword) { if (docObject.toString().indexOf(keyword,0) >= 0) return true; return false; }

  8. class Document class Document { private String text;   public Document() { text = ""; } public Document(String text) { this.text = text; } public String toString() { return text; } }

  9. 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(); } }

  10. 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

  11. class Question2Documents { public static booleanContainsKeyword(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("Rrogrammingin 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

  12. Abstract Classes • Design and implement an abstract class called SolidObject. A SolidObject has a surfacearea 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

  13. Solution abstract class SolidObject { public abstract double getSurfaceArea(); public abstract double getVolume(); public String toString() { return "Surface Area " + getSurfaceArea() + " Volume " + getVolume(); } }

  14. 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

  15. 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(); } }

More Related