This assignment should help you review what has been taught in CSC 130, includin
ID: 3549430 • Letter: T
Question
This assignment should help you review what has been taught in CSC 130, including the fundamentals of software development and Java programming (program structure, data types, strings, I/O, flow of control) plus the basic object-oriented design.
You are to design a Java application to carry out additions and subtractions for numbers of any length. A number is represented as an object which includes a sign and two strings for the whole and decimal parts of the number. And, the operations must be done by adding or subtracting characters directly. You are not allowed to convert these strings to numbers before the operation.
The program must use a "Number" class which includes at least the following methods:
Number ( );
Number (double n);
Number add (Number RHS);
Number subtract (Number RHS);
String toString ( );
Demo the program
This is what i have so far, have to do subtraction and I think we have to ask the user to enter two numbers.
public class Number{
private String whole;
private String decimal;
private String sign;
public static void main (String[] args){
System.out.println("Please enter two numbers");
Number x = new Number (23459.12345);
Number y = new Number (-4.56);
Number sum = x.add(y); // x calls add method to pass y
System.out.println ("x = " + x);
System.out.println ("y = " + y);
System.out.println("sum = " + sum);
}
public Number(){
whole = "0";
decimal = "0";
sign = "+";
}
public Number (double n){ // convert the number to a String
whole = "0";
decimal = "0";
sign = "+";
String temp = new Double(n).toString();// Double object converts to a String
if (temp.charAt(0) == '-'){
sign = "-";
temp = temp.substring(1); // takes out the minus sign
}
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){ //(RHS = Right Hand Side),
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){//if first whole number is longer than second whole number
for (int i = 1; i <= dif; i++)
RHS.whole = "0" + RHS.whole;// adds zeros to the front
}
else if (dif < 0)//if second whole number is longer than first whole number
{
dif = Math.abs (dif);
for (int i = 1; i <= dif; i++)
this.whole = "0" + this.whole;// adds zeros to the end
}
}
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();
// how you carry
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 String toString()
{
return sign + whole + "." + decimal;
}
}
Explanation / Answer
public class Number {
String sign;
String wholeNum;
String decimal;
Number(){
}
Number(double n){
if(n>0)
this.sign=