ASE Exercise 9 (Fall 2015) : Task 1: Questions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

ASE Exercise 9 (Fall 2015)

In this exercise we will focus on object oriented programming tasks.

Task 1: Questions
1. Real-world objects contain ___ and ___.
2. A software object's state is stored in ___.
3. A software object's behavior is exposed through ___.
4. Hiding internal data from the outside world, and accessing it only through publicly
exposed methods is known as data ___.
5. A blueprint for a software object is called a ___.
6. Common behavior can be defined in a ___ and inherited into a ___ using the ___
keyword.
7. A collection of methods with no implementation is called an ___.
8. A namespace that organizes classes and interfaces by functionality is called a ___.
9. The term API stands for ___?

10. The most basic control flow statement supported by the Java programming language
is the ___ statement.
11. The ___ statement allows for any number of possible execution paths.
12. The ___ statement is similar to the while statement, but evaluates its expression at the
___ of the loop.
13. How do you write an infinite loop using the for statement?
14. How do you write an infinite loop using the while statement?
15. Consider the following class:
public class IdentifyMyParts {
public static int x = 7;
public int y = 3;
http://www.ima.rwth-aachen.de

}
a. What are the class variables?
b. What are the instance variables?

IMA – RWTH Aachen University Direktorin: Univ.-Prof. Dr. rer. nat. Sabina Jeschke
Lehrstuhl für Informationsmanagement im Maschinenbau IMA 1. Stellvertreterin: apl.-Prof. Dr. habil. Ingrid Isenhardt
2. Stellvertreter: Dr. rer. nat. Frank Hees
Senior Advisor: Univ.-Prof. Dr.-Ing. em. Klaus Henning
Seite 2

c. What is the output from the following code:


IdentifyMyParts a = new IdentifyMyParts();
IdentifyMyParts b = new IdentifyMyParts();
a.y = 5;
b.y = 6;
a.x = 1;
b.x = 2;
System.out.println("a.y = " + a.y);
System.out.println("b.y = " + b.y);
System.out.println("a.x = " + a.x);
System.out.println("b.x = " + b.x);
System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x);

http://www.ima.rwth-aachen.de

IMA – RWTH Aachen University Direktorin: Univ.-Prof. Dr. rer. nat. Sabina Jeschke
Lehrstuhl für Informationsmanagement im Maschinenbau IMA 1. Stellvertreterin: apl.-Prof. Dr. habil. Ingrid Isenhardt
2. Stellvertreter: Dr. rer. nat. Frank Hees
Senior Advisor: Univ.-Prof. Dr.-Ing. em. Klaus Henning
Seite 3

Answers:
1. Real-world objects contain state and behavior.
2. A software object's state is stored in fields.
3. A software object's behavior is exposed through methods.
4. Hiding internal data from the outside world, and accessing it only through publicly
exposed methods is known as data encapsulation.
5. A blueprint for a software object is called a class.
6. Common behavior can be defined in a superclass and inherited into a subclass using
the extends keyword.
7. A collection of methods with no implementation is called an interface.
8. A namespace that organizes classes and interfaces by functionality is called a package.
9. The term API stands for Application Programming Interface.

10. The most basic control flow statement supported by the Java programming language
is the if-then statement.
11. The switch statement allows for any number of possible execution paths.
12. The do-while statement is similar to the while statement, but evaluates its expression
at the bottom of the loop.
13. for ( ; ; ) {

}
14. while (true) {

}
15. a. x
16. b. y
17. c. a.y = 5
b.y = 6
a.x = 2
http://www.ima.rwth-aachen.de

b.x = 2
IdentifyMyParts.x = 2

IMA – RWTH Aachen University Direktorin: Univ.-Prof. Dr. rer. nat. Sabina Jeschke
Lehrstuhl für Informationsmanagement im Maschinenbau IMA 1. Stellvertreterin: apl.-Prof. Dr. habil. Ingrid Isenhardt
2. Stellvertreter: Dr. rer. nat. Frank Hees
Senior Advisor: Univ.-Prof. Dr.-Ing. em. Klaus Henning
Seite 4

Task 1 (Skill Level 2)


A class called MyPoint, which models a 2D point with x and y coordinates, is designed as shown
in the class diagram.

It contains:

 Two instance variables x (int) and y (int).


 A "no-argument" (or "no-arg") constructor that construct a point at (0, 0).
 A constructor that constructs a point with the given x and y coordinates.
 Getter and setter for the instance variables x and y.
 A method setXY() to set both x and y.
 A toString() method that returns a string description of the instance in the format "(x, y)".
 A method called distance(int x, int y) that returns the distance from this point to another
point at the given (x, y) coordinates.
 An overloaded distance(MyPoint another) that returns the distance from this point to the
