Problem:
Design an algorithm that will handle conversions to decimal where the input character string may contains a decimal point.
Solution:
package com.myprograms;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class DecimalCharacterToIntegerConversion {
static String numberAsString;
List<Integer> integralPart = new ArrayList<Integer>();
List<Integer> fractionalPart = new ArrayList<Integer>();
public static void main(String[] args) {
DecimalCharacterToIntegerConversion characterToIntegerConversion = new DecimalCharacterToIntegerConversion();
characterToIntegerConversion.getTheNUmberAsString();
String[] number = numberAsString.split("\\.");
characterToIntegerConversion.convertIntegralPartToDecimal(number[0]);
characterToIntegerConversion.convertFractionalPartToDecimal(number[1]);
characterToIntegerConversion.printTheResult();
}
public void getTheNUmberAsString(){
Scanner s = new Scanner(System.in);
System.out.println("enter the characters which contain decimal point" );
numberAsString = s.nextLine();
s.close();
}
public void convertIntegralPartToDecimal(String s){
for(Character c: s.toCharArray()){
integralPart.add((int)c);
}
}
public void convertFractionalPartToDecimal(String s){
for(Character c: s.toCharArray()){
fractionalPart.add((int)c);
}
}
public void printTheResult(){
int integralNumber = 0;
int fractionallNumber = 0;
for(Integer n : integralPart){
integralNumber = integralNumber * 10 + n;
}
for(Integer n : fractionalPart){
fractionallNumber = fractionallNumber * 10 + n;
}
System.out.println("The ascii values for integralPart: " + integralPart);
System.out.println("The ascii values for fractionalPart: " + fractionalPart);
System.out.println("the decimal number is: " + integralNumber + "." + fractionallNumber);
}
}
Output:
enter the characters which contain decimal point
1984.84
The ascii values for integralPart: [49, 57, 56, 52]
The ascii values for fractionalPart: [56, 52]
the decimal number is: 55312.612
Design an algorithm that will handle conversions to decimal where the input character string may contains a decimal point.
Solution:
package com.myprograms;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class DecimalCharacterToIntegerConversion {
static String numberAsString;
List<Integer> integralPart = new ArrayList<Integer>();
List<Integer> fractionalPart = new ArrayList<Integer>();
public static void main(String[] args) {
DecimalCharacterToIntegerConversion characterToIntegerConversion = new DecimalCharacterToIntegerConversion();
characterToIntegerConversion.getTheNUmberAsString();
String[] number = numberAsString.split("\\.");
characterToIntegerConversion.convertIntegralPartToDecimal(number[0]);
characterToIntegerConversion.convertFractionalPartToDecimal(number[1]);
characterToIntegerConversion.printTheResult();
}
public void getTheNUmberAsString(){
Scanner s = new Scanner(System.in);
System.out.println("enter the characters which contain decimal point" );
numberAsString = s.nextLine();
s.close();
}
public void convertIntegralPartToDecimal(String s){
for(Character c: s.toCharArray()){
integralPart.add((int)c);
}
}
public void convertFractionalPartToDecimal(String s){
for(Character c: s.toCharArray()){
fractionalPart.add((int)c);
}
}
public void printTheResult(){
int integralNumber = 0;
int fractionallNumber = 0;
for(Integer n : integralPart){
integralNumber = integralNumber * 10 + n;
}
for(Integer n : fractionalPart){
fractionallNumber = fractionallNumber * 10 + n;
}
System.out.println("The ascii values for integralPart: " + integralPart);
System.out.println("The ascii values for fractionalPart: " + fractionalPart);
System.out.println("the decimal number is: " + integralNumber + "." + fractionallNumber);
}
}
Output:
enter the characters which contain decimal point
1984.84
The ascii values for integralPart: [49, 57, 56, 52]
The ascii values for fractionalPart: [56, 52]
the decimal number is: 55312.612
No comments:
Post a Comment