Can someone help me please? I need help writing this code using Java language. Step 1 The
Char Type Create a complex type called Char that models the primitive type char. In your class
you are going to want to have the following constructors: Java Description Charo Char0 Default
constructor that sets the data section of the class to null (binary 0) overloaded constructor that
takes a primitive char as an argument. It should set the data Char (char c) Char(char c) section of
the class to the argument. overloaded constructor that takes a primitive int as a parameter. The
data section of the Char(int co Char(int c) class should be set as a character from the argument.
Chan(const Char(inal Overloaded constructor that takes the complex Char type as a parameter,
The data section Char &c;) Char c) of the class should be set with the data section of the
argument Char(string Char (String overloaded Constructor that takes a string type as a parameter.
The data section of the class should be set to the first character in the string. Your class should
have the following mutators
Solution
Contains:
1. Custom classes - Char.java and BigDecimal.java
2. Exception classes - CharException.java and BigDecimalException.java
3. Test Classes - CharTest.java and BigDecimalTest.java
Char.java
**
* Custom Char class
* @author Anonymous
*
*/
public class Char {
private char value;
public Char(){
this.value = \'\\0\';
}
public Char(char c){
this.value = c;
}
public Char(int c){
this.value = (char)c;
}
public Char(final Char c){
this.value = c.getValue();
}
public Char(String s){
this.value = s.charAt(0);
}
public char getValue(){
return this.value;
}
public void equals(final Char c){
this.value = c.getValue();
}
public void equals(char c){
this.value = c;
}
public void equals(int c) throws CharException{
if(c<32 || c>127)
throw new CharException(\"Invalid Character\");
this.value = (char) c;
}
public char toChar(){
return this.value;
}
public int toInt(){
return (int)this.value;
}
public String toString(){
return String.valueOf(this.value);
}
/**
* To convert char to Hex string
* @return
*/
public String toHexString(){
return String.format(\"%04x\", (int)this.value);
}
public String add(char c){
return toString().concat(String.valueOf(c));
}
public String add(Char c){
return toString().concat(String.valueOf(c.getValue()));
}
}
BigDecimal.java
import java.util.ArrayList;
import java.util.List;
/**
* Custom BigDecimal class
* @author Anonymous
*
*/
public class BigDecimal {
List list;
/**
* Overloaded Constructors
*/
public BigDecimal(){
list = new ArrayList();
list.add(new Char(\'0\'));
list.add(new Char(\'.\'));
list.add(new Char(\'0\'));
}
public BigDecimal(String value) throws BigDecimalException{
list = new ArrayList();
//For numbers like .99
if(value.startsWith(\".\")){
list.add(new Char(\'0\'));
}
validateString(value);
for(int i=0;i();
List valList = value.getValue();
for(int i=0;i getValue(){
return this.list;
}
/**
* Add Method
* @param other
* @return
* @throw.
Can someone help me please I need help writing this code using Java.pdf
1. Can someone help me please? I need help writing this code using Java language. Step 1 The
Char Type Create a complex type called Char that models the primitive type char. In your class
you are going to want to have the following constructors: Java Description Charo Char0 Default
constructor that sets the data section of the class to null (binary 0) overloaded constructor that
takes a primitive char as an argument. It should set the data Char (char c) Char(char c) section of
the class to the argument. overloaded constructor that takes a primitive int as a parameter. The
data section of the Char(int co Char(int c) class should be set as a character from the argument.
Chan(const Char(inal Overloaded constructor that takes the complex Char type as a parameter,
The data section Char &c;) Char c) of the class should be set with the data section of the
argument Char(string Char (String overloaded Constructor that takes a string type as a parameter.
The data section of the class should be set to the first character in the string. Your class should
have the following mutators
Solution
Contains:
1. Custom classes - Char.java and BigDecimal.java
2. Exception classes - CharException.java and BigDecimalException.java
3. Test Classes - CharTest.java and BigDecimalTest.java
Char.java
**
* Custom Char class
* @author Anonymous
*
*/
public class Char {
private char value;
public Char(){
this.value = '0';
}
public Char(char c){
this.value = c;
2. }
public Char(int c){
this.value = (char)c;
}
public Char(final Char c){
this.value = c.getValue();
}
public Char(String s){
this.value = s.charAt(0);
}
public char getValue(){
return this.value;
}
public void equals(final Char c){
this.value = c.getValue();
}
public void equals(char c){
this.value = c;
}
public void equals(int c) throws CharException{
if(c<32 || c>127)
throw new CharException("Invalid Character");
this.value = (char) c;
}
public char toChar(){
return this.value;
}
public int toInt(){
3. return (int)this.value;
}
public String toString(){
return String.valueOf(this.value);
}
/**
* To convert char to Hex string
* @return
*/
public String toHexString(){
return String.format("%04x", (int)this.value);
}
public String add(char c){
return toString().concat(String.valueOf(c));
}
public String add(Char c){
return toString().concat(String.valueOf(c.getValue()));
}
}
BigDecimal.java
import java.util.ArrayList;
import java.util.List;
/**
* Custom BigDecimal class
* @author Anonymous
*
*/
public class BigDecimal {
List list;
4. /**
* Overloaded Constructors
*/
public BigDecimal(){
list = new ArrayList();
list.add(new Char('0'));
list.add(new Char('.'));
list.add(new Char('0'));
}
public BigDecimal(String value) throws BigDecimalException{
list = new ArrayList();
//For numbers like .99
if(value.startsWith(".")){
list.add(new Char('0'));
}
validateString(value);
for(int i=0;i();
List valList = value.getValue();
for(int i=0;i getValue(){
return this.list;
}
/**
* Add Method
* @param other
* @return
* @throws BigDecimalException
*/
public BigDecimal add(BigDecimal other) throws BigDecimalException{
double num1 = this.wholeNumber()+this.fraction();
double num2 = other.wholeNumber()+other.fraction();
return new BigDecimal(String.valueOf(num1+num2));
5. }
/**
* Subtract Method
* @param other
* @return
* @throws BigDecimalException
*/
public BigDecimal subtract(BigDecimal other) throws BigDecimalException{
double num1 = this.wholeNumber()+this.fraction();
double num2 = other.wholeNumber()+other.fraction();
return new BigDecimal(String.valueOf(num1-num2));
}
/**
* Get Whole Number
* @return
*/
public int wholeNumber(){
List subList;
if(containsDecimal()){
int index=0;
for(Char c:this.list){
if('.' == c.toChar()){
break;
}
index++;
}
subList = this.list.subList(0,index);
}else{
subList = this.list;
}
String s="";
for(Char c:subList){
s+=c.toString();
6. }
return Integer.parseInt(s);
}
/**
* Get fraction part of whole number
* @return
*/
public double fraction(){
String fraction;
List subList;
if(containsDecimal()){
int index=0;
for(Char c:this.list){
if('.' == c.toChar()){
break;
}
index++;
}
subList = this.list.subList(index+1, this.list.size());
fraction="0.";
for(Char c:subList){
fraction+=c.toString();
}
}
else{
//If no fraction part, return default fraction = 0.0
fraction = "0.0";
}
return Double.parseDouble(fraction);
}
7. public double toDouble(){
return this.wholeNumber()+this.fraction();
}
public String toString(){
return String.valueOf(toDouble());
}
public Char at(int index){
return this.list.get(index);
}
private boolean isNumber(Char ch){
char c = ch.toChar();
return (c>='0' && c<='9');
}
/**
* Private method to check if the number has a decimal point
* @return
*/
private boolean containsDecimal() {
boolean contains = false;
for(Char c:this.list){
if('.' == c.toChar()){
contains = true;
}
}
return contains;
}
/**
* Private Method to validate if the string contains a valid number
* @param s
* @throws BigDecimalException
8. */
private void validateString(String s) throws BigDecimalException{
//For numbers like 499.
if(s.endsWith(".")){
throw new BigDecimalException("Format Exception: Number cannot end with decimal
point");
}
int decimalCount = 0;
for(int i=0;i1)
throw new BigDecimalException("Invalid Number. String contains more than 1 decimal
point");
}
}
CharException.java
public class CharException extends Exception{
private static final long serialVersionUID = 1L;
public CharException(String message){
super(message);
}
}
BigDecimalException.java
public class BigDecimalException extends CharException {
private static final long serialVersionUID = 1L;
public BigDecimalException(String message) {
super(message);
}
}
CharTest.java
/**
*
9. * @author Anonymous
*
*/
public class CharTest {
public static void main(String args[]){
Char ch = new Char('A');
Char c = new Char('B');
System.out.println(ch.add(c));
System.out.println(ch.toChar() + " In Hex: "+ch.toHexString());
System.out.println(ch.toChar() + " In Int: "+ch.toInt());
try{
ch.equals(140);
System.out.println(ch.toChar());
}catch(CharException ce){
System.out.println(ce.getMessage());
}
Char x = new Char(34);
System.out.println(x.toString());
}
}
BigDecimalTest.java
/**
* BigDecimal Test Class
* Reads numbers from a file
* Outputs 2 file - 1 file contains whole part and 2 file contains fraction part
* @author Anonymous
*
*/
public class BigDecimalTest {
10. public static void main(String args[]) throws IOException{
List numberList = new ArrayList();
try {
//TODO
FileInputStream fis = new FileInputStream("<>");
Scanner sc = new Scanner(fis);
while(sc.hasNextLine()){
try{
BigDecimal b = new BigDecimal(sc.nextLine());
numberList.add(b);
}catch(BigDecimalException bde){
System.out.println(bde.getMessage());
}
}
List wholeList = new ArrayList();
List fractionList = new ArrayList();
for(BigDecimal bd:numberList){
wholeList.add(bd.wholeNumber());
fractionList.add(bd.fraction());
}
//TODO
File file = new File("<>/wholeNumber.txt");
File file1 = new File("<>/fraction.txt");
if(!file.exists())
file.createNewFile();
if(!file1.exists())