The Cube class have two constructors, one which accepts three parameters that are converted into the tree attributes of the cube, and another one that doesn't require any parameter and therefore creates an "empty" cube. My question is how can a boolean method check if the cubes are valid or empty? Is there a way to do that without the need of checking each one of the attributes?
class Application {
public static void main(String[] args) {
Cube c1 = new Cube(4, 3, 6);
Cube c2 = new Cube();
System.out.println(isNotEmpty(c1));
System.out.println(isNotEmpty(c2));
}
public static boolean isNotEmpty(Cube cube) {
if (/*cube attributes are NOT empty*/) {
return true;
} else {
return false;
}
}
public static class Cube {
private int height;
private int width;
private int depth;
public Cube() {}
public Cube(int height, int width, int depth) {
this.height = height;
this.width = width;
this.depth = depth;
}
public int getHeight() { return height; }
public int getWidth() { return width; }
public int getDepth() { return depth; }
}
}
Since it appears that the only state which a Cube has are the height, width, and depth, then you could actually just use null to represent an empty Cube.
It doesn't make much sense to call a cube with no dimensions a cube in the first place. Using null as a marker might make the most sense.
Either change one (or more) of your int fields to be an Integer Object, or introduce a new Boolean field isSet or get rid of your empty constructor
1) If you use an Integer Object you can test to see if it is null where -as int primitives have a default value of 0
2) If you have a Boolean field you can default it to false and set it to true in your proper constructor
Use a bool flag isEmptyCube in the constructor. At the time of object creation, it will be automatically marked correctly whether it is blank or not.
public static class Cube {
//...
private boolean isEmptyCube;
public Cube() {isEmptyCube = true;}
public Cube(int hight, int width, int depth) {
//...
isEmptyCube = false;
}
public isCubeEmpty() { return isEmptyCube;}
It seems a so tricky question. At first, we have to have any criteria: What is an empty object?. When we have some criteria, even single, we must check it.
From the reason when we are considering the Cube c3 = new Cube(0, 0, 0) like is not empty, so, here is one of ways:
public class CubeApplication {
public static void main(String[] args) {
Cube c1 = new Cube(4, 3, 6);
Cube c2 = new Cube();
Cube c3 = new Cube(0, 0, 0);
System.out.println(c1.isEmpty());
System.out.println(c2.isEmpty());
System.out.println(c3.isEmpty());
}
static class Cube {
private int hight;
private int width;
private int depth;
private boolean isEmpty;
public Cube() {
this.isEmpty = false;
}
public Cube(int hight, int width, int depth) {
this.hight = hight;
this.width = width;
this.depth = depth;
this.isEmpty = true;
}
public boolean isEmpty() {
return this.isEmpty;
}
public int getHight() {
return this.hight;
}
public int getWidth() {
return this.width;
}
public int getDepth() {
return this.depth;
}
}
}
OUTPUT:
true
false
true
Related
I'm writing a Rectangle class and it has 2 instance variables which are 2 points that are called _pointSW and _pointNE. I have to define the width and the height of the rectangle, but I can't use any other variables besides the 2 points.
I want to ask how I could write the getWidth() method, for example using just the _pointNE, or maybe better write a private method (because I can't use any new public methods) to define the width and the height, and then use it in other methods and if that's an option, then how do I actually write it? thanks!
If your SW means South-West and NE means North-East:
public class Tuple<X, Y> {
public final X x;
public final Y y;
public Tuple(X x, Y y) {
this.x = x;
this.y = y;
}
}
public class Rectangle {
private Tuple<double,double> pointSW;
private Tuple<double,double> pointNE;
public Rectangle(final Tuple<double,double> sw, final Tuple<double,double> ne) {
this.pointSW = sw;
this.pointNE = ne;
}
public double getWidth() {
return Math.abs(this.pointNE.x - this.pointSW.x);
}
public double getHeight() {
return Math.abs(this.pointSW.y - this.pointNE.y);
}
public void setWidth(final double width) {
this.pointNE.x = this.point.SW.x + width;
}
public void setHeight(final double eight) {
this.pointNE.y = this.point.SW.y + eight;
}
}
I develop a java application to generate a 3D object as a .obj file. I would like to visualize this object in a viewer3D of my application before exporting it but I only have a java object containing a list of faces and vertices of my 3D object. From my list of faces and vertices I would like to create a javafx type Shape or MeshView.
In fact, I'm trying to convert my java object into a javafx 3D object.
I have implement a Face Object :
public class Face {
private int id, idVertice1, idVertice2, idVertice3;
public Face(int idVertice1, int idVertice2, int idVertice3) {
this.idVertice1 = idVertice1;
this.idVertice2 = idVertice2;
this.idVertice3 = idVertice3;
}
public int getIdVertice1() {
return idVertice1;
}
public int getIdVertice2() {
return idVertice2;
}
public int getIdVertice3() {
return idVertice3;
}
}
I have too an Vertices class :
public class Vertices {
private double x, y, z;
public Vertices(double line, double height, double column) {
x = column;
y = height;
z = line;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
And her is my Mesh class :
public class Mesh {
private TreeMap<Double, TreeMap<Double, Vertices>> setOfVertices;
private LinkedList<Face> setOfFaces;
public Mesh() {
setOfFaces = new LinkedList();
setOfVertices = new TreeMap<Double, TreeMap<Double, Vertices>>();
}
public TreeMap getSetOfVertices() {
return setOfVertices;
}
public LinkedList<Face> getSetOfFaces() {
return setOfFaces;
}
How convert this Mesh object to a JavaFX mesh object into my viewer 3D (my viewer 3D is a subScene) :
Is this possible?
This is my sticking point: I need to create an array of Chair objects with default woodType. I am able to declare the array itself, but obviously all the values are null. When I try to instantiate each Chair object in the array, I get errors. I'm not sure what I am doing wrong when trying to instantiate, please help.
public class PAssign3 {
public static void main(String[] args) {
TableSet set1 = new TableSet();
TableSet set2 = new TableSet(5, 7, 4);
// Chair chr1 = new Chair();//this works properly, setting wood as Oak
// Chair chr2 = new Chair("Pine");//works
}
}
class TableSet {
Table table = new Table();
private int numOfChairs = 2;
//creates an array that can hold "numOfChairs" references to same num of
//chair objects; does not instantiate chair objects!!!
Chair[] chairArr = new Chair[numOfChairs];
//instantiate each chair object for length of array
//this loop does not work; Error: illegal start of type
for (int i = 0; i < numOfChairs.length; i++) {
chairArr[i] = new Chair();
}
public TableSet() {
}
public TableSet(double width, double length, int numOfChairs) {
table = new Table(width, length);
this.numOfChairs = numOfChairs;
chairArr = new Chair[numOfChairs];
//this loop also does not work; Error: int cannot be dereferenced
for (int i = 0; i < numOfChairs.length; i++) {
chairArr[i] = new Chair();
}
}
public void setNumOfChairs(int numOfChairs) {
this.numOfChairs = numOfChairs;
}
public int getNumOfChairs() {
return numOfChairs;
}
public String getChairWoodType() {
return chairArr[0].getWoodType();
}
}
class Table {
private double width = 6;
private double length = 4;
public Table() {
}
public Table(double width, double length) {
this.width = width;
this.length = length;
}
public void setWidth(double width) {
this.width = (width < 0) ? 0 : width;
}
public void setLength(double length) {
this.length = (length < 0) ? 0 : width;
}
public double getWidth() {
return width;
}
public double getLength() {
return length;
}
}
class Chair {
private String woodType = "Oak";
public Chair() {
}
public Chair(String woodType) {
this.woodType = woodType;
}
public void setWoodType(String woodType) {
this.woodType = woodType;
}
public String getWoodType() {
return woodType;
}
}
int is a simple type in Java and does not have any methods or fields. Just omit this .length and the error is gone. It seems to me it already stores the actual number of chairs (2).
im working on some old tasks given by my Programming course.
Im supposed to create a Rectangle class with some basic functions and a lot of Comparable and Comparator classes.
This is what I've got already:
public class Rectangle {
private int length, width;
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
public int length() {
return length;
}
public int width() {
return width;
}
public int area() {
return length + width;
}
public int perimeter() {
return 2 * length + 2 * width;
}
}
public class RectangleComparable extends Rectangle implements Comparable<RectangleComparable> {
public RectangleComparable(int length, int width) {
super(length, width);
}
#Override
public int compareTo(RectangleComparable r) {
if (this.area() < o.area()) return -1;
else if (this.area > o.area()) return 1;
else return 0;
}
}
public class RectangleComparatorArea implements Comparable<RectangleComparable> {
private int area;
#Override
public int compareTo(RectangleComparable r) {
if (area < r.area()) return -1;
else if (area > r.area()) return 1;
else return 0;
}
}
public class RectangleComparatorPerimeter implements Comparable<RectangleComparable> {
private int perimeter;
#Override
public int compareTo(RectangleComparable r) {
if (perimeter < r.perimeter()) return -1;
else if (perimeter > r.perimeter()) return 1;
else return 0;
}
}
public class ComparableComparator<T> implements java.util.Comparator<T> {
public int compare(T o1, T o2) {
return 0;
}
}
What are methods like .compareTo, .equals etc. called? Like, when I got some
obj1.compareTo(obj2);
how can I refer obj1, when I implement my own compareTo method?
Also the ComparableComparator.compare method should work for Rectangles and also other Generic types but I got no idea how to start this.
this . That's the reference to the object whose compareTo get called.
See how jdk's implementation works: java.util.Comparators$NaturalOrderComparator (this is implementation detail and is hidden, tough)
There is an example of "Implementing an Interface" in Java tutorial. I have repeated this example but it doesn't work. NetBeans shows the mistake on te left of RectanglePlus class declaration. And mistake is:
rectangleplus.RectanglePlus is not abstract and does not override
abstract method isLargerThan(rectangleplus.Relatable) in
rectangleplus.Relatable
I did the same as written in tutorial. Why it shows the mistake? Here is my implementation of the project.
The name of the project is RectanglePlus.
The name of the package is rectangleplus.
1st file in the project is Interface Relatable:
package rectangleplus;
public interface Relatable {
int isLarger(Relatable other);
}
2nd file in the project is Main Class RectanglePlus with helper class Point:
package rectangleplus;
public class RectanglePlus implements Relatable {
public int width = 0;
public int height = 0;
public Point origin;
// four constructors
public RectanglePlus() {
origin = new Point(0, 0);
}
public RectanglePlus(Point p) {
origin = p;
}
public RectanglePlus(int w, int h) {
origin = new Point(0, 0);
width = w;
height = h;
}
public RectanglePlus(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 the rectangle
public int getArea() {
return width * height;
}
// a method required to implement
// the Relatable interface
public int isLargerThan(Relatable other) {
RectanglePlus otherRect
= (RectanglePlus)other;
if (this.getArea() < otherRect.getArea())
return -1;
else if (this.getArea() > otherRect.getArea())
return 1;
else
return 0;
}
public static void main(String[] args) {
// TODO code application logic here
}
}
class Point {
int top;
int left;
int x;
int y;
public Point(int t, int l) {
top = t;
left = l;
}
}
Why there is nothing said about abstraction in the tutorial example? Should the tutorial example work without mitakes?
Thank you.
In the interface, you declare the method isLarger but in the class you declare isLargerThan Change one to the other name and it will go fine.
You're not correctly implementing the isLarger() method in the Relatable interface. Rename the isLargerThan(Relatable other) method so it looks like this:
#Override
int isLarger(Relatable other) {
}
It's a good idea to use the #Override annotation, it allows you to catch errors like the one in the question.