Question

Use java, you are to create a brand new base class called NSpaceCoordinate in a file...

Use java, you are to create a brand new base class called NSpaceCoordinate in a file called nspace/NSpaceCoordinate.java. It has the following requirements:

1. It must have a single private field, an ArrayList specialized (using the generics feature discussed in the lecture) to holding values of type double (actually, Double).

2. Provide a constructor that takes a double[] and sets up the ArrayList field to have those same values.

3. Provide a method called getNthCoordinate(int i).

4. Implement the Comparable interface and set up the compareTo method to implement a dictionary ordering. Example: (1, -2.5, -3) < (1, -2.5, 9) (because -3 < 9) (1, -2.5) < (1, -2.5, 9) (because there is no third coordinate in the value on the left hand side)

5. Implement the magnitude and toString methods. toString should print out coordinates by enclosing them in parentheses and with components separated by commas. Next, copy over the twospace/TwoSpaceCoordinate.java, twospace/Complex.java, twospace/PolarComplex.java and twospace/StandardComplex.java files from the Lecture 11 demo. Change twospace.TwoSpaceCoordinate so that it extends nspace.NSpaceCoordinate. Make changes to TwoSpaceCoordinate so that it makes "maximum use" of the NSpaceCoordinate base class. The public interface for TwoSpaceCoordinate should not change. Basically, the TwoSpaceCoordinate file should get a lot shorter as you eliminate redundant/unnecessary code inside the implementation. Do not change any of the *Complex.java files. Finally, write a test driver (app/Main.java) and have its main method do the following:

1. Create an ArrayList containing 3 StandardComplex objects whose values are taken from the command line parameters. For example, calling your app.Main driver as follows should create an array with values 2+i, 2-i, and 2: java app.Main 2 1 2 -1 2 0

2. Use Collections.sort to sort the array.

3. For each element in the array (after sorting), print a single line containing the element's value (you can rely on StandardComplex's normal toString method) and the element's absolute value (call the abs() method), separated by a comma. So in this example you should be printing something similar to the following (might have different spacing/precision but in the same order): 2-i,2.23

2,2

2+i,2.23

mentioned class in step 5:

1.

package twospace;

public interface Complex {
public double re();
public double im();
public double r();
public double th();

public Complex plus(Complex b);
public Complex minus(Complex b);
public Complex times(Complex b);
public double abs();
}

2.

package twospace;

public class PolarComplex implements Complex {
public double r;
public double th;

public PolarComplex(double r, double th) {
this.r = r;
this.th = th;
}

public double re() {
return r * Math.cos(th);
}

public double im() {
return r * Math.sin(th);
}

public double r() {
return r;
}

public double th() {
return th;
}

// return a new Complex object whose value is (this + b)
public Complex plus(Complex b) {
double real = this.re() + b.re();
double imag = this.im() + b.im();
return new StandardComplex(real, imag);
}

// return a new Complex object whose value is (this - b)
public Complex minus(Complex b) {
Complex a = this;
double real = a.re() - b.re();
double imag = a.im() - b.im();
return new StandardComplex(real, imag);
}

// return a new Complex object whose value is (this * b)
public Complex times(Complex b) {
return new PolarComplex(this.r() * b.r(), this.th() + b.th());
}

public double abs() {
return Math.abs(r);
}

public String toString() {
double im = this.im();
double re = this.re();
if (im == 0) return re + "";
if (re == 0) return im + "i";
if (im < 0) return re + " - " + (-im) + "i";
return re + " + " + im + "i";
}
}

3.

package twospace;

