Java Game - Breaking Down A Method To Smaller Methods And Where To Call
Aug 6, 2014
I have a question about a method I have. In my game, I had a move method to move my player left and right, and originally this method was huge, as it also took care checking collisions and a few other things. I broke it up a bit and put the collisions in their own methods and call them from again another method... Here is an extract which I hope illustrates my point:
private static final double MOVE_SPEED = 0.2;
private static final double MAX_MOVE_SPEED = 3.5;
private static final double STOP_SPEED = 0.18;
private double xPos;
private double yPos;
[Code] .....
Something I thought might be a good idea is to check the direction collision when im doing the calculations for that direction:
Then I thought instead i can check it after both of these steps:
if(moveLeft || dx < 0.0) {
checkLeft();
}
I guess my question is quite general: How much is acceptable to break up a method? How many chains of method calls is acceptable? Is it ok to call the same method from different nearby places?
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package part1; import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.swing.*; class brick {
I am trying to make a 2d graphical animation using the java swing classes in order to make a frame. I had a basic version working when all of the code was under the main method, but when I moved some into another method it broke it. With y understanding of java my code should work as I create a variable of the method containing the code and then assign the size and exit button. However this causes many problems such as my BeaconFrame method informing me that it is unused when I have used it. Here is my code:
import javax.swing.*; import java.awt.*; public class BelishaBeacon { public void BeaconFrame() { JFrame frame = new JFrame(); JPanel panel1 = new JPanel();
In Main.java, How can i call method in CircleCalculationMethod.java ?
Should I put everything in same folder ??Should i do something like "import CircleCalculationMethod.java"Should i do something like create a package ...
I am trying to pass an object of type Product p to my editProduct method, however trying to call p.getName(); doesn't work and throws a NullPointerException. The same kind of thing works for my displayRecord method (in a different class) and I can call .getName() on Product p, also passed as an argument to that method. Below is my editProduct class. The NullPointerExcepion is being thrown at line 61 (i.e., nameField.setText(p.getName());).
I don't know if I explained right, so here's a line thing of how the classes relate:
And as a side note: adding the line p = new Product(); fixes it and successfully runs the class (including the Save and Quit parts) but obviously I want it to specifically refer to the Product I pass to the method.
I'm asking a question because I don't understand how Product p could possibly be null, because the argument is passed through my DisplayRecord class, which also takes a Product p argument and works. In that class, I have declared Product prod = p; and prod is what I am passing to editProduct.
I am trying to call a java method in jsp. The main idea is to hide menu based on the user who logs in. The java class flows like this :
public class UserVerification { public static void main(String[] args) { UserVerification obj = new UserVerification(); System.out.print(obj.GetUserVerification("abc"));
I used java and jsf. I created dynamic datatable in java file. Can i call java method from setOnchange() event?
I am able to call java script function from setOnchange() event. See the below code which is working fine for java script.
HtmlSelectOneMenu selectOneMenu = new HtmlSelectOneMenu(); selectOneMenu.setStyleClass("dropdownStyleTwo"); selectOneMenu.setOnchange("openWin(this);IGNORE_UN LOAD=false");
I wrote openwin() function in java script. But i am not able to call java method change().
Code which is not working.
HtmlSelectOneMenu selectOneMenu = new HtmlSelectOneMenu(); selectOneMenu.setStyleClass("dropdownStyleTwo"); selectOneMenu.setOnchange("myclass.change();IGNORE _UNLOAD=false");
myclass is the bean of class Test. If user select any value from dropdown i want to call change java method. This function will apply the same selected dropdown value to the other record also.
I am new to Java and have been learning it. I have a question here. I came across the following Java class and trying to understand it thoroughly but got confused how it is able to call an abstract method. Here is the code I am referring to :
package sampleapps.gui; import javax.swing.*; import java.awt.*; public class InnerClassAnimationExample { int x=70, y=70; public static void main(String[] args) {
[Code] ....
So, in the code above, there is an inner class NewMyDrawPanel which has a paintComponent(Graphics g) method. I have highlighted 2 lines of code above.
Line 1 : Graphics2D g2d = (Graphics2D) g; Line 2 : g2d.fillOval(x,y,40,40);
I understand we are type casting reference g to Graphics2D reference g2d and we are calling fillOval() method on g2d. I don't see a fillOval() method in Graphics2D class but it is there in Graphics class and fillOval method is an abstract method.
So, my question here is :
1. If we are not able to instantiate an abstract class(Graphics2D and Graphics classes), how are we able to access the fillOval() abstract method,
2. Secondly, since the fillOval() method is an abstract method, it does not have any implementation for the method.
However, when I call the method fillOval() on Graphics2D reference, I was able to draw and fill an oval of the specified co-ordinates. So, where would the actual implementation code be?
I will try to explain what i want to code , a HiLo game where the user can choose up to 3 different levels(1-10, 1-100, 1-1000) with these 3 methods
public static void main(String[] args) {...} public static int playGame(int maxNumber) {...} public static void giveResponse(int answer, int guess) {...}
with no Random , i will use the:
int number = (int)(Math.random() * max) +1; to generate numbers
I have tried so many times , but i don't get it ....
My code so far :
import java.util.Scanner; public class HL{ public static void main(String[] args ){ Scanner s = new Scanner(System.in);
My question is simple : I only own a List<Object>How can I set each element of _instanceMethod parameter from my List ?? If i decided to iterate through my list such as :
for (Object obj : myList){ _instanceMethod(obj); }
How can I correctly "populate" the _instanceMethod varargs signaturee by the n elements of my list ?
how come you can call non static methods from other classes(objects when they are created from main) but not static methods in the same class as the main method??
example I cannot call the method maximum from the main method aslong as its not static BUT i can call other objects non static methods from main??
class test{ public static void main(String [] args){ Scanner input = new Scanner(System.in); //create new Scanner object //for input int number1; int number2;
Write a program that prompts the user to enter an integer m and find the smallest integer n such that m * n is a perfect square. (Hint: Store all smallest factors of m into an array list. n is the product of the factors that appear an odd number of times in the array list. For example, consider m = 90, store the factors 2, 3, 3, 5 in an array list. 2 and 5 appear an odd number of time in the array list. So, n is 10.)
so far my program is just like this.
import java.lang.Math; import java.util.Scanner; public class PerfectSquare { public static void main(String[] args) { Scanner m = new Scanner(System.in); int Fint;
[Code] .....
how do i make the program find the smallest integer n?
And in the server getCustomerList() accessed to database, how many times getCustomerList() would be called from I request the xhtml page?. I have read this would be called several times because of JSF internals and It would be better to store it in a variable and access this variable.
1. Is this true this would be called several times? why?
2. If the previous statement was true, how to avoid it, I mean not call the method from a service?
Life Cycle Call back methods(init(), destroy(),...) are not transactional by default and expecting this in coming EJB releases (EJB 3.x / EJB 4.x). I was expecting next EJB release along with Java 8, but it stays at 3.2
I am writing a Bingo program for a class and have reached a roadblock. How do I call 5 numbers from the array and where do I store them so I can populate them back into my JLabels on the Bingo card. I'm sure there are more complicated ways to do this, but I'd like to keep it simple so I actually understand the process. I understand that I will need to shuffle the numbers in the array before choosing them, but I also am not sure how to do that.
int [] BArray = {1,2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; for (int index = 0; index > 5; index++) { }
For reference I am programming Java in BlueJ. I am fairly new to the language and I am having trouble with sorting.
I am trying to call / test all of the 5 sorting methods (at the same time) in the main class. To be specific, the sorted list has to technically outputted 5 times.
I figured out how to call / test Quicksort:
Sorting.quickSort(friends, 0, friends.length-1);
But the others are not working correctly. Specifically these:
For reference, this is the output when it is not sorted:
Smith, John 610-555-7384 Barnes, Sarah215-555-3827 Riley, Mark 733-555-2969 Getz, Laura 663-555-3984 Smith, Larry464-555-3489 Phelps, Frank322-555-2284 Grant, Marsha243-555-2837
This is the output when it is sorted:
Barnes, Sarah215-555-3827 Getz, Laura 663-555-3984 Grant, Marsha243-555-2837 Phelps, Frank322-555-2284 Riley, Mark 733-555-2969 Smith, John 610-555-7384 Smith, Larry464-555-3489
This is the class Sorting, which I should note is all correct:
public class Sorting{ /** * Swaps to elements in an array. Used by various sorting algorithms. * * @param data the array in which the elements are swapped * @param index1 the index of the first element to be swapped * @param index2 the index of the second element to be swapped */ private static <T extends Comparable<? super T>> void swap(T[] data, int index1, int index2){ T temp = data[index1]; data[index1] = data[index2];
[Code]...
This is the Main class in which I am supposed to call the sorting methods, SortPhoneList:
public class SortPhoneList{ /** * Creates an array of Contact objects, sorts them, then prints * them. */ public static void main (String[] args){ Contact[] friends = new Contact[7]; friends[0] = new Contact ("John", "Smith", "610-555-7384"); friends[1] = new Contact ("Sarah", "Barnes", "215-555-3827");
I am just trying to test this array, for a locker combination program that involves classes...but the array is printing out the whacky numbers for the location. When I try to call the method in the main, it does not work. How do I call a method that exist within a class into the main method?
public class locker { public static void main(String[] args) { CombinationLock();
I am receiving two errors that I'm not sure how to fix. The first is illegal start of expression at line
"public void actionPerformed(ActionEvent e)"
and no suitable method found for intersect JButton at line
"if(blocker01.getBounds().intersects(r_wall))" .
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class gamePanel01 extends JPanel implements KeyListener { character ch1 = new character("Hero");
I've never had to do a void method call. I have my void method in one class and my main (where I want to do the call) in another. How do you actually call a void method?
I had to make a program that ran a craps game using methods and I keep getting an error ....
Here is my code :
public class CrapsGame { public static void main(String[] args) { System.out.println(playGame()); }//end of main method public static int rollDie(int n){ return (int)(Math.random()*n)+ 1; }//end of rollDie method
[Code] .....
And this is the error im getting
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - variable roll might not have been initialized at CrapsGame.playGame(CrapsGame.java:29) at CrapsGame.main(CrapsGame.java:4) Java Result: 1
My code has a method where the users input a bunch of variables and then those variables get added together in a new variable. What I want to know is how do I call the variable that is in the other method to another method?
import java.util.*; public class Calc{ public static void main (String [] args){ determinevalue1(); determinevalue2(); determineTotalvalue(double value1, double value2);
I am trying to get the program to ask the user to enter the project grade and if it is less than 65, then I want the program to display "fail" or if it is greater than 64 than I want it to display "passed". In this project I have to include a method and I am trying to call it in my if-else statement. I keep getting an error message saying "Project.java:143: error: incompatible types: void cannot be converted to int". I posted my code below. It's a long program, but this part is toward the bottom.
import java.util.Scanner; public class Project { public static void main(String[] args) { //call method welcomeMessage();
I have two comboBoxes - one in main and another in my 'windows' class. The code below is in main and references the comboBox in main but I need to use the comboBoxEnv out of my 'windows' class. How can I do that so it says ComboBoxEnv rather than comboBox?
JMenuItem menuExport = new JMenuItem("Export"); menuExport.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(comboBox.getSelectedItem() == null){
[Code] ....
I've gotten access to it by changing my code and making a method of it but I'm not sure of what I do now
private String eventName; private ClockDisplay timeOfEvent; public static final int MIDNIGHT_HOUR = 00; public static final int MINUTE_ZERO = 00; }
How can I create a method called public void addMinuteToTimeOfEvent() which calls the timeOfEvent's increment() method to add one minute to the timeOfEvent?