File: BitManips.java
· solution
public class BitManips {
int rotateLeft(int x, int k) {
return ;
}
int isMultipleOfFour(int x) {
return ;
}
Assume that you do not know the number of bits in your number.
int isOdd(int x) {
return ;
}
int times35(int x) {
return ;
}
}
Write a method that, given an SList
, an int
j
, and an int
k
, return an SList
with elements at indexes k
, k+j
, k+2*j
, .... Do not change the original list.
File: Slist.java
· solution
class SListNode {
Object item; SListNode next;
SListNode(Object item, SListNode next) {
this.item = item; this.next = next;
}
SListNode(Object item) {
this(item, null);
}
}
public class SList {
private SListNode head;
public SList(SListNode head) {
this.head = head;
}
public static Object multiples(SList list, int j, int k) {
}
}
List l;
if (use_linked_list) {
l = new LinkedList();
} else {
l = new ArrayList();
}
In the above example, assume LinkedList
and ArrayList
are both child classes of List
.
static types = the declared type = checked at compile time
We don't need to run the code to know that l
is a List
.
dynamic type = the actual type = checked at run time
When we run the code, depending on the situation, l
might either be a LinkedList
or ArrayList
.
What would Java do? Notes:
HashSet
andArrayList
are both child classes ofCollection
.Collection
has anadd
andsize
method.ArrayList
additionally has asort
andget
method.
Collection c;
if (use_set) {
c = new HashSet();
} else {
c = new ArrayList();
}
c.isEmpty();
c.size();
c.sort();
c.get(0);
c.add(1);
c.add(1);
c.size(); // Will this equal 1 or 2?
File: CompileTimeErrorTest.java
· solution
public class CompileTimeErrorTest {
public string howOld(age) {
if age <= 18 {
return "Not very old";
} else if (age > 21) {
return "Really old";
}
}
}
File: RuntimeErrorTest.java
· solution
public class RuntimeErrorTest {
private Person p;
public RuntimeErrorTest() {
String personName = p.getName();
int nameLength = personName.length();
System.out.println(nameLength);
}
public static void main(String[] args) {
RuntimeErrorTest t = new RuntimeErrorTest();
}
}
class Person {
public String getName() {}
}
To get the car rolling!
File: Vehicle.java
· solution
import java.util.ArrayList;
public abstract class Vehicle {
int seats;
int wheels;
int fuel;
int mpg;
int trunkSize;
ArrayList<Object> trunk;
public Vehicle(int seats, int wheels, int fuel, int mpg) {
this.seats = seats;
this.wheels = wheels;
this.fuel = fuel;
this.mpg = mpg;
this.trunk = new ArrayList<Object>();
this.trunkSize = 0;
}
public void putInTrunk(Object item) {
System.out.println("There is no room in the Trunk");
}
float range() {
return fuel * mpg;
}
}
class Car extends Vehicle {
public Car(int fuel, int mpg) {
super(4, 4, fuel, mpg);
this.trunkSize = 2;
}
public void putInTrunk(Object item) {
if (this.trunk.size() < this.trunkSize) {
trunk.add(item);
} else {
super.putInTrunk(item);
}
}
}
class Motorcycle extends Vehicle {
public Motorcycle(int fuel, int mpg) {
super(1, 2, fuel, mpg);
}
}
/* Fill this class in assuming that the trunkSize of a Truck is 5 */
public class Truck extends Car {
public Truck(int fuel, int mpg) {
}
}
What will happen after each of these snippets of code are compiled/run?
Vehicle v1 = new Vehicle(3,4,20,10);
System.out.println("Range of v1: " + v1.range());
Vehicle v2 = new Car(20,20);
System.out.println("Range of v2: " + v2.range());
Vehicle v3 = new Motorcycle(10,40);
System.out.println("Range of v3: " + v3.range());
System.out.println("Number of seats of v2 " + v2.seats);
System.out.println("Number of seats of v3 " + v3.seats);
System.out.println("Number of wheels of v2" + v2.wheels);
System.out.println("Number of wheels of v3" + v3.wheels);
v2.putInTrunk("Backpack");
v2.putInTrunk("Laptop");
v2.putInTrunk("Shoes");
v3.putInTrunk("Backpack");
v3.putInTrunk("Laptop");
v3.putInTrunk("Shoes");