Wednesday, May 21, 2014

Linked List of Lists: Listception

My second assignment was pretty cool since I started separating things out into several classes (separated here by comment lines). The program description is below.

package hanson_h_2;

//hailey hanson
//Programming Assignment 2
//April 30th, 2014
//
//This program will read in from a text file, separate the words and get rid of punctuation and lowercase letters (making them all caps)
//then it will sort the words into alphabetical order and print them in a list that includes the numbered position
//of the word so that instead of listing a word twice, it will be listed as word: #occurance, #occurance, etc.
//

public class hanson_h_2{

public static void main(String[] args) {

wordIndex wi = new wordIndex("C:\\Users\\vicki\\Desktop\\java_text_file.txt");
}
}
/********************************************************************/

package hanson_h_2;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class wordIndex {

boolean statusVar;
wList myList = new wList();

int counter=0; //will keep track of word numbers

public wordIndex(String filename) {

Scanner s = null;
String word = null;

try {
s = new Scanner(new File(filename));
while(s.hasNext()){
word = s.next();
word = word.toUpperCase();
word = word.replaceAll("[^a-zA-Z]",  "");
counter++;
myList.add(word, counter);
statusVar = true;
}
} catch (FileNotFoundException e) {
statusVar = false;
}
print();
}

boolean status(){
if (statusVar == true)
return true;
else
return false;
}

void print(){
// prints the sorted list of words - one per line - followed by a space-separated
// list of references (in increasing order). The reference is the word #,
// beginning with 1 for the first word, 2 for the second, etc.
if(status() == true)
myList.print();
else if (status() == false)
System.out.println("Sorry, couldn't open file");


}

}

/********************************************************************************/

package hanson_h_2;

public class wList {

wNode header; //this is the sentinel node

public wList() {//not sure about these parameters
header = new wNode(null, null);//empty list (header points to zero and is zero and stuff)
}//end of wList constructor

void add(String data, int index){ //will add new wNode onto wList in alphabetical order
wNode newNode = null;

if (header.next == null){ //if list is empty
newNode = new wNode(data, header.next);
header.next = newNode;
newNode.list = new rList();
newNode.list.add(index);
}//end of if statement

else{ //there is already at least one thing in the list
wNode traverseNode = header.next; //will step through list to check for comparisons between these 2 nodes

while (traverseNode != null){//look through wList
if (data.equals(traverseNode.data)){ //if two wNodes have the same data
traverseNode.list.add(index); //add the index of the word onto the rList
return; //get outta town... add method town
}//end of if statement

traverseNode =  traverseNode.next; //move to  next node in wList
}//end of while loop

traverseNode = header.next; //if the node is not already in wList, move back to beginning of wList
wNode previousNode = header;

while (traverseNode != null){
if (data.compareTo(traverseNode.data) < 0){
newNode = new wNode(data, traverseNode); //make new node and put it in before right node
previousNode.next = newNode; //change previous pointer
break;
}//end of if statement
if (traverseNode.next == null){ //if this is the last thing in the list
newNode = new wNode(data, traverseNode.next); //make your node with the data piece, put it at end
traverseNode.next = newNode; //switch previous node's pointer to point to new node
break;
}//end of if statement
previousNode = traverseNode;
traverseNode = traverseNode.next; //move along through wList
}//end of while loop

newNode.list = new rList();
newNode.list.add(index);//add index number to the rList

}//end of else
}//end of add method

public void print(){
wNode currentNode = header.next; //start at beginning on wList
while (currentNode != null){
System.out.print(currentNode.data);//print the word
currentNode.list.print(); //print its position
System.out.println("\n"); //print a newline
currentNode = currentNode.next; //go to next node
}//end of while
}//end of print()

public class wNode
{

String data; //the data will be a word from my index
wNode next; //pointer to next node
rList list; //header node to rList. Is number associated with word

public wNode(String data, wNode next){
this.data = data;
this.next = next;
}//end of wNode constructor
}//end of wNode class
}//end of wList class

/********************************************************************************/

package hanson_h_2;

public class rList {

rNode header;

public rList() {
header = new rNode(0, null);
}

void add(int ref){  //adds word to list and ref to the end of the list
rNode currentNode = header.nextNode;

if (header.nextNode == null){
rNode newNode = new rNode(ref, header.nextNode);
header.nextNode = newNode;
}
else {
while(currentNode.nextNode != null) //while i am not at the end of the list
currentNode = currentNode.nextNode; //step forward one node
rNode newNode = new rNode(ref, currentNode.nextNode);
currentNode.nextNode = newNode;
}
}

public void print(){
rNode currentNode = header.nextNode;
while (currentNode != null){
System.out.print(" " + currentNode.index);
currentNode = currentNode.nextNode;
}
}

private class rNode {

int index; //data piece of node to tell where word is located in file
rNode nextNode; //pointer to next node
//should maybe set nextNode equal to null...?

public rNode(int ref, rNode next) {
index = ref;
nextNode = next;
}

}

}

