Question

I have Majority of the code written but I just need to implement the <,>,and = sign in the code. I have posted the instructions for the whole code. I will add what I have at the end.

Objectives:

  • Implement basic class concepts in Java

  • Implement inheritance with super and sub-classes

  • Implement basic polymorphism concepts

    Problem: The TARDIS has been infected by a virus which means it is up to Doctor Who to manually enter calculations into the TARDIS interface. The calculations necessary to make the TARDIS work properly involve real, imaginary and complex numbers. The Doctor has asked you to create a program that will evaluate numerical expressions so that he can quickly enter the information into the TARDIS.

    Details:

    • Classes

o Number class – Number.java

▪ Attributes
• Real number (double)

▪ Methods

  • Default constructor

  • Overloaded constructor – pass in value for number• Accessor
    • Mutator

• toString

• equals
o Complex number class – Complex.java

▪ Extends number class (-5 points if not)▪ Attributes:

• Imaginary number (double)▪ Methods

  • Default constructor

  • Overloaded Constructor – pass in real and imaginary numbers

o Call super constructor

  • Accessor

  • Mutator

  • toString

  • equals

  • Read in the entire expression as a string and parse it into the proper objectso Store each part of the in the correct attribute of the object

  • Use the toString function to display the number when necessary

  • Both the real and imaginary parts may be floating point values

  • Validate that each expression contains no invalid characters

o The only valid letter in a complex number is i (lower case)

o Check that each expression contains a valid operator
• Each number in the expression must be stored in the appropriate object

o Real numbers are not to be stored in a complex object

  • If a line contains invalid data, ignore the line

  • Numbers may be represented in 3 ways

o Complex (real + imaginary)
▪ <number><+ or -><number>i

o Real only
▪ <number>

o Imaginary only
▪ <number>i

  • Numbers may be positive or negative

  • Results for arithmetic operators will be numerical

  • Results for relational operators will be Boolean

  • Calculating less/greater than of a complex or imaginary number will be determined by analyzing

    the magnitude of each complex number

  • All functions in Main.java that would normally require a real or complex number parameter object must use Object parameters instead (-10 points)

o Use the instanceof operator to determine the type of object and perform the proper actions based on the object type

User Interface: There will be no user interface for this program. All I/O will be performed with a filesInput:

  • Prompt the user for the name of the file containing the numerical expressions

  • Each line in the file will contain an expression to be evaluated

  • Valid line format: <number><space><operator><space><number>

    o <number> can be real or complex

  • Valid operators

o + (add)
o – (subtract)
o * (multiply)
o / (divide)
o < (less than)
o > (greater than)o = (equal)

• There will be a newline at the end of each line except for the last line (which may or may not have a newline)

Output:

  • All output will be written to a file named results.txt.

  • Output format:
    o <expression><tab><value>

  • Numerical values will be rounded to 2 decimal places.

  • Relational operators will evaluate to true or false

  • Each expression will be listed on separate lines

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Quick Access Package Explorer X - *Main.java X *Number.java Complex.java Task List de ce este Proj 1 v2 E JRE System Library

Main:

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.PrintWriter;

