Showing posts with label linkedlist. Show all posts
Showing posts with label linkedlist. Show all posts

Computing the polynomial mathematical function in java

Problem:

A polynomial function is a very famous mathematical function.
The greatest exponent, n, is called the degree of the polynomial. For example, p(x) = 7x4 2 is a
polynomial of degree 4. The simplest polynomials are constant polynomials such as p(x) = 6
(degree 0) and linear polynomials such as p(x) = 9x + 6 (degree 1). The unique zero polynomial
p(x) = 0 is defined to have degree 1. In this section we present a Polynomial class whose
instances represent mathematical polynomials and which supports the usual algebraic operations
on polynomials.
A polynomial can be regarded as a sum of distinct terms. A term is a mathematical function of
the form t (x) = cxe, where c is any real number and e is any nonnegative integer. The number c is
called the coefficient, and the number e is called the exponent.
To define a class whose objects represent polynomials, we use a linked list of Term objects.
For example, the polynomial p(x) = 3x2 2x + 5 could be represented as a list of three elements,
where the first element represents the term 3x2, the second element represents the term 2x, and
the third element represents the (constant) term 5.

Write a java program that computes the polynomial function.

Output:

Not applicable

Solution:

public class Polynomial {
private List<Term> list = new LinkedList<Term>();
public static final Polynomial ZERO = new Polynomial();
private Polynomial() { // default constructor
}
public Polynomial(double coef, int exp) {
if (coef != 0.0) {
list.add(new Term(coef, exp));
}
}
public Polynomial(Polynomial p) { // copy constructor
for (Term term : p.list) {
this.list.add(new Term(term));
}
}
public Polynomial(double... a) {
for (int i=0; i<a.length; i++) {
if (a[i] != 0.0) {
list.add(new Term(a[i], i));
}
}
}
public int degree() {
if (list.isEmpty()) {
return -1;
} else {
return list.get(list.size()-1).exp;
}
public boolean isZero() {
return list.isEmpty();
}
public Polynomial plus(Polynomial p) {
if (this.isZero()) {
return new Polynomial(p);
}
if (p.isZero()) {
return new Polynomial(this);
}
Polynomial q = new Polynomial();
ListIterator<Term> it = list.listIterator();
ListIterator<Term> itp = p.list.listIterator();
while (it.hasNext() && itp.hasNext()) {
Term term = it.next();
Term pTerm = itp.next();
if (term.exp < pTerm.exp) {
q.list.add(new Term(term));
itp.previous();
} else if (term.exp == pTerm.exp) {
q.list.add(new Term(term.coef + pTerm.coef, term.exp));
} else { // (term.exp > pTerm.exp)
q.list.add(new Term(pTerm));
it.previous();
}
}
while (it.hasNext()) {
q.list.add(new Term(it.next()));
}
while (itp.hasNext()) {
q.list.add(new Term(itp.next()));
}
return q;
}
public String toString() {
if (this.isZero()) {
return "0";
}
Iterator<Term> it = list.iterator();
StringBuilder buf = new StringBuilder();
boolean isFirstTerm = true;
while (it.hasNext()) {
Term term = it.next();
double c = term.coef;
int e = term.exp;
if (isFirstTerm) {
buf.append(String.format("%.2f", c));
isFirstTerm = false;
} else {
if (term.coef < 0) {
buf.append(String.format(" - %.2f", -c));
} else {
buf.append(String.format(" + %.2f", c));
}
}
if (e == 1) {
buf.append("x");
} else if (e > 1) {
buf.append("x^" + e);
}
}
return buf.toString();
}
private static class Term {
private double coef;
private int exp;
public Term(double coef, int exp) {
if (coef == 0.0 || exp < 0) {
throw new IllegalArgumentException();
}
this.coef = coef;
this.exp = exp;
}
public Term(Term that) { // copy constructor
this(that.coef, that.exp);
}
}
}
Read More

Testing the LinkedHashSet Class in Java

Problem:

Write a java problem that tests the LinkedHashSet class.

Output:

[IT, VA, SM, CH]
[IT, SM, CH]
[IT, SM, CH, VA]

Solution:

public class TestLinkedHashSet {
public static void main(String[] args) {
Set<String> ital = new LinkedHashSet<String>();
Collections.addAll(ital, "IT", "VA", "SM", "CH");
System.out.println(ital);
ital.remove("VA");
System.out.println(ital);
ital.add("VA");
System.out.println(ital);
}
}
Read More

Testing the TreeSet Class in Java

Problem:

Write a java problem that tests the treeset class.

Output:

[GB, IN, NG, PH, PK, US, ZA]
[GB, IN, KE, NG, PH, PK, US, ZA]
[GB, IN] [KE, NG, PH, PK] [US, ZA]
engl.first(): GB
engl.last(): ZA
engl.lower("KE"): IN
engl.higher("KE"): NG

Solution:

public class TestTreeSet {
public static void main(String[] args) {
NavigableSet<String> engl = new TreeSet<String>();
Collections.addAll(engl, "IN", "US", "PK", "NG", "PH", "GB", "ZA");
System.out.println(engl);
engl.add("KE");
System.out.println(engl);
SortedSet<String> head = engl.headSet("KE");
SortedSet<String> mid = engl.subSet("KE", "US");
SortedSet<String> tail = engl.tailSet("US");
System.out.printf("%s %s %s%n", head, mid, tail);
System.out.printf("engl.first(): %s%n", engl.first());
System.out.printf("engl.last(): %s%n", engl.last());
}
}
Read More

Testing the HashSet Class in Java

Problem:

Write a program that tests 10 of the 15 methods of HashSet class in java.

Output:

set.isEmpty(): true
[FR, US, RU, GB, CN]
set.size(): 5
set.contains("GB"): true
set.contains("JP"): false
set.isEmpty(): false
[FR, US, RU, BR, GB, CN]
[US, RU, BR, GB, CN]
US RU BR GB CN
[JP, FR, US, RU, GB, DE, IT, CA]
[US, RU, GB]
[BR, CN]
[US, RU, BR, GB, CN]
[]

Solution:

public class TestHashSet {
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
System.out.printf("set.isEmpty(): %b%n", set.isEmpty());
Collections.addAll(set, "CN", "FR", "GB", "RU", "US");
System.out.println(set);
System.out.printf("set.size(): %d%n", set.size());
System.out.printf("set.contains(\"GB\"): %b%n", set.contains("GB"));
System.out.printf("set.contains(\"JP\"): %b%n", set.contains("JP"));
System.out.printf("set.isEmpty(): %b%n", set.isEmpty());
set.add("BR");
System.out.println(set);
set.remove("FR");
System.out.println(set);
String[] array = set.toArray(new String[0]);
for (String string : array) {
System.out.printf("%s ", string);
}
System.out.println("");
Set<String> g8 = new HashSet<String>();
Collections.addAll(g8, "CA", "DE", "FR", "GB", "IT", "JP", "RU", "US");
System.out.println(g8);
g8.retainAll(set);
System.out.println(g8);
set.removeAll(g8);
System.out.println(set);
set.addAll(g8);
System.out.println(set);
set.clear();
System.out.println(set);
}
}
Read More