given MyPoint instance another.

Write the code for the class MyPoint. Also write a test program (called TestMyPoint) to test all
the methods defined in the class.
http://www.ima.rwth-aachen.de

IMA – RWTH Aachen University Direktorin: Univ.-Prof. Dr. rer. nat. Sabina Jeschke
Lehrstuhl für Informationsmanagement im Maschinenbau IMA 1. Stellvertreterin: apl.-Prof. Dr. habil. Ingrid Isenhardt
2. Stellvertreter: Dr. rer. nat. Frank Hees
Senior Advisor: Univ.-Prof. Dr.-Ing. em. Klaus Henning
Seite 5

Task 2 (Skill Level 4) – depends on Task 1


A class called MyCircle, which models a circle with a center (x, y) and a radius, is designed as
shown in the class diagram. The MyCircle class uses an instance of MyPoint class (created in
the previous task) as its center.

The class contains:

 Two private instance variables: center (an instance of MyPoint) and radius (int).
 A constructor that constructs a circle with the given center's (x, y) and radius.
 An overloaded constructor that constructs a MyCircle given a MyPoint instance as center,
and radius.
 Various getters and setters.
 A toString() method that returns a string description of this instance in the format "Circle
@ (x, y) radius=r".
 A getArea() method that returns the area of the circle in double.

Write the MyCircle class. Also write a test program (called TestMyCircle) to test all the
methods defined in the class.
http://www.ima.rwth-aachen.de

IMA – RWTH Aachen University Direktorin: Univ.-Prof. Dr. rer. nat. Sabina Jeschke
Lehrstuhl für Informationsmanagement im Maschinenbau IMA 1. Stellvertreterin: apl.-Prof. Dr. habil. Ingrid Isenhardt
2. Stellvertreter: Dr. rer. nat. Frank Hees
Senior Advisor: Univ.-Prof. Dr.-Ing. em. Klaus Henning
Seite 6

Task 3 (Skill Level 4) – depends on Task 1


A class called MyTriangle, which models a triangle with 3 vertices, is designed as follows. The
MyTriangle class uses three MyPoint instances as the three vertices.

The class contains:

 Three private instance variables v1, v2, v3 (instances of MyPoint), for the three vertices.
 A constructor that constructs a MyTriangle with three points v1=(x1, y1), v2=(x2, y2),
v3=(x3, y3).
 An overloaded constructor that constructs a MyTriangle given three instances of MyPoint.
 A toString() method that returns a string description of the instance in the format "Triangle
@ (x1, y1), (x2, y2), (x3, y3)".
 A getPerimeter() method that returns the length of the perimeter in double. You should
use the distance() method of MyPoint to compute the perimeter.
 A method printType(), which prints "equilateral" if all the three sides are equal, "isosceles"
if any two of the three sides are equal, or "scalene" if the three sides are different.

Write the MyTriangle class. Also write a test program (called TestMyTriangle) to test all the
methods defined in the class.
http://www.ima.rwth-aachen.de

IMA – RWTH Aachen University Direktorin: Univ.-Prof. Dr. rer. nat. Sabina Jeschke
Lehrstuhl für Informationsmanagement im Maschinenbau IMA 1. Stellvertreterin: apl.-Prof. Dr. habil. Ingrid Isenhardt
2. Stellvertreter: Dr. rer. nat. Frank Hees
Senior Advisor: Univ.-Prof. Dr.-Ing. em. Klaus Henning
Seite 7

Task 4 (Skill Level 5)


Take a look at the class diagram provided below.

http://www.ima.rwth-aachen.de

IMA – RWTH Aachen University Direktorin: Univ.-Prof. Dr. rer. nat. Sabina Jeschke
Lehrstuhl für Informationsmanagement im Maschinenbau IMA 1. Stellvertreterin: apl.-Prof. Dr. habil. Ingrid Isenhardt
2. Stellvertreter: Dr. rer. nat. Frank Hees
Senior Advisor: Univ.-Prof. Dr.-Ing. em. Klaus Henning
Seite 8

Write a superclass called Shape (as shown in the class diagram), which contains:

 Two instance variables color (String) and filled (boolean).


 Two constructors: a no-arg (no-argument) constructor that initializes the color to "green"
and filled to true, and a constructor that initializes the color and filled to the given values.
 Getter and setter for all the instance variables. By convention, the getter for a boolean
variable xxx is called isXXX() (instead of getXxx() for all the other types).
 A toString() method that returns "A Shape with color of xxx and filled/Not filled".

Write a test program to test all the methods defined in Shape.

Write two subclasses of Shape called Circle (reuse MyCircle from previous task) and Rectangle,
as shown in the class diagram.