import java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class Main{

static PrintWriter output;

public static void main(String[] args)throws FileNotFoundException

{

output = new PrintWriter("results.txt");

Scanner in = new Scanner(System.in);

String file=in.nextLine();

FileReader fr;

try {

fr = new FileReader(file);

Scanner sc=new Scanner(fr);

while(sc.hasNextLine()){

String line=sc.nextLine();

pareseString(line);

//pareseString(line);

}

}

catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

pareseString("6 * 3+2i");

pareseString("2 - 3");

output.close();

}

//This is the add method, taking object c2 as parameter, and adding it to .this to return

public static Complex add(Complex c1, Complex c2) {

return new Complex(c1.getRealNumber() + c2.getRealNumber(), c1.getImaginaryNumber() + c2.getImaginaryNumber());

}

//Now the subtract method, followed by the methods to multiply and divide according to hand-out rules.

public static Complex substract(Complex c1, Complex c2) {

return new Complex(c1.getRealNumber() - c2.getRealNumber(), c1.getImaginaryNumber() - c2.getImaginaryNumber());

}

public static Complex multiply(Complex c1,Complex c2) {

Complex c3 = new Complex();

c3.setRealNumber( c1.getRealNumber() * c2.getRealNumber() - c1.getImaginaryNumber() * c2.getImaginaryNumber());

c3.setImaginaryNumber( c1.getRealNumber() * c2.getImaginaryNumber() - c1.getImaginaryNumber() * c2.getRealNumber());;

return c3;

}

public static Complex divide(Complex c1,Complex c2) {

Complex c3 = new Complex();

c3.setRealNumber( c1.getRealNumber() / c2.getRealNumber() - c1.getImaginaryNumber() / c2.getImaginaryNumber());

c3.setImaginaryNumber( c1.getRealNumber() / c2.getImaginaryNumber() - c1.getImaginaryNumber() / c2.getRealNumber());;

return c3;

}

public static void pareseString(String line){

String [] strArr=line.split(" ");

String cn1=strArr[0];

String operation=strArr[1];

String cn2=strArr[2];

Complex c1=validation(cn1);

Complex c2=validation(cn2);

if(c1!=null && c2!=null){

switch (operation) {

case "+":

output.println(add(c1, c2));

break;

case "-":

output.println(substract(c1, c2));

break;

case "*":

output.println(multiply(c1, c2));

break;

case "/":

output.println(divide(c1, c2));

break;

}

}

}

private static Complex validation(String comp) {

String numberNoWhiteSpace = comp.replaceAll("\\s","");

// Matches complex number with BOTH real AND imaginary parts.

Pattern patternA = Pattern.compile("([-]?[0-9]+\\.?[0-9]?)([-|+]+[0-9]+\\.?[0-9]?)[i$]+");

// Matches ONLY real number.

Pattern patternB = Pattern.compile("([-]?[0-9]+\\.?[0-9]?)$");

// Matches ONLY imaginary number.

Pattern patternC = Pattern.compile("([-]?[0-9]+\\.?[0-9]?)[i$]");

Matcher matcherA = patternA.matcher(numberNoWhiteSpace);

Matcher matcherB = patternB.matcher(numberNoWhiteSpace);

Matcher matcherC = patternC.matcher(numberNoWhiteSpace);

double realNumber=0.0;

double imaginaryNumber=0.0;

Complex cn=null;

if (matcherA.find()) {

realNumber = Double.parseDouble(matcherA.group(1));

imaginaryNumber = Double.parseDouble(matcherA.group(2));

cn=new Complex(realNumber, imaginaryNumber);

}

else if (matcherB.find()) {

realNumber = Double.parseDouble(matcherB.group(1));

imaginaryNumber = 0;

cn=new Complex(realNumber, imaginaryNumber);

}

else if (matcherC.find()) {

realNumber = 0;

imaginaryNumber = Double.parseDouble(matcherC.group(1));

cn=new Complex(realNumber, imaginaryNumber);

}

return cn;

}

}

-----------------

Numbers class

class Number{

private double realNumber;

public Number(double realNumber) {

this.realNumber = realNumber;

}

public Number() {

this.realNumber = realNumber;

}

public double getRealNumber() {

return realNumber;

}

public void setRealNumber(double realNumnber) {

this.realNumber = realNumnber;

}

@Override

public String toString() {

// TODO Auto-generated method stub

return this.getRealNumber()+"";

}

@Override

public boolean equals(Object obj) {

//TODO Auto-generated method stub

if(obj instanceof Number){

Number cn=(Number)obj;

return this.getRealNumber()==cn.getRealNumber();

}

return false;

}

}

-------------------------------------------------------------

Complex Class

class Complex extends Number{

public double getImaginaryNumber() {

return imaginaryNumber;

}

public void setImaginaryNumber(double imaginaryNumber) {

this.imaginaryNumber = imaginaryNumber;

}

double imaginaryNumber;

public Complex(double realNumnber,double imaginaryNumber) {

super(realNumnber);

this.imaginaryNumber=imaginaryNumber;

// TODO Auto-generated constructor stub

}

public Complex() {

super();

}

@Override

public String toString() {

// TODO Auto-generated method stub

return String.format("%.2f", this.getRealNumber())+"+"+String.format("%.2f",this.getImaginaryNumber())+"i";

}

@Override

public boolean equals(Object obj){

// TODO Auto-generated method stub

if(obj instanceof Complex){

Complex cn=(Complex)obj;

return this.getRealNumber()==cn.getRealNumber() && cn.getImaginaryNumber()==this.getImaginaryNumber();

}

return false;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution)=>

Please find the answer below. Hope you will like this answer.

private static String Equality(Complex c1, Complex c2) {
   // TODO Auto-generated method stub
   String response ="";
   if(c1.equals(c2)){
      
       response=String.format("%.2f", c1.getRealNumber())+"+"+String.format("%.2f",c1.getImaginaryNumber())+"i" +" is Equal to "+String.format("%.2f", c2.getRealNumber())+"+"+String.format("%.2f",c2.getImaginaryNumber());

   }else {
       response=String.format("%.2f", c1.getRealNumber())+"+"+String.format("%.2f",c1.getImaginaryNumber())+"i" +" is not Equal to "+String.format("%.2f", c2.getRealNumber())+"+"+String.format("%.2f",c2.getImaginaryNumber());

   }
  
   return response;
}

// To implement this method first i will check real part if they are equal then i will go check imaginary one else not.
private static String greaterThan(Complex c1, Complex c2) {
   // TODO Auto-generated method stub4
   // TODO Auto-generated method stub
  
  
   String response ="";
   if(c1.getRealNumber()>= c2.getRealNumber()){
       if( c1.getRealNumber()== c2.getRealNumber()) {
          
           if( c1.getImaginaryNumber() > c2.getImaginaryNumber()) {
              
               response=String.format("%.2f", c1.getRealNumber())+"+"+String.format("%.2f",c1.getImaginaryNumber())+"i" +" is greater Than "+String.format("%.2f", c2.getRealNumber())+"+"+String.format("%.2f",c2.getImaginaryNumber());  

           }else{
              
               response=String.format("%.2f", c1.getRealNumber())+"+"+String.format("%.2f",c1.getImaginaryNumber())+"i" +" is not greater Than "+String.format("%.2f", c2.getRealNumber())+"+"+String.format("%.2f",c2.getImaginaryNumber());  

           }
                      
       }else {
          
           response=String.format("%.2f", c1.getRealNumber())+"+"+String.format("%.2f",c1.getImaginaryNumber())+"i" +" is greater Than "+String.format("%.2f", c2.getRealNumber())+"+"+String.format("%.2f",c2.getImaginaryNumber());  

       }
      
   }else {
  
       response=String.format("%.2f", c1.getRealNumber())+"+"+String.format("%.2f",c1.getImaginaryNumber())+"i" +" is not Greater Than "+String.format("%.2f", c2.getRealNumber())+"+"+String.format("%.2f",c2.getImaginaryNumber());

   }
  
   return response;

}

private static String lessThan(Complex c1, Complex c2) {
   // TODO Auto-generated method stub
   String response ="";
   if(c1.getRealNumber()<= c2.getRealNumber()){
       if( c1.getRealNumber() == c2.getRealNumber()) {
          
           if( c1.getImaginaryNumber() < c2.getImaginaryNumber()) {
              
               response=String.format("%.2f", c1.getRealNumber())+"+"+String.format("%.2f",c1.getImaginaryNumber())+"i" +" is Less Than "+String.format("%.2f", c2.getRealNumber())+"+"+String.format("%.2f",c2.getImaginaryNumber());  

           }else{
              
               response=String.format("%.2f", c1.getRealNumber())+"+"+String.format("%.2f",c1.getImaginaryNumber())+"i" +" is not Less Than "+String.format("%.2f", c2.getRealNumber())+"+"+String.format("%.2f",c2.getImaginaryNumber());  

           }
                      
       }else {
          
           response=String.format("%.2f", c1.getRealNumber())+"+"+String.format("%.2f",c1.getImaginaryNumber())+"i" +" is Less Than "+String.format("%.2f", c2.getRealNumber())+"+"+String.format("%.2f",c2.getImaginaryNumber());  

       }
      
   }else {
  
       response=String.format("%.2f", c1.getRealNumber())+"+"+String.format("%.2f",c1.getImaginaryNumber())+"i" +" is not Less Than "+String.format("%.2f", c2.getRealNumber())+"+"+String.format("%.2f",c2.getImaginaryNumber());

   }
  
   return response;
}

Need to add these line in switch case:-

case "<":

output.println(lessThan(c1, c2));

break;

case ">":

output.println(greaterThan(c1, c2));

break;

case "=":

output.println(Equality(c1, c2));

break;

Add a comment
Answer #2

Hi,

I have implemented three methods, less than(<), greater than(>) and equals. as given in the question, the less/greater than of a complex or imaginary number will be determined by analyzing the magnitude of each complex number.

****************************************************************

Main.java

****************************************************************

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.PrintWriter;

import java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class Main {

   static PrintWriter output;

   public static void main(String[] args) throws FileNotFoundException

   {

       output = new PrintWriter("results.txt");

       Scanner in = new Scanner(System.in);

       String file = in.nextLine();

       FileReader fr;

       try {

           fr = new FileReader(file);

           Scanner sc = new Scanner(fr);

           while (sc.hasNextLine()) {

               String line = sc.nextLine();

               pareseString(line);

               // pareseString(line);

           }

       }

       catch (FileNotFoundException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

       pareseString("6 * 3+2i");

       pareseString("2 - 3");

       output.close();

   }

   // This is the add method, taking object c2 as parameter, and adding it to .this
   // to return

   public static Complex add(Complex c1, Complex c2) {

       return new Complex(c1.getRealNumber() + c2.getRealNumber(), c1.getImaginaryNumber() + c2.getImaginaryNumber());

   }

   // Now the subtract method, followed by the methods to multiply and divide
   // according to hand-out rules.

   public static Complex substract(Complex c1, Complex c2) {

       return new Complex(c1.getRealNumber() - c2.getRealNumber(), c1.getImaginaryNumber() - c2.getImaginaryNumber());

   }

   public static Complex multiply(Complex c1, Complex c2) {

       Complex c3 = new Complex();

       c3.setRealNumber(c1.getRealNumber() * c2.getRealNumber() - c1.getImaginaryNumber() * c2.getImaginaryNumber());

       c3.setImaginaryNumber(
               c1.getRealNumber() * c2.getImaginaryNumber() - c1.getImaginaryNumber() * c2.getRealNumber());
       ;

       return c3;

   }

   public static Complex divide(Complex c1, Complex c2) {

       Complex c3 = new Complex();

       c3.setRealNumber(c1.getRealNumber() / c2.getRealNumber() - c1.getImaginaryNumber() / c2.getImaginaryNumber());

       c3.setImaginaryNumber(
               c1.getRealNumber() / c2.getImaginaryNumber() - c1.getImaginaryNumber() / c2.getRealNumber());
       ;

       return c3;

   }

   public static Boolean lessThan(Complex c1, Complex c2) {
       double magC1 = Math.sqrt(Math.pow(c1.getRealNumber(), 2) + Math.pow(c1.getImaginaryNumber(), 2));
       double magC2 = Math.sqrt(Math.pow(c2.getRealNumber(), 2) + Math.pow(c2.getImaginaryNumber(), 2));
       if (magC1 < magC2)
           return true;
       else
           return false;
   }

   public static Boolean greaterThan(Complex c1, Complex c2) {
       double magC1 = Math.sqrt(Math.pow(c1.getRealNumber(), 2) + Math.pow(c1.getImaginaryNumber(), 2));
       double magC2 = Math.sqrt(Math.pow(c2.getRealNumber(), 2) + Math.pow(c2.getImaginaryNumber(), 2));
       if (magC1 > magC2)
           return true;
       else
           return false;
   }
  
   public static Boolean equal(Complex c1, Complex c2) {
       return c1.equals(c2);
   }

   public static void pareseString(String line) {

       String[] strArr = line.split(" ");

       String cn1 = strArr[0];

       String operation = strArr[1];

       String cn2 = strArr[2];

       Complex c1 = validation(cn1);

       Complex c2 = validation(cn2);

       if (c1 != null && c2 != null) {

           switch (operation) {

           case "+":

               output.println(add(c1, c2));

               break;

           case "-":

               output.println(substract(c1, c2));

               break;

           case "*":

               output.println(multiply(c1, c2));

               break;

           case "/":

               output.println(divide(c1, c2));

               break;

           case "<":

               output.println(lessThan(c1, c2));

               break;
           case ">":

               output.println(greaterThan(c1, c2));

               break;
           case "=":

               output.println(equal(c1, c2));

               break;

           }

       }

   }

   private static Complex validation(String comp) {

       String numberNoWhiteSpace = comp.replaceAll("\\s", "");

       // Matches complex number with BOTH real AND imaginary parts.

       Pattern patternA = Pattern.compile("([-]?[0-9]+\\.?[0-9]?)([-|+]+[0-9]+\\.?[0-9]?)[i$]+");

       // Matches ONLY real number.

       Pattern patternB = Pattern.compile("([-]?[0-9]+\\.?[0-9]?)$");

       // Matches ONLY imaginary number.

       Pattern patternC = Pattern.compile("([-]?[0-9]+\\.?[0-9]?)[i$]");

       Matcher matcherA = patternA.matcher(numberNoWhiteSpace);

       Matcher matcherB = patternB.matcher(numberNoWhiteSpace);

       Matcher matcherC = patternC.matcher(numberNoWhiteSpace);

       double realNumber = 0.0;

       double imaginaryNumber = 0.0;

       Complex cn = null;

       if (matcherA.find()) {

           realNumber = Double.parseDouble(matcherA.group(1));

           imaginaryNumber = Double.parseDouble(matcherA.group(2));

           cn = new Complex(realNumber, imaginaryNumber);

       }

       else if (matcherB.find()) {

           realNumber = Double.parseDouble(matcherB.group(1));

           imaginaryNumber = 0;

           cn = new Complex(realNumber, imaginaryNumber);

       }

       else if (matcherC.find()) {

           realNumber = 0;

           imaginaryNumber = Double.parseDouble(matcherC.group(1));

           cn = new Complex(realNumber, imaginaryNumber);

       }

       return cn;

   }

}

Add a comment
Know the answer?
Add Answer to:
I have Majority of the code written but I just need to implement the <,>,and =...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A complex number is a number in the form a + bi, where a and b...

    A complex number is a number in the form a + bi, where a and b are real numbers and i is sqrt( -1). The numbers a and b are known as the real part and imaginary part of the complex number, respectively. You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas: a + bi + c + di = (a + c) + (b + d)i a + bi - (c + di)...

  • (Reading & Writing Business Objects) I need to have my Classes be able to talk to...

    (Reading & Writing Business Objects) I need to have my Classes be able to talk to Files. How do I make it such that I can look in a File for an account number and I am able to pull up all the details? The file should be delimited by colons (":"). The Code for testing 'Select' that goes in main is: Account a1 = new Account(); a1.select(“90001”); a1.display(); Below is what it should look like for accounts 90000:3003:SAV:8855.90 &...

  • Java Assignment Calculator with methods. My code appears below what I need to correct. What I...

    Java Assignment Calculator with methods. My code appears below what I need to correct. What I need to correct is if the user attempts to divide a number by 0, the divide() method is supposed to return Double.NaN, but your divide() method doesn't do this. Instead it does this:     public static double divide(double operand1, double operand2) {         return operand1 / operand2;     } The random method is supposed to return a double within a lower limit and...

  • Write a Java method that will take an array of integers of size n and shift...

    Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...

  • This is my code that i need to finish. In BoxRegion.java I have no idea how...

    This is my code that i need to finish. In BoxRegion.java I have no idea how to create the constructor. I tried to use super(x,y) but It is hard to apply. And Also In BoxRegionHashTable, I don't know how to create displayAnnotation BoxRegion.java ------------------------------------------------ public final class BoxRegion { final Point2D p1; final Point2D p2; /** * Create a new 3D point with given x, y and z values * * @param x1, y1 are the x,y coordinates for point...

  • Problem: Use the code I have provided to start writing a program that accepts a large...

    Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...

  • What is the output of the following code? class C1 { public double PI=3.1; public void...

    What is the output of the following code? class C1 { public double PI=3.1; public void m1() { System.out.println("M1 of C1"); } } class C2 extends C1 { public double PI=3.14; public void m1() { System.out.println("M1 of C2"); } } public class C { public static void main(String[] args) { C1 obj = new C2(); obj.m1(); } } M1 of C1 M1 of C2 None of the above

  • What is the output of the following code? class C1 { public double PI=3.1; public void...

    What is the output of the following code? class C1 { public double PI=3.1; public void m1() { System.out.println("M1 of C1"); } } class C2 extends C1 { public double PI=3.14; public void m1() { System.out.println("M1 of C2"); } } public class C { public static void main(String[] args) { C1 obj = new C2(); System.out.print(obj.PI); } } 3.1 3.14 None of the above

  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class...

    Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class Car {    private int year; private String model; private String make; int speed; public Car(int year, String model, String make, int speed) { this.year = year; this.model = model; this.make = make; this.speed = speed; } public Car() { } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getModel() { return model; }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT