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
}
}
}0>
No comments:
Post a Comment