implement delete() method of linkedList

Problem:

Implement the delete() method of a Linked List

Output:

Not applicable.

Solution:

Node delete(Node start, int x) {
// precondition: the list is in ascending order;
// postconditions: the list is in ascending order, and if it did
// contains x, then the first occurrence of x has been deleted;
if (start == null || start.data > x) { // x is not in the list
return start;
} else if (start.data == x) { // x is the first element in the list
return start.next;
}
for (Node p = start; p.next != null; p = p.next) {
if (p.next.data > x) {
break; // x is not in the list
} else if (p.next.data == x) { // x is in the p.next node
p.next = p.next.next; // delete it
break;
}
}
return start;
}
Read More

Inserting into a Nonempty Sorted Linked List of Integers

Problem:

Write a method that adds elements into a Nonempty Sorted Linked List of Integers

Output:

Not applicable.

Solution:

void insert(Node start, int x) {
// PRECONDITIONS: the list is in ascending order, and x > start.data;
// POSTCONDITIONS: the list is in ascending order, and it contains x;
Node p = start;
while (p.next != null) {
if (p.next.data > x) break;
p = p.next;
}
p.next = new Node(x,p.next);
}
Read More

Writing a node class with two constructors

Problem:

Write a node class with two constructors

Output:

Not applicable.

Solution:

class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
Node(int data, Node next) {
this.data = data;
this.next = next;
}
}
Read More

Implementing an ordered list using LinkedList and ListIterator in Java

