I'm working with Libgdx but I have a basic java question. I'm trying to access the overridden methods from and interface in another class via a call but I'm not sure how. This is what I've got so far :
Java Code:
public interface Controller {
public void show ();
}
public class MainActivity extends AndroidApplication implements Controller {
@Override
public void showAd(boolean show) {
System.out.println("TEST");
[Code] ....
Right now this code returns a null pointer at the call.
I have following code. In this code CSClient is an interface. All methods of CSClient are implementaed in CSClientImpl class. Do I not need CS Client Impl imported in this code ?
How can I call getBranch() of CSClient, which is not implemented in CSClient as " this. getCsClient(). get Branch (new CSVPath(vpath), true);" ? This code works fine without any error in eclipse.
How can a method getBranch(), which is implemented in CSClientImpl class be used in this code without importing CSClientImpl ?
I have two classes, MonsterGame (shown below) and Monster. Monster has a public accessor method to get it's private character variable, which represents the first character of the name of the Monster in question.
I'm just a bit confused as to why am I unable to cycle through the array of Monsters I have in the MonsterGame class and call
m.getCharacter(); (pretty much the last line of code) public class MonsterGame{
private static final char EMPTY_SQUARE = ' '; private char[][] gameBoard = new char[10][10]; private Monster[] monsters = new Monster[4]; private int maxXBoardSize = gameBoard.length -1; private int maxYBoardSize = gameBoard[0].length -1;
[Code] ....
I understand that there need to be instances of objects to call methods, but is that not the case here? the Monster objects are have already been created, no? Do I need to create an index for the array? is the for loop not enough?
I can call a child method from a main method. If I have a parent called "Assessment", a child called "Quiz", and another child called "test". Can I instantiate an object like Assessment a = new test(); and call a method in test.
I know the method can be called if I instantiate the test t = new test() but that defeats the purpose of inheritance...
So I'm just a little unclear about this, but how would I call methods from the 'top' of an inheritance chain? I say 'top' because Object is the top... E.g.:
public class AClass { public void myMethod() { ... } } public class BClass extends AClass { public void myMethod() { ... } } public class CClass extends AClass { public void myMethod() { ... } }
Assuming that BClass.myMethod() completely overrides AClass.myMethod() (so that there is no call to super.myMethod() in BClass.myMethod()) How can I call AClass.myMethod() from CClass.myMethod()?
I'm wondering about the use of exceptions to handle errors that might occur during file I/O when the I/O is done by a method implementing an interface's method. The idea is for the interface to provide a uniform way for application code to read (and write, though I'm not addressing that in this post) a document from a file, given a File object that specifies the on-disk location of the document. The "document" can be an instance of any class the application programmer wants it to be, provided that it can be created from a file stored on disk. Here's the interface definition:
public interface DocumentRamrod<T> { public T openDocumentFile(File file) throws FileNotFoundException; }
A simple implementation, when T is a class that just holds a String, might look like this (Please overlook the fact that there is no call to the BufferedReader's close method, as it's not needed for this example.):
public class MyRamrod implements DocumentRamrod<OneLineOfText> { public OneLineOfText openDocumentFile(File file) throws FileNotFoundException { return new OneLineOfText(new BufferedReader(new FileReader(file)).readLine()); } }
But, that one line where the file is read (Line 5) might generate an IOException.To cope with it, I could add a try-catch to the implementation like this:
public class MyRamrod implements DocumentRamrod<OneLineOfText> { public OneLineOfText openDocumentFile(File file) throws FileNotFoundException { try { return new OneLineOfText(new BufferedReader(new FileReader(file)).readLine()); } catch (IOException ex) { Logger.getLogger(MyRamrod.class.getName()).log(Level.SEVERE, null, ex); } } }
Or, I could add that to the list of exceptions defined for the method in the interface, like this:
public interface DocumentRamrod<T> { public T openDocumentFile(File file) throws FileNotFoundException, IOException }
But that's where I'm getting nervous, as it makes me realize that, with an infinite number of possible implementations of openDocumentFile, I can't predict what all the exceptions thrown might be.should I have openDocumentFile simply throw Exception, and let the application programmer sort out which one(s) might actually be thrown, should I keep listing them as it become clear which ones are likely to be thrown, or should I not have openDocumentFile throw any exceptions and let the application programmer deal with it in the implementation of openDocumentFile (with try-catch blocks, etc.)? In Good Old C, I'd have passed back a null to indicate some general failure, with the various callers up the call-stack having to either deal with it or pass that back themselves (until some routine up the stack finally did deal with it), but that seems like an approach the whole exception mechanism was designed to avoid.
I'm thinking the right choice is to have openDocumentFile throw Exception, and let the application programmers decide which subclasses of Exception they really want to deal with. But I have learned to be humble about the things I think, where Java is concerned,
Basically I'm trying to code this program but I keep getting error can't be applied to given types. I know it has to do with my method trying to be called by an array, but I'm just kinda lost.
Write a program that prompts the user to input cash amounts and use a method to calculate the average amount for a week given 7 inputs of daily cash intake amounts for a cash register. Use an array to store the values. Recall that average is the sum of the values divided by the number of values. The method should take argument(s) of type double and return average weekly cash amount as a double.
Make sure you test your program using the test data given below and calls the averageCash() method defined above passing in the values. Verify that the input values are greater than or equal to 0. Negative numbers are not allowed. Output the values used and the averageCash value.
import java.util.*; public class ArrayHandout { public static void main(String args[]) { int[] a=new int[6]; Scanner sc=new Scanner(System.in);
This current one is to calculate a planes holding pattern. I have to write a method to prompt user to enter speed in knots, then it converts it to km/hr. Another method to calc. pattern width using the speed, another method to calc. pattern length, than a main method which would call and print out the speed in knots, speed in km, pattern width and length.My current problem is on the second method. It works in that I can enter the values and it gives me the correct answers, however it's asking me to enter the speed twice, instead of just once. Anything I try just results in errors and won't compile.
import java.util.Scanner ; //main method public class TitleRemoved { public static void main(String[] args) { double airSpeedKm = airSpeedOts () ; System.out.println("That speed is " + airSpeedKm + " km/hr.") ;
[code].....
I want my code to not only work, but be organized and easily readable as well, so I want to avoid bad habits.
why interfaces inherit prototype of all the non final methods of the object class in itself? Object class is parent class of all the class and Interface is not the class.
I am writing a game in Java for Android (although my question isn't Android or Game Dev specific).
I have a SceneManager class and a Scene interface and then various other classes that implement the Scene interface (Code at the end of this post).
Basically, in my MainGame class (which also implements the Scene Interface for Touch Event capturing purposes) I hold the bulk of my game code. Methods in this class are then called from my Level classes. (most of these are needed in all levels so it makes sense to hold them here and call them from the levels to eliminate unnecessary code duplication)
So, I have Level1, Level2......... Level20 classes which all implement Scene.
Now, the problem comes because in only 2 of my Levels something can happen (that can't in the other 18) and I need to run a response method in these 2 levels (the method isn't exactly the same, the response to this event happening is different for both levels).
To run common methods from my classes, I use my Scene Manager like this:
This works great as all Level's have an updateLogic(); and render(); method.
So from my mainGame class, I am doing something like : (pseudo code)
public void checkIfSomethingHappened(){ if (something happens){ if (currentLevel==5){ Level5.response();}
[Code]....
The above would be called from my 2 level classes. So something like:
MainGame.checkIfSomethingHappened(); //Called in addition to the normal methods that make up that level
I don't really want to have this (second) 'if' statement here in the middle of my performance critical game loop.
What I'm after is something like this:
if (something happens){ SceneManager.getInstance().getCurrentScene().response(); }
However, this would require me to put stubs in the other 18 classes.
I'm thinking there must be a way to do this as the SceneManager already knows the current scene so it seems a waste checking it again via an if (or switch) statement. What is the best way to do this without having to put stubs into classes that don't require this method?
In this project each individual will create a data analysis program that will at a minimum,
1) read data in from a text file, 2) sort data in some way, 3) search the data in some way, 4) perform at least three mathematical manipulations of the data, 5) display results of the data analysis in numeric/textual form, and 6) display graphs of the data. In addition, 7) your program should handle invalid input appropriately and 8) your program should use some "new" feature that you have not been taught explicitly in class.
(Note: this is to give you practice learning new material on your own - a critical skill of today's programmer.) If you do not have a specific plan in mind for your project, below is a specific project that meets all of the qualifications as long as 7) and 8) are addressed in the implementation.
Everything is done except I need to call my methods in my GradeTester.
GradeBook:
/** *This class creates an array called scores. *This class determines the length of the array scores and determines the last grade in the array scores. *This class sorts the array using a bubble sort, and searches the array. *This class calculates the mean, standard deviation, and the median of the grades in the array scores. *Once the grades in the array is sorted, the class then calculates the highest and lowest grades in the array. */
public class GradeBook { public final int MAXARRAY_SZ = 20; double [] scores = new double [MAXARRAY_SZ]; int lastGrade = 0; double mean = 0;
package Experimentation; import javax.swing.*; import java.awt.event.*; public class SimpleGUI1B implements ActionListener { JButton button; public static void main(String[] args) { SimpleGUI1B gui = new SimpleGUI1B(); gui.go();
[code]...
This is a program from Head First Java! since main is static it shouldn't be able to call non-static methods because statics do not use any instance variable values but in the above program we're call a non-static method go() how is it possible?
i am creating a desktop application. In this application, firstly there is a log in form. Now, i want to display the another form on the same window i.e by removing log in form a new form..
Using JPA 2.1, I have been trying without luck to call a function that returns a REF CURSOR containing RECORD TYPES. Is there a way of doing this?
One Example of trying:
Query query = em.createNativeQuery("select my_package.my_function from dual"); Object a = query.getSingleResult();
I have also tried to use the StoredProcedureQuery though this does not look like it can return the functions value.
Produced
org.springframework.orm.jpa.JpaSystemException: No Dialect mapping for JDBC type: -10; nested exception is org.hibernate.MappingException: No Dialect mapping for JDBC type: -10 at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:244)
I am calling a jsp page from my servlet using the requestdispatcher.forward(myjsp.jsp) method. myjsp.jsp is creating a new thread which is parallely processing along with the servlet. The issue is that the jsp page is not displayed until the servlet finishes its execution, although the new thread is created by jsp and is executing in parallel. How do we have the jsp page displayed even when the servlet is executing.
I just wanted to ask that when we invoke a function by passing certain arguments in it ,so for instance I have certain variables like a,b and c with some values assigned to them,so when I invoke a function like func(a*b , 8, c+a) where the function func accepts parameters like func(int x,int y,int z) ,so is there any order of evaluation of expressions defined like the value of c+d would be evaluated first and then assigned to variable z ,because in C we have sequence points and since comma when acting like a separator is not a sequence point so any of the expression can be executed first ,so basically is there any sequence in which the function arguments are assigned to the parameters of the function or any relation with the stack implementation.
Even in C ,the printf starts executing its expressions from right to left so basically in why does it happen there when comma is a separator in a function argument.
The idea is to create a program to add plants and retrieve plants from a Nursery. I created a class with the properties of the plants and also there is the class an Array list to keep track of the plants entered ( they will have to be printed later) (I am not too sure if adding a public class there is the best option.
The program will allow the user to pick and action and from there the code will do something. I don't want to have all the code inside 'main' The problem is in line 114.This is what I have so far.
Java Code:
package plant.nursery; import java.util.ArrayList; import java.util.Scanner; /**Class to create a plant for a nursery. public class PlantNursery
I need to get the string encodedString from the method encode able to be used in the decode method.
Java Code:
public String encode(String plainText) { int prepareString; int shiftChar; String preparedString2 = prepareString(plainText); String encodedString = ""; for(int c = 0 ; c < preparedString2.length();c++)
I need to create an applet that displays a grid of command buttons which I have done. I then need to create a new class that draws a silly picture of an alien, which I have also done. Where I am completely stuck and confused, is that I do not know how to get the drawing into the applet. My code is below. I really do not fully understand why this is not working or how to get it working.
import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MartianGame extends JApplet implements ActionListener { DrawMartian aMartian = new DrawMartian(); DrawJupiterian aJupiterian = new DrawJupiterian();