Rational String To Rational Number

At this point we are able to view the entire project of rational numbers with a keyboard input as a string of rational numbers. The following diagram will give us an overview the outline of the steps for implementation.


Class Implementation Strategy:

  1. class Main

main method

  • get from keyboard the string of a rational numbers (bubble 1)
  • split the string into numStr and denumStr (bubble 2)
  1. class StrToNum (buble 3)
  2. StrToNumConversion to convert a string w/o sign to positive number.
  3. SignStrToNumConverssion to convert a string with sign to integer.
  4. class Rational (bubble 4)
  5. reduce rational number to its simplest form
  6. print it in fraction or in decimal with round-off at the hundredths.

Structure chart:

PHASE 1:

I decide to copy Lab2 phase 9. Here is the two classes:

import java.io.*;

import java.lang.*; // include it because of String

class StrToNum //

{

// characteristics or component

String strNum; //

// constructor to create an object from the caller who provides parameter

public StrToNum(String InputStrNum)

{ strNum = InputStrNum;}

// method to convert strNum into a number which is returned as an integer.

public int StrToNumConversion()

{

// < body of the conversion>

int Num = 0;

int totalNum = 0;

char firstChar;

while (strNum != null)

{ // body of while

//get the first char and save it to a char var

firstChar = strNum.charAt(0);

//reduce the strNum

if (strNum.length() == 1) strNum = null;

else strNum = strNum.substring(1);

//convert this char into a number

if (firstChar == '0') Num = 0;

else if (firstChar == '1') Num = 1;

else if (firstChar == '2') Num = 2;

else if (firstChar == '3') Num = 3;

else if (firstChar == '4') Num = 4;

else if (firstChar == '5') Num = 5;

else if (firstChar == '6') Num = 6;

else if (firstChar == '7') Num = 7;

else if (firstChar == '8') Num = 8;

else if (firstChar == '9') Num = 9;

else System.out.println ("ERROR");

totalNum = totalNum* 10 + Num;

} // End of While

// System.out.println (" After the while loop, total Number is " + totalNum);

return totalNum;

} // End of StrToNumConversion

} //End of class StrToNum

import java.io.*;

import java.lang.*; // include it because of String

import StrToNum.*;

class SampleCaller

{

// method to call, in this case, the main

public static void main(String[] args)

{

// read from the keyboard

InputStreamReader reader = new InputStreamReader(System.in);

BufferedReader console = new BufferedReader(reader);

System.out.print("please enter a string number>");

String input="";

try { input = console.readLine();}

catch (IOException e ){ System.out.println(e); System.exit(1); }

// declare an object variable the wrapping input

StrToNum A;

A = new StrToNum (input);

int Value = A.StrToNumConversion();

System.out.println("The equivalent value =" + Value);

} // End of main

} //End of class SampleCaller

PHASE 2:

Since StrToNumConverssion converts only string without sign to number, the new method to extend to string with sign is needed. Let call the new method SignStrToNumString. This new method should be in the class StrToNum. However, the class SampleCaller will call SignStrToNumConverssion(), instead call StrToNumConversion().

Here is the modified code of class StrToNum:

import java.io.*;

import java.lang.*;

class StrToNum //

{

// characteristics or component

String strNum; //

// constructor to create an object from the caller who provides parameter

public StrToNum(String InputStrNum)

{ strNum = InputStrNum;}

// method to convert strNum into a number which is returned as an integer.

public int StrToNumConversion()

{

// < body of the conversion>

int Num = 0;

int totalNum = 0;

char firstChar;

while (strNum != null)

{ // body of while

//get the first char and save it to a char var

firstChar = strNum.charAt(0);

//reduce the strNum

if (strNum.length() == 1) strNum = null;

else strNum = strNum.substring(1);

//convert this char into a number

if (firstChar == '0') Num = 0;

else if (firstChar == '1') Num = 1;

else if (firstChar == '2') Num = 2;

else if (firstChar == '3') Num = 3;

else if (firstChar == '4') Num = 4;

else if (firstChar == '5') Num = 5;

else if (firstChar == '6') Num = 6;

else if (firstChar == '7') Num = 7;

else if (firstChar == '8') Num = 8;

else if (firstChar == '9') Num = 9;

else System.out.println ("ERROR");

totalNum = totalNum* 10 + Num;

} // End of While

// System.out.println (" After the while loop, total Number is " + totalNum);

return totalNum;

} // End of StrToNumConversion

public int SignStrToNumConversion()

{

char firstChar= strNum.charAt(0);

int signValue = 1;

if (firstChar == '+') { strNum = strNum.substring(1);}

else if (firstChar == '-') { signValue = -1; strNum = strNum.substring(1);}

int value = StrToNumConversion();

value = signValue*value;

System.out.println("From SignStrToNumConversion; value =" + value);

return value;

} // End of SignStrToNumConversion

} //End of class StrToNum

Here is a modification of class SampleCaller:

import java.io.*;

import java.lang.*;

import StrToNum.*;

class SampleCaller

{

// method to call, in this case, the main

public static void main(String[] args)

{

// read from the keyboard

InputStreamReader reader = new InputStreamReader(System.in);

BufferedReader console = new BufferedReader(reader);

System.out.print("please enter a string number>");

// try-catch using IOException

String input="";

try

{

input = console.readLine();

}

catch(IOException e)

{

System.out.print("We CAUGHT an " + e);

System.exit(1);

}

// declare an object variable the wrapping input

StrToNum A;

A = new StrToNum (input);

int value = A.SignStrToNumConversion();

System.out.println("The equivalent value =" + value);

} // End of main

} //End of class

Testing:

A:\>javac SampleCaller.java

A:\>java SampleCaller

please enter a string number>-375

The equivalent value =-375

The equivalent value =-375

A:\>java SampleCaller

please enter a string number>+45

The equivalent value =45

The equivalent value =45

A:\>java SampleCaller

please enter a string number>207

The equivalent value =207

The equivalent value =207

A:\>

Inheritance:

Observation:

We add a new method SignStrToNumConversion() into the class StrToNum. This method will take care of the case the string has a sign symbol. That means it is an extension of the method StrToNumConversion().

In reality we might not have a source code for the class StrToNum to add the new method SignStrToNumConversion() because the class might be written by someone else. Hence, in this case we have no choice but to leave class StrToNum untouchable. However, we can build the new method, let’s name it StrToInt, which is an extension of the class StrToNum.

SPPage 110/28/2018