The Circle class contains:

 An instance variable radius (double).


 Three constructors as shown. The no-arg constructor initializes the radius to 1.0.
 Getter and setter for the instance variable radius.
 Methods getArea() and getPerimeter().
 Override the toString() method inherited, to return "A Circle with radius=xxx, which is a
subclass of yyy", where yyy is the output of the toString() method from the superclass.

The Rectangle class contains:

 Two instance variables width (double) and length (double).


 Three constructors as shown. The no-arg constructor initializes the width and length to 1.0.
 Getter and setter for all the instance variables.
 Methods getArea() and getPerimeter().
 Override the toString() method inherited, to return "A Rectangle with width=xxx and
length=zzz, which is a subclass of yyy", where yyy is the output of the toString() method
from the superclass.

Write a class called Square, as a subclass of Rectangle. Convince yourself that Square can be
modeled as a subclass of Rectangle. Square has no instance variable, but inherits the instance
variables width and length from its superclass Rectangle.

 Provide the appropriate constructors (as shown in the class diagram).


http://www.ima.rwth-aachen.de

 Override the toString() method to return "A Square with side=xxx, which is a subclass of
yyy", where yyy is the output of the toString() method from the superclass.
 Do you need to override the getArea() and getPerimeter()? Try them out.
 Override the setLength() and setWidth() to change both the width and length, so as to
maintain the square geometry.

IMA – RWTH Aachen University Direktorin: Univ.-Prof. Dr. rer. nat. Sabina Jeschke
Lehrstuhl für Informationsmanagement im Maschinenbau IMA 1. Stellvertreterin: apl.-Prof. Dr. habil. Ingrid Isenhardt
2. Stellvertreter: Dr. rer. nat. Frank Hees
Senior Advisor: Univ.-Prof. Dr.-Ing. em. Klaus Henning
Seite 9

Task 5 (Skill Level 6)


Rewrite the superclass Shape and its subclasses Circle, Rectangle and Square, as shown in the
class diagram.

http://www.ima.rwth-aachen.de

IMA – RWTH Aachen University Direktorin: Univ.-Prof. Dr. rer. nat. Sabina Jeschke
Lehrstuhl für Informationsmanagement im Maschinenbau IMA 1. Stellvertreterin: apl.-Prof. Dr. habil. Ingrid Isenhardt
2. Stellvertreter: Dr. rer. nat. Frank Hees
Senior Advisor: Univ.-Prof. Dr.-Ing. em. Klaus Henning
Seite 10

In this task, Shape shall be defined as an abstract class, which contains:

 Two protected instance variables color(String) and filled(boolean). The protected variables
can be accessed by its subclasses and classes in the same package. They are denoted with
a '#' sign in the class diagram.
 Getter and setter for all the instance variables, and toString().
 Two abstract methods getArea() and getPerimeter() (shown in italics in the class diagram).

The subclasses Circle and Rectangle shall override the abstract methods getArea() and
getPerimeter() and provide the proper implementation. They also override the toString().

Write a test class to test these statements involving polymorphism and explain the outputs.
Some statements may trigger compilation errors. Explain the errors, if any.

http://www.ima.rwth-aachen.de

IMA – RWTH Aachen University Direktorin: Univ.-Prof. Dr. rer. nat. Sabina Jeschke
Lehrstuhl für Informationsmanagement im Maschinenbau IMA 1. Stellvertreterin: apl.-Prof. Dr. habil. Ingrid Isenhardt
2. Stellvertreter: Dr. rer. nat. Frank Hees
Senior Advisor: Univ.-Prof. Dr.-Ing. em. Klaus Henning
Seite 11

Task 6 (Skill Level 7)


You are asked to write a discount system for a beauty salon, which provides services and sells
beauty products. It offers 3 types of memberships: Premium, Gold and Silver. Premium, gold
and silver members receive a discount of 20%, 15%, and 10%, respectively, for all services
provided. Customers without membership receive no discount. All members receive a flat 10%
discount on products purchased (this might change in future). Your system shall consist of
three classes: Customer, Discount and Visit, as shown in the class diagram. It shall compute
the total bill if a customer purchases $x of products and $y of services, for a visit.

Also write a test program to exercise all the classes.

Solutions: We added an additional class CustomerManager to manage multiple visits from one
customer and also to allow the definition of customers in advance.
http://www.ima.rwth-aachen.de

IMA – RWTH Aachen University Direktorin: Univ.-Prof. Dr. rer. nat. Sabina Jeschke
Lehrstuhl für Informationsmanagement im Maschinenbau IMA 1. Stellvertreterin: apl.-Prof. Dr. habil. Ingrid Isenhardt
2. Stellvertreter: Dr. rer. nat. Frank Hees
Senior Advisor: Univ.-Prof. Dr.-Ing. em. Klaus Henning

You might also like