Objects-type case

Assignment Help JAVA Programming
Reference no: EM13963495

Objects vs. Type Case

With object-oriented programming, classes and objects can be used to avoid "type- case" statements. Here is a program in which a form of case statement is used that inspects a user-de?ned type tag to distinguish between different classes of shape objects. This program would not statically type check in most typed languages because the correspondence between the tag ?eld of an object and the class of the object is not statically guaranteed and visible to the type checker. However, in an untyped language such as Smalltalk, a program like this could behave in a computationally reasonable way:

enum shape tag {s point, s circle, s rectangle }; class point {
shape tag tag;
int x; int y;
point (int xval, int yval)
{ x = xval; y = yval; tag = s point; }
int x coord () { return x; }
int y coord () { return y; }
void move (int dx, int dy) { x += dy; y += dy; }
};
class circle { shape tag tag; point c;
int r;
circle (point center, int radius)
{ c = center; r = radius; tag = s circle }
point center () { return c; }
int radius () { return radius; }
void move (int dx, int dy) { c.move (dx, dy); }
void stretch (int dr) { r += dr; }
};
class rectangle { shape tag tag; point tl;
point br;
rectangle (point topleft, point botright)
{ tl = topleft; br = botright; tag = s rectangle; }
point top left () { return tl; }
point bot right () { return br; }
void move (int dx, int dy) { tl.move (dx, dy); br.move (dx, dy); }
void stretch (int dx, int dy) { br.move (dx, dy); }

};
/* Rotate shape 90 degrees. */ void rotate (void *shape) {
switch ((shape tag *) shape) {
case s point: case s circle:
break;
case s rectangle:
{
rectangle *rect = (rectangle *) shape; int d = ((rect->bot right ().x coord ()
- rect->top left ().x coord ()) - (rect->top left ().y coord ()
- rect->bot right ().y coord ())); rect->move (d, d);
rect->stretch (-2.0 * d, -2.0 * d);
}
}
}

(a) Rewrite this so that, instead of rotate being a function, each class has a rotate method and the classes do not have a tag.

(b) Discuss, from the point of view of someone maintaining and modifying code, the differences between adding a triangle class to the ?rst version (as previously written) and adding a triangle class to the second [produced in part (a) of this question].

(c) Discuss the differences between changing the de?nition of rotate (say, from 90? to the left to 90? to the right) in the ?rst and the second versions. Assume you have added a triangle class so that there is more than one class with a nontrivial rotate method.

Reference no: EM13963495

Questions Cloud

Problem regarding the protocol conformance : We can compare Smalltalk interfaces to classes that use protocols, which are lists of operation names (selectors). When a selector allows parameters, as in at: put: , the selector name includes the colons but not the spaces.
Franchise arrangement between mcdonald''s and its franchisees : The franchise arrangement between McDonald's and its franchisees is summarized
Advantages and disadvantages of optimization : In Smalltalk, each class contains a pointer to the class template. This template stores the names of all the instance variables that belong to objects created by the class.
Specific heat of superheated steam at 150kpa is 2kj/deg : What diameters of inlet and outlet pipes would be required for a flow of 2270kg per hour with a velocity of 15m/s both before and after the reducing valve?
Objects-type case : With object-oriented programming, classes and objects can be used to avoid "type- case" statements. Here is a program in which a form of case statement is used that inspects a user-de?ned type tag to distinguish between different classes of shape ..
What are the capacity implications of the marketing campaign : Suppose that AlwaysRain Irrigation's marketing department will undertake an intense ad campaign for the bronze sprinklers, which are more expensive but also more durable than the plastic ones. What are the capacity implications of the marketing cam..
Which of the following statements is necessarily true : If the temperature of the contents is increased by 10 C degrees, the reading on the Kelvin thermometer should increase by 273 K
What is covalent bonding : What is covalent bonding? why is it different from ionic bonds?
Calculate the first order energy shifts for the ground : Consider a particle in the one dimensional infinite square well potential with width L. The perturbation potential W(x) = b sin(xpi/L) is added to the bottom of the infinite well. Calculate the first order energy shifts for the ground and first e..

Reviews

Write a Review

JAVA Programming Questions & Answers

  Implement the employee class and its three child classes

Implement the Employee class and its three child classes (ManagementEmployee, EngineeringEmployee and AdministrationEmployee), with all the necessary attributes and methods.

  Java :a user-defined starting position (other than 0,0)

modifying the program so that it can start from a user-defined starting position (other than 0,0) and search for a user defined ending point (other than row-1, column-1) .

  Write an expression that concatenates the string variable

Write an expression that concatenates the string variable suffix onto the end of the string variable prefix.

  Display the value of the tostring method of a customer type

display the value of the toString method of a customer type

  Implement a class called fulladdress and inherit it

I need assistance in commenting on the enclosed code, consider things like code layout, use of variable names, general readability and understandability.

  Recursion in binary search trees

Assignment is intended to give you practice writing recursive code that traverses a common computer programing structure: a binary search tree.

  Write program to keep track of a hardware stores inventory

Write a program to keep track of a hardware store's inventory. The store sells various items. For each item in the store, the following information is kept: item ID, item name, number of pieces in stock, manufacturer's price of the item, and the s..

  Creating an event handler for the enter button

The application should provide users with the option of adding or multiplying the numbers. Users should enter each number in a TextBox. After entering each number, the user clicks a Button, and the number is inserted in a ListBox.

  Implement a game called bunko-poker

Implement a game called Bunko-Poker. The gameplay is an easily programmed version of the popular game Yahtzee. Your program will make use of the supplied static functions Dice.roll() and, in cases where you might need the string ordered, Dice.ordered..

  Implement a third component of the compiler

To detect duplicate declarations using the same identifier, you call the method retrieveOneLevel. This method returns a pointer to the identifier entry if the identifier was declared before in the current scope and null otherwise.

  Write a bash script larger x y that compares the sizes

Write a bash script larger X Y that compares the sizes of two specified files X and Y, and reports which file is larger. For example, if X is larger, the output should be "File X is larger", while if Y is larger, the output should be "File Y is la..

  Java socket hello i need to this assignment done in net

hello i need to this assignment done in net beans . and i want comment in code .also screen shots of running program

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd