I am working on a project and it asks me to "Provide appropriate names and data types for each of the instance variables. Maintain two GVdie objects" under class fields. I am unsure as to what is being asked when asking for two objects as instance variables and how I would write that...
Let's pretend I'm working on an RPG. Like in all RPGs, there are items found all throughout the imaginary world. Each player and NPC can obtain an item. My question will concern those items.
In other words, I'd like to use instances of a class in multiple places of the code. Each instance will have its own, individual values of instance variables, with one obvious exception: itemQuantity should have a different value in playerInventory, npcInventory, etc. Also, I'd like a list of all items that can be found in the game. This list doesn't need itemQuantity at all.
class Items { String itemName; float itemWeight; int itemQuantity;
[Code] ....
The question is: should I really make itemQuantity an instance variable of the Item class? It seems as though for each copy of the Item class I should create a separate copy with different value of itemQuantity, but that's not very efficient. Where is the error in my logic?
What's important is that there may be plenty items in a game and a player may be given power to create new items during the course of the game.
I'm just wondering why variables in interface can't be instance scope?
interface Test{ int a; }
And then
Test test = new TestImpl(); test.a=13;
Yes, it violates OO, but I don't see why this is not possible? Since interface is not an implementation, therefore it can;t have any instance scope variable. I can't find the correlation of interface being abstract and being able to hold instance scope variable. There's gotta be another reason. I'm just curious about any programmatic limitation, not deliberate design constraint. the example of programmatic limitation is like when Java forbids multiple inheritance since when different parents have the exact same method, then the child will have trouble determining which method to run at runtime.
Alright, I have a JavaFX gui that is creating a new instance of data calculation to graph in a chart; however, the data is not updating each time the Platform.runLater() feature executes. Each time an event occurs, a new instance with the same variable name occurs. I use to get methods to retrieve the data I want, so shouldn't the values update each time the new instance is created? This is a very condensed version of what happens with the event, but this is what is not working correctly.
Event: solarPlot = new SolarTracker(); solarPlot.getElevation(); solarPlot.getAzimuth(); Class constructor : public SolarTracker() {
I'm not really sure I understand the functional difference between a static and final variable/field. Oracle defines Class Variable as:
Class Variables (Static Fields) A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change.
If static will have the same value regardless of how many times it's used, then why use final (or vice versa)?
I have finished a Java 101 class but the most we did was create a gui for a timer program. I figure I need more training and every book I pick up wants to teach me hello world and the difference between a class and object again.
package Threads; // THIS PROGRAM WILL HAVE TWO THREADS i.e. "main" AND ANOTHER THREAD (SYSTEM WILL NAME IT "Thread-0" //THE STORY IS THAT WE WILL START Thread-0 FROM main AND LET IT EXECUTE. //main WILL WAIT AND LET IT EXECUTE FOR 5 MINUTES. //IF IT FINISHES ITS EXECUTION BEFORE 5 MINUTES, WELL AND GOOD; //BUT IF IT DOESN'T, WE WILL INTERRUPT IT. //AFTER INTERRUPTION, WE WILL DECIDE TO WAIT INDEFINITELY.
public class SimpleThreadsCopy { public static void threadMessage(String s){ String sThreadName= Thread.currentThread().getName(); System.out.format("%s: %s%n", sThreadName, s);
[Code] ....
The statement against which I have written many *'s gives the following error.
No enclosing instance of type SimpleThreadsCopy is accessible. Must qualify the allocation with an enclosing instance of type SimpleThreadsCopy (e.g. x.new A() where x is an instance of SimpleThreadsCopy).
Now that a similar "error-free" code is given here, what's wrong with this piece of code and what should I do about it?
Trying to understand the error statement, I replaced the erroneous statement with
Java Code : Thread t= new Thread(new SimpleThreadsCopy().new MessageLoop()); mh_sh_highlight_all('java');
And the error got fixed. From that I understand that the inner class is just kinda a nonstatic member of the outer class and it will be accessed by the objects of the outer class only.
But then why doesn't the code in the tutorial give an error?
So in the code below I create an instance of my own triangle class and use one of its methods. The thing is I use one of my triangle classes methods in a method other the main method of my main program so I'm thinking it can't access it?
Any way here's the code for my triangle class
import java.util.Scanner; public class QudratullahMommandi_Triangle_06 { Scanner keyboard = new Scanner(System.in); private double side1; private double side2; private double side3;
[Code] ....
and here's the error message
QudratullahMommandi_S_06.java:46: error: cannot find symbol { triangle1.outPut(); ^ symbol: variable triangle1 location: class QudratullahMommandi_S_06 1 error
package com.emp; public class salarybean { private String name; private Double days; private Double id; public String getName() { return name;
[code]...
now i want to retrieve all these values in another servlet where i want to do some calculation but not able to retrieve it is showing null and indicating for this value in my eclispe IDE " Iterator<salarybean> itr=list.iterator(); "
public class Time extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException
I am having some problem accessing variables from an array instance of a class. Heres what i have done;
In the main class:
Example obj[]= new Example[4];
In the main class constructor:
obj[0] = new Example(0); obj[1] = new Example(1); obj[2] = new Example(2); obj[3] = new Example(3);
In the main update() method:
if(condition) //update
In the Example class constructor:
private boolean change = false;
In the Example class update() method:
if(x >20) change= true;
Now, i want to access the variable change from the main class, how do i do it? The 'condition' in the if statement is the condition of wether the change variable ia true or false. How do i access it?
I have to create an application that deals with maps.
I first have to create the instance variables for the class.
So very simply if my hashmap is going to consist of football clubs and players. Football clubs being a string value for the key and players being a set of strings for the values. How would I go about creating the instance variable in a class for this?
I can't seem to find anything that specifically deals with instance variables and constructors for maps.
public String firstName() Returns the customer's first name public String lastName() Returns the customer's last name public double balance() Returns the customer's account balance
Finally I need to create a driver to test my class. And create several accounts and verify that the first name, last name, and balance methods work properly. This is my code below.. I don't know if I did it right.
public class BankAccount { String firstName, lastName; double balance; public BankAccount(String firstName, String lastName, double balance) {
I am currently trying to use Junit to test a whole bunch of stuff. I almost have full line coverage but I am getting hung up on testing an if statement that consists of whether or not an object is an instance of another class. This class happens to be an interface, and even the object is an interface. Weird I know but I just want to know how to get into that if statement. I realize testing interfaces might be a waste of time but I still really want to know how. Here is an example of what I am trying to test:
Java Code:
if(x instance of y){ //where x and y are both interface objects doSomething(); } mh_sh_highlight_all('java');
What is the difference between extending JFrame in one class and simply constructing a new JFrame object in that same class? What benefits do I have with each solution, providing I want to use that class to create the GUI. Is it the same or are there differences rather than not having to reference to a new JFrame to be able to use its functions?
Write an instance method, contains, that has one explicit parameter of type Rectangle. The method should return true if every point of the rectangle determined by the explicit parameter is on or within the rectangle determined by the implicit parameter. It should return false otherwise.
How do I create an instance of a class in a method?
I am a bit rusty whenever I think of instances. I always think of main method and objects when I see instance which gets me confused on what to do when I am not in a main method. The example:
I have a abstract class, School, and inside School I have some methods that must preform some action on an instance. For example, there is a move() method that must move the instance of School. Another method named, personOld(), which returns whether or not an instance of School surpassed some determined age.