I get headaches when I have to write nearly 10 lines of code to say 2 Objects are equal, when their type is equal and both's attribute is equal. You can easily see that in this way of writing the number of lines increase drastically with your number of attributes.
public class Id implements Node {
private String name;
public Id(String name) {
this.name = name;
}
public boolean equals(Object o) {
if (o == null)
return false;
if (null == (Id) o)
return false;
Id i = (Id) o;
if ((this.name != null && i.name == null) || (this.name == null && i.name != null))
return false;
return (this.name == null && i.name == null) || this.name.equals(i.name);
}
}
A lead developer on my project has taken to referring to the project's toString() implementations as "pure cruft" and is looking to remove them from the code base.
I've said that doing so would mean that any clients wishing to display the objects would have to write their own code to convert the object to string, but that was answered with "yes they would".
Now specifically, the objects in this system are graphic elements like rectangles, circles, etc and the current representation is to display x, y, scale, bounds, etc...
So, where does the crowd lie?
When should you and when shouldn't you implement toString?
Hello All,
I have a following ArrayList,
[Title,Data1,Data2,Data3]
[A,2,3,4]
[B,3,5,7]
And I would like to convert this one like this,
[Title,A,B]
[Data1,2,3]
[Data2,3,5]
[Data3,4,7]
I'm bit confused with the approach. Any hint would be much appreciated.
Thanks.
Why is it that
class swi22
{
public static void main(String[] args)
{
int a=98;
switch(a)
{
default:{ System.out.println("default");continue;}
case 'b':{ System.out.println(a); continue;}
case 'a':{ System.out.println(a);}
}
System.out.println("Switch Completed");
}
}
gives error as: continue outside of loop
Why doesn't the following program return 0, since I am accessing p from a new A(), which has not had main called on it?
public class A {
public int p = 0;
public static void main(String[] args) {
p = Integer.parseInt(args[0]);
new B().go();
}
}
class B {
public void go() {
System.out.println(new A().p);
}
}
Hello,
I am trying to display the output as "1(10) 2(23) 3(29)" but instead getting output as "1 2 3 (10)(23)(29)". I would be grateful if someone could have a look the code and possible help me. I don't want to use arraylist.
the code this
// int[] Groups = {10, 23, 29}; in the constructor
public String toString()
{
String tempStringB = "";
String tempStringA = " ";
String tempStringC = " ";
for (int x = 1; x<=3; x+=1)
{
tempStringB = tempStringB + x + " ";
}
for(int i = 0; i < Group.length;i++)
{
tempStringA = tempStringA + "(" + Groups[i] + ")";
}
tempStringC = tempStringB + tempStringA;
return tempStringC;
}
I have following Class, I need to get type in constructor, how can I do that?
public abstract class MyClass<T> {
public MyClass()
{
// I need T type here ...
}
}
defined variable:
LinkedList list1=new LinkedList();
Object get() in list1 obtains a node of list1
Object remove() in list1 deletes a node of list1
count() is length of list1
for(int i=1;i<list1.count();i++){
if(list1.get(i).startsWith('"',0)) //Error here
list1.remove(i);
}
Error: cannot find symbol
symbol: method charAt(int)
location: class Object
how to fix this problem?
I would like to delete the node in list1 which starts with (").
in c or c++
function comlen is defined such
int comlen(char *p,char *q){
int i=0;
while *p && (*p++==*q++)
i++;
return i;
is this code equivalent of this function
int comlen(String s,String m){
int i=0;
while (i<s.length() && s.charAt(i)==m.charAt(i)){
i++;
}
return i;
?
please help
class Parent
{
private void method1()
{
System.out.println("Parent's method1()");
}
public void method2()
{
System.out.println("Parent's method2()");
method1();
}
}
class Child extends Parent
{
public void method1()
{
System.out.println("Child's method1()");
}
}
class test {
public static void main(String args[])
{
Parent p = new Child();
p.method2();
}
}
I'm confuse why does in Parent::method2() when invoking method1() it will cal Parents method1() and not Childs method1 ? I see that this happens only when method1() is private? Can someone explain me why ?
Thanks you.
I wrote paging logic:
My requirement: total elements to display:100 per page,if i click next it should display next 100 records,if i click previous 100 records.
Initial varaible values:
showFrom:1,
showTo:100
max elements:depends on size of data.
pageSize:100.
Code:
if(p*emphasized text*aging.getAction().equalsIgnoreCase("Next")){
paging.setTotalRec(availableList.size());
showFrom = (showTo + 1);
showTo = showFrom + 100- 1;
if(showTo >= paging.getTotalRec())
showTo = paging.getTotalRec();
paging.setShowFrom(showFrom);
paging.setShowTo(showTo);
}
else if(paging.getAction().equalsIgnoreCase("Previous")){
showTo = showFrom - 1;
showFrom = (showFrom - 100);
paging.setShowTo(showTo);
paging.setShowFrom(showFrom);
paging.setTotalRec(availableList.size());
}
Here i can remove and add the elements to the existing data.above code works fine if i add and remove few elements.but if i remove or add 100 elements at a time counts are not displaying properly above code works fine if i add and remove few elements.
Should I point out that I am a begginer at this?
double averageMonthlyTemp() {
double[] amt = new double[52];
int sum = 0;
int index = 0;
for (int i = 0; i < temp.length - 1; i = i + 7) {
//where temp is an existiing
//previously initialized array
//of 365 elements, form 0 to 364
for (int j = 0; j < 7; j++) {
sum = sum + temp[i + j];
if (j % 7 == 6) {
double average = ((double) sum) / 7;
amt[index] = average;
index++;
sum = (int) 0;
}
}
}
return amt;
}
When I try to compile, I get an "incompatible types" error, with the "amt" at return amt marked in red. Does somebody know why?
public abstract class Parent {
private Parent peer;
public Parent() {
peer = new ??????("to call overloaded constructor");
}
public Parent(String someString) {
}
}
public class Child1 extends parent {
}
public class Child2 extends parent {
}
When I construct an instance of Child1, I want a "peer" to automatically be constructed which is also of type Child1, and be stored in the peer property. Likewise for Child2, with a peer of type Child2.
The problem is, on the assignment of the peer property in the parent class. I can't construct a new Child class by calling new Child1() because then it wouldn't work for Child2. How can I do this? Is there a keyword that I can use that would refer to the child class? Something like new self()?
Hi,
Thread class has run method to implement the business logic that could be executed in parallel.But I want implement different business logics in a single run method and to run simultaneously.How to get this feature.
thanks
Is there a way to get an element id of a list to get it later through list.get(index)
when using
for(Object obj: o)
construction
Only define a local var and manually incrementing it?
Anything simpler?
Lets us take instances of two classes
public abstract class Shapes
{
public abstract void draw(Graphics g);
}
public class Rectangle extends Shapes
{
public void draw(Graphics g)
{
//implementation of the method
}
}
here the class Rectangle has extended class Shapes and implicitly it extends class Object.I know no other extension is possible but cant we call inheriting classes Shapes and Object multiple inheritance?(Since inheriting two classes is multiple inheritance from one perspective)
I'm writing a little application for my BlackBerry and I need to send some data to a webpage (Via GET or POST)
Can anyone advise on the way to do this with the BlackBerry.
In JTable, if you are inserting values to that row of 4 in first column, Then the focus goes to first row and second column by default. I want to focus the next column of the same row( ie., row 4). How to set requestFocus() in JTable by row wise.?