Você está na página 1de 9

Part 1

Fill in the blanks with the correct answers.


a. Real-world objects contain and .
Attributes and behaviour

b. A software object's state is stored in .


fields

c. A software object's behavior is exposed through .


methods

d. Hiding internal data from the outside world, and accessing it only through publicly exposed
methods is known as data .
Encapsulation

e. A blueprint for a software object is called a .


Class

f. Common behavior can be defined in a and inherited into a


using the keyword.
Super class, subclass, extended.

g. A collection of methods with no implementation is called an .


interfaces
Part 2
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.
This is not allowed since static can only refer to static variable only. Instance variable
can only access the instance method only.

b) What is wrong with the following code?

The function .getRadius is not written.

C is an instance variable.So method2 could not access the getRadius.


c) In the following code, radius is in private in the Circle class and myCircle is an object of
the Circle class. Does the following highlighted code cause any problems? Explain why.

myCircl.radius is not allowed because o radius is already declared as private.

The accesor/getter for radius is not written.

d) If a class contains only private data fields and no set methods, is the class mutable?

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.
Part 3
Design a class named Rectangle to represent a rectangle. The class contains:

 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

public class Rectangle


{
double 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 Height
public void setHeight(double newHeight)
{
height = newHeight;
}
//Set Width
public 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 area
public double findArea()
{
return height*width;
}

// Find Parameter
public double findPerimter()
{
return (height*2) + (width*2);
}

}
Part 4
Design a class named Stock that contains:

 A string data field named symbol for the stock's symbol. Done
 A string data field named name for the stock's name. Done
 A double data field named previousClosingPrice that stores the stock price for the
previous day. Done
 A double data field named currentPrice that stores the stock price for the current time.
Done
 A constructor that creates a stock with specified symbol and name.
 The accessor methods for all data fields. Done
 The mutator methods for previousClosingPrice and currentPrice.
 A method named changePercent() that returns the percentage changed from
previousClosingPrice to currentPrice.

Public class Stock {


// Data type/field initation
String symbol;
String name;
double previousClosingPrice;
double currentPrice;

//Not required but can write, if u want….


Stock(String s, String n){

Symbol = s;

Name = n;

//Constructor that creates default stock.


public Stock()

String symbol = "$";

String name = "Anything Write Here";

}
// Accessor/Getter Method for all the data fields.
public String getSymbol()
{
return symbol;
}
Public String getName()
{
Return name;
}

Public double getPreviousClosingPrice()


{
Return previousClosingPrice;
}

Public double getCurrentPrice()


{
Return currentPrice;
}

Public double changePercent()


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

}
}
Part 5

a) Identify the problems in the following classes:

b) Does every class have a toString method? How is it used? Is it appropriate to override
this method?

c) Suppose that Fruit, Apple, Orange, Golden Delicious Apple, and Macintosh Apple are
declared as on 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?
II. Is fruit instanceof Apple?
III. Is fruit instanceof GoldenDelicious?
IV. Is fruit instanceof Macintosh?
V. Is orange instanceof Orange?
VI. Is orange instanceof Fruit?