Does import with wildcard behave differently with user package - java
//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.)
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); } }
Java - Cannot resolve constructor
I am new to Java programming. I have three classes which are Main, Point and Rectangle. I can use all constructors in Rectangle class except this one: Rectangle(Point p, int w, int h). Java compiler gives: "Cannot resolve constructor 'Rectangle(java.awt.Point, int, int)'" error. Thanks. Here my classes: Main.java import java.awt.*; import java.awt.Point; import java.awt.Rectangle; public class Main { public static void main(String[] args) { Point originOne = new Point(5,10); Rectangle rectOne = new Rectangle(originOne, 100, 200); } } Point.java public class Point { public int x = 0; public int y = 0; //contructor public Point(int a, int b){ x = a; y = b; } } Rectangle.java public class Rectangle { public Point origin; public int width = 0; public int height = 0; //four contructors public Rectangle() { origin = new Point(0, 0); } public Rectangle(Point p){ origin = p; } public Rectangle(int w, int h){ origin = new Point(0,0); width = w; height = h; } public Rectangle(Point p, int w, int h) { origin = p; width = w; height = h; } // a method for moving the rectangle public void move(int x, int y) { origin.x = x; origin.y = y; } //a method for computing the area of rectangle public int getArea() { return width * height; } }
Your answer is fairly simple. You've imported the wrong import, rather than import java.awt.Rectangle, import your own class. By doing: import <nameOfTheProject>.Rectangle; If I had a project in Eclipse called MyShapes and had the same classes. I would import it like: import MyShapes.Rectangle; Now this obviously depends on how your file structure is, but if it's inside a sub-folder (sub-package), I would do like: import MyShapes.ShapesClasses.Rectangle; This also applies to the Point class if you're planning on using your own Point class and not Java.awt's one!
How to implement one generic method for two classes in java
I have an interface that has one ordinary method and one generic method. I have implemented ordinary method for two different classes, but do not now how to do that with generic method. Here is my code: Sphere.java: public class Sphere implements GeometricShape<Sphere> { private double radius; public Sphere (double radius) { this.radius = radius; } public double volume() { return (4.0 / 3.0) * Math.PI * radius * radius * radius; } public void describe() { System.out.println("Sphere[radius=" + radius + "]"); } #Override public Sphere supersize() { this.radius*=2; return new Sphere(radius); } } Rectangle.java public class Rectangle implements TwoDShape { private double width, height; public Rectangle (double width, double height) { this.width = width; this.height = height; } public double area() { return width * height; } public double perimeter() { return 2.0 * (width + height); } public void describe() { System.out.println("Rectangle[width=" + width + ", height=" + height + "]"); } #Override public Rectangle supersize() { this.width*=2; this.height*=2; return new Rectangle(width, height); } } TwoDShape.java: public interface TwoDShape extends GeometricShape { public double area(); } ThreeDShape.java: public interface ThreeDShape extends GeometricShape<ThreeDShape> { public double volume(); } GeometricShape.java: public interface GeometricShape<T extends GeometricShape<T>> { public void describe(); public T supersize(); } and finally main class ArrayListExample.java: import java.util.ArrayList; public class ArrayListExample { public static void describe_all( ArrayList<? extends GeometricShape> shapes ) { for(int i=0;i<shapes.size();i++) { shapes.get(i).describe(); } System.out.println("Total number of shapes:"+ shapes.size()); } public static void main(String[] args) { System.out.println("The describe() method:"); System.out.println(); System.out.println("Example rectangles"); ArrayList<Rectangle> rects = new ArrayList<Rectangle>(); rects.add(new Rectangle(2.0, 3.0)); rects.add(new Rectangle(5.0, 5.0)); describe_all(rects); System.out.println(); ArrayList<Sphere> spheres = new ArrayList<Sphere>(); spheres.add(new Sphere(10.0)); spheres.add(new Sphere(50.0)); spheres.add(new Sphere(0.0)); System.out.println("Example spheres"); describe_all(spheres); System.out.println(); System.out.println("The supersize() method:"); System.out.println(); ArrayList<Rectangle> double_rects = supersize_list(rects); describe_all(double_rects); System.out.println(); ArrayList<Sphere> double_spheres = supersize_list(spheres); describe_all(double_spheres); } } How can I implement supersize_list method that it takes supersize method from both rectangle and sphere and outputs like Rectangle[width=4.0, height=6.0] Rectangle[width=10.0, height=10.0] Total number of shapes: 2 Sphere[radius=20.0] Sphere[radius=100.0] Sphere[radius=0.0] Total number of shapes: 3 Could you help me with this, please? I greatly appreciate your help!
The class hierarchy looks inconsistent. For example, you have ThreeDShape extends GeometricShape<ThreeDShape> and TwoDShape extends GeometricShape at the same time, for no obvious reason. It's not fun to write a generic method for these types. Here's a less-confusing version. (I hope) Note: I choose not to change the size of the shape itself in supersize method, instead let it return a bigger shape while keeping the original unchanged. 1. GeometricShape /** * A geometric shape interface. You can do two things with it. * 1. Ask it to describe itself (to stdout); * 2. Ask it to return a bigger version of itself (double the size). */ public interface GeometricShape<T extends GeometricShape<T>> { /** * Print a description to STDOUT */ void describe(); /** * Returns a bigger shape. * #return Something that's a GeometricShape */ T supersize(); } 2. Shape2D and Rectangle /** * A 2-dimensional shape. * It has area. * Its supersize() method should return a Shape2D instance. */ public interface Shape2D<T extends Shape2D<T>> extends GeometricShape<T> { double area(); } /** * A rectangle. */ public final class Rectangle implements Shape2D<Rectangle> { private final double width; private final double height; public Rectangle(double width, double height) { this.width = width; this.height = height; } #Override public String toString() { return "Rectangle{" + "width=" + width + ", height=" + height + '}'; } #Override public void describe() { System.out.println(this); } #Override public Rectangle supersize() { return new Rectangle(width*2, height*2); } #Override public double area() { return width * height; } } 3. Shape3D and Sphere /** * A 3-dimensional shape. * It has volume. * Its supersize() method should return a Shape3D instance. */ public interface Shape3D<T extends Shape3D<T>> extends GeometricShape<T> { double volume(); } /** * A sphere */ public final class Sphere implements Shape3D<Sphere> { private final double radius; public Sphere(double radius) { this.radius = radius; } #Override public String toString() { return "Sphere{" + "radius=" + radius + '}'; } #Override public void describe() { System.out.println(this); } #Override public Sphere supersize() { return new Sphere(radius*2); } #Override public double volume() { return 4*Math.PI*Math.pow(radius, 3)/3; } } Now the generic method that transforms a list public static <T extends GeometricShape<T>> List<T> supersize_list(List<T> list) { List<T> result = new ArrayList<>(); for (T shape : list) { result.add(shape.supersize()); } return result; }
You do not need to return a new Object. For Rectangle for example #Override public void supersize() { this.width*=2; this.height*=2; } is sufficient
Inheritance casting
I have two class. class Vector{ ........ Vector(int x, int y, int z){...........} public Vector sum(Vector vc){ Vector result; ...........//all working and store to Vector result. return result; } public Vector subtract(Vector vc){................//codes} } class Velocity extends Vector{ Velocity(int x, int y, int z){......} ................ } class Test{ public static void main(String args){ Velocity v1=new Velocity(14,14,14); Velocity v2=new Velocity(14,14,14); Vector result=v1.sum(v2); //here I want to get this result as Velocity //I don't know how to get it.... } } In similar manner I have Acceleration, Force, Momentum, Displacement class which extends Vector class....And all have same problem......besides this
Vector.java: public interface Vector<T extends Vector> { public int getX(); public int getY(); public int getZ(); public T sum(T other); } BaseVector.java: public abstract class BaseVector<T extends Vector> implements Vector<T> { private final int x; private final int y; private final int z; public BaseVector(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } protected abstract T createNew(int x, int y, int z); #Override public T sum(T other) { return createNew(x + other.getX(), y + other.getY(), other.getZ()); } #Override public int getX() { return x; } #Override public int getY() { return y; } #Override public int getZ() { return z; } #Override public String toString() { return "BaseVector [x=" + x + ", y=" + y + ", z=" + z + "]"; } } Velocity.java: public class Velocity extends BaseVector<Velocity> { public Velocity(int x, int y, int z) { super(x, y, z); } #Override protected Velocity createNew(int x, int y, int z) { return new Velocity(x, y, z); } } Test.java: public static void main(String[] args) { Velocity v1 = new Velocity(14, 14, 14); Velocity v2 = new Velocity(14, 14, 14); Velocity result = v1.sum(v2); System.out.println(result); } }
Vector result = ((Velocity)v1).sum(v2); ========================================================= This will not help, because you defined sum in Vector, returning Vector. To make result a Velocity you would need a sum method returning type Velocity, rather than the one in Vector that returns a Vector. A better alternative might be to provide a Velocity constructor that takes a Vector Velocity result = new Velocity(v1.sum(v2));
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)