The Beginnings of Java: Rational Numbers Program

So here I am just under a month away from my summer vacation. I met with an adviser this morning at WSU-Vancouver and decided that I won't be taking a class this summer since it won't take any time off of my last two years of school. So this means that my near future will consist of making coffee, enjoying the sun and hopefully getting a clue on GitHub. My adviser advised me (ha) to think about taking on an English minor since I told her I really like writing. I'm kind of torn, though, since I can't see giving more time and money to school while I'm already struggling to pay my tuition for the next two years.

The struggle is real.

I started working at a coffee shop a few weeks ago, on top of my Physics TA job, so I've been extremely busy trying to balance those, a Tech Writing class and also learning Java!
I meant to post my Java Hello World program forever and a day ago, but now it seems insignificant since I'm doing big scary programs like this. This was my first assignment in my Advanced Data Structures class and, as I've mentioned, I love clean, organized writing so I like this code :)

//Hailey Hanson
// April 18, 2014
//Programming Assignment 1-Rational Numbers
//
//This program will take in rational numbers and add, subtract, multiple, divide, reduce and convert them to doubles (if prompted)
//

public class Rat {

private int num; //numerator of fraction
private int denom; //denominator of fraction

Rat (int num, int denom)//will be referenced when there are two inputs
{
this.num = num;
this.denom = denom;
}

Rat (int num)//will be referenced with there is one input
{
this.num = num;
this.denom = 1;
}

public int num() //returns numerator of x (a rational number)
{
return num; //return x's numerator piece
}

public int denom()
{
return denom; //returns x's denominator piece
}

public Rat add(Rat n) //adds "this" Rat to n Rat
{
int newnum = (this.num()*n.denom())+(this.denom()*n.num()); //makes new numerator
int newdenom = (this.denom()*n.denom()); //makes new denominator
Rat newRat = new Rat(newnum,newdenom);//makes new Rat using new num and denom
newRat.reduce();
return newRat;
}

public Rat sub(Rat n) //subtracts "this" Rat and n Rat
{
int newnum = (this.num()*n.denom())-(this.denom()*n.num());//makes new numerator
int newdenom = (this.denom()*n.denom());//makes new denominator
Rat newRat = new Rat(newnum,newdenom);//makes new Rat
newRat.reduce();
return newRat;
}

public Rat times(Rat n) //multiplies "this" Rat and n Rat
{
Rat newRat = new Rat(n.num()*this.num(), n.denom()*this.denom());//makes new Rat
newRat.reduce();
return newRat;
}

public Rat div(Rat n)//divides "this" Rat and n Rat
{
Rat newRat = new Rat(this.num()*n.denom(), n.num()*this.denom());//makes new Rat
newRat.reduce();
return newRat;
}

public double toDouble()//converts Rat to a double
{
if (this.denom == 0)//if undefined
{
if (this.num()>0)//undefined and positive = infinity
{
return(Double.POSITIVE_INFINITY);
}
else if(this.num() == 0)//undefined and 0 = NaN
{
return(Double.NaN);
}
else if (this.num() <0 and="" negative="-infinity</p" undefined=""> {
return(Double.NEGATIVE_INFINITY);
}
}
return ((double) this.num()/this.denom());
}

private int GCF(int a,int b)//find greatest common factor to use in reduce method
{
if (a%b ==0) return b;
else while(a%b != 0)
{
int x = a%b;
a=b;
b=x;
}
return b;
}

public void reduce()//would normally be private, but will be public for testing purposes
{
if (denom !=0)
{
if(num == 0)
{
denom = 1;//special request for assignment
}
else
{
int gcf = GCF(num, denom);
num=num/gcf; ///reduce numerator
denom=denom/gcf;//reduce denominator
}
}
}

public void print() //print method
{
this.reduce(); //reduce Rat  before printing

if(denom == 1)
{
System.out.println(num);//reduce fractions with denominator of 1
}
else if (denom < 0 )
{
System.out.println(this.num*(-1) + "/"+ (-1)*this.denom()); //fix negative fractions
}
else
{
System.out.println(this.num() + "/" + this.denom()); //print normal rational fraction
}
}
}