Question
Java programs: 1. Create the class Ellipse with all the required instance variables, the constructor, all the get and set methods, the isValid and toString
Java programs:
1. Create the class Ellipse with all the required instance variables, the constructor, all the get and set methods, the isValid and toString methods, as described in the Project 3 assignment.
2. Create the class EllipseTester and test all the Ellipse methods you implemented in step 1.
Make sure that the names of your methods match those listed above exactly, including capitalization. The number of parameters and their order must also match. You can change the parameter names if you want. All methods should be public. 3 Class EllipseTester: This class should be your driver class and should contain the main method. For each method in your class Ellipse, your main method in EllipseTester should have at least one test case that tests that method.
This is the code I have so far, but it doesn't work, not sure where the error is,
import java.lang.*;
public class Ellipse{
private double x;
private double y;
private double width;
private double height;
private boolean rectMode;
private String color;
public Ellipse(){
x=0.0;
y=0.0;
height=0.0;
width=0.0;
}
Ellipse(double x,double y, double width, double height, boolean rectMode, String color){
if(isValid(x, y, width, height, rectMode, color)){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
this.rectMode=rectMode;
this.color=color;
}
else{
this.x=0;
this.y=0;
this.width=1;
this.height=1;
this.rectMode=false;
this.color="red";
}
}
public double getX(){
return x;
}
public double getY(){
return y;
}
public double getWidth(){
return width;
}
public double getHeight(){
return height;
}
public boolean getRectMode(){
return rectMode;
}
public String getColor(){
return color;
}
public void setX(double x){
if(isValid(x, this.y, this.width, this.height, this.rectMode, this.color)){
this.x=x;
}
}
public void setY(double y){
if(isValid(this.x, y, this.width, this.height, this.rectMode, this.color)){
this.y=y;
}
}
public void setWidth(double width){
if(isValid(this.x, this.y, width, this.height, this.rectMode, this.color)){
this.width=width;
}
}
public void setHeight(double height){
if(isValid(this.x, this.y, this.width, height,this.rectMode, this.color)){
this.height=height;
}
}
public void setRectMode(boolean rectMode){
if(isValid(this.x, this.y, this.width, this.height, rectMode, this.color)){
this.rectMode=rectMode;
}
}
public void setColor(String color){
if(isValid(this.x, this.y, this.width, this.height, this.rectMode, color)){
this.color=color;
}
}
public boolean equals(Ellipse ellipse){
if(this.equals(ellipse))
return true;
else
return false;
}
public double computePerimeter(){
height= height/2;
width= width/2;
double circumference=0.0;
if(height>=width){
double a=height-width;
double b=height+width;
double h= Math.pow(a, 2)/Math.pow(b, 2);
double f=10+Math.sqrt(4-3*h);
double g=1.0+3*h/f;
circumference= Math.PI*b*g;
}
else if(height
double a=width-height;
double b=width+height;
double h= Math.pow(a, 2)/Math.pow(b, 2);
double f=10+Math.sqrt(4-3*h);
double g=1+3*h/f;
circumference= Math.PI*b*g;
}
else{
double b=width+height;
circumference=Math.PI*b;
}
return circumference;
}
public double computeArea(){
height=height/2;
width=width/2;
double area=0.0;
area= Math.PI*height*width;
return area;
}
public boolean containsPoint(double x, double y){
if(this.x==x && this.y==y)
return true;
else
return false;
}
public static boolean isValid(double x,double y, double width, double height, boolean rectMode, String color){
if(x==0 && y==0)
rectMode=false;
else
rectMode=true;
if ((x=-100)&&(y=-100)&&(height>=0 && height=0 && width
return true;
else
return false;
}
public String toString(){
return "Perimeter of Ellipse: "+computePerimeter()+" Area of Ellipse: "+computeArea()+" Color of Ellipse: "+getColor();
}
}
import java.util.*;
public class EllipseTester {
public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.print("Enter X:"); double x=scan.nextDouble(); System.out.print("Enter Y:"); double y=scan.nextDouble(); System.out.print("Enter Width:"); double width=scan.nextDouble(); System.out.print("Enter Height:"); double height=scan.nextDouble(); System.out.print("Enter Color:"); String color=scan.nextLine(); Ellipse ellipse= new Ellipse(x, y, height, width, rectMode, color); System.out.println(ellipse.toString()); } }
Project Details: For this project you will have to write two classes. Class Ellipse is a class that represents an axis-aligned ellipse. Class EllipseTester is the driver class, used to test the Ellipse class Class ellipse: An instance of the class Ellipse represents a valid axis-aligned ellipse. It has several instance variables that one needs to use to describe an ellipse, as well as methods that operate on these variables. Ellipse instance variables.: Class Ellipse will have the following instance variables: x-a double that represents the x coordinate of the ellipse y-a double that represents the y coordinate of the ellipse * width - a double that represents the width of the ellipse . height - a double that represents the height of the ellipse rectMode - a boolean: if true (x,y) is the upper left corner of the ellipse bounding box; if false (x,y) is the ellipse's center point color - a String that represents the color of the ellipse . . For an ellipse to be valid, the width and height must be between 0 and 1 inclusive, the ellipse area must be contained within a bounding box defined by the points (-100, -100) and (100, 100), and the color must be one of the following: "black", "red", "green", "blue" Ellipse constructor: The Ellipse constructor should be: Ellipse (double x, double y, double height, double width, String color, boolean rectMode) The constructor should check that these values represent a valid ellipse. If they do, then it should initialize the ellipse to these values. Otherwise, the ellipse should be initialized to an ellipse at (0, 0), width 1, height 1, rectMode false, color "redStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started