source codes discussed in class earlier

here are the source codes that were discussed earlier:

========

public class ListClass {

Node list;

Node last;

public ListClass()

{

list = new Node(“”);

last = list;

}

public void addElement(String d)

{

Node newNode = new Node(d);

last.setNext(newNode);

last = newNode;

}

public void addElement(String d, int index)

{

Node pointer = list;

for(int i = 0; i < index; i++)

{

pointer = pointer.getNext();

}

Node newNode = new Node(d);

newNode.setNext(pointer.getNext());

pointer.setNext(newNode);

}

public String getElement(int index)

{

Node pointer = list;

for(int i = 0; i <= index; i++)

{

pointer = pointer.getNext();

}

return pointer.getData();

}

public void printList()

{

Node pointer = list;

while(pointer != null)

{

System.out.println(pointer.getData());

pointer = pointer.getNext();

}

}

public void deleteElement(int index)

{

Node pointer = list;

for(int i = 0; i < index; i++)

{

pointer = pointer.getNext();

}

pointer.setNext(pointer.getNext().getNext());

}

public static void main(String[] args) {

ListClass sample = new ListClass();

sample.addElement(“hello”);

sample.addElement(“world”);

sample.addElement(“How are you?”);

sample.printList();

sample.addElement(“Philip”);

sample.addElement(“Christian”);

sample.addElement(“Zuniga”);

sample.addElement(“Hello”, 3);

sample.deleteElement(4);

sample.printList();

// TODO Auto-generated method stub

}

}

============

public class Node {

private String data;

private Node next;

public Node(String d)

{

data = d;

next = null;

}

public String getData()

{

returndata;

}

public Node getNext()

{

returnnext;

}

public void setData(String d)

{

data = d;

}

public void setNext(Node n)

{

next = n;

}

}

=============

import java.util.ArrayList;

public class Person {

private String name;

privateintage;

private ArrayList<Person> friends;

public Person(String n, int a)

{

name = n;

age = a;

friends = new ArrayList<Person>();

}

public String getName()

{

returnname;

}

public int getAge()

{

returnage;

}

public ArrayList<Person> getFriends()

{

return friends;

}

public void setName(String n)

{

name = n;

}

publicvoid setAge(int a)

{

age = a;

}

public void addFriend(Person p)

{

if (!friends.contains(p))

{

friends.add(p);

p.getFriends().add(this);

}

}

public void deleteFriend(Person p)

{

friends.remove(p);

p.getFriends().remove(this);

}

public ArrayList<Person> getMutualFriends(Person p)

{

ArrayList<Person> mutual = new ArrayList<Person>();

for(int i = 0; i < friends.size();i++)

{

if (p.getFriends().contains(friends.get(i)))

mutual.add(friends.get(i));

}

returnmutual;

}

public void printAllFriends()

{

for(int i = 0; i < friends.size(); i++)

System.out.println(friends.get(i).getName());

}

public ArrayList<Person> getNthDegreeFriends(int n)

{

ArrayList<Person> previousConnected = new ArrayList<Person>();

ArrayList<Person> currentConnected = new ArrayList<Person>();

ArrayList<Person> temp = new ArrayList<Person>();

currentConnected.addAll(friends);

for(int i = 0; i <= n; i++)

{

for(int j = 0; i < currentConnected.size(); j++)

{

temp.removeAll(temp);

for(int k = 0; k < currentConnected.get(j).getFriends().size(); k++)

{

if(!(currentConnected.contains(currentConnected.get(j).getFriends().get(k))))

{

if (!(previousConnected.contains(currentConnected.get(i).getFriends().get(k))))

{

temp.add(currentConnected.get(i).getFriends().get(k));

}

}

}

}

previousConnected.addAll(currentConnected);

currentConnected.removeAll(currentConnected);

currentConnected.addAll(temp);

}

return currentConnected;

}

public ArrayList<Person> getNthMutualFriends(Person p, int n)

{

ArrayList<Person> list1 = this.getNthDegreeFriends(n);

ArrayList<Person> list2 = p.getNthDegreeFriends(n);

ArrayList<Person> mutual = new ArrayList<Person>();

for (int i = 0; i < list1.size(); i++)

{

if (list2.contains(list1.get(i)))

mutual.add(list1.get(i));

}

return mutual;

}

}

=============

import java.util.ArrayList;

