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

  Creates a new magazinenode object and adds it to the end

The public variables are accessed by the MagazineList class.

  What is the purpose of public , protected keywords in java

What is the purpose of public , protected keywords in java?

  User and prints the number of occurrences of each letter

File CountLetters.java contains a program that reads a word from the user and prints the number of occurrences of each letter in the word. In reviewing the code, note that the word is converted to all upper case first, then each letter is translated ..

  Print two random integers

Type two statements using nextInt() to print two random integers between 0 and 9. End with a newline. Ex:

  Main method to test a student graduate record examination

I need a Java solution named TestGRE.java with a main method to test a Student Graduate Record Examination Score Managment System using BinarySearchTree java classes.

  Design and implement a java program to determine the sum

Design and implement a Java program that will gather a group of floating point numbers and determine the sum and average of the data entered. The program should use separate methods for inputting the data, calculating the sum, calculating the aver..

  Using a link list implementation

Using a link list implementation, simulate the war car game. The program should read an input of several line of card values and output the winner and the number of rounds it takes.

  Given a hash function hkey prepare a java simulation

given a hash function hkey prepare a java simulation program to determine each of the subsequent quantities after

  Write a program that prompts a user to enter a us dollar

Write a program that prompts a user to enter a US dollar amount. Output a table similar to the one in the example below. The second prompt should ask the user to enter a 1 to convert to euros, a 2 to convert to pounds and a 3 to convert to ruble..

  Develop a java program

Develop a Java program which, given the width, length and depth (in metres), of a swimming pool, determines and outputs.

  List various bindings that are needed to determine semantics

List the various bindings that are required to determine the semantics when the statement is executed. For each binding, indicate the binding time used for the language.

  Create a class that inherits from your pizzaorder class

Create a class that inherits from your PizzaOrder class called SeatedPizzaOrder. SeatedPizzaOrder has the three data values that any PizzaOrder has (size, toppings, and price) but also has a tablenumber (of type integer) and serverName (of type st..

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