Java polymorpshim: Array of Object - java

I am working on a class assignment that got me stuck for weeks now. I am wondering if someone can illustrate how I can find the solution.
I am working with three classes: Triangle, Circle and Rectangle.
An a abstract class: Shape
The constructor for each object:
Triangle: public Triangle(Position pos, String fillColor, String lineColor,
double side)
Circle: public Circle(Position pos, String fillColor, String lineColor,
double radius)
Rectangle: public Rectangle(Position pos, String fillColor, String lineColor,
double width, double height)
The assignment is asking to:
Add a new class called ShapeArray. This class will also have a main method, and It will also prompt the user for a given arraySize(between 2 and 20),then it will create an array that will contain objects of typeTriangle,Circle, and Rectangle. The size of the array is arraySize.
2..Populate this array randomly with objects that can be of any of the following types: Triangle, Circle, and Rectangle. The state of each object(e.g.,position, or radius, etc.) is also randomly assigned.
I cannot seem to figure out how I can create and fill an array of different object types(rectangle, circle and triangle) using a loop and at the same generating random value for each the object parameters at the constructor level.
I create some of the parameters but could not create random string parameter for linecolor and fillColor parameters.
This is what I came up with:
package homework.session10;
import java.util.*;
public class ShapeArray {
public static void main(String[] args){
System.out.println("Please pick a number between 2-20 for the array size");
Scanner keyboard = new Scanner (System.in);
int arraySize = keyboard.nextInt();
ShapeArray [] shapeArray = new ShapeArray[arraySize];
Random rnd = new Random();
int posX = rnd.nextInt(11);
int posY = rnd.nextInt(11);
double recWidth = rnd.nextDouble(11.0);
double recHeight = rnd.nextDouble(11.0);
double cirRadius = rnd.nextDouble(11.0);
double triSide = rnd.nextDouble(11.0);
String fillColorObject = keyboard.next();
String lineColorObject = keyboard.next();
Position position1 = new Position(posX,posY);
//ShapeArray
}
public void sortShape(Figure[] array1){
Arrays.sort(array1);
}
public void findShape(Figure[] array2 ){
}
}

you can do something like this. Create a parent class in which the Rectangle, Circle, and Triangle will have common ancestor class:
public abstract class Shape {
protected Position pos;
protected String fillColor;
protected String lineColor;
protected double side;
public Shape(Position pos, String fillColor, String lineColor, double side)
{
this.pos = pos;
this.fillColor = fillColor;
this.lineColor = lineColor;
this.side = side;
}}
then for each shape, create a separate class each, inheriting from abstract Shape class
class Rectangle extends Shape {
public Rectangle(Position pos, String fillColor, String lineColor, double side){
super(pos,fillColor,lineColor,side);
}}
(do the rest for other shapes, Circle and Triangle)
class ShapeArray{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
int arraySize = keyboard.nextInt();
Shape[] shapes = new Shape[arraySize];
//your code to set random values
for(int i = 0; i< arraySize;i++){
shapes[i] = new Rectangle(new Position(1,2),"red","blue",2.0);
}
}}
the part shapes[i] = new Rectangle(new Position(1,2),"red","blue",2.0);
is what the "polymorphism" refers to. As long as the class inherits from a parent class (Shape), that subclass (Rectangle) is allowed to be assigned to its parent class.

Related

About having various constructors and parameters for java

I have written the instructions below and till now, I've came up with having two parameters and letting the method to assign the value and retrieving it. However, one of the instruction I had to follow was to include one constructor with no parameters, so I'm wondering what statement should I make inside the constructor without any parameters. It would be wonderful if anyone gives be a instruction. This is the code I've came up so far.
public class Rectangle {
//first constructor no parameters
//public<class name> (<parameters>)<statements>}
//two parameters one for length, one for width
//member variables store the length and the width
//member methods assign and retrieve the length and width
//returning the area and perimeter
static int recPerimeter(int l, int w) {
return 2*(l+w);
}
static int recArea(int l, int w) {
return l*w;
}
public static void main(String[] args) {
int p = recPerimeter(5, 3);
System.out.println("Perimeter of the rectangle : " + p);
int a = recArea(5,3);
System.out.println("Area of the rectangle : " + a);
}
}
First off, I would take some time to read the java tutorials. At least the "Covering the Basics"
There is a ton wrong with your example. You should store the the attributes of a rectangle - width and length as data members of the class which will get initialized with values through the constructors. If a default constructor is called with no values, then set the attributes to whatever you want. I set them to zero in the example.
Also, you need to normally create an instance of your class and then access it. Big red flag if you are having to prepend "static" to everything.
public class Rectangle {
private int recLength;
private int recWidth;
public Rectangle() {
recLength = 0;
recWidth = 0;
}
public Rectangle( int l, int w ) {
recLength = l;
recWidth = w;
}
public int calcPerimeter() {
return 2*(recLength+recWidth);
}
public int calcArea() {
return recLength*recWidth;
}
public static void main (String [] args) {
Rectangle rec = new Rectangle(5,3);
System.out.println("Perimeter = "+ rec.calcPerimeter());
System.out.println("area = " + rec.calcArea());
}
}

Having trouble with passing user defined parameters inside my object

I'm being asked to pass more arguments to match my constructor but I have no idea what to pass into them.
I have multiple instance variables but only a few of them will be defined by the user (the vertices) and the others are going to defined with their respective methods. If I take everything except my vertices outside of my constructor to solve the error I am left with my final output being left as 0 for most of my reports.
Is my constructor the problem or the parameters in my object at fault?
import java.lang.Math;
public class Triangle {
//instance variables
private double VertAx, VertAy, VertBx, VertBy, VertCx, VertCy;
private double lengthAB, lengthBC, lengthCA;
private double Perimeter, Area;
private double H = Perimeter/2;
//Triangle Constructor
public Triangle(double userVertAx, double userVertAy, double userVertBx, double userVertBy, double userVertCx, double userVertCy, double userlengthAB, double userlengthBC, double userlengthCA, double userPerimeter, double userArea, double userH) {
userVertAx = this.VertAx;
userVertAy = this.VertAy;
userVertBx = this.VertBx;
userVertBy = this.VertBy;
userVertCx = this.VertCx;
userVertCy = this.VertCy;
userlengthAB = this.lengthAB;
userlengthBC = this.lengthBC;
userlengthCA = this.lengthCA;
userPerimeter = this.Perimeter;
userArea = this.Area;
userH = this.H;
}
public double lengthAB(double userVertAx, double userVertAy, double userVertBx, double userVertBy) {
return lengthAB = Math.sqrt( (Math.pow((userVertBx - userVertAx), 2)) + (Math.pow((userVertBy - userVertAy), 2)));
}
public double lengthBC(double userVertBx, double userVertBy, double userVertCx, double userVertCy) {
return lengthBC = Math.sqrt( (Math.pow((userVertCx - userVertBx), 2)) + (Math.pow((userVertCy - userVertBy), 2)));
}
public double lengthCA(double userVertCx, double userVertCy, double userVertAx, double userVertAy) {
return lengthCA = Math.sqrt( (Math.pow((userVertAx - userVertCx), 2)) + (Math.pow((userVertAy - userVertCy), 2)));
}
public void setPerimeter(double userlengthAB, double userlengthBC, double userlengthCA) {
Perimeter = userlengthAB + userlengthBC + userlengthCA;
}
public double getPerimeter() {
return Perimeter;
}
public void setArea(double userlengthAB, double userlengthBC, double userlengthCA, double userH) {
Area = Math.sqrt(userH*(userH-userlengthAB)*(userH-userlengthBC)*(userH-userlengthCA));
}
public double getArea() {
double Area = getArea();
return Area;
}
public String toString() {
return String.format("Vertices: A(%f, %f) B(%f, %f) C(%f, %f)\nSide Lengths: AB=%f BC=%f CA=%f\nPerimeter: %f\nArea: %f", VertAx, VertAy, VertBx, VertBy, VertCx, VertCy, lengthAB, lengthBC, lengthCA, Perimeter, Area);
}
}
public class TriangleTest {
public static void main(String[] args) {
#SuppressWarnings("resource")
Scanner Vertices = new Scanner(System.in);
System.out.println("Welcome to the Triangle Test enter each coordinate of your three vertices SEPERATELY");
System.out.println("Enter Vertex A X");
Double VAX = Vertices.nextDouble();
System.out.println("Enter Vertex A Y");
Double VAY = Vertices.nextDouble();
System.out.println("Enter Vertex B X");
Double VBX = Vertices.nextDouble();
System.out.println("Enter Vertex B Y");
Double VBY = Vertices.nextDouble();
System.out.println("Enter Vertex C X");
Double VCX = Vertices.nextDouble();
System.out.println("Enter Vertex C Y");
Double VCY = Vertices.nextDouble();
//ERROR
Triangle UserTriangle = new Triangle(VAX, VAY, VBX, VBY, VCX, VCY);
//ERROR ^
UserTriangle.lengthAB(VAX, VAY, VBX, VBY);
UserTriangle.lengthBC(VBX, VBY, VCX, VCY);
UserTriangle.lengthCA(VCX, VCY, VAX, VAY);
UserTriangle.getPerimeter();
UserTriangle.getArea();
System.out.println(UserTriangle.toString());
}
}
I am expecting some way to pass the right parameters into my UserTriangle but I am confused as to how. Thank you for any help anyone can provide. My understanding with classes and objects were good with implementing user input but this one seems so tricky to me considering some of the variables are defined in methods and some are defined by the user.
Constructor called with a mismatched number of arguments
You defined your constructor as accepting 12 arguments, but then you called it with only 6 arguments. This is the error you're referring to. To solve this you have 3 options
Provide all the 12 arguments the constructor needs
Define your constructor as receiving 6 arguments
Refactor (see below for instructions), which is the way to go in my opinion
Reverse the initialization statements in your constructor
To initialize your attributes write this.VertAx = userVertAx instead of userVertAx = this.VertAx; (reverse the statement basically)
This goes for all the other attributes too (userlengthAB, userPerimeter, etc...)
Note
It's better to use the Java naming conventions so you can make the difference say between attributes and classes. Attributes and variables should start with a lowercase and classes with an uppercase.
Edit: Refactoring suggestion
An even better writing is to use less arguments in your constructor. Having too many arguments is considered a code smell and will make your code less readable/maintainable, etc...
To handle that you can encapsulate some concepts in classes. For example you can have
public class Vertex {
private double x;
private double y;
public Vertex(double x, double y) {
this.x = x;
this.y = y;
}
public class TriangleVertices {
private vertexA;
private vertexB;
private vertexC;
public TriangleVertices (Vertex a, Vertex b, Vertex c) {
vertexA = a;
vertexB = b;
vertexC = c;
}
}
public class Triangle {
private TriangleVertices vertices;
// other attributes
// You have now 5 arguments less!
public Triangle(TriangleVertices vertices, // other attributes) {
this.vertices = vertices;
// Initialize other attributes
}
}
Here "this" is a keyword which points the constructor to the variables that are declared in the created class (in your case created class is Triangle). we use "this" keyword when
the variable of parameter has the same name as the variable declared in the created class.For example;
class A {
int NewVar;
A (double NewVar){
this.NewVar = NewVar; //here this.NewVar is pointing to NewVar of type int
}
}
Change this in your code. This might solve your problem.
public Triangle(double userVertAx, double userVertAy, double userVertBx, double userVertBy, double userVertCx, double userVertCy, double userlengthAB, double userlengthBC, double userlengthCA, double userPerimeter, double userArea, double userH) {
this.VertAx = userVertAx;
this.VertAy = userVertAy;
this.VertBx = userVertBx;
this.VertBy = userVertBy;
this.VertCx = userVertCx;
this.VertCy = userVertCy;
this.lengthAB = userlengthAB;
this.lengthBC = userlengthBC;
this.lengthCA = userlengthCA ;
this.Perimeter = userPerimeter ;
this.Area = userArea;
this.H = userH ;
}

My Java object array only returns the last element

I'm trying to complete a Java lab exercise that asks to display the volumes of objects (cylinders) in an array. Whenever I try to print the array the output always seems to be the last object in the array, when I'd like them all to print.
Here is my code for Cylinder.java:
public class Cylinder implements Comparable<Cylinder> {
public static double radius;
public static double height;
public static String name;
public Cylinder(double radius, double height, String name){
this.radius = radius;
this.height = height;
this.name = name;
}
#Override
public int compareTo(Cylinder obj) {
Cylinder other = obj;
int result;
if (this.volume() > other.volume()){
result = -1;
}
else if (this.volume() < other.volume()){
result = 1;
}
else {
result = 0;
}
return result;
}
public double volume(){
double volume = Math.pow(radius, 2.0)*Math.PI*height;
return volume;
}
public double surface(){
double surface = (4.0*Math.PI)*Math.pow(radius, 2.0);
return surface;
}
public String toString(){
return "Name: " + name + ", radius: " + radius + ", height: " + height ;
}
}
TestSolids.java, to print the array:
import java.util.Arrays;
public class TestSolids {
public static void testCylinderSort(Cylinder[] cylinders){
for(int i = 0; i < cylinders.length; i++){
double volume = cylinders[i].volume();
System.out.println("Volume of Cylinder " + (i+1) + " " + volume);
}
}
public static void main(String[] args){
final Cylinder[] CYLINDERS = { new Cylinder(10, 5, "one"), new Cylinder(5, 10, "two"), new Cylinder(7, 7, "three") };
System.out.println(Arrays.toString(CYLINDERS));
testCylinderSort(CYLINDERS);
}
}
My output:
[Name: three, radius: 7.0, height: 7.0, Name: three, radius: 7.0, height: 7.0, Name: three, radius: 7.0, height: 7.0]
Volume of Cylinder 1 1077.566280181299
Volume of Cylinder 2 1077.566280181299
Volume of Cylinder 3 1077.566280181299
The output shows that I can print the different indexes of the array, but for some reason, they all reference the last element of the array and I can't figure out why that is. If anyone could tell me what's going on here and how I can print all the array objects I'd be very thankful.
You variables declared are static.
You need to remove the static as the static belongs to the class and not a member variable. Thus every time you initialize the values or radius for the cylinders, the following cylinder values always will overwrite the values of the previous ones.
use :
public double radius;
public double height;
public String name;
PS:
You might want to make them private and make public getter and setter methods for better code
Your instance variables are declared as static.
public static double radius;
public static double height;
public static String name;
Static variables are global for that class.
So when you create three objects
final Cylinder[] CYLINDERS = { new Cylinder(10, 5, "one"), new Cylinder(5, 10, "two"), new Cylinder(7, 7, "three") };
The radius, height and name variables are continuously replaced. Consequently the values of these variables are the last values set.
new Cylinder(7, 7, "three")
This is what you perceive as the "iteration always returning the last element".
What's actually happening is that different objects are returned, but they have the same set of variables (at class level), instead of having a set of variables per object (at object level).
The fix is to remove the static keyword.

In Java, can 1 instance variable have 2 potential values?

I have been instructed to "Write a class, Triangle, with one instance variable that takes two string values, (filled or not filled)".
I'm new to Java, and still haven't come across a situation where you could have two potential values for one instance variable.
How would I do this?
main method was given:
public static void main(String[] args)
{
TwoDPolygon polygons[] = new TwoDPolygon[3];
polygons[0] = new Triangle("filled", 8.5, 12.0);
polygons[1] = new Triangle("not Filled", 6.5, 7.5);
polygons[2] = new Triangle(7.0);
for (int i=0; i<polygons.length; i++)
{
System.out.println("Object is " + polygons[i].getName());
System.out.println("Triangle " + polygons[i].getStatus());
System.out.println("Area is " + polygons[i].area());
}
}
Ok I have redesigned the code based on your updated question.
First of all, you need an abstract class called TwoDPolygon. This class is an abstract representation of all your polygons. It contains the constructors and the methods you need.
abstract class TwoDPolygon {
protected String filled;
protected double x;
protected double y;
protected TwoDPolygon(String filled, double x, double y){
this.filled=filled;
this.x=x;
this.y=y;
}
protected TwoDPolygon(double x, double y){
this.x=x;
this.y=y;
}
protected TwoDPolygon(double y){
this.y=y;
}
abstract String getName();
abstract String getStatus();
abstract Double area();
}
Then the next step is to create the Triangle class. You will have to extend the abstract TwoDPolygon. This is the code:
public class Triangle extends TwoDPolygon {
//the first constructor
public Triangle(String filled, double x, double y) {
super(filled, x, y);
}
//the second one
public Triangle(double x, double y){
super(x,y);
}
//the third one
public Triangle(double y){
super(y);
}
public String getName() {
return "Triangle";
}
public String getStatus() {
return filled;
}
public Double area() {
//Insert code here which calculates the area
return 0.0;
}
}
This is all. Every time when you instantiate a Triangle polygon it will chose the right constructor based on the parameters you supply. Now when you run your main you will have the following output:
Object is Triangle
Triangle filled
Area is 0.0
Object is Triangle
Triangle not Filled
Area is 0.0
Object is Triangle
Triangle null
Area is 0.0
Note: The area's code is not done. You will have to do that but I guess that shouldn't be a problem.
Also I have created three constructors as you said, but I don't know the parameters of the third one. I just guessed that it has only the x and y value.
I hope this is what you're looking for!! It shouldn't be that hard to adapt to your specific requirements, as I think it looks almost done.
It is most likely meant that it takes one argument with 2 valid values:
class Triangle {
Triangle(String val) {
if (!"filled".equals(val) || !"not filled".equals(val))
throw ...;
}
}
or enum type
public enum Type {
FILLED,
NOT)FILLED
}
I think what is meant is you have one boolean instance variable named isFilled.
then you could have something like this:
boolean isFilled;
public triangle(String filled, int x, int y) {
if (filled == "filled") {
isFilled = true;
} else if (filled == "notFilled") {
isFilled = false;
} else {
//handle exception or whatever
}
}
That way you can have one instance variable but still use a string in the constructor. I don't think this is a very practical thing to do but if that is what your assignment said then that is a good way to do it. I hope I helped!

