Return to Snippet

Revision: 34101
at October 17, 2010 15:22 by trusktr


Updated Code
import java.util.*;

public class Number {
	
	/* Data Objects */
		private double value;
	
	/* Constructors */
		public Number(){
			value = 0;
		}
		public Number(double inputvalue){
			value = inputvalue;
		}
		public Number(Number n){
			value = n.value;
		}
	
	/* Accessors */
		public double value(){
			double n = value;
			return n;
		}
	
	/* Mutators */
		public void set_value(double inputvalue){
			value = inputvalue;
		}
		public void set_value(Number inputvalue){
			value = inputvalue.value;
		}
		public MyString myString(int n){
			Formatter fmt = new Formatter();
			MyString output = new MyString("" + fmt.format("%."+n+"f", value));
			return output;
		
			// MY OWN ALGORITHM FOR SETTING THE PRECISION OF MY NUMBER.
//			// say we start with value=17.55745 and we want a decimal precision of n=2.
//			// make an int version of double:
//			int b = (int)value; // b == 17
//			// subtract int version from double to leave the decimal places:
//			double c = value - b; // c = 17.55745 - 17 == .55745
//			// multiply by (10^n) to find the decimals.
//			double d = c * Math.pow(10,n); // d == 55.745
//			// make another int of the decimals:
//			int e = (int)d; // e == 55
//			// subtract the int from the decimal double:
//			double f = d - e; // f == .745
//			// if f is < 0.5 then leave e alone. // e == 55
//			// if f is >= 0.5
//			if ( f >= 0.5 ) {
//				// then add 1 to e:
//				e++; // e == 56
//			}
//			// now output the parts as a string:
//			MyString output = new MyString("" + b + "." + e);
//			return output;
			
			
//			// say we start with value=17.00745 and we want a decimal precision of n=2.
//			// make an int version of double:
//			int b = (int)value; // b == 17
//			// subtract int version from double to leave the decimal places:
//			double c = value - b; // c = 17.00745 - 17 == .00745
//			// multiply by (10^n) to find the decimals.
//			double d = c * Math.pow(10,n); // d == 00.745
//			// make another int of the decimals:
//			int e = (int)d; // e == 0
//			if (e < Math.pow(10,n-1)) {
//				
//			}
//			// subtract the int from the decimal double:
//			double f = d - e; // f == .745
//			// if f is < 0.5 then leave e alone. // e == 55
//			// if f is >= 0.5
//			if ( f >= 0.5 ) {
//				// then add 1 to e:
//				e++; // e == 56
//			}
//			// now output the parts as a string:
//			MyString output = new MyString("" + b + "." + e);
//			return output;
		}
		public MyString myString(){
			return new MyString("" + value);
		}
		public String toString(){
			return "" + value;
		}

		public Number plus(Number n){
			return new Number(value + n.value);
		}
		public Number plus(double n){
			return new Number(value + n);
		}
		public Number minus(Number n){
			return new Number(value - n.value);
		}
		public Number minus(double n){
			return new Number(value - n);
		}
		public Number times(Number n){
			return new Number(value * n.value);
		}
		public Number times(double n){
			return new Number(value * n);
		}
		public Number over(Number n){
			return new Number(value / n.value);
		}
		public Number over(double n){
			return new Number(value / n);
		}
}

Revision: 34100
at October 17, 2010 11:57 by trusktr


Initial Code
import java.util.*;

public class Number {
	
	/* Data Objects */
		private double value;
	
	/* Constructors */
		public Number(){
			value = 0;
		}
		public Number(double inputvalue){
			value = inputvalue;
		}
		public Number(Number n){
			value = n.value;
		}
	
	/* Accessors */
		public double value(){
			double n = value;
			return n;
		}
	
	/* Mutators */
		public void set_value(double inputvalue){
			value = inputvalue;
		}
		public void set_value(Number inputvalue){
			value = inputvalue.value;
		}
		public MyString myString(int n){
			Formatter fmt = new Formatter();
			MyString output = new MyString("" + fmt.format("%."+n+"f", value));
			return output;
		
			// MY OWN ALGORITHM FOR SETTING THE PRECISION OF MY NUMBER.
//			// say we start with value=17.55745 and we want a decimal precision of n=2.
//			// make an int version of double:
//			int b = (int)value; // b == 17
//			// subtract int version from double to leave the decimal places:
//			double c = value - b; // c = 17.55745 - 17 == .55745
//			// multiply by (10^n) to find the decimals.
//			double d = c * Math.pow(10,n); // d == 55.745
//			// make another int of the decimals:
//			int e = (int)d; // e == 55
//			// subtract the int from the decimal double:
//			double f = d - e; // f == .745
//			// if f is < 0.5 then leave e alone. // e == 55
//			// if f is >= 0.5
//			if ( f >= 0.5 ) {
//				// then add 1 to e:
//				e++; // e == 56
//			}
//			// now output the parts as a string:
//			MyString output = new MyString("" + b + "." + e);
//			return output;
			
			
//			// say we start with value=17.00745 and we want a decimal precision of n=2.
//			// make an int version of double:
//			int b = (int)value; // b == 17
//			// subtract int version from double to leave the decimal places:
//			double c = value - b; // c = 17.00745 - 17 == .00745
//			// multiply by (10^n) to find the decimals.
//			double d = c * Math.pow(10,n); // d == 00.745
//			// make another int of the decimals:
//			int e = (int)d; // e == 0
//			if (e < Math.pow(10,n-1)) {
//				
//			}
//			// subtract the int from the decimal double:
//			double f = d - e; // f == .745
//			// if f is < 0.5 then leave e alone. // e == 55
//			// if f is >= 0.5
//			if ( f >= 0.5 ) {
//				// then add 1 to e:
//				e++; // e == 56
//			}
//			// now output the parts as a string:
//			MyString output = new MyString("" + b + "." + e);
//			return output;
		}
		public MyString myString(){
			return new MyString("" + value);
		}
		public String toString(){
			return "" + value;
		}

		public Number plus(Number n){
			return new Number(value + n.value);
		}
		public Number plus(double n){
			return new Number(value + n);
		}
		public Number minus(Number n){
			return new Number(value - n.value);
		}
		public Number minus(double n){
			return new Number(value - n);
		}
		public Number times(Number n){
			return new Number(value * n.value);
		}
		public Number times(double n){
			return new Number(value * n);
		}
		public Number over(Number n){
			return new Number(value / n.value);
		}
		public Number over(double n){
			return new Number(value / n);
		}
}

Initial URL


Initial Description


Initial Title
cisp401 Number.java

Initial Tags
java

Initial Language
Java