• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

few queries with the program

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;
public class Vector
{
/*public Vectors(int x,int y)
{
System.out.println("in the vector constructor");
}*/
public Vector()
{
System.out.println("in the empty constructor");
}
public Vector(int x ,int y)
{
System.out.println("in the vector constructor");
}
public Vector(int z)
{
System.out.println("in the vector with storage capacity");
}
public boolean addElement(Object obj)
{
System.out.println("added into the vector");
return true;
}
public final int capacity()
{
System.out.println("capacity of the constructor");
return 6 ;
}
public final boolean contains(Object obj)
{
System.out.println("in the contains method");
return true;
}
public static void main(String args[])
{
Vector v = new Vector(5,1);
System.out.println("" +v);
Vector w = new Vector(3);//capacity is 3 but still taking velagala>3
System.out.println("" +w);
Vector x = new Vector();
System.out.println("" +x);
boolean c=x.addElement("venkat");
System.out.println("" +c);
boolean d = w.addElement("velagala");
System.out.println("" +d);
System.out.println("" +w.capacity());//y this is not returning the capacity
//of the vector
boolean f=x.contains("simha");//y this is method is returning true when there is no simha in the vector collection named x
System.out.println("" +f);
}
}

hi all,
i have few queries with the above program.
can anyone try to fix the problems which i placed in the comments

thanks,
satya
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is already a class Vector in the package java.util that you're importing. You should not call your own class "Vector". Use a different name, otherwise the compiler doesn't know if it should use your class Vector or the class Vector in package java.util.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

System.out.println("" +w.capacity());//y this is not returning the capacity
//of the vector


Because you have hard coded it to return 6

boolean f=x.contains("simha");//y this is method is returning true when there is no simha in the vector collection named x


Because you have hard coded it to return true.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!