circle object constructor not defined - java

I am trying to make class circle extend the Shape class but keep getting a error from JUnit saying the constructor Circle(Point, int) is undefined how would i define the Circle constructor differently from public Circle(Point[] center, int aradius)?
import java.awt.Point;
public abstract class Shape {
private String name;
private Point[] points;
protected Shape(){};
protected Shape(String aName) {
name = aName;
}
public final String getName() {
// TODO Implement method
return name;
}
protected final void setPoints(Point[] thePoints) {
points = thePoints;
}
public final Point[] getPoints() {
// TODO Implement method
return points;
}
public abstract double getPerimeter();
public static double getDistance(Point one, Point two) {
double x = one.getX();
double y = one.getY();
double x2 = two.getX();
double y2 = two.getY();
double x3 = x - x2;
double y3 = y - y2;
double ypow = Math.pow(y3, 2);
double xpow = Math.pow(x3, 2);
double added = xpow + ypow;
double distance = Math.sqrt(added);
return distance;
}
}
Circle.java
import java.awt.Point;
public class Circle extends Shape{
private double radius;
public Circle(Point[] center, int aradius) {
if(radius < 0){
radius = 0;
}
else{
radius = aradius;
}
this.setPoints(center);
}
#Override
public double getPerimeter() {
double perim = 2 * Math.PI * radius;
return perim;
}
public double getRadius(){
return radius;
}
}

Just pass it a single Point, not an array.
public Circle(Point center, int aradius)

Related

Must implement inherited abstract method error

I'm getting an error for Sphere.java that says
The type sphere must implement the inherited abstract method GeometricObjects.hit(Ray)
Though I don't see how I am getting this error as I've defined the hit method as shown below.
Below is the Sphere.java code:
package Objects;
import Utility.*;
public class Sphere extends GeometricObjects {
Point3d center;
double radius;
public Sphere(Point3d center, double radius, Color color) {
this.center = new Point3d(center);
this.radius = radius;
this.color = new Color(color);
}
public double hit(Ray ray) {
double a = ray.direction.dot(ray.direction);
double b = 2*ray.origin.sub(center).dot(ray.direction);
double c = ray.origin.sub(center).dot(ray.origin.sub(center)) - radius*radius;
double discriminant = b*b - 4*a*c;
if (discriminant < 0.0){
return 0.0;
}
else {
double t = (-b - Math.sqrt(discriminant)) / (2*a);
if (t > 10E-9) {
return t;
}
else {
return 0.0;
}
}
}
}
This is the GeometricObjects.java code:
package Objects;
import Utility.*;
public abstract class GeometricObjects {
public Color color;
public abstract double hit(Ray ray);
}
And heres the Ray.java code:
package Utility;
public class Ray {
public Point3d origin;
public Vector direction;
public Ray(Point3d origin, Vector direction) {
this.origin = new Point3d(origin);
this.direction = new Vector(direction);
}
}

Cannot find symbols while using abstract classes

