Cannonball flight coordinate tracking - java

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.

Related

Java polymorpshim: Array of Object

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.

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

Parameters and arguments from one class to another in java

I'm half way through an assignment in java where I have two classes and need to calculate a x- and y-position. I get half of the tests correct but can't seem to get the two last ones correct. Could you perhaps guide my in the right direction?
I have one class calle Point and one PointMain. I get the arguments in PointMain and need to create the right methods in Point. I can't make any changes in the class PointMain as I got that from the assignment.
Class Point:
public class Point {
private int x = 0;
private int y = 0;
public Point() {
}
public Point(int xPoint, int yPoint) {
x = xPoint;
y = yPoint;
}
public String toString() {
return x + "," + y;
}
public double distanceTo(Point p2) {
double avstand = Math.sqrt(((p2.x*1 - p2.x*2) * (p2.x*1 - p2.x*2)) + ((p2.y*1 - p2.y*2) * (p2.y*1 - p2.y*2)));
return avstand;
}
public void move(int iPoint, int jPoint) {
x = x + iPoint; // I have a problem with this that it doesn't add
y = y + jPoint; // the 3,4 that I got from p2 with the 5,-2.
}
public void moveToXY(int xTag, int yTag) {
}
public boolean isEqualTo(Point p2) { //And I'm not really sure on how to
return false; //construct this method either...
}
}
Class PointMain:
public class PointMain {
public static void main(String[] args) {
Point p1 = new Point();
Point p2 = new Point(3,4);
System.out.println(p1.toString()); // ==> (0,0)
System.out.println(p2.toString()); // ==> (3,4)
if (p1.isEqualTo(p2)) // False!
System.out.println("The two points are equal");
double dist = p1.distanceTo(p2);
System.out.println("Point Distance: "+dist);
p2.move(5,-2); // ==> (8,2)
p1.moveToXY(8,2); // ==> (8,2)
System.out.println(p1);
System.out.println(p2);
if (p1.isEqualTo(p2)) // True!
System.out.println("The two points are equal");
}
}
First of all you should add getters
public int getX()
{
return x;
}
public int getY()
{
return y;
}
Than implement isEqual
public boolean isEqualTo(Point p2) {
return x == p2.getX() && y == p2.getY();
}
you can also declare x and y public and then there is no need for getters and code is simpler as you can see in implementation of java.awt.Point.
I don't see problem with "move" function.
And last
public void moveToXY(int xTag, int yTag) {
x = xTag;
y = yTag;
}
For additional info you can lookup how java.awt.Point implemented, and work on your function parameters naming iPoint/jPoint is horrible names
For the equality method, think that in which condition the two points are equal. Consider using if for validating the condition and getters for getting the points x and y.
And I don't see any problem with the move method.
And as #Juniar said in the comments, there's another problem in your distanceTo method. You want to get x and y of p2 but they are private variables so you can't have them by this way. In this case your are having the x and y of the object which the method is called on. So the output won't be desirable. (see getters again)

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!

How to use ArrayList in Java?

I am new trying to learn programing and java, I am currently working in a program but I need help starting it, I would really appreciate it if anyone could help me.
I have to Implement a class IrregularPolygon that contains an array list of Point2D.Double objects.
The Point2D.Double class defines a point specified in double precision representing a location in (x, y) coordinate space. For example, Point2D.Double(2.5, 3.1) constructs and initializes a point at coordinates (2.5, 3.1).
I'm going to be Using this declarations as a starting point for my lab work.
import java.awt.geom.*; // for Point2D.Double
import java.util.ArrayList; // for ArrayList
public class IrregularPolygon {
private ArrayList <Point2D.Double> myPolygon;
// constructors
public IrregularPolygon() { }
// public methods
public void add(Point2D.Double aPoint) { }
public double perimeter() { }
public double area() { }
}
I would like some tips on how to Write methods that compute the perimeter and the area of a polygon. To compute the perimeter, and to compute the distance between adjacent points, and total up the distances. The area of a polygon with corners is the absolute value of:
I suggest Math.hypot(point1.getX() - point2.getX(), point1.getY() - point2.getY()) for the distance of to adjacent points. For the perimeter (just as you stated) sum up all distances (don't forget to add the distance from the last to the first point). For the area see How do I calculate the surface area of a 2d polygon?.
I hope you can find this example helpful:
import java.awt.geom.*;
import java.util.ArrayList;
public class IrregularPolygon {
private ArrayList<Point2D.Double> myPolygon;
// constructors
public IrregularPolygon() {
this.myPolygon = new ArrayList<Point2D.Double>();
}
// public methods
public void add(Point2D.Double aPoint) {
this.myPolygon.add(aPoint);
}
public double perimeter() {
// compute perimeter
return 0;
}
public double area() {
// compute area
return 0;
}
#Override
public String toString() {
String result = "IrregularPolygon [";
for (Point2D.Double point : myPolygon) {
result += "|X: " + point.x + ". Y: " + point.y + "|";
}
result += "]";
return result;
}
public static void main(String[] args) {
IrregularPolygon irregularPolygon = new IrregularPolygon();
Point2D.Double point1 = new Point2D.Double(0, 2);
Point2D.Double point2 = new Point2D.Double(2, 4);
Point2D.Double point3 = new Point2D.Double(3, 5);
irregularPolygon.add(point1);
irregularPolygon.add(point2);
irregularPolygon.add(point3);
System.out.println(irregularPolygon.toString());
}
}
You can find in the toString() method how to use the ArrayList in Java, as well as in the add() method, so the implementation of the methods perimeter() and area() just depends on the specs you have. You just need to extrapolate that methods to complete the class.
Hope it helps.
Clemencio Morales Lucas.
this is what i have so far:
import java.awt.geom.*;
import java.util.ArrayList;
public class IrregularPolygon {
private ArrayList <Point2D.Double> myPolygon;
public IrregularPolygon()
{
myPolygon = new ArrayList < Point2D.Double > ();
}
public void add(Point2D.Double aPoint)
{
myPolygon.add(aPoint);
}
public void print()
{
for (Object element : myPolygon)
{
System.out.println(element);
}
}
public double distance(Point2D pt)
{
double distance = 0;
x.distance(y);
return distance;
}
public double perimeter()
{
double distance = 0;
for(int x = 0; x < aPoint; x++){ // cords given in groups might have to / by 2
get.point();
if(myPolygon[] != null){
get.nextPoint();
}
else{
distance += thePoint.distance(nextPoint)
}
}
return distance;
}
public double area()
{
double area = 0;
return area;
}
}
//that's irregular polygon. this is polygon application
import javax.swing.*;
import java.awt.geom.Point2D;
public class PolygonApplication
{
public static void main() {
String userInput;
int pointNumber = 1;
Point2D.Double nextPoint;
IrregularPolygon myPolygon = new IrregularPolygon();
do {
userInput = JOptionPane.showInputDialog("Enter x coordinate for point " + pointNumber);
if (userInput != null) {
double x = Double.parseDouble(userInput);
userInput = JOptionPane.showInputDialog("Enter y coordinate for point " +
pointNumber);
if (userInput != null) {
double y = Double.parseDouble(userInput);
nextPoint = new Point2D.Double(x,y);
myPolygon.add(nextPoint);
pointNumber += 1;
}
}
}
while (userInput != null);
myPolygon.print();
}
}

Categories