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

  Balanced and competitive business practices

The United States has several laws that are intended to further fair, balanced, and competitive business practices. Do you think that such laws are effective? If so, why? If not, why not? Be sure to provide evidence to support your position one way o..

  A mini game made in java using zen graphics

a mini game made in Java using Zen graphics .

  Create a gui with a text field and a button

The java program needs to create a GUI with a text field and a button, with an actionlistener. When the button is clicked, whatever text typed into the text field is stored in a variable and written to a text file

  What are the gui components in java

What are the GUI components in Java? Explain at least two of them

  Setting up the form page

Download and save the attached comment CGI mailer script form-mail2.pl to your server's cgi-bin directory, and change the permissions on the script to make it executable (not writable).

  Create a java program to calculate

Create a Java Program to calculate the all grades inputted by a student, the average grade for the student, the letter grade for the student, and the valid inputted scores displayed in ascending order.

  Write a calculator program for integer numbers in java

Write a calculator program for integer numbers in java. The calculator should be able to do addition, subtraction, multiplication, and division for integer numbers.

  Java program to decide whether integer is perfect number

For example, 6 is a perfect number because 6 = 1 + 2 + 3. Write a Java program that decides whether integer is a perfect number.

  Program translates a number into the closest letter grade

Write a Java program that translates a number into the closest letter grade. For example, the number 2.8 (which might have been the average of several grades) would be converted to B-

  Writing a program to manage a student database

Writing a program to manage a student database. The interface to the program should be a GUI that looks similar to the following: A combo box should allow the user to select one of the three database actions shown

  Program that uses a library database of books

Write a Java program that uses a library database of books and patron data, as described in Exercise R22.2. Patrons should be able to check out and return books.

  Define multi-threaded programming in java using locks

Define Multi-threaded Programming in Java Using Locks, To practice programming cooperating, synchronized multiple threads of execution

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