Practicing abstract classes with shapes. The goal is to get the total area of the 3 shapes using abstract classes. So far this is what I have.
I'm not sure whether I did this part right:
static double sumArea(Shape[] arr){
// Sum up the areas of all the shapes using getArea()
return arr.getArea();
}
I keep getting error saying that it doesn't find the symbol h (height), w (width), tw (top_width). Anybody know why it's not finding these symbols?
public class TestShape{
public static void main(String args[]){
Point p = new Point(1, 1);
Shape[] arr = {
new Rectangle(p, 3, 4),
new Parallelogram(p, 5, 6, Math.PI/6.0),
new Trapezoid(p, 5, 6, 2)
};
System.out.println("SUM_AREA = " + sumArea(arr));
}
static double sumArea(Shape[] arr){
// Sum up the areas of all the shapes using getArea()
return arr.getArea();
}
}
class Point{
double x, y;
Point(){
this(0, 0);
}
Point(double x, double y){
this.x = x;
this.y = y;
}
public String toString(){
return "[" + x + ", " + y + "]";
}
}
abstract class Shape{
Shape(){
}
Shape(Point p){
this.p = p;
}
public Point getPosition(){
return p;
}
public void setPosition(Point p){
this.p = p;
}
// Abstract method
public abstract double getArea();
}
abstract class Quadrangle extends Shape{
protected double width, height;
Quadrangle(Point p, double w, double h){
this.p = p;
this.width = w;
this.height = h;
}
public double getWidth(){
return w;
}
public double getHeight(){
return h;
}
public void setWidth(double w){
this.weight = w;
}
public void setHeight(double h){
this.height = h;
}
}
class Rectangle extends Quadrangle{
Rectangle(Point p, double w, double h){
this.p = p;
this.width = w;
this.height = h;
}
public boolean isSquare(){
if(w == h){
return "Error";
}
}
#Override /** Return Area */
public double getArea(){
return w * h;
}
}
class Parallelogram extends Quadrangle{
protected double angle;
Parallelogram(Point p, double w, double h, double angle){
this.p = p;
this.weight = w;
this.height = h;
this.angle = angle;
}
public double getAngle(){
return angle;
}
public void setAngle(double a){
this.angle = a;
}
#Override /** Return Area */
public double getArea(){
return w * h;
}
}
class Trapezoid extends Quadrangle{
protected double top_width;
Trapezoid(Point p, double w, double h, double top_width){
this.p = p;
this.width = w;
this.height = h;
this.top_width = top_width;
}
public double getTopWidth(){
return top_width;
}
public void setTopWidth(double tw){
this.top_width = tw;
}
#Override /** Return Area */
public double getArea(){
return ((w + tw) / 2) * h;
}
}
The names w, tw and so on only exist as parameters. When you want to access the values you save in the constructors, you have to use the left hand side name: this.[width or whatever].
Also, rewrite sumArea to something like this:
static double sumArea(Shape[] arr){
// Sum up the areas of all the shapes using getArea()
double totalArea = 0;
for (Shape shape : arr) {
totalArea += shape.getArea();
}
return totalArea;
}

How to handle multiple constructors?

