SlideShare a Scribd company logo
1 of 11
Download to read offline
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
* @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));
}
/**
* 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();
}
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);
}
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
*/
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
/**
*
* @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 {
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())
file1.createNewFile();
PrintWriter pw = null;
//Writing Whole Numbers
pw = new PrintWriter(file);
for(Integer in:wholeList){
pw.write(String.valueOf(in));
pw.println();
}
pw.flush();
pw.close();
//Writing Fraction Numbers
pw = new PrintWriter(file1);
for(Double db:fractionList){
pw.write(String.valueOf(db));
pw.println();
}
pw.flush();
pw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

More Related Content

Similar to Can someone help me please I need help writing this code using Java.pdf

Comp 328 final guide
Comp 328 final guideComp 328 final guide
Comp 328 final guidekrtioplal
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)Tak Lee
 
Riga Dev Day 2016 - Having fun with Javassist
Riga Dev Day 2016 - Having fun with JavassistRiga Dev Day 2016 - Having fun with Javassist
Riga Dev Day 2016 - Having fun with JavassistAnton Arhipov
 
Hive Functions Cheat Sheet
Hive Functions Cheat SheetHive Functions Cheat Sheet
Hive Functions Cheat SheetHortonworks
 
JavaOne 2015 - Having fun with Javassist
JavaOne 2015 - Having fun with JavassistJavaOne 2015 - Having fun with Javassist
JavaOne 2015 - Having fun with JavassistAnton Arhipov
 
Chapter i(introduction to java)
Chapter i(introduction to java)Chapter i(introduction to java)
Chapter i(introduction to java)Chhom Karath
 
Core csharp and net quick reference
Core csharp and net quick referenceCore csharp and net quick reference
Core csharp and net quick referenceilesh raval
 
Core c sharp and .net quick reference
Core c sharp and .net quick referenceCore c sharp and .net quick reference
Core c sharp and .net quick referenceArduino Aficionado
 
JEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistJEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistAnton Arhipov
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfaromanets
 
Java Generics
Java GenericsJava Generics
Java Genericsjeslie
 
Java 5 New Feature
Java 5 New FeatureJava 5 New Feature
Java 5 New Featurexcoda
 
Rooms and MoreCan you please help me the JAVA programLabInherit.pdf
Rooms and MoreCan you please help me the JAVA programLabInherit.pdfRooms and MoreCan you please help me the JAVA programLabInherit.pdf
Rooms and MoreCan you please help me the JAVA programLabInherit.pdfmumnesh
 

Similar to Can someone help me please I need help writing this code using Java.pdf (20)

Comp 328 final guide
Comp 328 final guideComp 328 final guide
Comp 328 final guide
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)
 
week-6x
week-6xweek-6x
week-6x
 
Riga Dev Day 2016 - Having fun with Javassist
Riga Dev Day 2016 - Having fun with JavassistRiga Dev Day 2016 - Having fun with Javassist
Riga Dev Day 2016 - Having fun with Javassist
 
C++11
C++11C++11
C++11
 
Hive Functions Cheat Sheet
Hive Functions Cheat SheetHive Functions Cheat Sheet
Hive Functions Cheat Sheet
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
JavaOne 2015 - Having fun with Javassist
JavaOne 2015 - Having fun with JavassistJavaOne 2015 - Having fun with Javassist
JavaOne 2015 - Having fun with Javassist
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
Chapter i(introduction to java)
Chapter i(introduction to java)Chapter i(introduction to java)
Chapter i(introduction to java)
 
Core csharp and net quick reference
Core csharp and net quick referenceCore csharp and net quick reference
Core csharp and net quick reference
 
Core c sharp and .net quick reference
Core c sharp and .net quick referenceCore c sharp and .net quick reference
Core c sharp and .net quick reference
 
JEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistJEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with Javassist
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Java 5 Features
Java 5 FeaturesJava 5 Features
Java 5 Features
 
Java 5 New Feature
Java 5 New FeatureJava 5 New Feature
Java 5 New Feature
 
Built-in Classes in JAVA
Built-in Classes in JAVABuilt-in Classes in JAVA
Built-in Classes in JAVA
 
Rooms and MoreCan you please help me the JAVA programLabInherit.pdf
Rooms and MoreCan you please help me the JAVA programLabInherit.pdfRooms and MoreCan you please help me the JAVA programLabInherit.pdf
Rooms and MoreCan you please help me the JAVA programLabInherit.pdf
 
Arrays
ArraysArrays
Arrays
 

More from marketing413921

Complete the implementation of the Weighted Graph that we began in t.pdf
Complete the implementation of the Weighted Graph that we began in t.pdfComplete the implementation of the Weighted Graph that we began in t.pdf
Complete the implementation of the Weighted Graph that we began in t.pdfmarketing413921
 
C++ in XcodePass the values by reference to complete the programmi.pdf
C++ in XcodePass the values by reference to complete the programmi.pdfC++ in XcodePass the values by reference to complete the programmi.pdf
C++ in XcodePass the values by reference to complete the programmi.pdfmarketing413921
 
Assume an infix expression can only contain five operators, + .pdf
Assume an infix expression can only contain five operators,    + .pdfAssume an infix expression can only contain five operators,    + .pdf
Assume an infix expression can only contain five operators, + .pdfmarketing413921
 
appropriate nursing interventionsmanagement -COPD -Crackles, wheezi.pdf
appropriate nursing interventionsmanagement -COPD -Crackles, wheezi.pdfappropriate nursing interventionsmanagement -COPD -Crackles, wheezi.pdf
appropriate nursing interventionsmanagement -COPD -Crackles, wheezi.pdfmarketing413921
 
Arrange the following event in chronological order. The Roman Republi.pdf
Arrange the following event in chronological order. The Roman Republi.pdfArrange the following event in chronological order. The Roman Republi.pdf
Arrange the following event in chronological order. The Roman Republi.pdfmarketing413921
 
Why do we need internal control in an organization What is its purp.pdf
Why do we need internal control in an organization What is its purp.pdfWhy do we need internal control in an organization What is its purp.pdf
Why do we need internal control in an organization What is its purp.pdfmarketing413921
 
Why Diamond and semiconductor material like Silicon are used in elect.pdf
Why Diamond and semiconductor material like Silicon are used in elect.pdfWhy Diamond and semiconductor material like Silicon are used in elect.pdf
Why Diamond and semiconductor material like Silicon are used in elect.pdfmarketing413921
 
Which of the following was NOT a goal of Titchener’s psychologyA..pdf
Which of the following was NOT a goal of Titchener’s psychologyA..pdfWhich of the following was NOT a goal of Titchener’s psychologyA..pdf
Which of the following was NOT a goal of Titchener’s psychologyA..pdfmarketing413921
 
You discover a new kind of fungus with large, square-shaped spores. .pdf
You discover a new kind of fungus with large, square-shaped spores. .pdfYou discover a new kind of fungus with large, square-shaped spores. .pdf
You discover a new kind of fungus with large, square-shaped spores. .pdfmarketing413921
 
Write 1-2 pages of the definitions of the followings (Use APA forma.pdf
Write 1-2 pages of the definitions of the followings (Use APA forma.pdfWrite 1-2 pages of the definitions of the followings (Use APA forma.pdf
Write 1-2 pages of the definitions of the followings (Use APA forma.pdfmarketing413921
 
Which cell type does not move materials Which of the following cell .pdf
Which cell type does not move materials  Which of the following cell .pdfWhich cell type does not move materials  Which of the following cell .pdf
Which cell type does not move materials Which of the following cell .pdfmarketing413921
 
What is the practical relevanceirrelevance of different digital med.pdf
What is the practical relevanceirrelevance of different digital med.pdfWhat is the practical relevanceirrelevance of different digital med.pdf
What is the practical relevanceirrelevance of different digital med.pdfmarketing413921
 
using MATLABplease write down the code to compute these functions.pdf
using MATLABplease write down the code to compute these functions.pdfusing MATLABplease write down the code to compute these functions.pdf
using MATLABplease write down the code to compute these functions.pdfmarketing413921
 
What are the main duties of the Canadian Engineering Accreditation B.pdf
What are the main duties of the Canadian Engineering Accreditation B.pdfWhat are the main duties of the Canadian Engineering Accreditation B.pdf
What are the main duties of the Canadian Engineering Accreditation B.pdfmarketing413921
 
A supererogatory action is one in which a person must engage. T or F.pdf
A supererogatory action is one in which a person must engage. T or F.pdfA supererogatory action is one in which a person must engage. T or F.pdf
A supererogatory action is one in which a person must engage. T or F.pdfmarketing413921
 
The purpose of specialized lacunae are to provide structure to th.pdf
The purpose of specialized lacunae are to  provide structure to th.pdfThe purpose of specialized lacunae are to  provide structure to th.pdf
The purpose of specialized lacunae are to provide structure to th.pdfmarketing413921
 
The following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdfThe following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdfmarketing413921
 
Prerequisites — Classes or Knowledge Required for this Course.pdf
Prerequisites — Classes or Knowledge Required for this Course.pdfPrerequisites — Classes or Knowledge Required for this Course.pdf
Prerequisites — Classes or Knowledge Required for this Course.pdfmarketing413921
 
Please dont answer if you cannot complete all the requirements. Th.pdf
Please dont answer if you cannot complete all the requirements. Th.pdfPlease dont answer if you cannot complete all the requirements. Th.pdf
Please dont answer if you cannot complete all the requirements. Th.pdfmarketing413921
 
Part 1 Select a controversial current topic and describe the litera.pdf
Part 1 Select a controversial current topic and describe the litera.pdfPart 1 Select a controversial current topic and describe the litera.pdf
Part 1 Select a controversial current topic and describe the litera.pdfmarketing413921
 

More from marketing413921 (20)

Complete the implementation of the Weighted Graph that we began in t.pdf
Complete the implementation of the Weighted Graph that we began in t.pdfComplete the implementation of the Weighted Graph that we began in t.pdf
Complete the implementation of the Weighted Graph that we began in t.pdf
 
C++ in XcodePass the values by reference to complete the programmi.pdf
C++ in XcodePass the values by reference to complete the programmi.pdfC++ in XcodePass the values by reference to complete the programmi.pdf
C++ in XcodePass the values by reference to complete the programmi.pdf
 
Assume an infix expression can only contain five operators, + .pdf
Assume an infix expression can only contain five operators,    + .pdfAssume an infix expression can only contain five operators,    + .pdf
Assume an infix expression can only contain five operators, + .pdf
 
appropriate nursing interventionsmanagement -COPD -Crackles, wheezi.pdf
appropriate nursing interventionsmanagement -COPD -Crackles, wheezi.pdfappropriate nursing interventionsmanagement -COPD -Crackles, wheezi.pdf
appropriate nursing interventionsmanagement -COPD -Crackles, wheezi.pdf
 
Arrange the following event in chronological order. The Roman Republi.pdf
Arrange the following event in chronological order. The Roman Republi.pdfArrange the following event in chronological order. The Roman Republi.pdf
Arrange the following event in chronological order. The Roman Republi.pdf
 
Why do we need internal control in an organization What is its purp.pdf
Why do we need internal control in an organization What is its purp.pdfWhy do we need internal control in an organization What is its purp.pdf
Why do we need internal control in an organization What is its purp.pdf
 
Why Diamond and semiconductor material like Silicon are used in elect.pdf
Why Diamond and semiconductor material like Silicon are used in elect.pdfWhy Diamond and semiconductor material like Silicon are used in elect.pdf
Why Diamond and semiconductor material like Silicon are used in elect.pdf
 
Which of the following was NOT a goal of Titchener’s psychologyA..pdf
Which of the following was NOT a goal of Titchener’s psychologyA..pdfWhich of the following was NOT a goal of Titchener’s psychologyA..pdf
Which of the following was NOT a goal of Titchener’s psychologyA..pdf
 
You discover a new kind of fungus with large, square-shaped spores. .pdf
You discover a new kind of fungus with large, square-shaped spores. .pdfYou discover a new kind of fungus with large, square-shaped spores. .pdf
You discover a new kind of fungus with large, square-shaped spores. .pdf
 
Write 1-2 pages of the definitions of the followings (Use APA forma.pdf
Write 1-2 pages of the definitions of the followings (Use APA forma.pdfWrite 1-2 pages of the definitions of the followings (Use APA forma.pdf
Write 1-2 pages of the definitions of the followings (Use APA forma.pdf
 
Which cell type does not move materials Which of the following cell .pdf
Which cell type does not move materials  Which of the following cell .pdfWhich cell type does not move materials  Which of the following cell .pdf
Which cell type does not move materials Which of the following cell .pdf
 
What is the practical relevanceirrelevance of different digital med.pdf
What is the practical relevanceirrelevance of different digital med.pdfWhat is the practical relevanceirrelevance of different digital med.pdf
What is the practical relevanceirrelevance of different digital med.pdf
 
using MATLABplease write down the code to compute these functions.pdf
using MATLABplease write down the code to compute these functions.pdfusing MATLABplease write down the code to compute these functions.pdf
using MATLABplease write down the code to compute these functions.pdf
 
What are the main duties of the Canadian Engineering Accreditation B.pdf
What are the main duties of the Canadian Engineering Accreditation B.pdfWhat are the main duties of the Canadian Engineering Accreditation B.pdf
What are the main duties of the Canadian Engineering Accreditation B.pdf
 
A supererogatory action is one in which a person must engage. T or F.pdf
A supererogatory action is one in which a person must engage. T or F.pdfA supererogatory action is one in which a person must engage. T or F.pdf
A supererogatory action is one in which a person must engage. T or F.pdf
 
The purpose of specialized lacunae are to provide structure to th.pdf
The purpose of specialized lacunae are to  provide structure to th.pdfThe purpose of specialized lacunae are to  provide structure to th.pdf
The purpose of specialized lacunae are to provide structure to th.pdf
 
The following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdfThe following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdf
 
Prerequisites — Classes or Knowledge Required for this Course.pdf
Prerequisites — Classes or Knowledge Required for this Course.pdfPrerequisites — Classes or Knowledge Required for this Course.pdf
Prerequisites — Classes or Knowledge Required for this Course.pdf
 
Please dont answer if you cannot complete all the requirements. Th.pdf
Please dont answer if you cannot complete all the requirements. Th.pdfPlease dont answer if you cannot complete all the requirements. Th.pdf
Please dont answer if you cannot complete all the requirements. Th.pdf
 
Part 1 Select a controversial current topic and describe the litera.pdf
Part 1 Select a controversial current topic and describe the litera.pdfPart 1 Select a controversial current topic and describe the litera.pdf
Part 1 Select a controversial current topic and describe the litera.pdf
 

Recently uploaded

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 

Recently uploaded (20)

Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

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())
  • 11. file1.createNewFile(); PrintWriter pw = null; //Writing Whole Numbers pw = new PrintWriter(file); for(Integer in:wholeList){ pw.write(String.valueOf(in)); pw.println(); } pw.flush(); pw.close(); //Writing Fraction Numbers pw = new PrintWriter(file1); for(Double db:fractionList){ pw.write(String.valueOf(db)); pw.println(); } pw.flush(); pw.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }