Main file - PuppyL2.java
package puppyl2;
import java.util.Scanner;
public class PuppyL2 {
private Puppy first;
private Puppy last;
private int size;
public PuppyL2() {
this.size = 0;
}
/**
* Insert the element at the end of the queue
* @param name, age element
*/
public void addLast(String name, int age) {
Puppy n = new Puppy(name,age);
if (getSize() == 0) {
this.first = n;
this.last = n;
} else {
this.last.nnext = n;
this.last = n;
}
size++;
}
public int getSize() {
return size;
}
/**
* Query, whether is the queue empty
*
* @return true if the queue is empty, false otherwise
*/
public boolean isEmpty() {
return this.size == 0;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
Puppy curr = first;
for (int i = 0; i < this.size; i++) {
builder.append(curr.PuppyName);
builder.append("(").append(curr.PuppyAge).append("), ");
curr = curr.nnext;
}
return builder.toString();
}
/**
* Remove the first element
*
* @return element
*/
public String deteteFirst() {
if (getSize() == 0) {
throw new IllegalStateException("Queue is empty");
}
String value = first.PuppyName;
first = first.nnext;
size--;
return value;
}
/**
* Return the first element
* @return PuppyName
*/
public String getFirst() {
if (getSize() == 0) {
throw new IllegalStateException("Queue is empty");
}
return first.PuppyName;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PuppyL2 qq = new PuppyL2();
qq.addLast("Alik",5);
qq.addLast("Betty",7);
qq.addLast("Rex",3);
System.out.println(qq.deteteFirst());
System.out.println(qq); }
}
Second file - Puppy.java
package puppyl2;
public class Puppy {
String PuppyName;
int PuppyAge;
Puppy nnext;
Puppy(String name, int age) {
this.PuppyName = name;
this.PuppyAge = age;
}
@Override
public String toString() {
String r = new String();
r = this.PuppyName+"("+this.PuppyAge+"), ";
return r;
}
}