Download presentation
Presentation is loading. Please wait.
1
ICS 201 Introduction to Computer Science
Problem Solving #2 ICS 201 Introduction to Computer Science
2
Which method overrides a method in the superclass?
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) { } 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 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 that is derived from Document and includes member variables for the sender, recipient, and title of an message. Implement appropriate accessor and mutator methods. The body of the 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 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; }
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 class extends Document { private String sender; private String recipient; private String title; public (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 boolean ContainsKeyword(Document docObject, String keyword) { if (docObject.toString().indexOf(keyword,0) >= 0) return true; return false; } public static void main(String[] args) 1= new ("Rrogramming in Java", "Hmad","Saad","Programming"); 2 = new (" 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( 1,"Java")) System.out.println(" 1"); if (ContainsKeyword( 2,"Java")) System.out.println(" 2"); if (ContainsKeyword(file1,"Java")) System.out.println(" File1"); if (ContainsKeyword(file2,"Java")) System.out.println(" File2"); } // Question2Documents Searches for the first occurrence of a character or substring.
12
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
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(); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.