Cannonball flight coordinate tracking

So I just started working with ArrayList and awt.Point. What I am trying to accomplish here is to output an array of X and Y coordinates as the cannonball is in flight. However, when I run the program, I get a bunch of Point[x=0,y=0] within the array.
I think part of the problem maybe in return Point. I return a Point in bowling.Move() and bowling.getLocation(). It is possible that one is overriding the other? I feel that I am close to my result, but at a lose on how to get there.
import java.awt.Point;
import java.util.ArrayList;
import java.util.Scanner;
public class Cannonball {
public static void main(String[] args) {
//Part 1: Open Scanner
Scanner keyboard = new Scanner(System.in);
//Part 2: Create a new cannonball
Ball bowling = new Ball(0);
//Part 3: Ask user for initial angle and starting velocity
System.out.println("Alright, give us the angle at which to fire: ");
bowling.setAngle(keyboard.nextDouble());
System.out.println("And what is the initial velocity: ");
bowling.setVel(keyboard.nextDouble());
//Part 4: Return the points of the cannonball's flight
for(int i=0; i<bowling.shoot.size(); i++) System.out.println(bowling.shoot);
//Part x: Close input
keyboard.close();
}
}
class Ball{
private double xPos, yPos, deltaSec;
private double alpha, v;
private double yVel, xVel;
private static final double gravity = -9.81;
public Ball(double xPos){
this.xPos=xPos;
yPos=0;
}
public Point move(double deltaSec){
xPos += xVel*deltaSec;
yPos += yPos*deltaSec;
return new Point();
}
public void yVel(){
yVel=v*Math.sin(alpha)*(deltaSec*gravity);
}
public void xVel(){
xVel=v*Math.cos(alpha);
}
public Point getLocation(double xPos, double yPos){
return new Point();
}
public void setAngle(double aplha){
this.alpha=alpha;
}
public void setVel(double v){
this.v=v;
}
public ArrayList<Point> shoot = new ArrayList<Point>();
{
while(deltaSec<60){
move(deltaSec);
shoot.add(getLocation(xPos, yPos));
deltaSec++;
}
}
}
If you want to return a Point that represents the x and y coordinates at that timestamp, you should pass them to the point. Point() will create a point with coordinates 0/0.
You should either call the Point(x,y) constructor (which uses integer screen coordinates - intended to represent pixels!) or use Point2D resp. Point2D.Double( x, y).
Update:
Here's an example implementation of what I guess is intended for getLocation:
//Point version
public Point getLocation(double xPos, double yPos){
return new Point((int)xPos, (int)yPos); //Point only takes int coordinates
}
//Point2D version
public Point2D getLocation(double xPos, double yPos){
return new Point2D.Double( xPos, yPos );
}
move should do the same, if it needs to return a point at all.
UPDATE 2:
You seem to have added this code:
public ArrayList<Point> shoot = new ArrayList<Point>();
//I add this line to highlight the difference: the line above is no method signature
{
while(deltaSec<60){
move(deltaSec);
shoot.add(getLocation(xPos, yPos));
deltaSec++;
}
}
Note that this is not a method but an initializer block which will run at object creation time. I guess this it not intended.

Categories