Requirements: You are to write a class called Point – this will represent a geom
ID: 3880021 • Letter: R
Question
Requirements: 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);
}
Explanation / Answer
Ans :)
//Point class
public class Point {
private int x;
private int y;
public Point(){
x=3;
y=-5;
}
public Point(int x,int y){
this.x=x;
this.y=y;
}
public Point(Point p){
try{
if(p==null){
throw new IllegalArgumentException("The Copy objcet is null");
}else{
this.x=p.x;
this.y=p.y;
}
}catch(IllegalArgumentException e){
System.out.println(e);
}
}
public String toString(){
return "("+String.valueOf(x)+", "+String.valueOf(y)+")";
}
public boolean inQuadrant(int theQuadrant){
boolean status=false;
try{
if(!(theQuadrant>=1&&theQuadrant<=4)){
throw new IllegalArgumentException("The Quadrant not in range 1-4 :"+theQuadrant);
}else{
status=true;
}
}catch(IllegalArgumentException e){
System.out.println(e);
}
return status;
}
public boolean onXAxis(){
boolean status=false;
if(x>=0)
status=true;
return status;
}
public void translate(int xmove, int ymove){
x=x+xmove;
y=y+ymove;
}
public boolean equals(Object obj){
boolean status=false;
if(this.equals(obj)){
status=true;
}
return status;
}
}
//TermInterface
public interface TermInterface {
public String toString();
public double evaluate(double value);
public int degree();
public Term derivative();
public void addIn(Term anotherTerm);
public Term multiplyBy(Term anotherTerm);
public boolean equals(Object obj);
}
//Term class
public class Term implements TermInterface{
private int coefficient;
private int power;
public Term(){
coefficient=2;
power=3;
}
public Term(int coefficient,int power){
this.coefficient=coefficient;
this.power=power;
}
public Term(Term t){
try{
if(t==null){
throw new IllegalArgumentException("The Copy objcet is null");
}else{
this.coefficient=t.coefficient;
this.power=t.power;
}
}catch(IllegalArgumentException e){
System.out.println(e);
}
}
@Override
public String toString(){
String result=null;
if(coefficient==0){
result="0";
}else if(power==0){
result="";
}else if(coefficient==1&&power==1){
result="x";
}else if(coefficient==-1&&power==-1){
result="-x";
}else if(power==1){
result=String.valueOf(coefficient)+"x";
}else if(coefficient==1){
result="x^"+String.valueOf(power);
}else if(coefficient==-1){
result="-x^"+String.valueOf(power);
}else{
result=String.valueOf(coefficient)+"x^"+String.valueOf(power);
}
return result;
}
@Override
public int degree() {
if(coefficient==0)
return 0;
else
return power;
}
@Override
public Term derivative() {
Term term=null;
double derivative=coefficient*(power-1);
term=new Term(coefficient,power-1);
return term;
}
@Override
public boolean equals(Object obj){
boolean status=false;
if(this.equals(obj)){
status=true;
}
return status;
}
@Override
public double evaluate(double value) {
return value;
}
@Override
public void addIn(Term anotherTerm) {
try{
if(anotherTerm==null)
{
throw new IllegalArgumentException("The anotherterm object is null");
}else if(anotherTerm.power!=power){
throw new IllegalArgumentException("The anotherterm object power and current poer not same");
}else{
Term term=new Term(anotherTerm);
}
}catch (IllegalArgumentException e) {
System.out.println(e);
}
}
@Override
public Term multiplyBy(Term anotherTerm) {
Term term=null;
try{
if(anotherTerm==null)
{
throw new IllegalArgumentException("The anotherterm object is null");
}else{
term=new Term(anotherTerm);
}
}catch (IllegalArgumentException e) {
System.out.println(e);
}
return term;
}
}
Explanation : Check the above code as per given requirements.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.