Docsity

Assignment 1 Problems 5 for Programming Language Technology | EECS 368, Assignments of Programming Languages

Toggle sidebar
bg1
EECS 368 Programming Language Paradigms
Fall Semester, 2007
Homework #1
Problem 1:
Using Java, define a class called Point that defines a Cartesian coordinate. Use
accessor and modifier methods to obtain and set x and y values – neither of which
should be directly visible outside the class. Define a concrete move method that
accepts two values and adds them to the current x and y values specified in the class
Provide two constructors: (i) one that uses no parameters and creates a point at the
origin; and (ii) one that takes two double type parameters and creates a point using
parameter values as the initial position.
Problem 2:
Define a new abstract class called Shape. This class should define: (i) a position field
of class Point; (ii) a concrete move method; (iii) an abstract area method; and (iv) an
abstract perimeter method. The move method should accept two parameters of type
double that specify values to be added to x and y respectively. The area and perimeter
methods should take no arguments. Make sure the position field is not public and that
area and perimeter both return double.
Problem 3:
Extend the abstract Shape class to implement classes for Rectangle, Circle, and
Square. Each class should have constructors and use accessor and modifier methods
to manipulate the data representing the specific shape. You are free to use a
representation of your choice, but you must implement area and perimeter methods as
defined in the abstract class. No data fields in your abstract class should be public.
Problem 4:
Design and implement a new class called Composite, also a subclass of Shape. A
composite shape is formed when a collection of shapes are grouped together. Thus, a
composite shape must somehow contain a collection of other shapes, potentially
composite shapes themselves. This collection must not be public. Your Composite
class must define an instance method add that allows a new shape to be added to the
composite shape’s collection of shapes. Do not worry about remove or modify
methods for internal shapes in the composite shape.
As a subclass of Shape, your Composite class must provide move, area, and
perimeter, methods. The move method should move each individual shape defined in
the composite shape. The area and perimeter methods should return the sum of
individual shape areas and perimeters respectively.
Problem 5:
Add instance methods max and sort to your Composite class that: (i) find the largest
shape in the shape array; and (ii) sort the elements of the shape collection by
increasing area. The max method should find the largest shape in the composite shape
and return Null if the shape collection is empty. The sort method should sort the
elements of the shape collection from smallest to largest.
Notes:
Please provide test methods and a testClass with a main method that will demonstrate
your code. See the handout on testing that is available on the Resources Page of the
course website. Remember that everyone, especially you, will be happier if we
understand how to execute and test your assignment.
The important concepts from this homework are:
1. Figuring out how to use the Java compiler and runtime environment.
2. Defining classes and subclasses.
3. Defining abstract classes and methods.

Partial preview of the text

Download Assignment 1 Problems 5 for Programming Language Technology | EECS 368 and more Assignments Programming Languages in PDF only on Docsity!

EECS 368 Programming Language Paradigms

Fall Semester, 2007

Homework

Problem 1: Using Java, define a class called Point that defines a Cartesian coordinate. Use accessor and modifier methods to obtain and set x and y values – neither of which should be directly visible outside the class. Define a concrete move method that accepts two values and adds them to the current x and y values specified in the class Provide two constructors: (i) one that uses no parameters and creates a point at the origin; and (ii) one that takes two double type parameters and creates a point using parameter values as the initial position. Problem 2: Define a new abstract class called Shape. This class should define: (i) a position field of class Point ; (ii) a concrete move method; (iii) an abstract area method; and (iv) an abstract perimeter method. The move method should accept two parameters of type double that specify values to be added to x and y respectively. The area and perimeter methods should take no arguments. Make sure the position field is not public and that area and perimeter both return double. Problem 3: Extend the abstract Shape class to implement classes for Rectangle , Circle , and Square. Each class should have constructors and use accessor and modifier methods to manipulate the data representing the specific shape. You are free to use a representation of your choice, but you must implement area and perimeter methods as defined in the abstract class. No data fields in your abstract class should be public. Problem 4: Design and implement a new class called Composite , also a subclass of Shape. A composite shape is formed when a collection of shapes are grouped together. Thus, a composite shape must somehow contain a collection of other shapes, potentially composite shapes themselves. This collection must not be public. Your Composite class must define an instance method add that allows a new shape to be added to the composite shape’s collection of shapes. Do not worry about remove or modify methods for internal shapes in the composite shape. As a subclass of Shape , your Composite class must provide move , area , and perimeter , methods. The move method should move each individual shape defined in the composite shape. The area and perimeter methods should return the sum of individual shape areas and perimeters respectively. Problem 5: Add instance methods max and sort to your Composite class that: (i) find the largest shape in the shape array; and (ii) sort the elements of the shape collection by increasing area. The max method should find the largest shape in the composite shape and return Null if the shape collection is empty. The sort method should sort the elements of the shape collection from smallest to largest. Notes: Please provide test methods and a testClass with a main method that will demonstrate your code. See the handout on testing that is available on the Resources Page of the course website. Remember that everyone, especially you, will be happier if we understand how to execute and test your assignment. The important concepts from this homework are:

  1. Figuring out how to use the Java compiler and runtime environment.
  2. Defining classes and subclasses.
  3. Defining abstract classes and methods.