How to fix method error - java

I just want to make sure I'm correct here. I am trying to add methods to
Change height
Change width
Change coordinates
Calculate perimeter
Calculate area
public class MyRectangle {
public int width;
public int height;
public int y;
public int x;
public MyRectangle()
{
width=10;
height=10;
y=10;
x=10;
public int MyRectangle;
public MyRectangle(int width, int height, int y, int x, int MyRectangle) {
this.width = width;
this.height = height;
this.y = y;
this.x = x;
this.MyRectangle = MyRectangle;
}
}
and I'm also getting a illegal start of expression error on my method.

This is your problem, you can't have methods within a method.
But this was due to you not closing your brackets for your methods.
I fixed your code and added the methods you wanted:
public class MyRectangle {
//Best to group your variables up here
public int MyRectangle;
public int width;
public int height;
public int y;
public int x;
public MyRectangle() {
width = 10;
height = 10;
y = 10;
x = 10;
}//Make sure to close this method with the bracket
public MyRectangle(int width, int height, int y, int x, int MyRectangle) {
this.width = width;
this.height = height;
this.y = y;
this.x = x;
this.MyRectangle = MyRectangle;
}
/**
* Changes the current height to the given new height
* #param newHeight
*/
public final void changeHeight(int newHeight) {
height = newHeight;
}
/**
* Changes the current width to the given new width
* #param newWidth
*/
public final void changeWidth (int newWidth) {
width = newWidth;
}
/**
* Calculates the current perimeter based on the width and height
* #return parameter ofd the rectangle
*/
public final int getPerimeter() {
return ((2 * width) + (2 * height));
}
/**
* Calculates the area based on the width and height
* #return area of the rectangle
*/
public final int getArea() {
return (width * height);
}
public final void changesXCoordinate(int newX){
x = newX;
}
public final void changesYCoordinate(int newY){
y = newY;
}
public final void changesCoordinate(int newX, int newY) {
x = newX;
y = newY;
}
}
I will explain more soon, just wanted to post the correct code first :P
As it stands, it's kinda hard to understand what else you are looking for.
If this is what you are looking for, please mark this as the correct answer :D

Related

What is missing from my code in order for the volume to be read?

Everything compiles and works. The program is not reading the Volume value and comes out as 0.0
===========================================================================
Write a superclass encapsulating a rectangle. A rectangle has two attributes representing the width and the height of the rectangle. It has methods returning the perimeter and the area of the rectangle. This class has a subclass, encapsulating a parallelepiped, or box. A parallelepiped has a rectangle as its base, and another attribute, its length; it has two methods that calculate and return its area and volume. You also need to include a client class (with the main method) to test these two classes.
public class Rectangle
{
protected double width;
protected double height;
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
public double getWidth()
{
return width;
}
public void setWidth(double width)
{
this.width = width;
}
public double getHeight()
{
return height;
}
public void setHeight(double height)
{
this.height = height;
}
public double getArea()
{
return width * height;
}
public double getPerimeter()
{
return 2 * (width + height);
}
}
public class Box extends Rectangle
{
protected double length;
public Box(double length)
{
super(length, length);
}
public double getLength()
{
return length;
}
public void setLength(double length)
{
this.length = length;
}
public double getVolume()
{
return width * height * length;
}
}
public class TestRectangle
{
public static void main(String[] args)
{
Rectangle rectangle = new Rectangle(2,4);
Box box = new Box(5);
System.out.println("Rectangle:" );
System.out.println("The area is " + rectangle.getArea());
System.out.println("The perimeter is " +rectangle.getPerimeter());
System.out.println("The volume is " + box.getVolume());
}
}
No errors. Missing value for volume.
Your Box simply needs to set the length field.
public Box(double length)
{
super(length, length);
this.length = length;
}
Although, this assumes your Box is always going to be a cube, and not some arbitrary box shape.
The problem here is you are never assigning any value to the length variable of your box class. The current output of your code in your example would be the following for getVolume(): 5 * 5 * 0 which always returns 0.
Assuming your width, length, height are the same, you still need to assign the length to the variable:
public Box(double length)
{
super(length, length);
this.length = length;
}

Changing height of texture makes texture appear above the ground

I want to change the height of my texture to a random height (with a specified range), and I almost figured out how, but the problem I have is that the texture (tower) is now above the ground. I want to stretch the texture while it remains on the same position, but changes height length.
I have a Tower class and a Scrollable class. In the Tower class I generate a random height in the reset method, but the problem is that I don't know what exactly I have to add or write to put the texture on the correct position (so that it isn't above the ground).
Here is the Tower class:
public class Tower extends Scrollable {
private Random r;
// When Tower's constructor is invoked, invoke the super (Scrollable)
// constructor
public Tower(float x, float y, int width, int height, float scrollSpeed) {
super(x, y, width, height, scrollSpeed);
// Initialize a Random object for Random number generation
r = new Random();
}
#Override
public void reset(float newX) {
// Call the reset method in the superclass (Scrollable)
super.reset(newX); // newX
// Change the height to a random number
Random r = new Random();
int low = 0;
int high = 15;
int result = r.nextInt(high-low) + low;
height = result;
}
}
And here's the Scrollable class:
public class Scrollable {
protected Vector2 position;
protected Vector2 velocity;
protected int width;
protected int height;
protected boolean isScrolledLeft;
public Scrollable(float x, float y, int width, int height, float scrollSpeed) {
position = new Vector2(x, y);
velocity = new Vector2(scrollSpeed, 0);
this.width = width;
this.height = height;
isScrolledLeft = false;
}
public void update(float delta) {
position.add(velocity.cpy().scl(delta));
// If the Scrollable object is no longer visible:
if (position.x + width < 0) {
isScrolledLeft = true;
}
}
// Reset: Should Override in subclass for more specific behavior.
public void reset(float newX) {
position.x = newX;
isScrolledLeft = false;
}
public boolean isScrolledLeft() {
return isScrolledLeft;
}
public float getTailX() {
return position.x + width;
}
public float getX() {
return position.x;
}
public float getY() {
return position.y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
Maybe it's important to know that I have a GameRenderer class which has a drawTowers() method, which is then used in a render() method.
That's my drawTowers() method:
private void drawTowers() {
batcher.draw(AssetLoader.texture1, tower1.getX(), tower1.getY() + tower1.getHeight(),
tower1.getWidth(), midPointY - (tower1.getHeight()));
batcher.draw(AssetLoader.texture2, tower2.getX(), tower2.getY() + tower2.getHeight(),
tower2.getWidth(), midPointY - (tower2.getHeight()));
batcher.draw(AssetLoader.texture3, tower3.getX(), tower3.getY() + tower3.getHeight(),
tower3.getWidth(), midPointY - (tower3.getHeight()));
batcher.draw(AssetLoader.texture4, tower4.getX(), tower4.getY() + tower4.getHeight(),
tower4.getWidth(), midPointY - (tower4.getHeight()));
}
You are drawing the tower too high, you need to be adding half the height rather than the whole height.
Here in drawTowers():
batcher.draw(AssetLoader.texture1, tower1.getX(), tower1.getY() + tower1.getHeight() / 2, tower1.getWidth(), midPointY - (tower1.getHeight()));
Do the same for the other towers.
This may not be perfectly correct but it shouldn't be far off.

Android: how to display an image using bitmap?

I have created a 10x10 grid for a game, and now i am trying to get the image i have for the player to display inside one of the grid squares. I have a Game, Player and Draw class. in the Game class the x and y position of the player are set, in the Player class is where i set the values for the x and y position and create the bitmap. In the Draw class I create the grid and call it in the onDraw method. I was wondering how do I call the bitmap in the onDraw so that it will display? I dont know if this makes any sense but any help or tips would be good.
public class Game {
Bitmap image;
int x;
int y;
int height;
int width;
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public int getX()
{
return x;
}
public int getY(){
return y;
}
public int getHeight(){
return height;
}
public int getWidth(){
return width;
}
}
public class Player extends Game {
public Player(Bitmap res, int w, int h, Context context){
image = res;
x = 300;
y = 300;
height = 300;
width = 300;
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.sokobanplayer1);
}
}
public class Draw extends View {
Paint red = new Paint();
Paint green = new Paint();
int rectSide = 1000;
public Draw(Context context) {
super(context);
red.setColor(Color.RED);
green.setColor(Color.GREEN);
red.setStrokeWidth(8);
green.setStrokeWidth(8);
}
public void drawGrid(Canvas canvas) {
int width = canvas.getWidth();
int height = canvas.getHeight();
float startX = (width / 2) - (rectSide / 2);
float stopX = (width / 2) + (rectSide / 2);
float startY = (height / 2) - (rectSide / 2);
float stopY = (height / 2) + (rectSide / 2);
for (int i = 0; i < 1100; i += 100) {
float newY = startY + i;
canvas.drawLine(startX, newY, stopX, newY, green);
}
for (int i = 0; i < 1100; i += 100) {
float newX = startX + i;
canvas.drawLine(newX, startY, newX, stopY, green);
}
}
public void drawPlayer(Bitmap res, int w, int h, Context context){
Bitmap image = res;
int x = 300;
int y = 300;
int height = 300;
int width = 300;
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.sokobanplayer1);
}
#Override
public void onDraw(Canvas canvas) {
drawGrid(canvas);
//canvas.getResources(image, x, y, null);
}
}