Problem:

Implement an ordered list using LinkedList and ListIterator (the one in the book). You need to provide the following methods: AddSorted, Retrieve, Delete and Search.

Output:

Not available.

Solution:

/**------------------MyComparable.java---------------------*/
public interface MyComparable 
{
 public int MyCompareTo(Object O);
}

/**------------------OrderedList.java---------------------*/
import java.util.*;
public class OrderedList<E extends MyComparable>
{
 private LinkedList<E> linkedList = new LinkedList<E>();
 private ListIterator<E> listIterator;

 public void AddSorted(E entry)
 {
  listIterator = linkedList.listIterator();
  
  while(listIterator.hasNext())
  {
   if(entry.MyCompareTo(listIterator.next())>0)
   {
    listIterator.previous();
    listIterator.add(entry);
    return;
   }
  }
  
  listIterator.add(entry);
 }
 
 public void Delete(E entry)
 {
  listIterator=linkedList.listIterator();
  
  while(listIterator.hasNext())
  {
   if(entry.MyCompareTo(listIterator.next())==0)
   {
    listIterator.remove();
    
    System.out.println("Deleted");
    
    return;
   }    
  }
 }
 
 public E Retrieve(E entry)
 {
  listIterator = linkedList.listIterator();
  
  while(listIterator.hasNext())
  {
   if( entry.MyCompareTo(listIterator.next()) == 0)
   {
    return listIterator.previous();
   }    
  }
  
  throw new NoSuchElementException();
 }
 
 public void Search(E entry)
 {
  listIterator = linkedList.listIterator();
  
  while(listIterator.hasNext())
  {
   if(entry.MyCompareTo(listIterator.next())==0)
   {
    System.out.println("Found");
    return;
   }    
  }
  System.out.println("Not Found");
 }
 
}
/**----------------------Students.java-------------------------*/
public class Student implements MyComparable
{
 private String name;
 private int id;
 
 
 public Student(String name, int id) {
  this.name = name;
  this.id = id;
 }
 
 public String getName() {
  return name;
 }


 public void setName(String name) {
  this.name = name;
 }


 public int getId() {
  return id;
 }


 public void setId(int id) {
  this.id = id;
 }
 
 public int MyCompareTo(Object o)
    {
        Student student =(Student) o;
        
        if(id < student.getId())
            return -1;
        
        else if(id > student.getId())
            return 1;
        
        else
            return 0;
    }
 
 public String toString() {
  return "Student, " + name + "\n" + "Id, " + id;
 }
}
/**-------------------------Tester.java------------------------------*/
public class Tester
{
 public static void main(String[] args)
 {
  OrderedList<Student> student = new OrderedList<Student>();
  
   student.AddSorted(new Student("name1",1));
   student.AddSorted(new Student("name2",2));
   student.AddSorted(new Student("name3",3));
   student.AddSorted(new Student("name4",4));
   student.AddSorted(new Student("name5",5));

   System.out.println("Searching for \"name1\": ");
   //Searching based on id number not name
   student.Search(new Student("name1",1)); 
   System.out.println();
  
   System.out.println("Retrieving \"name1\": ");
   System.out.println(student.Retrieve(new Student("name1",1)));
   System.out.println();
  
   System.out.println("Deleting \"name1\": ");
   student.Delete(new Student("name1",1));
   System.out.println();
   
   System.out.println("Re-searching for  \"name1\": ");
   student.Search(new Student("name1",1));
   System.out.println();
  
 }
}
Read More

Implementing a Market System using LinkedList in Java

Problem:

Product
- id : int
- name : String
- expYear : int
+ Product(id : int, name : String, expYear : int)
+ setId(id : int) : void
+ getId() : int
+ setName(name : String) : void
+ getName(): String
+ setExpYear(expYear : int) : void
+ getExpYear() : int

Market
- products : LinkedList<Product>
+ Market()
+ getProducts() : LinkedList<Product>
+ addProduct(Product product) : void
+ expireProducts() : void

You are asked to implement a simple market system. The system is composed of two classes which are Product and Market.
The Product class has three private variables which are id, name, and expiration year. The class’ constructor should initialize these variables and there should be setter and getter methods for them.
The Market class has one private variable which is a linked list of type Product. The constructor should initialize the linked list. There are three methods to implement in this class. The first method is getProducts which returns the linked list. The method addProduct should add the product in its parameter to the linked list. Finally, expireProducts should remove from the list the products which have an expiration year less than 2012.
Write a tester class that has a Market object and add five products using the addProducts method. After adding, print the list, then call expireProducts and print the list after removing products that have an expiration year less than 2012.