public class Polynomial {

privateintdeg;

private ArrayList coeffs;

public Polynomial(int d, ArrayList c)

{

deg = d;

coeffs.addAll(c);

}

public int evaluate(int x)

{

int eval = 0;

for(int i = 0; i < coeffs.size(); i++)

{

eval = (int) (eval + (int)coeffs.get(i) * Math.pow((double)x, (double)i));

}

returneval;

}

public ArrayList getCoeffs()

{

returncoeffs;

}

public int getDegree()

{

returndeg;

}

public Polynomial multiplyPoly(Polynomial p)

{

Polynomial s = new   Polynomial(0, null);

for(int i = 0;i < this.deg;i++)

{

s = s.addPoly(DistributeTerm(p, (int) this.coeffs.get(i), i));

}

returns;

}

public Polynomial DistributeTerm(Polynomial p, int term, int index)

{

ArrayList d = new ArrayList();

for (int i = 0; i < index; i++)

{

d.add(0);

}

for(int i = 0; i < p.getDegree(); i++)

{

d.add((int)p.getCoeffs().get(i)*term);

}

return new Polynomial(p.getDegree() + index, d);

}

public Polynomial addPoly(Polynomial p)

{

int d = Math.max(p.getDegree(), this.getDegree());

int e = Math.min(p.getDegree(),  this.getDegree());

ArrayList s  = new ArrayList();

for (int i = 0; i < e; i ++)

{

s.add((int)p.getCoeffs().get(i) + (int) this.coeffs.get(i));

}

for(int i = e; i < d; i++)

{

if(p.getDegree() == d)

{

s.add(p.getCoeffs().get(i));

}

else

{

s.add(this.coeffs.get(i));

}

}

return new Polynomial(d,s);

}

}

===============

public class Rational {

privateintnum;

privateintden;

privatebooleansign;

public Rational(int n, int d, boolean s)

{

int g = gcd(n,d);

num = n/g;

den = d/g;

sign = s;

}

public Rational (int n, boolean s)

{

num = n;

den = 1;

sign = s;

}

publicint gcd(int a, int b)

{

if (a <= b)

return gcd(b,a);

else

{

int c = a % b;

if (c == 0)

return b;

else

{

return gcd(b, c);

}

}

}

public int getNum()

{

returnnum;

}

public int getDen()

{

returnden;

}

publicboolean getSign()

{

returnsign;

}

publicvoid setNum(int n)

{

int g = gcd(n, den);

num = n/g;

den = den/g;

}

publicvoid setDen(int d)

{

int g = gcd(num, d);

num = num/g;

den = d/g;

}

publicvoid setSign(boolean s)

{

sign = s;

}

public boolean isPositive()

{

returnsign;

}

public boolean isProperFraction()

{

if (num < den)

returntrue;

else

returnfalse;

}

public boolean isImproperFraction()

{

if (num >= den)

returntrue;

else

returnfalse;

}

public boolean isWholeNumber()

{

if (den == 1 || num == 0)

returntrue;

else

returnfalse;

}

public Rational add(Rational r)

{

if(sign == r.sign)

{

int a = num * r.getDen() + den * r.getNum();

int b = den*r.getDen();

if (sign)

return new Rational(a,b,true);

else

return new Rational(a,b,false);

}

else

{

if(sign)

{

int a = num*r.getDen() – den *r.getNum();

int b = den*r.getDen();

if(a > 0)

return new Rational(a, b, true);

else

return new Rational(a, b, false);

}

else

{

int a = den * r.getNum() – num*r.getDen();

int b = den*r.getDen();

if(a > 0)

return new Rational(a, b, true);

else

return new Rational(a, b, false);

}

}

}

public Rational subtract(Rational b)

{

Rational c = new Rational(b.getNum(), b.getDen(), b.getSign());

c.setSign(!c.getSign());

return add(c);

}

public Rational Multiply(Rational r)

{

int a = num * r.getNum();

int b = den * r.getDen();

if (sign == r.getSign())

return new Rational(a, b, true);

else

return new Rational(a, b, false);

}

public Rational Divide(Rational r)

{

if(r.getNum() == 0)

returnnull;

else

{

Rational s = new Rational(r.getDen(), r.getNum(), r.getSign());

return Multiply(s);

}

}

public void Print()

{

if (sign)

{

System.out.println(num + “/” + den);

}

else

{

System.out.println(“-“ + num + “/” + den);

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

Rational p  = new Rational (3, 6, true);

p.Print();

Rational q = new Rational (1, 3, false);

q.Print();

p.subtract(q).Print();

q.Print();

}

}