OOD cousework.doc_第1页
OOD cousework.doc_第2页
OOD cousework.doc_第3页
OOD cousework.doc_第4页
OOD cousework.doc_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

(Answer FIVE (5) out of EIGHT (8) questions)Question 1Fill in the blanks with the correct answers.(a) Real-world objects contain state and behavior. (4 marks)(b) A software objects state is stored in fields. (2 marks)(c) A software objects behavior is exposed through methods.(2 marks)(d) Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data encapsulation. (2 marks)(e) A blueprint for a software object is called a class.(2 marks)(f) Common behavior can be defined in a superclass and inherited into a subclass using the extends keyword. (6 marks)(g) A collection of methods with no implementation is called an interface. (2 marks) (Total: 20 marks)Question 2Below is an example of a program used to calculate the area and perimeter of a circle. The formulas for calculating the area and perimeter are shown below. Please fill in the missing statements denoted (a) to (t).public class calculateCirclepublic static void main (String args)/create a circe with radius 5.0Circle myCircle = new Circle(5.0);System.out.println(The area of the circle of radius + myCircle.getRadius() + is + myCircle.findArea();/increase myCircles radius witby 10%myCircle. setRadius(myCircle. getRadius()*1.1);System.out.println(The area of the circle of radius + myCircle.getRadius()+ is + myCircle.findArea();/Declare circle class with private radius and accessor methodclass Circleprivate double radius;/default constructorpublic Circle()radius = 1.0;/Construct a circle with a specified radiuspublic Circle(double radius)radius = r;/getter method for radiuspublic double getRadius()return radius;/setter method for radiuspublic void setRadius(r)radius = newRadius;/find circle areapublic double findArea()return radius*radius*3.142;(Total: 20 marks)Question 3(a) What is the output for the following code?(i) False.(4 marks)(b) How can a Date be created for the current time? How is it used to display the current time?import java.text.SimpleDateFormat;import java.util.Calendar;public class DateAndTime public static final String DATE_FORMAT_NOW = dd-MM-yyyy HH:mm:ss; public static String dt; public static String DateTime() Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); return sdf.format(cal.getTime(); public static void main(String args) DateAndTime dAT = new DateAndTime(); dt = dAT.DateTime(); System.out.println(dt); (6 marks)(c) Which packages contain the classes Date, JFrame, JOptionPane, System and Math?import java.util.Dateimport javax.swing.Jframeimport javax.swing.JOptionPaneimport java.lang.Systemimport java.lang.Math(10 marks)(Total: 20 marks)Question 4(a) Can we invoke an instance method or reference an instance variable from a static method? Can we invoke a static method or reference a static variable from an instance method? Justify your answer.Ans:This is not allowed since static can only refer to static variable only. Instance variablecan only access the instance method only.(6 marks)(b) What is wrong with the following codes? (i)The function .getRadius is not written.(ii) C is an instance variable.So method2 could not access the getRadius.(4 marks)(c) In the following code, radius is private in the Circle class and myCircle is an object of the Circle class. Does the following highlighted code cause any problems? Explain why.(i) myCircl.radius is not allowed because o radius is already declared as private.(ii) The accesor/getter for radius is not written.(6 marks)(d) If a class contains only private data fields and no set methods, is the class mutable?Ans:Yes. Getter or mutator method should be constructed in-order to return a value. If not getter or setter method the value cant be accessed from outside the class.(4 marks)(Total: 20 marks)Question 5Design a class named Rectangle to represent a rectangle. The class should contain: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A string data field named color that specifies the color of a rectangle. Hypothetically, assume that all rectangles have the same color. The default color is white. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. The accessor and mutator methods for all three data fields. A method named getArea() that returns the area of this rectangle. A method named getPerimeter() that returns the perimeter.(Total: 20 marks)public class Rectangledouble width;double height;String color;public Rectangle()width = 1;height = 1;color = white;public Rectangle(double w, double h)width = w;height = h;/ The getter and setter method/Set Heightpublic void setHeight(double newHeight)height = newHeight;/Set Widthpublic void setWidth(double newWidth)width = newWidth;public void setColor(String c)color = c;public double getHeight()return height;public double getWidth()return width;public String getColor()return color;/Find areapublic double findArea()return height*width;/ Find Parameterpublic double findPerimter()return (height*2) + (width*2);Question 6Design a class named Stock that contains: A string data field named symbol for the stocks symbol. A string data field named name for the stocks name. A double data field named previousClosingPrice that stores the stock price for the previous day. A double data field named currentPrice that stores the stock price for the current time. A constructor that creates a stock with specified symbol and name. The accessor methods for all data fields. The mutator methods for previousClosingPrice and currentPrice. A method named changePercent() that returns the percentage changed from previousClosingPrice to currentPrice.(Total: 20 marks)public class StockString symbol;String name;double previousClosingPrice;double currentPrice;Stock(String s, String n)symbol = s;name = n;public Stock()String symbol = $;String name = Anything Write Here;public String getSymbol()return symbol;public String getName()return name;public double getPreviousClosingPrice()return previousClosingPrice;public double getCurrentPrice()return currentPrice;public void setPreviousClosingPrice(double newPreviousClosingPrice)previousClosingPrice= newPreviousClosingPrice;public void setCurrentPrice(double newCurrentPrice)currentPrice= newCurrentPrice;/ A method named changePercent().public double changePercent()return (currentPrice - previousClosingPrice)*100);Question 7(a) Identify the problems in the following classes:public class Circleprivate double radius;public Circle(double radius)radius=radius;public double getRadius()return radius;public double getArea()return radius*radius*Math.PI;class B extends Circleprivate double length;B(double radius, double length)super(radius);length=length;public double getArea()return getArea()*length;(8 marks)(b) Does every class have a toString method? How is it used? Is it appropriate to override this method?Ans:The ToString method can be overridden using standard syntax. The method must return a string and should include important information about the underlying object. To demonstrate, create a new console application and add the following class, which represents web site information:class WebSiteprivate string _name;private string _url;public string Name get return _name; set _name = value; public string Url get return _url; set _url = value; (6 marks)(c) Suppose that Fruit, Apple, Orange, Golden Delicious Apple, and Macintosh Apple are declared as in the diagram below. Assume that fruit is an instance of GoldenDelicious and orange is an instance of Orange, answer the following questions:(i) Is fruit instanceof Orange?Yes.(ii) Is fruit instanceof Apple?Yes.(iii) Is fruit instanceof GoldenDelicious?Yes.(iv) Is fruit instanceof Macintosh?Yes.(v) Is orange instanceof Orange?Yes.(vi) Is orange instanceof Fruit?No.(6 marks)(Total: 20 marks)Question 8(a) How do you throw an exception? Can you throw multiple exceptions in one throw statement?Ans:Before you can catch an exception, some code somewhere must throw one. Any code can throw an exception: your code, code from a package written by someone else such as the packages that come with the Java platform, or the Java runtime environment. Regardless of what throws the exception, its always thrown with thethrowstatement.(4 marks)(b) What

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论