Solution:

/**-----------------------Market.java------------------*/
import java.util.LinkedList;
import java.util.ListIterator;

public class Market {
 
 private LinkedList products;
 
 public Market() {
  products = new LinkedList();
 }
 
 public LinkedList getProducts() {
  return products;
 }
 
 public void addProduct(Product product) {
  products.add(product);
 }
 
 public void expireProducts() {
  ListIterator itr = products.listIterator(0);
  
  while (itr.hasNext()) {
   Product temp = itr.next();
   
   if (temp.getExpYear() < 2012)
    itr.remove();
  }
 }
}

/*-----------------------OrderedList.java-------------*/
import java.util.LinkedList;
import java.util.ListIterator;

public class OrderedList> {
 
 private LinkedList list;
 
 public OrderedList() {
  list = new LinkedList();
 }
 
 public void addSorted(E item) {
  ListIterator itr = list.listIterator(0);
  
  while (itr.hasNext()) {
   E temp = itr.next();
   
   if (item.compareTo(temp) < 0) {
    itr.previous();
    itr.add(item);
    return;
   }
  }
  
  list.add(item);
 }
 
 public E retrieve(E item) {
  ListIterator itr = list.listIterator(0);
  
  while (itr.hasNext()) {
   E temp = itr.next();
   
   if (temp.equals(item))
    return temp;
  }
  
  return null;
 }
 
 public void delete(E item) {
  ListIterator itr = list.listIterator(0);
  
  while (itr.hasNext()) {
   E temp = itr.next();
   
   if (temp.equals(item)) {
    itr.remove();
    return;
   }
  }
 }
 
 public int search(E item) {
  ListIterator itr = list.listIterator(0);
  
  while (itr.hasNext()) {
   E temp = itr.next();
   
   if (temp.equals(item))
    return itr.previousIndex();
  }
  
  return -1;
 }
}

/**-----------------------Product.java---------------------*/
public class Product {

 private int id;
 private String name;
 private int expYear;
 
 public Product(int id, String name, int expYear) {
  this.id = id;
  this.name = name;
  this.expYear = expYear;
 }
 
 public int getId() {
  return id;
 }
 
 public void setId(int id) {
  this.id = id;
 }
 
 public String getName() {
  return name;
 }
 
 public void setName(String name) {
  this.name = name;
 }
 
 public int getExpYear() {
  return expYear;
 }
 
 public void setExpYear(int expYear) {
  this.expYear = expYear;
 }
 
 public String toString() {
  return "Product "+id+": "+name+" "+expYear;
 }
}
Read More

Creating a LinkedList Application in Java

Problem:

Item
- type : String
- name : String
- price : int
+ item(type : String, name : String, price : int)
// setters getters
+ toString() : String

Employee

- name : String
- id : int 
+ Employee(name : String, id : int)
 // setters getters
 + toString() : String


Supermarket
-item: LinkedList<Item>
 - Employee : Linked List <Employee>
+Supermarket()
+ addItem(item : Item) : void
+ deleteltem(name : String) : void
+ getAllChocolates() : LinkedList<Item>
+ getAllDairy() : LinkedList<Item>
+ getAllCandies() : LinkedList<Item>
+ hasItem(name : String) : Boolean
-requestOrder(name : String) : void
+ printltems() : void



You have to implement the previous three classes. The class Item has three private variables, a constructor, setters and getters, and a toString method. The class Employee has two private variables, a constructor, setters and getters, and a toString method. The class Supermarket has a LinkedList of items, a linked list of employees, and a couple of methods that apply operations on the LinkedList.

-addEmployee( employee: Employee): Inserts the specific employee at the beginning of the list -removeEmployees(): Removes all elements from this list
- addItem(item : Item): takes an item as parameter and adds it to items
- deleteltem (name : String): deletes an item from Item with the specified name
-getAllChocolates (): returns a LinkedList<Item> that contains all the items that have a type equal to Chocolate in items
- getAllDairy (): returns a LinkedList< Item > that contains all the items that have a type equal to Dairy in items
– getAllCandies (): returns a LinkedList<Item> that contains all the items that have a type equal to candies In items
 -hasItem(name : String): returns true if the item with the specified name is found in items; else, it returns false and calls requestOrder(name)
