Professional Documents
Culture Documents
ASE Exercise 9 (Fall 2015) : Task 1: Questions
ASE Exercise 9 (Fall 2015) : Task 1: Questions
ASE Exercise 9 (Fall 2015) : Task 1: Questions
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
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
It contains:
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
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
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
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:
Write two subclasses of Shape called Circle (reuse MyCircle from previous task) and Rectangle,
as shown in the class diagram.
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.
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
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
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
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