Docsity

Intermediate Class Exercise - Object Oriented Programming I | CMSC 132, Assignments of Computer Science

Toggle sidebar
bg1
Inner Class Exercise
1. Define a class named Factorial that has the following methods:
a. Factorial(lowerLimit, upperLimit)
b. toString() โ€“ returns a string with the factorials of numbers starting at lowerLimit
(inclusive) and ending at upperLimit (inclusive). For example, for lowerLimit 2 and
upperLimit 4 the string returned will be: โ€œ2, 6, 24โ€
2. Expand the Factorial class you defined in 1. by having the class implement the Iterable
interface. Use an inner class to implement the iterator.
3. Repeat (2.) but use an anonymous inner class.
4. A Car class is defined as follows:
public abstract class Car {
private String make;
public abstract void start();
public void setMake(String make) { this.make = make; }
public String getMake() { return make ;}
}
Complete the assignment statement below so we can define a hybrid car object that
has an instance variable representing battery power. In addition the object will be
associated with a start() method that decreases the battery power by 100 units. The
initial value of battery power is 3000 units. You must use an anonymous inner class.
public static void main(String[] args) {
Car Hybrid = // COMPLETE THIS ASSIGNMENT
}
5. Rewrite the MyList example presented in class, so we have a MyList that represents
a generic list, and one that implements an iterator with a remove method.

Partial preview of the text

Download Intermediate Class Exercise - Object Oriented Programming I | CMSC 132 and more Assignments Computer Science in PDF only on Docsity!

Inner Class Exercise

  1. Define a class named Factorial that has the following methods:

a. Factorial(lowerLimit, upperLimit) b. toString() โ€“ returns a string with the factorials of numbers starting at lowerLimit (inclusive) and ending at upperLimit (inclusive). For example, for lowerLimit 2 and upperLimit 4 the string returned will be: โ€œ2, 6, 24โ€

  1. Expand the Factorial class you defined in 1. by having the class implement the Iterable interface. Use an inner class to implement the iterator.
  2. Repeat (2.) but use an anonymous inner class.
  3. A Car class is defined as follows:

public abstract class Car { private String make; public abstract void start(); public void setMake(String make) { this.make = make; } public String getMake() { return make ;} }

Complete the assignment statement below so we can define a hybrid car object that has an instance variable representing battery power. In addition the object will be associated with a start() method that decreases the battery power by 100 units. The initial value of battery power is 3000 units. You must use an anonymous inner class.

public static void main(String[] args) { Car Hybrid = // COMPLETE THIS ASSIGNMENT }

  1. Rewrite the MyList example presented in class, so we have a MyList that represents a generic list, and one that implements an iterator with a remove method.