- requestOrder(name : String): prints "Requesting an order for "+name
- printltems(): prints the elements in item

In the tester class, you are required to create an object of type Supermarket and add to its lists a number of items and employees.

You should add each new employee to the beginning of the list.

You should retrieve all items that are chocolate and print them, retrieve all items that are dairy and print them, and retrieve all items that are candies and print them.

You should test the deleteltem method and print the items list after deletion.

Test the requestOrder method and verify that it is calling the requestOrder method if the item is not found in the list.

Finally, remove all employees and hire new employees.

Solution:

/**-----------------Employee.java-----------------------*/
public class Employee {
 
 private String name;
 private int id;
 
 public Employee(String name, int id) {
  this.name = name;
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 @Override
 public String toString() {
  return "[name=" + name + ", id=" + id + "]";
 }
}

/**-----------------Item.java-----------------------*/

public class Item {
 
 private String type;
 private String name;
 private int price;
 
 public Item(String type, String name, int price) {
  this.type = type;
  this.name = name;
  this.price = price;
 }

 public String getType() {
  return type;
 }

 public void setType(String type) {
  this.type = type;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getPrice() {
  return price;
 }

 public void setPrice(int price) {
  this.price = price;
 }

 @Override
 public String toString() {
  return "[type=" + type + ", name=" + name 
                             + ", price=" + price + "]";
 }
}


/**--------------SumperMarket.java-------------*/
import java.util.LinkedList;

public class SuperMarket {
 
 private LinkedList<Item> items;
 private LinkedList<Employee> employees;
 
 public SuperMarket() {
  items = new LinkedList<Item>();
  employees = new LinkedList<Employee>();
 }
 
 public void addItem(Item item) {
  items.add(item);
 }
 
 public void addEmployee(Employee employee) {
  employees.addFirst(employee);
 }
 
 public void deleteItem(String name) {
  for (int i = 0; i < items.size(); i++)
   if (items.get(i).getName().equals(name)) {
    items.remove(i);
    return;
   }
 }
 
 public void removeEmployees() {
  employees.clear();
 }
 
 public LinkedList<Item> getAllChocolates() {
  LinkedList<Item> chocolates = new LinkedList<Item>();
  
  for (int i = 0; i < items.size(); i++)
   if (items.get(i).getType().equalsIgnoreCase("chocolate"))
    chocolates.add(items.get(i));
  
  return chocolates;
 }
 
 public LinkedList<Item> getAllDairy() {
  LinkedList<Item> dairies = new LinkedList<Item>();
  
  for (int i = 0; i < items.size(); i++)
   if (items.get(i).getType().equalsIgnoreCase("dairy"))
    dairies.add(items.get(i));
  
  return dairies;
 }
 
 public LinkedList<Item> getAllCandy() {
  LinkedList<Item> candies = new LinkedList<Item>();
  
  for (int i = 0; i < items.size(); i++)
   if (items.get(i).getType().equalsIgnoreCase("candy"))
    candies.add(items.get(i));
  
  return candies;
 }
 
 public boolean hasItem(String name) {
  for (int i = 0; i < items.size(); i++)
   if (items.get(i).getName().equals(name))
    return true;
  
  requestOrder(name);
  return false;
 }
 
 public void printVehicles() {
  System.out.println(items);
 }
 
