Must implement inherited abstract method error - java

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);
}
}

Related

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;
}

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);
}
}

Does import with wildcard behave differently with user package

//With these I am getting compilation - error class not found
/*import Shape.TwoD.*;
import Shape.ThreeD.*;
import Shape.*;*/
import Shape.TwoD.Circle;
import Shape.TwoD.Line;
import Shape.ThreeD.Line3D;
import Shape.ThreeD.Sphere;
import Shape.Actions;
public class Test {
/**
* Creates a new instance of <code>Test</code>.
*/
int o;
public Test() {
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Actions obj[] = new Actions[4];
obj[0] = new Line(1,2,3,4);
obj[1] = new Circle(1,2,3);
obj[2] = new Line3D(1,2,3,4,5,6);
obj[3] = new Sphere(1,2,3,4);
for(Actions x: obj)
x.draw();
Actions.TwoD o =(Circle)obj[1];
System.out.println("Area of circle "+o.area());
o = (Sphere)obj[3];
System.out.println("Volume of sphere "+o.area());
}
}
Action is an interface which contains nested interface TwoD and ThreeD
Why import with wildcard not working in the above code? Am I using it wrong?
I couldn't find any related answer, if both wildcard and fully qualified imports are not working then there is problem in my code but in this case, compilation error occur class not found only when I use wildcard with import.
EDIT:
Sorry for the wrong naming convention, Line,Circle,Line3D and Sphere are the classes
Lineand Circle comes under Shape.TwoD
Line3D and Sphere comes under Shape.ThreeD
Actions.java:
package Shape;
public interface Actions {
interface ThreeD{
double volume();
}
interface TwoD{
double area();
}
void draw();
//void erase();
final double pi = 3.142857;
}
Line.java:
package Shape.TwoD;
public class Line implements Shape.Actions{
BaseObj.Point p1,p2;
public Line(int x1,int y1, int x2 ,int y2) {
p1 = new BaseObj.Point(x1,y1);
p2 = new BaseObj.Point(x2,y2);
}
public void draw(){
//System.out.println("Line between ("+p1.x+","+p1.y+") and ("+p2.x+","+p2.y+"));
System.out.println("Line between ("+p1.getx()+","+p1.gety()+") and ("+p2.getx()+","+p2.gety()+") has been drawn");
}
}
Circle.java:
package Shape.TwoD;
public class Circle extends BaseObj.Point implements Shape.Actions, Shape.Actions.TwoD{
protected int radius;
public Circle(int x, int y, int radius) {
super(x,y);
this.radius = radius;
}
public void draw(){
System.out.println("Circle with ("+x+","+y+") as center and radius "+radius+" units has been drawn");
}
public double area(){
return (pi*radius*radius);
}
}
Line3D.java:
package Shape.ThreeD;
public class Line3D implements Shape.Actions {
BaseObj.Point3D p1,p2;
public Line3D(int x1,int y1, int z1, int x2,int y2, int z2) {
p1 = new BaseObj.Point3D(x1,y1,z1);
p2 = new BaseObj.Point3D(x2,y2,z2);
}
public void draw(){
//System.out.println("Line between ("+p1.x+","+p1.y+") and ("+p2.x+","+p2.y+"));
System.out.println("Line between ("+p1.getx()+","+p1.gety()+","+p1.getz()+") and ("+p2.getx()+","+p2.gety()+","+p2.getz()+") has been drawn");
}
}
Sphere.java:
package Shape.ThreeD;
public class Sphere extends Shape.TwoD.Circle{
int z;
public Sphere(int x, int y, int z, int radius) {
super(x,y,radius);
this.z = z;
}
public void draw(){
System.out.println("Spere with ("+x+","+y+","+z+") as center and radius "+radius+" units has been drawn");
}
public double volume(){
return(radius*radius*pi*4/3);
}
public double area(){
System.out.println("Sphere is a 3D object so 2D quantitys doesnt apply");
return 0.0;
}
}
Edit2:
After correcting the names I got error that Actions interface is duplicate so I changed its name into ObjActions and the problem resolved. Thanks for the help. I hope the naming convention I used below is consistent with standard.
ObjActions.java
package shape;
public interface ObjActions {
interface Actions3D{
double volume();
}
interface Actions2D{
double area();
}
void draw();
//void erase();
final double pi = 3.142857;
}
Circle.java
package shape.twod;
public class Circle extends baseobj.Point implements shape.ObjActions, shape.ObjActions.Actions2D{
protected int radius;
public Circle(int x, int y, int radius) {
super(x,y);
this.radius = radius;
}
public void draw(){
System.out.println("Circle with ("+x+","+y+") as center and radius "+radius+" units has been drawn");
}
public double area(){
return (pi*radius*radius);
}
}
Line.java
package shape.twod;
public class Line implements shape.ObjActions{
baseobj.Point p1,p2;
public Line(int x1,int y1, int x2 ,int y2) {
p1 = new baseobj.Point(x1,y1);
p2 = new baseobj.Point(x2,y2);
}
public void draw(){
//System.out.println("Line between ("+p1.x+","+p1.y+") and ("+p2.x+","+p2.y+"));
System.out.println("Line between ("+p1.getx()+","+p1.gety()+") and ("+p2.getx()+","+p2.gety()+") has been drawn");
}
}
Line3D.java
package shape.threed;
public class Line3D implements shape.ObjActions {
baseobj.Point3D p1,p2;
public Line3D(int x1,int y1, int z1, int x2,int y2, int z2) {
p1 = new baseobj.Point3D(x1,y1,z1);
p2 = new baseobj.Point3D(x2,y2,z2);
}
public void draw(){
//System.out.println("Line between ("+p1.x+","+p1.y+") and ("+p2.x+","+p2.y+"));
System.out.println("Line between ("+p1.getx()+","+p1.gety()+","+p1.getz()+") and ("+p2.getx()+","+p2.gety()+","+p2.getz()+") has been drawn");
}
}
Sphere.java
package shape.threed;
public class Sphere extends shape.twod.Circle implements shape.ObjActions.Actions3D{
int z;
public Sphere(int x, int y, int z, int radius) {
super(x,y,radius);
this.z = z;
}
public void draw(){
System.out.println("Spere with ("+x+","+y+","+z+") as center and radius "+radius+" units has been drawn");
}
public double volume(){
return(radius*radius*pi*4/3);
}
}
Test.java
package test;
import shape.twod.*;
import shape.threed.*;
import shape.*;
/*import shape.twod.Circle;
import shape.twod.Line;
import shape.threed.Line3D;
import shape.threed.Sphere;
import shape.Actions;*/
public class Test {
/**
* Creates a new instance of <code>Test</code>.
*/
int o;
public Test() {
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
ObjActions obj[] = new ObjActions[4];
obj[0] = new Line(1,2,3,4);
obj[1] = new Circle(1,2,3);
obj[2] = new Line3D(1,2,3,4,5,6);
obj[3] = new Sphere(1,2,3,4);
for(ObjActions x: obj)
x.draw();
ObjActions.Actions2D o =(Circle)obj[1];
//Actions2D o =(Circle)obj[1];
System.out.println("Area of circle "+o.area());
ObjActions.Actions3D op = (Sphere)obj[3];
System.out.println("Volume of sphere "+op.volume());
}
}
The "packages" TwoD and ThreeD might get shadowed by the interfaces Action.TwoD and Action.ThreeD (or vice versa) as per JLS 7.5.2:
The declaration might be shadowed by a single-type-import declaration of a type whose simple name is Vector; by a type named Vector and declared in the package to which the compilation unit belongs; or any nested classes or interfaces.
The declaration might be obscured by a declaration of a field, parameter, or local variable named Vector.
(It would be unusual for any of these conditions to occur.)

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
}

circle object constructor not defined

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)

Categories

Resources