I have the following class to write:
Write a class named XYRectangle_LastName, where LastName is replaced with your last name.. The XYRectangle_LastName class should have the following fields:
An XYPoint named TopLeft. This stores the location of the topleft corner of a Rectangle.
A double named Length. This stores the length of the rectangle.
A double named Width. This stores the width of the rectangle.
The XYRectangle class should have the following methods:
A no-argument constructor that randomly determines the top left corner of the rectangle. The values for x and y should be between -10 and 10. Also, it chooses a random width and length for the rectangle with values between 5 and 10.
A 3 argument constructor that takes an XYPoint for the top left corner, a length, and a width.
A get method for length, width, topLeft, topRight, bottomLeft, and bottomRight
A set method for length, width, and topLeft
A boolean method named isInside that takes an XYPoint and determines if it is inside this rectangle.
A method named reflectX that returns a rectangle that has been reflected over the x-axis.
A method named reflectY that returns a rectangle that has been reflected over the y-axis.
This is the code I have so far:
public class XYRectangle {
private XYPoint topLeft;
private double length;
private double width;
public XYRectangle() {
Random rnd = new Random();
int x = (rnd.nextInt(21) - 10);
int y = (rnd.nextInt(21) -10);
XYPoint topLeft = new XYPoint(x, y);
int width = (rnd.nextInt(5) + 5);
int height = (rnd.nextInt(5) + 5);
}
public XYRectangle(XYPoint topLeft, double length, double width) {
this.topLeft = topLeft;
this.length = length;
this.width = width;
}
public double getLength() { return this.length; }
public void setLength(double length) { this.length = length; }
public double getWidth() { return this.width; }
public void setWidth(double width) { this.width = width; }
public XYPoint getTopLeft() { return this.topLeft; }
public void setTopLeft(XYPoint topLeft) { this.topLeft = topLeft; }
I'm having trouble with the topRight, bottomLeft, and bottomRight get methods and the reflect methods. I'm not even sure if the code I've written so far is write. Could anyone help and tell me how to proceed and if I've been doing something wrong?
You don't have the information about topRight, bottomLeft, and bottomRight, but having the topLeft corner and the width, length, it totally defines the other points:
topRight = new XYPoint(topLeft.getX() + length, topLeft.getY());
bottomRight = new XYPoint(topLeft.getX() + length, topLeft.getY() + width);
bottomLeft = new XYPoint(topLeft.getX(), topLeft.getY() + width);
You can decide to store this information when you construct your object or to calculate it each time the get method is called.
About the empty constructor, you are calling it "corner" when it should be called:
public XYRectangle(){
//code here
}
Usually when we override constructors we call the base constructor like this:
public XYRectangle(){
Random rnd = new Random();
int x = (rnd.nextInt(21) - 10);
int y = (rnd.nextInt(21) -10);
XYPoint topLeft = new XYPoint(x, y);
int width = (rnd.nextInt(5) + 5);
int height = (rnd.nextInt(5) + 5);
this(topLeft, width, height)
}
I hope you can figure out the reflection methods yourself. ;)
Related
I am using a tutorial to understand how sprites work using a draw() method as well as using a gameloop. I adjusted the code as far as I understand it for my own project.
The question I have is how can I access a different row of my sprite sheet besides the second row. My sprite sheet has 9 columns and 20 rows.
public class Sprite implements Drawable {
private static final int BMP_COLUMNS = 9;
private static final int BMP_ROWS = 20;
private int x = 0;
private int y = 0;
private int xSpeed = 5;
private Bitmap bmp;
float fracsect = 30;
private GameContent gameContent;
private int currentFrame = 0;
private int width;
private int height;
public Sprite(GameContent gameContent, Bitmap bmp) {
this.gameContent = gameContent;
this.bmp = bmp;
this.width = bmp.getWidth() / BMP_COLUMNS;
this.height = bmp.getHeight() / BMP_ROWS;
}
#Override
public void update(float fracsec) {
if (x > gameContent.getGameWidth() - width - xSpeed) {
xSpeed = -5;
}
if (x + xSpeed < 0) {
xSpeed = 5;
}
x = x + xSpeed;
currentFrame = ++currentFrame % BMP_COLUMNS;
}
#Override
public void draw(Canvas canvas) {
update(fracsect);
int srcX = currentFrame * width;
int srcY = 1*height - 41;
Rect src = new Rect(srcX +20 , srcY,srcX + width,srcY + height-38); // Generates
Rect dst = new Rect(x,y,x+width -30, y+height-30); // Scales
canvas.drawBitmap(bmp, src, dst, null);
}
}
How do I gain access to the second row and how can I change it for instance to the third or 4th row ?
What I understand so far is that using a sprite as an object as a bitmap instead via imageview the code implementation of the code works differently. Is there any advice on how to access a different row for the sprite sheet ? I used the android documentation as well as the tutorial to understand this process as far as I can.
Here is the tutorial also:
http://www.edu4java.com/en/androidgame/androidgame4.html
From what I can see you are iterating through the first row, drawing each sprite in turn, perhaps to create an animation.
If you want to draw the animation for the second row, you can simply add 'height' to 'srcY'. It looks like you had this idea but you substracted instead of adding. Remember that Y-coordinates on computers go from top to bottom (i.e. (0,0) is top-left, (0, 10) is 10 pixels lower).
Likewise for the third and fourth rows, just add 'height' to 'srcY' again.
I am trying to understand the Rectangle Class and MyCircle class, specifically the containsPoint methods in each one.
Here is the code:
public class MyRectangle extends GridItem {
private int height;
private int width;
public MyRectangle(int xValue, int yValue, int w, int h) {
x = xValue;
y = yValue;
width = w;
height = h;
}
public double getArea() {
return height * width;
}
public boolean containsPoint(int xValue, int yValue)
{
return xValue >= x &&
xValue <= x + width &&
yValue >= y &&
yValue <= y + height;
}
}
The confusion I'm having is, what does the containsPoint method mean?
How was this current code set up in this particular way, since isn't that supposed to return a boolean and not data types of the int?
Same dilemma for the MyCircle class.
public class MyCircle extends GridItem {
private int radius;
public MyCircle(int xValue, int yValue, int r)
{
x = xValue;
y = yValue;
radius = r;
}
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}
public boolean containsPoint(int xValue, int yValue) {
double dx = x - xValue;
double dy = y - yValue;
double distance = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
return distance <= radius;
}
}
What exactly are they meaning by the containsPoint method?
How do you interpret this?
Been stumped for days and this is part of a bigger assignment, but cannot comprehend the containsPoint method so it's affect the development of mySquare class.....
So far I've got this..
public class MySquare extends GridItem
{
private int side;
public MySquare(int xValue, int yValue, int s)
{
x = xValue;
y = yValue;
side = s;
}
#Override
public double getArea()
{
return side*side;
}
#Override
public boolean containsPoint(int xValue, int yValue)
{
return x && y;
}
}
How does one apply the containsPoint method in the Square class?
Thanks!
what does the containsPoint method mean?
The method just checks if the given point (the given x,y coordinates i.e. xValue, yValue) is within the current Square or Rectangle.
How was this current code set up in this particular way, since isn't that supposed to return a boolean and not data types of the int?
The method arguments are int because they indicate the x and y coordinates for the given point.
Been stumped for days and this is part of a bigger assignment, but cannot comprehend the containsPoint method so it's affect the development of mySquare class.....
Your sub-classes such as the Sqaure class is supposed to have a set of attributes such as x, y, width, height which indicates the position and size of the square. Based on this set of attributes, check if any given point (xValue, yValue) is within your current square. The same applies for Rectangle class.
The containsPoint is the method to check if a point is inside a specific rectangle / circle / shapes on 2D plane.
Comparing variables with each other will result in an boolean value.
Each comparison in containsPoint() in MyRectangle yields a boolean value which are then connected via and. This means that it will only return true if every single comparison yields true.
You would need to apply the same principle to MySquare.
Think about how the coordinates of the square compare to the coordinates of a point if the point is inside the square.
Let's consider the containsPoint of Rectangle.
Let's assume you have a rectangle of height 2 and width 3 starting at co-ordinate (1,1). So your rectangle would look like this
(1,3) (4,3)
------------
| |
| |
------------
(1,1) (4,1)
(In the above example) given two points xValue and yValue, containsPoint returns true if
xValue is between 1 and 4 (inclusive) and
yValue is between 1 and 3 (inclusive)
and false otherwise
Thus, containsPoint tells whether a given point lies on/within a shape.
The containsPoint method of a circle also does the same thing (whether a point lies within/on the circle), however the formula is a bit more involved. You can refer to the Euclidean distance for two dimensions to understand it better.
The containsPoint for a Square will be very similar to that of a rectangle except for using width and heigth, you would have only one side.
return xValue >= x &&
xValue <= x + side &&
yValue >= y &&
yValue <= y + side;
I am fairly new to Java and I am learning about Inheritance. I am trying to create a subclass called BetterRectangle under the superclass Rec1.
Rec 1 gets the x and y coordinates (location) and also gets the width and height (size) of the rectangle. BetterRectangle calculates the perimeter and area of the rectangle.
I get errors in the main method. It cannot find any of the symbols (i.e. cannot find rec1.getHeight(20) symbol).
public class Rec1 {
private double x;
private double y;
private double width;
private double height;
public void setLocation(double xCord, double yCord) {
x = xCord;
y = yCord;
}
public void setSize(double h, double w) {
height = h;
width = w;
}
public double getHeight(double h) {
return height;
}
public double getWidth(double w) {
return width;
}
}
public class BetterRectangle extends Rectangle {
public BetterRectangle(int x, int y, int width, int height) {
super(x, y, width, height);
super.setLocation(x, y);
super.setSize(width, height);
}
public double calcPerimeter() {
return super.getHeight() * 2 + super.getWidth() * 2;
}
public double calcArea() {
return super.getHeight() * super.getWidth();
}
}
Look closely:
Rectangle Rec1 = new Rectangle();
Rec1.getHeight(20);
The type of Rec1 is Rectangle. But the Rectangle class doesn't have a getHeight method. Maybe you wanted this:
Rec1 rec1 = new Rectangle();
rec1.getHeight(20);
Notice that I renamed the variable to rec1, and changed its type.
class BetterRectangle is extending Rectangle in your sample code. So here BetterRectangle would be child and Rectangle would be parent. Now BetterRectangle uses super keyword to access getHeight,getWidth etc...functions. since these functions are not present in Rectangle class it is giving compilation Error.
Instead of extending Rectangle class extend Rec1 class in BetterRectangle and then run the main Class Rectangle. This should work.
Look carefully here
Rectangle BetterRectangle = new Rectangle();
Rectangle Rec1 = new Rectangle();
You are declaring variables of type Rectangle instead of your own classes. I can't see your import statements but you are likely creating types of java.awt.rectangle. You're mixing up variable types and variable names. When creating instances of your own classes it should look like this:
Rec1 mySimpleRec = new Rec1();
BetterRectangle myBetterRec = new BetterRectangle()
When declaring things put them in this order "Type variableName = new Type()" or "Class variableName = new Class()".
My assignment was to create a class named MyRectangle to represent rectangles.
The required data fields are width, height, and color. Use double data type for width and height, and a String for color. Then Write a program to test the class MyRectangle. In the client program, create two MyRectangle objects. Assign a width and height to each of the two objects. Assign the first object the color red, and the second, yellow. Display all properties of both objects including their area.
I've written everything out and am getting no errors, but my output stays the same no matter what values I put in for the rectangles.
package MyRectangle;
public class MyRectangle{
private double width = 1.0;
private double height = 1.0;
private static String color = "black";
public MyRectangle(double par, double par1){
width ++;
height ++;
}
//Parameters for width, height, and color //
public MyRectangle(double widthParam, double heightParam, String colorParam){
width = widthParam;
height = heightParam;
color = colorParam;
width ++;
height ++;
}
// Accessor width //
public double getWidth(){
return width;
}
public void setWidth(double widthParam){
width = (widthParam >= 0) ? widthParam: 0;
}
// Accessor height //
public double getHeight(){
return height;
}
public void setHeight(double heightParam){
height = (heightParam >= 0) ? heightParam: 0;
}
// Accessor color //
public static String getColor(){
return color;
}
public static void setColor(String colorParam){
color = colorParam;
}
// Accessor area //
public double findArea(){
return width * height;
}
}
class MyRectangleTest {
#SuppressWarnings("static-access")
public static void main(String args[]) {
// Create triangle and set color value to red //
MyRectangle r1 = new MyRectangle(5.0, 25.0);
r1.setColor("Red");
System.out.println(r1);
System.out.println("The area of rectangle one is: " + r1.findArea());
// Create triangle and set color value to yellow //
MyRectangle r2 = new MyRectangle(3.0, 9.0);
r2.setColor("Yellow");
System.out.println(r2);
System.out.println("The area of rectangle one is: " + r2.findArea());
}
}
The constructor you are using makes no sense.
You ignore the passed rectangle dimensions, so you'll always get a 2 by 2 rectangle:
private double width = 1.0;
private double height = 1.0;
...
public MyRectangle(double par, double par1){
width ++;
height ++;
}
It should be something like :
public MyRectangle(double width, double height){
this.width = width;
this.height = height;
}
In addition, the color member shouldn't be static, unless you want all your rectangles to have the same color.
One last thing - in order for System.out.println(r1); and System.out.println(r2); to Display all properties of both objects, you must override toString():
#Override
public String toString()
{
return "width = " + width + " height = " + height + " color = " + color;
}
There are a couple of things wrong here:
The color member is static, which means in belongs to the class, instead of each instance having its own.
The (double, double) constructor doesn't store the height and the width.
Both constructors increment the height and the width, for no good reason.
Since you don't have a default constructor, the default values for the members are redundant - there's no flow where they won't be overwritten.
To sum it up, your class should be declared more or less like this:
public class MyRectangle {
private double width;
private double height;
private String color;
private static final String DEFAULT_COLOR = "black";
public MyRectangle(double width, double height) {
this (width, height, DEFAULT_COLOR);
}
public MyRectangle(double width, double height, String color) {
this.width = width;
this.height = height;
this.color = color;
}
// Rest of the required methods
}
Can someone please tell me what's wrong with this simple program? I'm getting output as "0".
package myConst;
public class Doconstructor
{
int length,width;
Doconstructor(int x, int y)
{
int area;
area = length * width;
System.out.println("area ="+area);
}
}
class work
{
public static void main(String args[])
{
Doconstructor d1 = new Doconstructor(10, 15);
}
}
Doconstructor d1 = new Doconstructor(10, 15);
// you are assigning values for x and y
But
Doconstructor (int x,int y)
{
int area; // you are never use x and y values for calculation
area = length *width; // so area remain 0 since current length and width is 0
System.out.println("area ="+area);
}
You need to change your code as follows.
Doconstructor (int x,int y)
{
int area;
this.length=x;
this.width=y;
area = length *width;
System.out.println("area ="+area);
}
Edit like this:-
package myConst;
public class Doconstructor
{
int length,width;
Doconstructor(int x, int y)
{
int area;
this.length=x;//Using this for current object
this.width=y;//Using this for current object
area = length * width;
System.out.println("area ="+area);
}
}
class work
{
public static void main(String args[])
{
Doconstructor d1 = new Doconstructor(10, 15);
}
}
Your output will be:
area =150
Must read this :
http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
You are not setting the values of length and width and by default they are both 0. You might have to do this:
Doconstructor(int x, int y){
int area;
area = x * y;
length = x;
width = y;
System.out.println("Area = "+area);
}
You're not using the variable values you pass to the constructor in it but rather the length and width values that have been initialized to 0. You want area = x * y; instead.
The length and width fields are implicitly initialized to 0. Multiply them and you get 0.
I think what you want is
length = y ;
width = x ;
int area = length * width ;
System.out.println("area ="+area);
You have this:
public class Doconstructor {
int length,width;
Doconstructor (int x,int y)
{
int area;
area = length *width;
System.out.println("area ="+area);
}
}
At no point do you set length or width equal to anything. Their initial values are 0 and your program is doing precisely what you told it to do. area = length * width = 0 * 0 = 0.
You also are not doing anything with the x or y that you passed to the constructor, but this probably was not your intention. When writing programs, you basically need to clearly instruct the computer to do what you want to do. It's not going to guess what you want. If you ignore x and y, and don't assign any values to length or width, then that is precisely what will happen and you cannot be surprised when you see the results you see.
you are writing int length,width at class level so length and width are set to 0 as default.
After that in the constructor you are not setting any values to length and width so you are the values for length and width are 0.Hence area is also 0
Please check this link for list of default values
Constructors are used to create objects and to set the attributes. You are not setting the attributes in your constructor. Here is how your constructor should look like.
Doconstructor(int x, int y){
length = x;
width = y;
}
Secondly you are mixing the logic of a constructor and a method. You are doing the calculation of area, which seems to be a perfect fit for another method in your class. so better move that logic in a separate method:
public int calculateArea() {
int area;
area = x * y;
return area;
}
Finally create an object using constructor to set the attributes length and width. And then call calculateArea method to do the business logic of calculating area.
public static void main(String args[]){
Doconstructor d1 = new Doconstructor(10, 15); // create object and set length & width
d1.calculateArea();
}
you are not assigning the value of x and y to the variables width and length. The default value of width and length are (int) 0. Thats why you are getting the output (0*0=0). First assign the values to the variables or use "area=x*y;" .