 private void requestOrder(String name) {
  System.out.println("Requesting an order for "+name);
 }
}
Read More

Creating a ListIterator Application in Java

Problem:

Game
-          title : String
-          type : String
-          released : Boolean
-          price : int
-         copiesSold : int
       // Constructor(s)
      // setters getters
    + toString() : String


GameViewer

- games : LinkedList<game>
+ GameViewer()
+ addGame(game : Game) : void
+ deleteGanne(title : String) : void
+ getReleasedGames() : LinkedList<Game>
+ getMostSellingGame() : Game
+ getLeastSellingGame() : Game
+ getHighestPriceGame() : Game
+ getLowestPriceGame() : Game
+ getAllGames() : LinkedList<Game>
+ printGames() : void


You have to implement the previous two classes. The class Game has four private variables, a constructor, setters and getters, and a toString method. The class GameViewer has a LinkedList of games and a couple of methods that apply operations on the LinkedList.

- addGame(game : game): takes a game as parameter and adds it to games
- deleteGame(title : String): deletes a game from games with the specified title
- getReleasedGames(): returns a LinkedList<Game> that contains all the games that have been released (released = true)
- getMostSellingGame (): returns the game with the most copies sold
- getleastSellingGame(): returns the game with the least copies sold
- getHighestPriceGanne(): returns the game with the highest price
- getLowestPriceGame(): returns the game with the lowest price
- getAllGames(): returns the games linked list
- printGames(): prints the games linked list
In the tester class, you are required to create an object of type GameViewer and add to its list a number of games.
You should test all of the class' methods and print the output of each in a well formed manner.
You should only use ListIterator to loop over the linked list. Any other way is not acceptable.



Solution:

/**-----------------------Game.java----------------------*/
public class Game {

 private String title;
 private String type;
 private boolean released;
 private int price;
 private int copiesSold;
 
 public Game(String title, String type, boolean released,
                                       int price, int copiesSold) {
  this.title = title;
  this.type = type;
  this.released = released;
  this.price = price;
  this.copiesSold = copiesSold;
 }

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public String getType() {
  return type;
 }

 public void setType(String type) {
  this.type = type;
 }

 public boolean isReleased() {
  return released;
 }

 public void setReleased(boolean released) {
  this.released = released;
 }

 public int getPrice() {
  return price;
 }

 public void setPrice(int price) {
  this.price = price;
 }

 public int getCopiesSold() {
  return copiesSold;
 }

 public void setCopiesSold(int copiesSold) {
  this.copiesSold = copiesSold;
 }

 @Override
 public String toString() {
   return "[title=" + title + ", type=" + type + ", released=" 
               + released + ", price=" + price + 
               ", copiesSold=" + copiesSold + "]";
 }
}
/**-----------------------GameViewer.java----------------------*/
import java.util.LinkedList;
import java.util.ListIterator;

public class GameViewer {

 private LinkedList<Game> games;
 
 public GameViewer() {
  games = new LinkedList<Game>();
 }
 
 public void addGame(Game game) {
  games.add(game);
 }
 
 public void deleteGame(String title) {
  ListIterator<Game> itr = games.listIterator(0);
  
  while (itr.hasNext()) {
   Game game = itr.next();
   if (game.getTitle().equalsIgnoreCase(title)) {
    itr.remove();
    return;
   }
  }
 }
 
 public LinkedList<Game> getReleasedGames() {
  ListIterator<Game> itr = games.listIterator(0);
  LinkedList<Game> released = new LinkedList<Game>();
  
  while (itr.hasNext()) {
   Game game = itr.next();
   if (game.isReleased())
    released.add(game);
  }
  
  return released;
 }
 
 public Game getMostSellingGame() {
  ListIterator<Game> itr = games.listIterator(0);
  Game mostSelling = games.getFirst();
  
  while (itr.hasNext()) {
   Game game = itr.next();
   if (game.getCopiesSold() > mostSelling.getCopiesSold())
    mostSelling = game;
  }
  
  return mostSelling;
 }
 
 public Game getLeastSellingGame() {
  ListIterator<Game> itr = games.listIterator(0);
  Game leastSelling = games.getFirst();
  
  while (itr.hasNext()) {
   Game game = itr.next();
   if (game.getCopiesSold() < leastSelling.getCopiesSold())
    leastSelling = game;
  }
  
  return leastSelling;
 }
 
 public Game getHighestPriceGame() {
  ListIterator<Game> itr = games.listIterator(0);
  Game highestPrice = games.getFirst();
  
  while (itr.hasNext()) {
   Game game = itr.next();
   if (game.getPrice() > highestPrice.getPrice())
    highestPrice = game;
  }
  
  return highestPrice;
 }
 
 public Game getLowestPriceGame() {
  ListIterator<Game> itr = games.listIterator(0);
  Game lowestPrice = games.getFirst();
  
  while (itr.hasNext()) {
   Game game = itr.next();
   if (game.getPrice() < lowestPrice.getPrice())
    lowestPrice = game;
  }
  
  return lowestPrice;
 }
 
 public LinkedList<Game> getAllGames() {
  return games;
 }
 
 public void printGames() {
  System.out.println(games);
 }
}
Read More

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact