How To Reference The Object
Jan 27, 2014
I am doing a Junit test case, but I keep having issues with AssertEquals ( object expected, object actual). I don't know how to reference the other actual object so that it can compare to the expected.
public void add (Distance d) throws CustomException
{
//I can also convert feet to inches and add all inches together and then divided inches to feet and inches
Distance result = new Distance();
int newFeet = this.feet + d.getFeet();
int newInches = this.inches + d.getInches();
if(newInches > 11)
{
newFeet = newFeet + (newInches/12);
[Code]...
View Replies
ADVERTISEMENT
Apr 15, 2014
For example I create an object like this:
BankAccount b = new SavingsAccount();
Now lets say that I want to access a method 'addInterest()' that is in the 'SavingsAccount' class I would have to do: '((SavingsAccount)s).addInterest();'
The question I have is why do I have to cast 'b' to SavingsAccount? Isn't the actual object reference of 'b' already an instance of 'SavingsAccount' class? How does the 'BankAccount' affect the object itself? I'm really confused as to what class is truly getting instantiated and how BankAccount and SavingsAccount are both functioning to make the object 'b'.
View Replies
View Related
Nov 6, 2014
I don't understand why the object reference variable 'a' cannot be recast from a thisA object reference to a thisB object reference.Is it the case that once a reference variable is linked to a particular object type then it cannot switch object types later on.I am facing the Java Associate Developer exam soon and I am just clearing up some issues in my head around object reference variable assignment,
class thisA {}
class thisB extends thisA { String testString = "test";}
public class CastQuestion2 {
public static void main(String[] args) {
thisA a = new thisA();
thisB b = new thisB();
[code]....
View Replies
View Related
Mar 28, 2014
Create an equals method that takes an object reference and returns true if the given object equals this object.
Hint: You'll need 'instanceof' and cast to a (Geocache)
So far I have:
public boolean equals(Object O){
if(O instanceof Geocache){
Geocache j=(Geocache) O;
if (this.equals(j)) //I know this is wrong... but I can't figure it out
return true;
}
else return false;
}
I think I have it correct up to the casting but I don't understand what I'm suppose to do with the this.equals(). Also I'm getting an error that I'm not returning a boolean... I get this all the time in other problems. I don't get why since I have to instances of returning booleans in this. "returns true if the given object equals this object" makes no sense to me. I assume the given object, in my case, is 'O'. What is 'this' object referring to?
View Replies
View Related
Dec 1, 2014
difference between object reference and object
View Replies
View Related
Oct 30, 2014
I have started working on a little project in my free time. It is just a simple text rpg that runs in a counsel window. I have 5 files each file contains 1 class.
public class SomnusCharacter {
private String gender = "";
private int age = 0;
private String race = "";
private int level = 0;
private int xp = 0;
[Code] ....
The chain of events right now is:
1. MainMenu is run
2. If user inputs n CreateCharactor is run
3. User inputs name, age, ect in SomnusCharacter object made in CreateCharacter
4. Intro (just rough demo for testing purposes) is run
5. If user inputs m Menu is run
6. Menu calls and prints out all the information from the object made in CreateCharacter
Step 6 is where I am having my problems. How can I reference (lets say the SomnusCharacter object made is called player) player from my Menu class? I know that if I made a new character that it would just create another SomunsCharacter object with the default values again.
View Replies
View Related
May 22, 2014
I read somewhere : "Java use clone() method of Object class to copy content of one object to the other. The problem will arrive if the Class that needs to be copied also contains reference to the other object."Not able to understand the second line.
View Replies
View Related
Jun 15, 2014
Check out the following basic code (assume that dog has a method called bark):
Dog d = new Dog();
Object o = d;
o.bark(); // error
But why? Isn't o just a pointer to a memory address which has the dog object? If so, why can't the compiler see that the object has a method called bark? Or, to ask the question another way, why is Java designed to check the object reference to see if the method exists instead of the object itself?
View Replies
View Related
Jul 26, 2014
I have the following code:
public class CollisionManager<T> {
private boolean collision = false;
private T mainEntity;
public <T extends Entities> void handleCollision(T mainEntity, T secondEntity){
this.mainEntity = mainEntity; // This is illegal.
}
}
Why "this.mainEntity = mainEntity" is incorrect and also show me the correct way to achieve this?
The error I am getting is "Type mismatch: cannot convert T to T"
View Replies
View Related
Jan 24, 2015
I don't really know what this means and it is sending an error when I try to run my program I am trying to set the x and y value (Int x and int y) to setVisible false at a specific time in my game but it keeps sending the error that int is not a reference type.
View Replies
View Related
Aug 25, 2014
I'm fairly new to Java, I'm very experienced with C++ and C# in which you can pass by reference - extremely useful. Take for example this bit of code in C#:
class MyClass
{
public MyClass(int i)
{
m_i = i;
}
public int m_i;
[Code] ...
Just at the end of this program x.m_i will be equal to 8. As far as I can see this is not possible in Java: you can't pass a double by reference, using a Double will kick in the autoboxing so that won't work either. The only "solution" in Java would be to pass in a double[] (of length 1) or to make a wrapper class, both nasty solutions because a user may want to just hold a double as a member of their class just as I have, for reasons such as not allocating more memory for a class and generally not being bloated.
View Replies
View Related
Mar 15, 2015
I am making an MVC program and I am not allowed to put the action listeners in the view class. I was able to get one button working fine but since I am unable to reference them I cannot give them both individual responses.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Controller {
HobbyList model;
ListView view;
[code]....
View Replies
View Related
Apr 17, 2014
public class {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double[] numbers = new double[10];
System.out.println("Enter " + numbers.length + "numbers");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = input.nextDouble();
[Code] .....
I am trying to reference the input "numbers[i]" to my mean method, how do i do that?
View Replies
View Related
Nov 12, 2014
I know in C++ It's possible to pass by reference but what about java?
For example, can i pass the address of a health variable into a Ninja class and then
Since Ninja inherits from an enemy class pass the health into the enemy class and within there have a function that returns that same health address and takes 5 from the health returning 95.
View Replies
View Related
Aug 29, 2014
I passed my abstract class private final reference to another concrete class and I used abstract class reference as parameter to that concrete class constructor and in my main method and null to that parameter then only that program executes correctly...i placing my code below ..if there is any error tell me where is error occurring then i will check my code...i think my code is right but little bit doubt abstract class concept.
{
}
class concept1 extends concept
{
private final concept parent;
public concept1(concept aparent)
{
parent=aparent;
System.out.println(parent);
}
public static void main(String args[])
{
//concept p=new concept1(null);
concept c=new concept1(null);
}}
View Replies
View Related
Mar 29, 2014
When I write a statement such as:
myType x;
This means that x is a reference to objects of type, "myType" and no memory has been set aside for an instance of this object. I think it's true that x also has the starting memory address of this object.
What I feel uncertain about is if x is telling the JVM(?) where it can find the starting address of this object, why do we care about the type that x is?
i.e., if we assign x to an object totally unrelated to this type then the compiler would complain - correct? If so, how come, since all x is doing is telling the JVM(?) where to find your object (via the address it contains).
I suppose all, hy a reference has a type associated with it if all the reference does is point you to the object?
View Replies
View Related
May 17, 2014
I am currently learning Java basic concepts and this is a very simple code which produces error depending on the way I write. It is as follows:
package testPackage;
interface ArrayInsideClassShock {
}
class Check{
ArrayInsideClassShock var[]; //Error line
var= new ArrayInsideClassShock[10];
}
Error in eclipse:Syntax error on token ";", , expected
But if I write it as:
package testPackage;
interface ArrayInsideClassShock {
}
class Check{
ArrayInsideClassShock var[]=new ArrayInsideClassShock[10];
}
It doesn't show any error.
View Replies
View Related
Feb 15, 2010
what these mean with examples..
1. A reference variable can be of only one type and once declared, that type can never be changed(although the object it references can change)
2. A reference is a varaible ,so it can be reassigned to other objects(unless the reference is declared final)
3. A reference variable's type determines the methods that can be invoked on the object the variable is referencing.
4. A reference variable can refer to any object of the same type as the declared reference , or - it can refer to any subtype of the declared type.
5. A reference variable can be declared as a class type or an interface type. If the variable is declared as an interface type, it can reference any object of any class that implements the interface.
View Replies
View Related
Jan 23, 2014
I don't understand why following code when executed returns false
String s1= " string".trim();
if(s1 == "string"){
System.out.println("true");
}
else{
System.out.println("false");
}
View Replies
View Related
Jan 29, 2014
How can i check the reference that is not duplicate and date format is DDDCCYYnnnnnnn, CC must be 20 and YY must not be less than the current year, nnnnnnn is the sequetial number of the file and to complete the 7 numeric characters, zero's must be populated in front of the number. DDD must be a valid reporting entity code. there must not be a duplicate. Code I try
private boolean test-date()
{
String s_Ref = this.transactionFile.getsReference();
boolean flag = true;
if(!isValidPattern(s_Ref, "[0-9]{14}"))
[Code] ....
The reference number is like 201401030234509
If there is duplicate show error code 105
View Replies
View Related
Feb 28, 2015
I am unable to understand the meaning of this sentence "final reference variables must be initialized before the constructor completes.",What is trying to imply?
View Replies
View Related
Sep 30, 2014
How are methods invoked on the `bird` parameter?
next.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
[Code] ....
`MyQueueBean` is intended to give out a bird once only, to exactly one end-user. Because it's application scoped, and not session scoped, getting attributes directly from the bean would give inconsistent results.
The birds application is from Facelets Essentials Guide to JavaServer Faces View Definition Framework: [URL] ....
how is the variable passed to the file? Once it's passed, how is it referenced?
View Replies
View Related
May 18, 2015
Is there a short term that means: "using an object reference with the datatype of a supertype to refer to an object with the datatype of a subtype"?
Saying "polymorphic reference" doesn't seem to be specific enough because ALL references other than those with the Object datatype are polymorphic. I saw one post that referred to these reference variables as a "supertype reference." In the absence of any other (more official) term, I may just use that term because calling a reference a "supertype reference" implies that there is a subtype. It sure beats what I was thinking about using: "sub-as-super." Which, when the inevitable mispronunciations occur, would lead to all kinds of off-color jokes.
Is there an "official" term that all of the dozens of Java books I have read seem to have missed? Or does everyone stumble along, spelling things out with a nearly sentence-long phrase.
View Replies
View Related
May 10, 2014
Is there a way to forward the text child of an anchor tag to its hyper reference?
For example, suppose I have an HTML file:
<a href = "ABC.jsp">SomeText</a>
Is there a way to send text 'SomeText' to ABC.jsp so it can be processed?
View Replies
View Related
Aug 14, 2014
I have just started to work on Java EE and I am not able to put the refrence of css and javascript in jsp page.
My senario is below: I have created script file inside myJavaScript Folder like Web Container-> myJavaScript ->Home.js. Similarlly for css:Web Container-> myCss ->Home.css.
My Jsp page palced inside Like: Web Container-> Home ->Home.jsp.
How to give the reference of js and css file in my Home.jsp page.
View Replies
View Related
Aug 27, 2014
if there is a possibility to use the pass by reference technique in java like in C++.URL....
View Replies
View Related