How can I put numbers in each part of the grid in a 10x10?

import java.awt.*;
import java.applet.*;
public class oef2ap extends Applet {
public void paint(Graphics g){
int x;
int y;
int width;
int height;
int teller;
width=10;
height=10;
teller= 0;
for(x=0;x<10;x++)
{
for(y=0;y<10;y++)
{
teller = teller + 1;
g.drawRect(x*width,y*height,width,height);
g.drawString(String.valueOf(teller), x, y);
}
}
}
}
This is my code but the g.drawstring doesn't give me what I want , it needs to put a ordered number from 1 to 100 in each rect.
did you forget multiply x,y in drawString ?
g.drawString(String.valueOf(teller), x*width, y*height);
Couple of issues:
1) You're not adjusting for the height location in when calling drawString you need to x & y by width and height respectively:
g.drawString(String.valueOf(teller), x * width, (y * height);
2) You also need to adjust the height downward again by height distance so your drawString ends up in the box:
g.drawString(String.valueOf(teller), x * width, (y * height)+height);
Putting it to work:
public class oef2ap extends Applet {
public void paint(Graphics g) {
int x;
int y;
int width;
int height;
int teller;
width = 25;
height = 25;
teller = 1;
for (x = 0; x < 10; x++) {
for (y = 0; y < 10; y++) {
g.drawRect(x * width, y * height, width, height);
g.drawString(String.valueOf(teller), x * width, (y * height)+height);
teller += 1;
}
}
}
}
Generates this output:

nothing happens when i execute the code. Got my methods right? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a code, and im supposed to return the rectangles area and perimeter on a JPanel. But when i execute nothing happens at all. Im suspecting an error somewhere in my methods cus i belive the rest is ok. Ill appricate all help.
Im just gonna show you my code in the JPanel.
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Rektanglar extends JPanel {
Rektanglar r1 = new Rektanglar ();
#Override
public void paintComponent (Graphics g) {
super.paintComponent (g);
g.drawString ("Rektanglar",10,20);
this.setBackground(Color.WHITE);
g.setColor(Color.BLUE);
g.fillRect(r1.getX(),r1.getY(), r1.getWidth(), r1.getHeight());
}
public int Y;
public int X;
public int width;
public int height;
public int Perimeter;
public int Area;
Rektanglar (){
width = 10;
height = 10;
X = 0;
Y = 0;
}
public void Rectangle(int x, int y, int w, int h) {
X = x;
Y = y;
width = w;
height = h;
}
public void setX(int X ){
this.X = X;
}
public int getX(int X){
return X;
}
public void setY(int Y){
this.Y = Y;
}
public int getY( int Y){
return Y;
}
public int getWidth( int width){
return width;
}
public int getHeight(int height){
return height;
}
public int getPerimeter(){
return (width + width + height + height );
}
public int getArea(){
return (height * width);}
}
}
If you ever actually try to construct an instance of Rektanglar, you'll get a stack overflow due to this:
public class Rektanglar extends JPanel {
Rektanglar r1 = new Rektanglar ();
...
}
That code says that in order to create one instance, you need to create another instance... which will create yet another instance, etc.
It's not at all clear why you've got r1 at all, but I strongly suggest you get rid of it...
I also suspect this:
public void Rectangle(int x, int y, int w, int h) {
X = x;
Y = y;
width = w;
height = h;
}
... is meant to be a constructor, in which case you'd have to write it as:
public Rektanglar(int x, int y, int w, int h) {
X = x;
Y = y;
width = w;
height = h;
}
Note that the name has to match the name of the class and you don't specify a return type.
Additionally, I'd suggest that:
You make all fields private
You compute the perimeter and area from the width and height, rather than keeping them as fields
You follow Java naming conventions for your variables (so x and y instead of X and Y).

Categories