Hi,
I've been trying to figure out this problem for a while, but I am completely stumped. I have to do the following.
Farey fractions of level one are defined as sequence ( 0/1, 1/1). This sequence is extended in level two to form a sequence (0/1, 1/2, 1/1), sequence (0/1, 1/3, 1/2, 2/3, 1/1) at level three, sequence (0/1, 1/4, 1/3, 1/2, 2/3, 3/4, 1/1) at level four, so that each level n, a new fraction a + b / c + d is inserted between two neighbor fractions a/c and b/d only if c + d <= n. Write a program which for a number n entered by the user creates--by constantly extending it-- a linked list of fractions at level n and then displays them.
I have the following classes
Fraction Class:
Farey Class:Code:public class Fraction
{
int numerator;
int denominator;
public Fraction(int num, int den)
{
numerator = num;
denominator = den;
}
//Outputs the fraction using the toString method.
public String toString()
{
return numerator + "/" + denominator;
}
public int getNumerator()
{
return numerator;
}
//Stores the denominator
public int getDenominator()
{
return denominator;
}
//Calculates if the two fractions are equal using cross multiplication.
public boolean equal(Fraction other)
{
if ((denominator * other.numerator) == (other.denominator * numerator))
return true;
else
return false;
}
}
SLL Class:Code:import java.util.Scanner;
public class Farey extends SLL <Fraction>
{
public static void main(String[] args)
{
int a, b, c, d;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the number of levels.");
int n = scan.nextInt();
System.out.println(n);
Fraction first = new Fraction(0, 1);
Fraction second = new Fraction(1, 1);
SLLNode<Fraction> tmp = new SLLNode<Fraction>(null);
SLLNode<Fraction> p = new SLLNode<Fraction>(first);
p.next = new SLLNode(second);
SLL<Fraction> list = new SLL<Fraction>();
list.addToHead(p.next.info);
list.addToHead(p.info);
list.printAll();
//System.out.println(p.info.toString() + ", " + p.next.info.toString());
while (p.info != null)
{
if (p.info.denominator != n - 1)
{
tmp = new SLLNode<Fraction>(list.deleteFromTail());
}
else if (p.info.denominator == n - 1 && p.info.numerator == 1)
{
}
}
}
}
SLLNode Class:Code:public class SLL<T> {
protected SLLNode<T> head, tail;
public SLL() {
head = tail = null;
}
public boolean isEmpty() {
return head == null;
}
public void addToHead(T el) {
head = new SLLNode<T>(el,head);
if (tail == null)
tail = head;
}
public void addToTail(T el) {
if (!isEmpty()) {
tail.next = new SLLNode<T>(el);
tail = tail.next;
}
else head = tail = new SLLNode<T>(el);
}
public T deleteFromHead() { // delete the head and return its info;
if (isEmpty())
return null;
T el = head.info;
if (head == tail) // if only one node on the list;
head = tail = null;
else head = head.next;
return el;
}
public T deleteFromTail() { // delete the tail and return its info;
if (isEmpty())
return null;
T el = tail.info;
if (head == tail) // if only one node in the list;
head = tail = null;
else { // if more than one node in the list,
SLLNode<T> tmp; // find the predecessor of tail;
for (tmp = head; tmp.next != tail; tmp = tmp.next);
tail = tmp; // the predecessor of tail becomes tail;
tail.next = null;
}
return el;
}
public void delete(T el) { // delete the node with an element el;
if (!isEmpty())
if (head == tail && el.equals(head.info)) // if only one
head = tail = null; // node on the list;
else if (el.equals(head.info)) // if more than one node on the list;
head = head.next; // and el is in the head node;
else { // if more than one node in the list
SLLNode<T> pred, tmp;// and el is in a nonhead node;
for (pred = head, tmp = head.next;
tmp != null && !tmp.info.equals(el);
pred = pred.next, tmp = tmp.next);
if (tmp != null) { // if el was found;
pred.next = tmp.next;
if (tmp == tail) // if el is in the last node;
tail = pred;
}
}
}
public void printAll() {
for (SLLNode<T> tmp = head; tmp != null; tmp = tmp.next)
System.out.print(tmp.info + ", ");
}
public boolean isInList(T el) {
SLLNode<T> tmp;
for (tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next);
return tmp != null;
}
}
The main class is the farey one. Ignore most of the code in that class, I was just making sure it was functional. The part I'm confused with is if the user inputs a large n, I would need to have to use p.next.next...next. I am missing a simpler way with dealing with this? I'm not really sure where to go from here. We are not supposed to change the SLL or SLLNode classes.Code:public class SLLNode<T> {
public T info;
public SLLNode<T> next;
public SLLNode() {
this(null,null);
}
public SLLNode(T el) {
this(el,null);
}
public SLLNode(T el, SLLNode<T> ptr) {
info = el; next = ptr;
}
}
Thank you
