Q1: 20 points Q2: 10 points Q3: 15 points Q4: 15 points Q5: 10 points Q6: 10 points Q7: 20 points Total: 100 points
import java.util.*; // we will use Stack
public class Dog {
private String name;
private int age;
public Dog (String newName, int newAge) {
name = newName;
age = newAge;}
public String getName {return name;}
public void setName (String newName) {name = newName;}
public int getAge ( ) {return age;}
public void setAge (int newAge) {age = newAge;}}
Question 7 uses this generic class from DJW:
public class LLNode<T> {
private LLNode link;
private T info;
public LLNode (T info) {this.info = info; link = null;}
public void setInfo (T info) {this.info = info;}
public T getInfo( ) {return info;}
public void setLink (LLNode link) {this.link = link;}
public LLNode getLink ( ) {return link;}}
String [ ]
and String [ ] [ ]
ArrayStringLog( )
and ArrayStringLog (int k)
constructors in DJW
ArrayStringLog
(DJW) and StringBag
(Project #1) classes
pop
in java.util.Stack
and pop
in DJW's stack classes
StringBag
called replace
, which takes a String
as a
parameter and
replaces a randomly-chosen element of the bag with the parameter, keeping all
the other elements in the same position.
isUnique
to
SudokuSolver
that takes a Board
as a parameter and
returns a boolean that is true
if the Board
denotes
a puzzle with one and only one solution. (The return value should be
false
if there is no solution or if there are more than one.)
Dog
class from above is present.
Include a brief justification of your answer.
// uses java.util.Stack names for methods
// Missing parens added 28 Sep 2014
Stack<Dog> left = new Stack<Dog>( );
Stack<Dog> right = new Stack<Dog>( );
right.push(new Dog("Duncan", 3)):
right.push(new Dog("Biscuit", 3));
right.push(new Dog("Cardie", 5));
right.push(new Dog("Ace", 6));
Dog newDog = new Dog("Sydney", 3));
while (!right.empty( ) && (right.peek( ).getAge( ) > newDog.getAge ( ))
left.push(right.pop( ));
right.push(newDog);
System.out.println(left.peek( ).getName( ));
int [ ] row = new int[9];
for (int i = 0; i < 9; i++)
row[i] = i + 1;
boolean duplicate = false;
for (int j = 0; j < 9; j++)
for (int k = 0; k < 9; k++)
if (row[j] == row[k]) duplicate = true;
if (duplicate)
System.out.println ("Duplicate element exists");
else System.out.println ("Elements are unique");
Dog ace = new Dog ("Ace", 6);
Dog biscuit = new Dog ("Biscuit", 3);
Dog cardie = new Dog ("Cardie", 5);
Dog golden = cardie;
Dog pointer = ace;
pointer.setName("Cardie");
golden.setName(ace.getName( ));
biscuit.setName(cardie.getName( ));
pointer.setName("Biscuit");
System.out.println(ace.getName( ));
System.out.println(cardie.getName( ));
public class Group<T> {
private T [ ] elements;
public Group ( ) {
elements = new T[100];}}
ArrayStack as = new ArrayStack (10);
as.push (new Dog ("Ace", 6));
as.push (new Dog ("Biscuit", 3));
as.push (new Dog ("Cardie", 5));
while (as.top( ) != null)
as.pop( );
as.push (new Dog ("Cardie", 5));
Integer [ ] ia = new Integer [10];
for (int i = 0; i < 5; i++)
ia[i] = i;
for (int j = 0; j < ia.length; j++)
ia[j]++;
LinkedStringLog
class)
public int sumLengths ( ) {
int sum = 0;
LLStringNode cur = log; // "log" is the head of the list of N elements
while (cur != null) {
sum += cur.getInfo( ).length( );
cur = cur.getLink( );}
return sum;}
// create a Board object b with all cells unfixed
// read an array of N Move objects and call b.move( ) for each
// set all nonzero elements of b to be fixed
// solve the resulting sudoku puzzle using the solution to Project #2
Kennel
so that each Kennel
object will
have an array dogs
of Dog
objects, and an
int
variable size
that will keep track of how
many dogs are in the array. Don't worry about error handling -- if bad input
to one of your methods causes an exception, that is fine. The class should
have the following methods:
Kennel (int k)
that creates an object that can
hold up to k Dog
objects,
boolean
method free (int pos)
that tells
whether position pos
now has no Dog
in it,
void
method insert (Dog d, int pos)
that
puts d
into position pos
if it is free, and does
nothing if it is not,
remove (int pos)
that removes and returns the
Dog
in position pos
, or returns null
if there is no Dog
there, and
void
method consolidate
that moves all
the Dog
objects to an initial segment of the locations -- to
locations 0
through size - 1
, where size
is the number of Dog
objects currently stored.
Dog
objects. An
AscendingAgeDogList
must have the property that every
Dog
in it has an age less than or equal to that of any
Dog
following it. A DescendingAgeDogList
has the
opposite property, that every Dog
in it has an age greater than
or equal to that of any Dog
following it. (You may abbreviate
these classes as AADL
and DADL
.)
(Note: These two classes are obviously going to be similar to each other. You may save yourself writing by designing one of them, and then clearly indicating what has to be changed to get the other.)
Using the LLNode<Dog>
class from above, define each class
to have the following methods:
void
method add (Dog d)
that puts
d
d at the head of the list if that meet's the list's
conditions, and does nothing otherwise,
boolean
method canAdd (Dog d)
that tells
whether d
may legally be added at the front of the list,
remove( )
that removes and returns the
Dog
from the head of the list, returning null
if the
list is empty, and
void
method insert (Dog d)
that inserts
d
into the list at the first legal place.
(Hint: The most difficult method to implement is insert
.
But you can do it by removing the objects that are in the way of the place
to insert, and storing them in an object of the other class.)
Last modified 28 September 2014