Hello I have problems with assignment where I need to create 3 constructors in one class. That initialize 2 coordinates for 2 corners in a rectangle. The error Eclipse is giving: "Duplicate method" and "Multiple markers on this line" errors with the Constructs.
public class Rectangle {
private double lowleftx;
private double lowlefty;
private double uprightx;
private double uprighty;
public Rectangle() {
this.lowleftx = 0;
this.lowlefty = 0;
this.uprightx = 1;
this.uprighty = 1;
}
public Rectangle(uprightx, uprighty) {
this.lowleftx = 0;
this.lowlefty = 0;
}
public Rectangle(uprightx, uprighty, lowleftx, lowlefty) {
this.lowleftx = lowleftx;
this.lowlefty = lowlefty;
this.uprightx = uprightx;
this.uprighty = uprighty;
}
public double getLowleftx() {
return lowleftx;
}
public void setLowleftx(double lowleftx) {
this.lowleftx = lowleftx;
}
public double getLowlefty() {
return lowlefty;
}
public void setLowlefty(double lowlefty) {
this.lowlefty = lowlefty;
}
public double getUprightx() {
return uprightx;
}
public void setUprightx(double uprightx) {
this.uprightx = uprightx;
}
public double getUprighty() {
return uprighty;
}
public void setUprighty(double uprighty) {
this.uprighty = uprighty;
}
}
As said in the comments, you forgot to add the type of the parameters :
public Rectangle(double uprightx, double uprighty...)
You can optimize your code by calling the constructor with all parameters from other constructors :
public class Rectangle {
private double lowLeftX;
private double lowLeftY;
private double upRightX;
private double upRightY;
public Rectangle(double lowLeftX, double lowLeftY, double upRightX, double upRightY) {
this.lowLeftX = lowLeftX;
this.lowLeftY = lowLeftY;
this.upRightX = upRightX;
this.upRightY = upRightY;
}
public Rectangle(double upRightX, double upRightY) {
this(0, 0, upRightX, upRightY); // = Rectangle(0, 0, upRightX, upRightY)
}
public Rectangle() {
this(0, 0, 1, 1); // = Rectangle(0, 0, 1, 1), or Rectangle(1, 1)
}
// ...
}
You can also create a class to represent a "point" (a coordinate with a X value and Y value) and use it in your Rectangle class :
// Point.java
public class Point {
private final double x;
private final double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
// ...
}
// Rectangle.java
public class Rectangle {
private final Point lowLeft;
private final Point upRight;
public Rectangle(final Point lowLeft, final Point upRight) {
this.lowLeft = lowLeft;
this.upRight = upRight;
}
public Rectangle(final Point upRight) {
this(new Point(0, 0), upRight);
}
public Rectangle() {
this(new Point(1, 1));
}
}
You have missed to specify the double datatype in the constructor arguments, so add it as shown below:
public Rectangle() {
this.lowleftx = 0;
this.lowlefty = 0;
this.uprightx = 1;
this.uprighty = 1;
}
public Rectangle(double uprightx, double uprighty) {
this.lowleftx = 0;
this.lowlefty = 0;
}
public Rectangle(double uprightx, double uprighty,
double lowleftx, double lowlefty) {
this.lowleftx = lowleftx;
this.lowlefty = lowlefty;
this.uprightx = uprightx;
this.uprighty = uprighty;
}
So, as seen in other answers: you forgot to specify the types for your parameters.
And for completeness: this is how you should really write down this constructors:
public Rectangle() {
this(1, 1);
}
public Rectangle(double x1, double y1) {
this(0, 0, x1, y1);
}
public Rectangle(double x1, double y1, double x2, double y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
In other words: avoid repeating code. You can delegate the real "assignment" work into your last ctor. And then you want to use easy to read but still meaningful names. When thinking about coordinates, x1/y1 is for example much easier to grasp then your approach.

Java - Cylinder Program - Unable to use main

I am so lost I don't even know what question to ask anymore. I have this program that is supposed to be able to construct a Cylinder with parameters radius and height. Then it should be able to call various methods to get and set radius as well as output surface area and volume. I can't get passed go because I cannot put anything in my main without an error about static non static methods being used in static. I don't even know what that means. I actually copy code from others into my compiler and it gives me the same error. Do I have some setting screwed up? I know this is probably too elementary for Stack Overflow but I am desperate at this point.
public class Miller_A03Q1 {
public static void main(String[] args) {
Cylinder cylinder1 = new Cylinder(1,17);
Cylinder cylinder2 = new Cylinder(3,8);
Cylinder cylinder3 = new Cylinder(2,12);
Cylinder cylinder4 = new Cylinder (1,14);
}
public class Cylinder{
private double radius = 0.0;
private double height= 0.0;
private double area = 0.0;
private double volume=0.0;
private String shape = "cylinder";
public Cylinder(double r,double h){
this.radius = r;
System.out.print(r);
this.height = h;
System.out.print(h);
}
public double getVolume(){
double volume = 3.14 * radius * radius * height;
return volume;
}
public double getArea(){
double circumference = 3.14 * 2 * radius;
double circleArea = 3.14 * radius * radius;
double area = (2 * circleArea) + (circumference * this.height);
return area;
}
public double getRadius(){
return this.radius;
}
public double getHeight(){
return this.height;
}
public void setHeight(double h){
this.height = h;
}
public void setRadius(double r){
this.radius = r;
}
#Override
public String toString(){
return this.shape + this.radius + this.height+ this.volume + this.area;
}
}
}
Inner classes are just like any other member (well, except for enums). If you don't explicitly declare them static, they won't be, so you won't be able to access them from a static context, such as main. To make a long story short - declare you Cylinder inner class as static, and you should be OK:
public class Miller_A03Q1 {
public static void main(String[] args) {
Cylinder cylinder1 = new Cylinder(1,17);
Cylinder cylinder2 = new Cylinder(3,8);
Cylinder cylinder3 = new Cylinder(2,12);
Cylinder cylinder4 = new Cylinder (1,14);
}
public static class Cylinder{
// etc...
I don't know if you need the outer class but if you just put the main method inside the Cylinder class it compiles for me....
public class Cylinder {
private double radius = 0.0;
private double height = 0.0;
private double area = 0.0;
private double volume = 0.0;
private String shape = "cylinder";
public Cylinder(double r, double h) {
this.radius = r;
System.out.print(r);
this.height = h;
System.out.print(h);
}
public double getVolume() {
double volume = 3.14 * radius * radius * height;
return volume;
}
public double getArea() {
double circumference = 3.14 * 2 * radius;
double circleArea = 3.14 * radius * radius;
double area = (2 * circleArea) + (circumference * this.height);
return area;
}
public double getRadius() {
return this.radius;
}
public double getHeight() {
return this.height;
}
public void setHeight(double h) {
this.height = h;
}
public void setRadius(double r) {
this.radius = r;
}
#Override
public String toString() {
return this.shape + this.radius + this.height + this.volume + this.area;
}
public static void main(String[] args) {
Cylinder cylinder1 = new Cylinder(1, 17);
Cylinder cylinder2 = new Cylinder(3, 8);
Cylinder cylinder3 = new Cylinder(2, 12);
Cylinder cylinder4 = new Cylinder(1, 14);
}
}

How to create an object from data in a file and assign it to an array?

I need to take information from a file and create them into objects and put them into an array so I can compare the areas of the objects and list in the array which object has the largest area and its location in the array.
I'm confused on how I take the information from the file and create each one into a object (circle or rectangle) and then assign that object into an array after it has been created. I think my other classes are fine, I'm just stuck on finishing the driver.
Normally, I would do something like Circle c1 = new Circle(); to create a new object, but how do I do that from a file with predefined information and assign it to an array?
Data:
“CIRCLE”, 1, “blue”, true
“RECTANGLE”, 1, 2, “blue”, true
“RECTANGLE”, 10, 2, “red”, true
“CIRCLE”, 2, “green”
“RECTANGLE”
“CIRCLE”
Driver:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("C:/Users/Charles/Desktop/GeometricObjectsData.txt"));
ArrayList<GeometricObject> list = new ArrayList<GeometricObject>();
while (input.hasNext()) {
String line = input.nextLine();
System.out.println(line);
}
}
}
GeometricObject:
public abstract class GeometricObject {
//class variables
private String color;
private boolean filled;
//constructors
public GeometricObject() {
super();
color = "white";
filled = false;
}
public GeometricObject(String color, boolean filled) {
super();
this.color = color;
this.filled = filled;
}
//mutators
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
//user-defined methods
public abstract double getArea();
public abstract double getPerimeter();
#Override
public String toString() {
return super.toString() + " \tColor=" + this.getColor() + " \tFilled=" + this.isFilled();
}
}
Circle:
public class Circle extends GeometricObject {
//class variables
private double radius;
//constructors
public Circle() {
super();
radius = 1;
}
public Circle(double radius, String color, boolean filled) {
super(color, filled);
this.radius = radius;
}
//mutators
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
//user-defined methods
#Override
public double getArea() {
//area of a circle
return (radius * radius * Math.PI);
}
#Override
public double getPerimeter() {
//perimeter of a circle
return (2 * radius * Math.PI);
}
#Override
public String toString() {
return super.toString() + "\nCircle: Radius=" + this.getRadius();
}
}
Rectangle:
public class Rectangle extends GeometricObject {
//class variables
private double height;
private double width;
//constructors
public Rectangle() {
super();
height = 1;
width = 1;
}
public Rectangle(double height, double width, String color, boolean filled) {
super(color,filled);
this.height = height;
this.width = width;
}
//mutators
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
//user-defined methods
#Override
public String toString() {
return super.toString() + "\nRectangle: Height=" + this.height + "\tWidth=" + this.width;
}
#Override
public double getArea() {
return (height * width);
}
#Override
public double getPerimeter() {
return (2 * height + 2 * width);
}
}
In your text file, you have special quotes around your shape items. This will make your life more difficult, so you should change that, if possible
Example of how to make objects (from your main method):
while (input.hasNext()) {
String line = input.nextLine();
System.out.println(line);
String[] parts = line.split(",");
if (parts[0].indexOf("Circle") != -1) {
Circle c = new Circle();
// ... parse the rest of the attributes to set up your circle
} else if ... // fill in the other shape cases
}

Categories

Resources