• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

jtips

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI all,

Can somebody plz explain how this returns true!!!

List l = new List();
if (l.getRows() == 4)
System.out.println(true);
else
System.out.println(false);


Thanx
Rajani
 
Mini Pilla
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebosy answer this.
I am giving the complete question.I didnot understand why the first answer is true.

1. class Q55 {
2. public static void main(String[] args) {
3. int n=1;

4 switch(n) {

5 case 1:
6 List l = new List();
7 if (l.getRows() == 4)
8 System.out.println(true);
9 else
10 System.out.println(false);

11 case 2:
12 List l1 = new List(5,true);
13 l1.add("Mercury");
14 l1.add("Venus");
15 l1.add("Earth");
16 l1.add("Mars");
17 l1.add("Jupiter");
18 l1.add("Saturn");
19 l1.add("Uranus");
20 l1.add("Neptune");
21 l1.add("Pluto");

22 if (l1.getItemCount() == 5)
23 System.out.println(true);
24 else
25 System.out.println(l1.getItemCount());
26 System.out.println(l1.getItem(5));

27 default:
28 System.out.println("java.awt.CheckboxGroup is NOT a Component");
29 System.out.println("CheckboxGroup extends java.lang.Object");

30 case 3:
31 Checkbox c = new Checkbox();
32 if (c.getState() == true) {
33 c.setState(true);
34 System.out.println(c.getState());
35 }
36 else
37 {
38 System.out.println(c.getState());
39 }

40 case 4:
41 TextField tf = new TextField(null, 18);
42 System.out.println(tf.getText());
43 System.out.println(tf.getColumns());
44 break;
45 }
46 System.exit(0);
47 }
48 }
The answer is
Prints
true
9
Saturn
"java.awt.CheckboxGroup is NOT a Component" and
"CheckboxGroup extends java.lang.Object"
false
� � ( empty space )
18

Thanks
Rajani
plz have patience to answer.
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am explaining all the conditions. There is no break after the case 1 so all the cases are exeuted.

Line 6 creates a new list with default 4 visible lines.
l.getRows() returns 4 so the if condition is true.
prints true.

Line 12 creates a list a 5 lines are visible in the scrolling list and the user can select multiple items from the list.You are adding 9 items, so at line 22 the if condition is false so it prints, 9 and 5th element in the list is Saturn. (index starts at 0)

Just prints all the lines.

line 31 creates a new check box with default state false. So line 38 prints false.

Line 41 creates a new Text field by setting the text to be displayed to null, and the number of colums to 18. If the text is null it is set to empty string so it prints an empty string and 18.
Hope this helps,
Vanitha.

[This message has been edited by Vanitha Sugumaran (edited August 11, 2001).]
 
Mini Pilla
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vanitha,
Thank you very much.
But I have small question at line 7.
Is the list created at line 6 has default value 4.How do we know that???Is the default value of list is 4???

Anyhow thanks again.
Rajani
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajani,
from the API


public List()
Creates a new scrolling list. By default, there are four visible lines and multiple selections are not allowed.


Vanitha.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi:
A List when created with a constructor that takes no parameter has a default of four visible rows.
Consider the following code:
import java.awt.*;
public class List1{
public static void main(String args[]){
List a=new List();
a.add("0");
a.add("1");
a.add("2");
a.add("3");
a.add("4");
a.add("5");
a.add("6");
a.add("7");
a.add("8");
System.out.println(a.getRows());
System.out.println(a.getItemCount());
List b = new List(5);
System.out.println(b.getRows());
System.out.println(b.getItemCount());
}
}

This will print
4
9
5
0
getRows returns the number of visible rows. So for the list a, it is 4 by default and it prints 4. But for the list b, the value 5 is specified explictily, hence it has 5 visible rows.
getItemCount returns the total number of items which is 9 for the list a and 0 for list b.
Hope it is clear now.
Gaja.
*************************************
When I started editing this reply, the query was not answered, but after posting it, I find that it has been answered! Instead of deleting this, I prefer editing this post with this message!

[This message has been edited by Gaja Venkat (edited August 11, 2001).]
 
Mini Pilla
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Vanitha,Gaja,
I am sorry that I was lazy to check thrugh the API.
I will check before posting.
Thanx
Rajani
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic