1 |
Handout A for e01 |
CS56 F18 |
BeachCruiser.java
1
2
3
4
5
6
7
8
9
public class BeachCruiser extends Bicycle {
private String color;
public String getColor() { return this.color; }
public BeachCruiser(double wheelDiameter,
String color) {
super(1,wheelDiameter); // beach cruisers have 1 gear
this.color = color;
}
}
Bicycle.java
1
2
3
4
5
6
7
8
9
10
11
public abstract class Bicycle {
private int numGears;
private double wheelDiameter;
public int getNumGears() { return this.numGears; }
public double getWheelDiameter() { return this.wheelDiameter; }
public Bicycle(int numGears,
double wheelDiameter) {
this.numGears = numGears;
this.wheelDiameter = wheelDiameter;
}
}
EBike.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class EBike extends Bicycle implements Rentable, Electric {
private String batteryType;
public double getCost(int miles, int time) {
// $1.00 for first thirty minutes, and any portion of 30 min after
return 1.00 + (time / 30 );
}
public int getChargeTime() { return 30; }
// SOMETHING MISSING HERE
private String name;
public String getName() { return this.name;}
public EBike( int numGears,
double wheelDiameter,
String name,
String batteryType) {
// MISSING LINES OF CODE HERE
}
}
Code for
GetAround
problem
Electric.java
1
2
3
4
public interface Electric {
public String getBatteryType(); // e.g. "36V","48V"
public int getChargeTime(); // minute to full charge
}
Rentable.java
1
2
3
public interface Rentable {
public double getCost(int miles, int time);
}
Scooter.java
1
2
3
4
5
6
7
8
public class Scooter implements Rentable, Electric {
public String getBatteryType() { return "12V"; }
public double getCost(int miles, int time) { return 1.00; }
public int getChargeTime() { return 30; }
private String name;
public String getName() { return this.name;}
public Scooter(String name) { this.name = name; }
}
Handout A, p. 2
class java.util.ArrayList<E>
The following excerpts from the javadoc for java.util.ArrayList<E>
may be
helpful to you in completing this exam.
Inheritance Hierarchy (complete)
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.ArrayList<E>
All Implemented Interfaces: | Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess |
Direct Known Subclasses: | AttributeList, RoleList, RoleUnresolvedList |
Constructors (complete)
ArrayList() |
Constructs an empty list with an initial capacity of ten. |
ArrayList(Collection<? extends E> c) |
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection’s iterator. |
ArrayList(int initialCapacity) |
Constructs an empty list with the specified initial capacity. |
Most important methods, with brief description
boolean |
add(E e) |
Appends the specified element to the end of this list. |
void |
add(int index, E element) |
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). throws IndexOutOfBoundsException if (index < 0 || index > size()) |
void |
clear() |
Removes all of the elements from this list. |
E |
get(int index) |
Returns the element at the specified position in this list. |
int |
indexOf(Object o) |
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. |
boolean |
isEmpty() |
Returns true if this list contains no elements. |
int |
lastIndexOf(Object o) |
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. |
E |
remove(int index) |
Removes the element at the specified position in this list. |
boolean |
remove(Object o) |
Removes the first occurrence of the specified element from this list, if it is present. |
E |
set(int index, E element) |
Replaces the element at the specified position in this list with the specified element. Returns the element previously at the specified position throws IndexOutOfBoundsException if (index < 0 || index >= size()) |
int |
size() |
Returns the number of elements in this list. |
void |
sort(Comparator<? super E> c) |
Sorts this list according to the order induced by the specified Comparator . |
Additional methods, listed by method signature only.
boolean addAll(Collection<? extends E> c) |
boolean addAll(int index, Collection<? extends E> c) |
Object clone() |
boolean contains(Object o) |
void ensureCapacity(int minCapacity) |
void forEach(Consumer<? super E> action) |
Iterator<E> iterator() |
ListIterator<E> listIterator() |
ListIterator<E> listIterator(int index) |
boolean removeAll(Collection<?> c) |
boolean removeIf(Predicate<? super E> filter) |
protected void removeRange(int fromIndex, int toIndex) |
void replaceAll(UnaryOperator<E> operator) |
boolean retainAll(Collection<?> c) |
Spliterator<E> spliterator() |
List<E> subList(int fromIndex, int toIndex) |
Object[] toArray() |
<T> T[] toArray(T[] a) |
void trimToSize() |
 |
Methods inherited from:
class java.util.AbstractList |
equals, hashCode |
class java.util.AbstractCollection |
containsAll, toString |
class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |
interface java.util.List |
containsAll, equals, hashCode |
interface java.util.Collection |
parallelStream, stream |
End of Handout