Question

You are to write a class called Point – this will represent a geometric point in...

You are to write a class called Point – this will represent a geometric point in a Cartesian plane (but x and y should be ints). Point should have the following:

Data:
 that hold the x-value and the y-value. They should be ints and must be private.

Constructors:

 A default constructor that will set the values to (3, -5)

 A parameterized constructor that will receive 2 ints (x then y) and set the data to what is received.

 A copy constructor that will receive a Point. If it receives null, then it should throw new IllegalArgumentException(<”your meaningful String here”>);

If it is OK, the it should initialize the data (of the new instance being created) to be the same as the Point that was received.

Methods:

 A toString() method that receives nothing and returns a String representing the current

instance. It should be in the form (x, y) (with a space after the comma).

 A method called inQuadrant(int theQuadrant) which returns true if the current instance is in theQuadrant, false otherwise. The quadrants are defined like in Math and do not include the axes themselves. If theQuadrant is not in the range 1-4, then inQuadrant should:

throw new IllegalArgumentException(<”your meaningful String here”>);

 A method called onXAxis() which returns true if the current instance is on the x-axis, false otherwise.

 A method called translate(int xmove, int ymove) that will return nothing and “translates,” or shifts, the data by the x and y distances that are received. So if a Point has data x=3 and y=5, translate(5,2) will cause it to change its data to x=8 and y=7.

 A method called equals(Object obj) which returns true if it is equal to obj. Note that it receives an Object; there is a particular way that this method should be implemented (see notes/demos).

another class called term:

Write a class called Term. It will represent a Mathematical term in a polynomial (for example 3x4) Your Term class should implement TermInterface.

It should

It should - -

-

have data to represent the coefficient and power. For simplicity, we will let them be ints.

have 3 constructors
A default constructor which will set the coefficient to 2 and the power to 3
A parameterized constructor which will receive the coefficient and the power (as ints) and set the data to what is received.
A copy constructor which will receive another Term and set the coefficient and power to be the same as the Term that is received (thus “copying” it). If the copy constructor receives a value of null, it should throw a new IllegalArgumentException(“your descriptive String here”);

It should
reuse the comments).