public class StandardComplex extends TwoSpaceCoordinate implements Complex {
// private final double real;
// private final double imag;
public StandardComplex(double real, double imag) {
// this.real=real;
// this.imag=imag;
super(real, imag);
}

// return a string representation of the invoking Complex object
public String toString() {
if (this.im() == 0) return this.re() + "";
if (this.re() == 0) return this.im() + "i";
if (this.im() < 0) return this.re() + " - " + (-this.im()) + "i";
return this.re() + " + " + this.im() + "i";
}

// return abs/modulus/magnitude
public double abs() {
return magnitude();
}

public double r() {
return abs();
}

public double th() {
return Math.atan2(this.im(), this.re());
}

// return a new Complex object whose value is (this + b)
public Complex plus(Complex b) {
double real = this.re() + b.re();
double imag = this.im() + b.im();
return new StandardComplex(real, imag);
}

// return a new Complex object whose value is (this - b)
public Complex minus(Complex b) {
Complex a = this;
double real = a.re() - b.re();
double imag = a.im() - b.im();
return new StandardComplex(real, imag);
}

// return a new Complex object whose value is (this * b)
public Complex times(Complex b) {
Complex a = this;
double real = a.re() * b.re() - a.im() * b.im();
double imag = a.re() * b.im() + a.im() * b.re();
return new StandardComplex(real, imag);
}

// return the real or imaginary part
public double re() { return getFirstCoordinate(); }
public double im() { return getSecondCoordinate(); }

public boolean equals(Object x) {
if (x == null || !(x instanceof Complex)) return false;
Complex that = (Complex) x;
return (this.re() == that.re()) && (this.im() == that.im());
}
}

4.

package twospace;

public class TwoSpaceCoordinate {
private final double first;
private final double second;

public TwoSpaceCoordinate(double first, double second) {
this.first = first;
this.second = second;
}

public final double getFirstCoordinate() {
return this.first;
}

public final double getSecondCoordinate() {
return this.second;
}

public final double magnitude() {
return Math.sqrt(this.first * this.first + this.second * this.second);
}
}

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

package twospace;

public interface Complex {
public double re();
public double im();
public double r();
public double th();

public Complex plus(Complex b);
public Complex minus(Complex b);
public Complex times(Complex b);
public double abs();
}

2.

package twospace;

public class PolarComplex implements Complex {
public double r;
public double th;

public PolarComplex(double r, double th) {
this.r = r;
this.th = th;
}

public double re() {
return r * Math.cos(th);
}

public double im() {
return r * Math.sin(th);
}

public double r() {
return r;
}

public double th() {
return th;
}

// return a new Complex object whose value is (this + b)
public Complex plus(Complex b) {
double real = this.re() + b.re();
double imag = this.im() + b.im();
return new StandardComplex(real, imag);
}

// return a new Complex object whose value is (this - b)
public Complex minus(Complex b) {
Complex a = this;
double real = a.re() - b.re();
double imag = a.im() - b.im();
return new StandardComplex(real, imag);
}

// return a new Complex object whose value is (this * b)
public Complex times(Complex b) {
return new PolarComplex(this.r() * b.r(), this.th() + b.th());
}

public double abs() {
return Math.abs(r);
}

public String toString() {
double im = this.im();
double re = this.re();
if (im == 0) return re + "";
if (re == 0) return im + "i";
if (im < 0) return re + " - " + (-im) + "i";
return re + " + " + im + "i";
}
}

3.

package twospace;

public class StandardComplex extends TwoSpaceCoordinate implements Complex {
// private final double real;
// private final double imag;
public StandardComplex(double real, double imag) {
// this.real=real;
// this.imag=imag;
super(real, imag);
}

// return a string representation of the invoking Complex object
public String toString() {
if (this.im() == 0) return this.re() + "";
if (this.re() == 0) return this.im() + "i";
if (this.im() < 0) return this.re() + " - " + (-this.im()) + "i";
return this.re() + " + " + this.im() + "i";
}

// return abs/modulus/magnitude
public double abs() {
return magnitude();
}

public double r() {
return abs();
}

public double th() {
return Math.atan2(this.im(), this.re());
}

// return a new Complex object whose value is (this + b)
public Complex plus(Complex b) {
double real = this.re() + b.re();
double imag = this.im() + b.im();
return new StandardComplex(real, imag);
}

// return a new Complex object whose value is (this - b)
public Complex minus(Complex b) {
Complex a = this;
double real = a.re() - b.re();
double imag = a.im() - b.im();
return new StandardComplex(real, imag);
}

// return a new Complex object whose value is (this * b)
public Complex times(Complex b) {
Complex a = this;
double real = a.re() * b.re() - a.im() * b.im();
double imag = a.re() * b.im() + a.im() * b.re();
return new StandardComplex(real, imag);
}

// return the real or imaginary part
public double re() { return getFirstCoordinate(); }
public double im() { return getSecondCoordinate(); }

public boolean equals(Object x) {
if (x == null || !(x instanceof Complex)) return false;
Complex that = (Complex) x;
return (this.re() == that.re()) && (this.im() == that.im());
}
}

4.

package twospace;

public class TwoSpaceCoordinate {
private final double first;
private final double second;

public TwoSpaceCoordinate(double first, double second) {
this.first = first;
this.second = second;
}

public final double getFirstCoordinate() {
return this.first;
}

public final double getSecondCoordinate() {
return this.second;
}

public final double magnitude() {
return Math.sqrt(this.first * this.first + this.second * this.second);
}
}

Add a comment
Know the answer?
Add Answer to:
Use java, you are to create a brand new base class called NSpaceCoordinate in a file...
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
  • Consider the following object-oriented code: abstract public class Number ( abstract public Number add(Number n); public...

    Consider the following object-oriented code: abstract public class Number ( abstract public Number add(Number n); public class Complex extends Number [ public Complex(double r, double i) public Complex add(Number n) [ Complex that - (Complex) n; return new Complex(this.re+that.re, this.im+that.im); private double re; private double im; public class Real extends Number public Real(double v) val-v public Real add(Number n) Real that Rea) n; return new Real(this.val+that.val) private double val; Unsurprisingly, the following code compiles. Number n- new Complex(2,3).add(new Real(1)); What...

  • Create the header file named “Complex.h” that contains the following class: The class Complex represents a...

    Create the header file named “Complex.h” that contains the following class: The class Complex represents a complex number which is a number of the form a + bi where a and b are real numbers and i2 = −1. The class should contain: Private double field named real. Private double field named imaginary. Public default constructor that assigns 1 to real and 0 to imaginary. Public overloaded constructor that takes a double as a parameter named real. It assigns real...

  • 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)...

  • This is a C++ programming question. Please provide the correct, workable code. Use the following three...

    This is a C++ programming question. Please provide the correct, workable code. Use the following three programs to help solve the problem. Provide comments throughout the code. Problem to solve: Code 1: Complex.h #pragma once #ifndef COMPLEX_H #define COMPLEX_H class Complex { private:    double real;    double imag; public:    // initialize the complex number to 0.0    Complex() : real(0.0), imag(0.0) {}    // initialize the complex number at declaration or new    Complex(double r, double i) :...

  • DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to...

    DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to be DataSetEmployee. DataSetEmployee should extend ArrayList<Employee> and have no instance variables. You'll have to decide what replaces the getPages concept for getMax/getMin. As a hint, what method of Employee returns a numeric value? Make a package com.acme.midmanager. This package should contain the Manager class. Make a package com.acme.personnel. This package should contain the DataSetEmployee class. Make a package com.acme.topmanagement. This package should contain the...

  • How to build Java test class? I am supposed to create both a recipe class, and...

    How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...

  • Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file...

    Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file with the appropriate name. public class BasicJava4 { Add four methods to the class that return a default value. public static boolean isAlphabetic(char aChar) public static int round(double num) public static boolean useSameChars(String str1, String str2) public static int reverse(int num) Implement the methods. public static boolean isAlphabetic(char aChar): Returns true if the argument is an alphabetic character, return false otherwise. Do NOT use...

  • Money Lab using arraylists in Java Using the Coin class below create a program with the...

    Money Lab using arraylists in Java Using the Coin class below create a program with the following requirements Create an arraylist that holds the money you have in your wallet. You will add a variety of coins(coin class) and bills (ones, fives, tens *also using the coin class) to your wallet arraylist. Program must include a loop that asks you to purchase items using the coins in your wallet. When purchasing items, the program will remove coins from the arraylist...

  • In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams...

    In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams and print it to the terminal via the driver class: import java.util.*; public class Racer implements Comparable { private final String name; private final int year; private final int topSpeed;    public Racer(String name, int year, int topSpeed){    this.name = name; this.year = year; this.topSpeed = topSpeed;    }    public String toString(){    return name + "-" + year + ", Top...

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