Suppose a real number is represented by java class, such as public class Real {
ID: 441038 • Letter: S
Question
Suppose a real number is represented by java class, such as public class Real { private long left, right where left and right represent the digits to the left and right of the decimal point, respectively. If left is a negative integer, then the represented real number is negative. a. write a routine to input a real number, and create an object representing that number. b. write a function that accepts such a structure and returns the real number represented by it. c. write routines add, subtract and multiply that accept two such structures and set the valure of the third structure to represent the number that is the sum, difference, and product, respectively, of the two input records. IT MUST BE IN JAVA....................................................Explanation / Answer
this is the code............... I have done only for addtion. i have written all the routines. you can use it in main function. public class Real { private long left,right; Scanner scan=new Scanner(System.in); public long getLeft() { return left; } public void setLeft(long left) { this.left = left; } public long getRight() { return right; } public void setRight(long right) { this.right = right; } public Real getRealNumber(Real real) { System.out.println("Enter number"); System.out.println("left:"); real.setLeft(Long.parseLong(scan.nextLine())); System.out.println("right:"); real.setRight(Long.parseLong(scan.nextLine())); return real; } public Real createRealObject() { String temp; String[] tempNum; System.out.println("Enter real number"); temp=scan.nextLine(); tempNum=temp.split("."); Real r=new Real(); r.setLeft(Long.parseLong(tempNum[0])); r.setRight(Long.parseLong(tempNum[1])); return r; } public Real add(Real r1,Real r2) { Real r=new Real(); if((r1.getRight()+r2.getRight())>1) { r.setLeft(r1.getLeft()+r2.getLeft()+1); r.setRight(r1.getRight()+r2.getRight()-1); return r; } else { r.setLeft(r1.getLeft()+r2.getLeft()); r.setRight(r1.getRight()+r2.getRight()); return r; } } public static void main(String[] args) { // you can you all the method above; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.