have methods to operate on the data. The methods are described in TermInterface (you can even

Using TermInterface

An interface is used to list the methods that must be implemented in your code. So in that sense, it enforces the design (since you can’t misspell a method name or leave out a method). Your Term class should start out like this:

public class Term implements TermInterface

{...
TermInterface must be in the same package as your Term class if you are compiling it at home. So you will have to download it from Canvas, put it in your program (either in same file or same project) and also submit it to HyperGrade.

The best way to use it is to copy all the code and use it as the outline for your methods. You can also use the comments as your method comments. Note that none of the methods in TermInterface have a body
( instead they just have a ; ). Do not change the TermInterface.java, but after you copy the methods, you will have to take out the ; and then implement the code.

If you use an interface and do not implement one of the methods (or you misspell it). It will not compile. The error you will get will say that “Term is not abstract and does not implement <method name>”.

public interface TermInterface

{

// toString() - returns its representation as a String 3x^2 for

example,

// But to make it look better, follow this logic in order...

// - if coefficient is 0, return "0"

// - else if the power is 0, return the coefficient (as

a String, concatenate with "")

// - else if the coefficient is 1 and the power is 1,

return "x"

// - else if the coefficient is -1 and the power is 1,

return "-x"

// - else if the power is 1, return coefficient and "x"

// - else if coefficient is 1, return "x^" and the power

// - else if the coefficient is -1, return "-x^" and the

power

// - else return coefficient and "x^" and power

public String toString();

//evaluate - evalutes with whatever double is received

public double evaluate(double value);

//degree - if coefficient is 0, then (it is a constant so) returns

0.

// else returns the power

public int degree();

//derivative - return a new Term that is the derivative. The

derivative

// is calculated by:

// the coefficient of the

derivative is the original coefficient times the power

// the power of the derivative

is the original power minus 1

public Term derivative();

//addIn: add another Term to itself.

//The Term that is received is not changed.

// if the Term that is received is null, throw a new

IllegalArgumentException(<your descriptive String here>)

// if the powers are not the same, throw a new

IllegalArgumentException(<your descriptive String here>)

public void addIn(Term anotherTerm);

//multiplyBy: multiply itself by anotherTerm - result is a new Term

that is created and returned.

//The original Term and the Term that is received are not changed.

// if the Term that is received is null, throw a new

IllegalArgumentException(<your descriptive String here>)

public Term multiplyBy(Term anotherTerm);

//equals: returns true if it is the same as what is received.

// If both coefficients are 0, they are automatically equal

// otherwise, both coefficients AND both exponents must be the

same

public boolean equals(Object obj);

}

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


Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i


Point.java
=========
public class Point {
private int x, y;
public Point() //default constructor
{
x = 3;
y = -5;
}
public Point(int x1, int y1)
{
x = x1;
y = y1;
}
public Point(Point p1)
{
if(p1 == null)
throw new IllegalArgumentException("null can not be passed to copy constructor");
x = p1.x;
y = p1.y;
}
public String toString()
{
return "(" + x + ", " + y + ")";
}
public boolean inQuadrant(int theQuadrant)
{
if(theQuadrant < 1 || theQuadrant > 4)
throw new IllegalArgumentException("Valid values for quadrant are 1-4");
if(theQuadrant == 1)
{
if(x > 0 && y > 0)
return true;
}
else if(theQuadrant == 2)
{
if(x < 0 && y > 0)
return true;
}
else if(theQuadrant == 3)
{
if(x < 0 && y < 0)
return true;
}
else if(theQuadrant == 4)
{
if(x > 0 && y < 0)
return true;
}
return false; //for all other cases
}
public boolean onXAxis()
{
if( y == 0)
return true;
else
return false;
}
public void translate(int xmove, int ymove)
{
x += xmove;
y += ymove;
}
public boolean equals(Object obj)
{
if(obj instanceof Point)
{
Point p1 = (Point)obj;
if(x == p1.x && y == p1.y)
return true;
else
return false;
}
else
return false;
}
}


Term.java
=========
public class Term implements TermInterface{
private int coefficient, power;
public Term()
{
coefficient = 0;
power = 0;
}
public Term(int coeff, int pow)
{
coefficient = coeff;
power = pow;
}
public int getCoefficient()
{
return coefficient;
}
public int getPower()
{
return power;
}
// toString() - returns its representation as a String 3x^2 for example,
// But to make it look better, follow this logic in order...
// - if coefficient is 0, return "0"
// - else if the power is 0, return the coefficient (as a String, concatenate with "")
// - else if the coefficient is 1 and the power is 1, return "x"
// - else if the coefficient is -1 and the power is 1, return "-x"
// - else if the power is 1, return coefficient and "x"
// - else if coefficient is 1, return "x^" and the power
// - else if the coefficient is -1, return "-x^" and the power
// - else return coefficient and "x^" and power
public String toString()
{
if(coefficient == 0)
return "0";
else if(power == 0)
return coefficient + "";
else if(coefficient == 1 && power == 1)
return "x";
else if(coefficient == -1 && power == 1)
return "-x";
else if(power == 1)
return coefficient + "x";
else if (coefficient == 1)
return "x^" + power;
else if(coefficient == -1)
return "-x^" + power;
else
return coefficient + "x^" + power;
}
//evaluate - evalutes with whatever double is received
public double evaluate(double value)
{
return coefficient * Math.pow(value, power);
}
//degree - if coefficient is 0, then (it is a constant so) returns 0.
// else returns the power
public int degree()
{
if(coefficient == 0)
return 0;
else
return power;
}
//derivative - return a new Term that is the derivative. The derivative
// is calculated by:
// the coefficient of the derivative is the original coefficient times the power
// the power of the derivative is the original power minus 1
public Term derivative()
{
return new Term(coefficient * (power-1), power-1);
}
//addIn: add another Term to itself.
//The Term that is received is not changed.
// if the Term that is received is null, throw a new IllegalArgumentException(<your descriptive String here>)
// if the powers are not the same, throw a new IllegalArgumentException(<your descriptive String here>)
public void addIn(Term anotherTerm)
{
if(anotherTerm == null)
throw new IllegalArgumentException("Term to be added should not be null");
if(power != anotherTerm.power)
throw new IllegalArgumentException("Terms to be added should have same power");
coefficient += anotherTerm.coefficient;
}
//multiplyBy: multiply itself by anotherTerm - result is a new Term that is created and returned.
//The original Term and the Term that is received are not changed.
// if the Term that is received is null, throw a new IllegalArgumentException(<your descriptive String here>)
public Term multiplyBy(Term anotherTerm)
{
if(anotherTerm == null)
throw new IllegalArgumentException("Term to be mulitiplied should not be null");
Term t = new Term(coefficient * anotherTerm.coefficient, power + anotherTerm.power);
return t;
}
//equals: returns true if it is the same as what is received.
// If both coefficients are 0, they are automatically equal
// otherwise, both coefficients AND both exponents must be the
public boolean equals(Object obj)
{
if(obj instanceof Term)
{
Term t = (Term)obj;
if(coefficient == t.coefficient)
{
if(coefficient == 0 || power == t.power )
return true;
}
}

return false; //all other cases
}
}


TermInterface.java(As given by instructor)
=================
public interface TermInterface
{
// toString() - returns its representation as a String 3x^2 for example,
// But to make it look better, follow this logic in order...
// - if coefficient is 0, return "0"
// - else if the power is 0, return the coefficient (as a String, concatenate with "")
// - else if the coefficient is 1 and the power is 1, return "x"
// - else if the coefficient is -1 and the power is 1, return "-x"
// - else if the power is 1, return coefficient and "x"
// - else if coefficient is 1, return "x^" and the power
// - else if the coefficient is -1, return "-x^" and the power
// - else return coefficient and "x^" and power
public String toString();
//evaluate - evalutes with whatever double is received
public double evaluate(double value);
//degree - if coefficient is 0, then (it is a constant so) returns 0.
// else returns the power
public int degree();
//derivative - return a new Term that is the derivative. The derivative
// is calculated by:
// the coefficient of the derivative is the original coefficient times the power
// the power of the derivative is the original power minus 1
public Term derivative();
//addIn: add another Term to itself.
//The Term that is received is not changed.
// if the Term that is received is null, throw a new IllegalArgumentException(<your descriptive String here>)
// if the powers are not the same, throw a new IllegalArgumentException(<your descriptive String here>)
public void addIn(Term anotherTerm);
//multiplyBy: multiply itself by anotherTerm - result is a new Term that is created and returned.
//The original Term and the Term that is received are not changed.
// if the Term that is received is null, throw a new IllegalArgumentException(<your descriptive String here>)
public Term multiplyBy(Term anotherTerm);
//equals: returns true if it is the same as what is received.
// If both coefficients are 0, they are automatically equal
// otherwise, both coefficients AND both exponents must be the
public boolean equals(Object obj);
}

Add a comment
Know the answer?
Add Answer to:
You are to write a class called Point – this will represent a geometric point in...
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
  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • JAVA Write a class called Pen that contains the following information: 1. Private instance variables for...

    JAVA Write a class called Pen that contains the following information: 1. Private instance variables for the price of the pen (float) and color of the pen (String). 2. A two-argument constructor to set each of the instance variables above. If the price is negative, throw an IllegalArgumentException stating the argument that is not correct. 3. Get and Set methods for each instance variable with the same error detection as the constructor. public class Pen {

  • Write a class called Player that has four data members: a string for the player's name,...

    Write a class called Player that has four data members: a string for the player's name, and an int for each of these stats: points, rebounds and assists. The class should have a default constructor that initializes the name to the empty string ("") and initializes each of the stats to -1. It should also have a constructor that takes four parameters and uses them to initialize the data members. It should have get methods for each data member. It...

  • PrintArray vi Create a class called PrintArray. This is the class that contains the main method....

    PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...

  • Create a new class called Point that represents a point in the Cartesian plane with two...

    Create a new class called Point that represents a point in the Cartesian plane with two coordinates. Each instance of Point should have to coordinates called x and y. The constructor for Point should take two arguments x and y that represent the two coordinates of the newly created point. Your class Point should override the __str__(self) method so that it returns a string showing the x- and y-coordinates of the Point enclosed in parentheses and separated by a comma....

  • Define a public method that is called isPrime() that returns a boolean and implements the Sieve...

    Define a public method that is called isPrime() that returns a boolean and implements the Sieve of Eratosthenes method. Define a public method that is called numberOfPrimes() that returns the number of prime numbers between 2 and the private attribute value. Show this object in a main method that allows the user to interact with all the public methods of your class. Make a class called MyNumber with an integer private attribute. Make a constructor that defines an integer parameter...

  • Create a class to represent a term in an algebraic expression. As defined here, a term...

    Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g. in the term 4x2, the coefficient is 4 and the exponent 2 in -6x8, the coefficient is -6 and the exponent 8 Your class will have a constructor that creates a Term object with a coefficient and exponent passed as parameters, and accessor methods that return the coefficient and the exponent. Your class...

  • Using your Dog class from earlier this week, complete the following: Create a new class called...

    Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...

  • package model; import java.util.Iterator; /** * This class implements interface PriorityList<E> to represent a generic *...

    package model; import java.util.Iterator; /** * This class implements interface PriorityList<E> to represent a generic * collection to store elements where indexes represent priorities and the * priorities can change in several ways. * * This collection class uses an Object[] data structure to store elements. * * @param <E> The type of all elements stored in this collection */ // You will have an error until you have have all methods // specified in interface PriorityList included inside this...

  • Write a class called Point that contains two doubles that represent its x- and y-coordinates. It...

    Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get and set methods for both fields. It should have a constructor that takes two double parameters and initializes its coordinates with those values. It should have a default constructor that initializes both coordinates to zero. It should also contain a method called distanceTo that takes as a parameter another Point and returns the distance from the Point that was passed as...

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