• 0

Java Questions


Question

so, I started working and am supposed to learn java now. I just have a few basic questions, so if u could help me with those that would be great.

1st: What is the constructor and what does it do?

2nd: Is there a general way to tell what is an object and what not?

3rd: What is the signature?

4th: When working with Vectors, what is the Iterator?

5th: I was supposed to write a little app that prints out all prime numbers from 2 to 100. I did a version of it, but it only prints out the number from 10 to 100. I know why it does that, but if u could post a modified or different version of that app it would be great. heres my code:

public class prime2 {

	public static void main(String[] args) {
  for (int p = 0; p <= 100; p++)
 	 if (p % 2 > 0 && p % 3 > 0 && p % 4 > 0 && p % 5 > 0
   	 && p % 6 > 0 && p % 7 > 0 && p % 8 > 0 && p % 9 > 0
   	 && p % 10 > 0) {
    System.out.print(p + " ");
    
 	 }
 	 System.out.println();
	}

	}

thx

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0
1st: What is the constructor and what does it do?

2nd: Is there a general way to tell what is an object and what not?

3rd: What is the signature?

4th: When working with Vectors, what is the Iterator?

1. Each object has a constructor whether you define it or not. The constructor runs when an object is instantiated, or when a new instance of that object type is created. It is the constructor's responsibility to initialize all private fields and perform any necessary work that must be done before the object can be used.

2. As far as I know, in java, everything is an object.

3. The signature is the name of the method, its accessibility (such as public or private), its return type, and the types and names of its parameters. For example:

public void switch(int a, int b) // C#, should be similar to java

What follows the signature is the code that is run when the method is called.

4. (In C#, I'm assuming it's the same) An iterator is used to loop through a collection (such as an array).

Link to comment
Share on other sites

  • 0

5. Firstly, get rid of p%4, p%6, p%8, p%9 and p%10 (they're redundent). The primes below 10 won't print since they're eliminated by the if statement. 2 is prime, but in your if, you have p%2 and since 2%2=0, the if statement evaluates as false and the System.print won't be executed.

Link to comment
Share on other sites

  • 0

Is there a general way to tell what is an object and what not?

Everything in Java is an object except - int, char, boolean, long .... and a few others

the Java's object version of "int" is "Integer"

her is an example

int i = 100;
Interger aNumber = new Interger(i);

//you can also do

String i = "100";
int aNumber = Integer.parseInt(i);

Long.parseLong()
Boolean.parseBoolean() ... etc

4th: When working with Vectors, what is the Iterator?

you don't use interator with vector, use use iterator with collections.

Vector v = new Vector();

v.addElement("Hi");
v.addElement("Hi2");
v.addElement("Hi3");

for(int i=0; ?i< v.length; i++) {
 ? String s = (String) v.ElementAt(i))
 ? System.out.println(s); 
}

Link to comment
Share on other sites

  • 0

Iterators as stated are generally used collections, in particular it's used together with Linked Lists.

Remember not to confuse and mistaken and call a constructor a "method" because many people think it's essentially a method with no return. It's not a method, because you can't even re-invoke it, it can only be invoked when you instantiate a new instance of it.

Link to comment
Share on other sites

  • 0

Iterator usage with a linked list

   List ary = new LinkedList();
    
    .....

    for (Iterator iter = ary.iterator(); iter.hasNext(); ) 
    {
        Object x = (Object) iter.next();
    }

BTW:

Interger aNumber = new Interger(i);

is spelt "Integer"

her is an example

is suppose to be "here is an example"

Damn my spelllling!

Link to comment
Share on other sites

  • 0
Anything you use "new" on is an Object.

not 100% true.

Strings for example can be constructed in two different ways

String str = "Hello World";

and

String str = new String("Hello World");

Java is suppose to be 100% object orientated ... ie everything is an object.

In reality Java does have variables that are not objects as such...

these non objects that we use are called primitives and have no functions at all.

We can tell "String" is an object because it has functions such as .length(), .charAt()...etc

We can tell int, long, float are not objects because they have no functions.

int x = 9;
System.println(x);

//we dont have functions for x .... like x.getValue(); or x.toString();

//javas object version of int is Integer so we use the .toString() 

Integer y = new Integer(1); 

System.out.println(y.toString());

//we can also parse primatives

y = Integer.parseInt(x);

System.out.println(y.toString());

Link to comment
Share on other sites

  • 0
not 100% true.

Strings for example can be constructed in two different ways

Yeah, there's ways around new to make an Object, but I was telling him the 100% true way that you know you're making an Object.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.