I have this code and everything works fine except the subtraction part. I can\'t
ID: 3549787 • Letter: I
Question
I have this code and everything works fine except the subtraction part. I can't use strings so please stick to my code. Also, i can't seem to change the sign for the subtraction part. Please help!
import java.util.Scanner;
public class Number{
private String whole;
private String decimal;
private String sign;
public static void main (String[] args){
System.out.println("Enter two numbers");
Scanner keyboard = new Scanner(System.in);
Double firstNumber = keyboard.nextDouble();
Double secondNumber = keyboard.nextDouble();
Number x = new Number (firstNumber);
Number y = new Number (secondNumber);
Number sum = x.add(y);
System.out.println ("x = " + x);
System.out.println ("y = " + y);
System.out.println("x + y = " + sum);
Number subtract = x.Subtract(y);
System.out.println("x - y = " + subtract);
}
public Number(){
whole = "0";
decimal = "0";
sign = "+";
}
public Number (double n){
whole = "0";
decimal = "0";
sign = "+";
String temp = new Double(n).toString();
if (temp.charAt(0) == '-'){
sign = "-";
temp = temp.substring(1);
}
int pos = temp.indexOf(".");
if (pos == -1)
whole = temp;
else
{
whole = temp.substring (0,pos);
decimal = temp.substring (pos+1);
}
}
public Number add (Number RHS){
this.alignWhole (RHS);
this.alignDecimal (RHS);
return this.addNum (RHS);
}
private void alignWhole (Number RHS){
int firstWholeL = this.whole.length();
int secondWholeL = RHS.whole.length();
int dif = firstWholeL - secondWholeL;
if (dif > 0){
for (int i = 1; i <= dif; i++)
RHS.whole = "0" + RHS.whole;
}
else if (dif < 0)
{
dif = Math.abs (dif);
for (int i = 1; i <= dif; i++)
this.whole = "0" + this.whole;
}
}
private void alignDecimal (Number RHS) {
int firstDecimalL = this.decimal.length ( );
int secondDecimalL = RHS.decimal.length ( );
int dif = firstDecimalL - secondDecimalL;
if (dif > 0)
{
for (int i = 1; i <= dif; i++)
RHS.decimal = RHS.decimal + "0";
}
else if (dif < 0)
{
dif = Math.abs (dif);
for (int i = 1; i <= dif; i++)
this.decimal = this.decimal + "0";
}
}
private Number addNum (Number RHS){
Number sum = new Number();
sum.decimal = "";
int carry = 0;
int decimalL = this.decimal.length();
for (int i = decimalL -1; i >= 0; i--)
{
char firstC = this.decimal.charAt(i);
char secondC = RHS.decimal.charAt(i);
int sumX = (firstC - 48) + (secondC - 48) + carry;
char sumC = (char)((sumX % 10) + 48);
carry = sumX / 10;
sum.decimal = sumC + sum.decimal;
}
sum.whole = "";
int wholeL = this.whole.length();
for (int i = wholeL -1; i >= 0; i--)
{
char firstC = this.whole.charAt (i);
char secondC = RHS.whole.charAt (i);
int sumX = (firstC - 48) + (secondC - 48) + carry;
char sumC = (char)((sumX % 10) + 48);
carry = sumX / 10;
sum.whole = sumC + sum.whole;
}
if (carry != 0)
sum.whole = "1" + sum.whole;
return sum;
}
public Number Subtract (Number RHS){
this.SubtractalignWhole (RHS);
this.SubtractalignDecimal (RHS);
return this.subNum (RHS);
}
private void SubtractalignWhole (Number RHS){
int firstWholeL = this.whole.length();
int secondWholeL = RHS.whole.length();
int dif = firstWholeL - secondWholeL;
if (dif > 0){
for (int i = 1; i <= dif; i++)
RHS.whole = "0" + RHS.whole;
}
else if (dif < 0)
{
dif = Math.abs (dif);
for (int i = 1; i <= dif; i++)
this.whole = "0" + this.whole;
}
}
private void SubtractalignDecimal (Number RHS) {
int firstDecimalL = this.decimal.length ( );
int secondDecimalL = RHS.decimal.length ( );
int dif = firstDecimalL - secondDecimalL;
if (dif > 0)
{
for (int i = 1; i <= dif; i++)
RHS.decimal = RHS.decimal + "0";
}
else if (dif < 0)
{
dif = Math.abs (dif);
for (int i = 1; i <= dif; i++)
this.decimal = this.decimal + "0";
}
}
private Number subNum (Number RHS){
Number subtract = new Number();
subtract.decimal = "";
int carry = 0;
int decimalL = this.decimal.length();
for (int i = decimalL -1; i >= 0; i--)
{
char firstC = this.decimal.charAt(i);
char secondC = RHS.decimal.charAt(i);
int sumX = (firstC - 48) - (secondC - 48) + carry;
char sumC = (char)((sumX % 10) + 48);
carry = sumX / 10;
subtract.decimal = sumC + subtract.decimal;
}
subtract.whole = "";
int wholeL = this.whole.length();
for (int i = wholeL -1; i >= 0; i--)
{
char firstC = this.whole.charAt (i);
char secondC = RHS.whole.charAt (i);
int sumX = (firstC - 48) - (secondC - 48) + carry;
char sumC = (char)((sumX % 10) + 48);
carry = sumX / 10;
subtract.whole = sumC + subtract.whole;
}
if (carry != 0)
subtract.whole = "1" + subtract.whole;
return subtract;
}
public String toString()
{
return sign + whole + "." + decimal;
}
}
Explanation / Answer
package chegg.crap;
import java.util.Scanner;
public class Number
{
private String whole;
private String decimal;
private String sign;
public static void main (String [ ] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter first number (in double format)");
Number numLhs = new Number(in.nextDouble());
System.out.println("Enter second Number");
Number numRhs = new Number(in.nextDouble());
in.close();
Number sum = numLhs.add(numRhs);
Number diff = numLhs.subtract(numRhs);
System.out.println("Sum fo numbers::"+sum.toString());
System.out.println("difference of numbers::"+diff.toString());
}
public Number(double n){
String thisNum = new Double(n).toString();
if(n>0){
this.sign="+";
this.whole = thisNum.substring(0, thisNum.indexOf("."));
this.decimal = thisNum.substring(thisNum.indexOf(".")+1);
}
else if(n<0){
this.sign="-";
this.whole = thisNum.substring(1, thisNum.indexOf("."));
this.decimal = thisNum.substring(thisNum.indexOf(".")+1);
}
}
public Number ( )
{
whole = "0";
decimal = "0";
sign = "+";
}
public Number add (Number RHS)
{
this.align(RHS);
if(RHS.sign.equals("-")){
return this.subtractNum(RHS);
}
else
return this.addNum(RHS);
}
public Number subtract(Number RHS)
{
this.align(RHS);
if(RHS.sign.equals("-")){
return addNum(RHS);
}
else
return this.subtractNum(RHS);
}
private Number addNum (Number RHS)
{
Number sum = new Number ( );
sum.decimal = "";
int carry = 0;
int decimalL = this.decimal.length ( );
for (int i = decimalL -1; i >= 0; i--)
{
char firstC = this.decimal.charAt (i);
char secondC = RHS.decimal.charAt (i);
int sumX = (firstC - 48) + (secondC - 48) + carry;
char sumC = (char)((sumX % 10) + 48);
carry = sumX / 10;
sum.decimal = sumC + sum.decimal;
}
sum.whole = "";
int wholeL = this.whole.length ( );
for (int i = wholeL -1; i >= 0; i--)
{
char firstC = this.whole.charAt (i);
char secondC = RHS.whole.charAt (i);
int sumX = (firstC - 48) + (secondC - 48) + carry;
char sumC = (char)((sumX % 10) + 48);
carry = sumX / 10;
sum.whole = sumC + sum.whole;
}
if (carry != 0)
sum.whole = "1" + sum.whole;
return sum;
}
private Number align(Number RHS){
int rhsDotPosition = RHS.toString().indexOf(".");
int numDotPosition = this.toString().indexOf(".");
String rhsWhole = RHS.toString().substring(1, rhsDotPosition);
String rhsDecimal = RHS.toString().substring(rhsDotPosition+1);
String numWhole = this.toString().substring(1, numDotPosition);
String numDecimal = this.toString().substring(numDotPosition+1);
int rhsWholeLength = rhsWhole.length();
int rhsDecimalLength = rhsDecimal.length();
int numWholeLength = numWhole.length();
int numDecimalLength = numDecimal.length();
if(rhsWholeLength>numWholeLength){
for(int i=0; i<rhsWholeLength-numWholeLength; i++){
numWhole = "0"+numWhole;
}
this.whole = numWhole;
}
else if(numWholeLength>rhsWholeLength){
for(int i=0; i<numWholeLength-rhsWholeLength; i++){
rhsWhole = "0"+rhsWhole;
}
RHS.whole = rhsWhole;
}
else{
// do nothing
}
if(rhsDecimalLength>numDecimalLength){
for(int i=0; i<rhsDecimalLength-numDecimalLength; i++){
numDecimal = numDecimal+"0";
}
this.decimal = numDecimal;
}
else if(numDecimalLength>rhsDecimalLength){
for(int i=0; i<numDecimalLength-rhsDecimalLength; i++){
rhsDecimal = rhsDecimal+"0";
}
RHS.decimal = rhsDecimal;
}
else{
// do nothing
}
return RHS;
}
private Number subtractNum(Number RHS)
{
Number difference = new Number ( );
difference.decimal = "";
difference.whole = "";
int carry = 0;
for(int i=this.decimal.length()-1;i>=0;i--){
char thisChar = this.decimal.charAt(i);
char rhsChar = RHS.decimal.charAt(i);
int thisLast = (((int)thisChar)-48)+carry;
int rhsLast = ((int)rhsChar)- 48;
if(thisLast<rhsLast){
carry = -1;
thisLast = thisLast+10;
difference.decimal = String.valueOf(thisLast-rhsLast)+difference.decimal;
}
else{
carry = 0;
difference.decimal = String.valueOf(thisLast-rhsLast)+difference.decimal;
}
}
for(int i=this.whole.length()-1;i>=0;i--){
char thisChar = this.whole.charAt(i);
char rhsChar = RHS.whole.charAt(i);
int thisLast = (((int)thisChar)-48)+carry;
int rhsLast = ((int)rhsChar)- 48;
if(thisLast<rhsLast){
carry = -1;
thisLast = thisLast+10;
difference.whole = String.valueOf(thisLast-rhsLast)+difference.whole;
}
else{
carry=0;
difference.whole = String.valueOf(thisLast-rhsLast)+difference.whole;
}
}
return difference;
}
public String toString ( )
{
return sign + whole + "." + decimal;
}
}