vatsalya rao

Ranch Hand
+ Follow
since Feb 14, 2007
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by vatsalya rao

Hello Dinesh,

Did you compile your code first of all?

apart from errors like ";" expected and "unclosed string literal" you will get 6/7 errors.

Iam giving you clues:

1)only one public class per file.
2)If you are extending an abstarct class but not overriding its methods,then your class must be declared abstract.

3)abstract methods can not have a body.


you compile your code of absstract classes first,that will surely give you some clarification about abstract classes.




Abstract classes can have method implementations in it,where as interfaces can not.


you can not instantiate an interface,but where as when you instantiate a concrete subclass that extends an abstract class,the abstract class constructor is called.

When an instance of B is created,its parent class constructor is called though its parent is an abtract class.

abstract class A
{

A()
{
System.out.println("A");
}

public abstract void display();//abstract methods should not have body

}

class B extends A

{

B()
{

System.out.println("B");

}

public void display()
{

System.out.println("display in concrete class");

}

}

public class demo
{
public static void main(String []args)
{

B b1=new B();
b1.display();

}

}


An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods.

An abstract class can contain fields, constructors,An interface can not contain fields, constructors, it has only the method signature but no implementation.

An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several interfaces but only one abstract class.
Hello Michael,

I got my mistake.And I changed my code in constructor of comboDemo() as follows

public comboDemo()
{

super("JComboDemo");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

Container c = getContentPane();

// After this,I am adding Label and JcomboBox components through c.add
//(Component
) method

}

I didnt create any JFrame object here.The components are added to container's ContentPane.

So,there is no use of creating frame1 in the constructor comboDemo() right?

If I create and set its visibility to true after adding all the components,still same problem of not displaying the components.


Apart from the lines I made in itlaics,please clarify any difference in o/p, if I create a new Jframe object and add components to it and without creating JFrame obj as the class already extends JFrame?
17 years ago
I wrote a program to display a JLabel and 2 JComboBoxes.

It is compiling correctly but not diisplaying either of them.

Unable to find the bug in my code.Previously I wrote small programs to display JButtons,JradioButtons,JTextFileds,JLabels and now moved on to JComboBox.

here my code goes


import javax.swing.*;
import java.awt.*;

public class comboDemo extends JFrame

{
private JLabel expiryLabel;

private JComboBox yearBox,monthBox;

public static void main(String ar[])

{

comboDemo obj1 = new comboDemo();

}

public comboDemo()
{
JFrame frame1 = new JFrame();
frame1.setVisible(true);
frame1.setTitle("Combo Demo");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container c = getContentPane();

c.setLayout(new FlowLayout());


expiryLabel = new JLabel("Expiration Date");

c.add(expiryLabel);

monthBox= new JComboBox();

yearBox= new JComboBox();

for(int i=1;i<13;i++)

monthBox.addItem(" " +i);//adding month number to first JComboBox.


for(int i=2008;i<2012;i++)

yearBox.addItem(" "+i);//adding year to second JComboBox

c.add(monthBox); //adding first JComboBox to container
c.add(yearBox); //adding second JcomboBox to container.

}


}


Can anybody please point out where iam going wrong in displaying?
17 years ago
As Mark pointed it out,check the type of the variable "Company info"

and also check that you have written a method setSupplier which takes a String as parameter.

You might have written setSupplier with any other paramater.
17 years ago
Awesome answers.Thank you Maneesh and Brian for your clear cut explainations.


Iam out of my issues now and started programming in EVENT HANDLING.

Thanks once again.
17 years ago
Thank you Maneesh.



Since yesterday I staretd programming GUI in my machine.

Till now I never wrote a siingle program abt GUIs.Just gone through tutorials in the net.


I used Flow Layout Manager,GridLayout and BorderLayout.Just to experiment I set setLayout to null and set bounds.


Two more issues are bothering me.


1) What ever the layout manager I use, If I resize the frame I created,I cant view the resized frame.it is oon the top of my command prompt window but just as a thin liine showing Frame title and with maximize,minimize and close buttons.

Why it is so?

2) If I wont write System.exit(0) in my code Iam left with an open Dos prompt if I write the newly created frame is just disappearing after creation.


Hope you will clear these too.

I had gone through translate(int x,int y) in Graphics class.

it will change the origin to the position indicated in x,y fields right?

Why did you ask me to go through this method?


My code is as follows



import java.awt.*;

import javax.swing.*;

import java.awt.event.*;


public class createFrame extends JFrame

{


public static void main(String[] args)

{

createFrame f1 = new createFrame();

}



public createFrame()
{

setTitle("frame 1");
setVisible(true);

Container c = getContentPane();


c.setLayout(null);




JButton bt1 =new JButton("Button1");

JButton bt2= new JButton("Button2");
JButton bt3= new JButton("Button3");
JButton bt4= new JButton("Button4");
JButton bt5= new JButton("Button5");
JButton bt6= new JButton("Button6");

JLabel lb1= new JLabel("Label1");
JLabel lb2=new JLabel("Label2");


JTextField txt1=new JTextField();
JTextField txt2=new JTextField();

c.add(bt1);
c.add(bt2);
c.add(bt3);
c.add(bt4);
c.add(bt5);
c.add(bt6);


c.add(lb1);
c.add(lb2);


c.add(txt1);
c.add(txt2);



bt1.setBounds(50,30,80,60);

bt2.setBounds(140,30,80,60);

bt3.setBounds(50,120,80,60);

bt4.setBounds(140,120,80,60);

bt5.setBounds(50,190,80,60);
bt6.setBounds(140,190,80,60);


lb1.setBounds(90,250,80,60);

lb2.setBounds(170,250,80,60);


txt1.setBounds(300,30,80,40);
txt2.setBounds(300,120,80,40);

// System.exit(0);
}
}



The last thing is is it ok to start programming GUI with swings rather than AWT classes?
17 years ago
I just run a program from a book,which creates 6 buttons on a frame.


The code that creates a frame,adding components to it,is OK with me.

But what confused me lot is


bt1.setBounds(30,10,80,60);

bt2.setBounds(130,10,80,60);

bt3.setBounds(30,70,80,60);

bt4.setBounds(130,70,80,40);

bt5.setBounds(30,130,80,40);
bt6.setBounds(130,130,80,40);

here the variables bt1,bt2,bt3,bt4,bt5,bt6 are Button Reference variables.

The first two arguments in setBounds() method are X and Y cooridinates respectively right?

If so,the buttons in the frame should be displayed in this fashioin


Button5 Button6


Button3 Button4


Button1 Button2



Because "Y" coordinate for Button1 and 2 is 10,so they should be at the bottom.


For Buttons 3 and 4 it is 70,so they should be above Buttons 1 and 2.


For buttons 5 and 6 Y coordinate is 130,so they should be on top.





But The frame displayed is as Follows



Button1 Button2

Button3 Button4


Button5 Button6




Actually thought twice and thrice before posting this,questioning myself "Did I forget my basic graph pointing?"

tried on paper too.

But could not get why the displayed frame is different from my expectations?


Hope somebody clears.
17 years ago
Hello all,

Till now I never Tried GUI on my machine,except reading thhrough different tutorials in the net.

TOday I tried to create a frame and just onebutton in it.

When i compile it is warning me to recompile with -Xlint and I did the same.
Here is the code for my simple program

import java.awt.*;
public class createButton
{

public static void main(String ar[])
{
Frame f = new Frame("First Frame");

Button b = new Button();
b.setLabel("Button 1");
f.add(b);
f.show();
System.exit(0);

}

}


It is compiling succesfully,but when I run the newly created frame is not visible anywhere.

though it is giving 2 warnings I run the program.
Is this the reason for my newly created frame invisibility?

One more thing that is confusing me is, in variuos tutorials in some code, the class is extending APPLET and some are using JFRAME from javax.swing class.

I am a beginner for GUI programming as I never tried before.

I just want to create some compenents in a container.


I want to create containers in my main()only and want to add components to it.

Is that not possible without extending Appplet claass or Swing classes?

If you find this silly,please spare me as a beginner in GUI.

Thanks in advance.
17 years ago
Hello all,

Iam new in this forum.

I have been in Java Ranch since a year,but in SCJP forum. I am overwhelmed by the replies I got for my doubts in that forum and learnt alot for certification and will continue.

Hope I will be benifitted with this forum too.

My question is I want to learn JDBC.I found tutorials on net,but want to know what I have to install in my machine.Iam having Java 1.6 in my machine.

Thank in advance.
Source:http://www.techinterviews.com/?p=281

How many objects are eligible for garbage
collection once execution has reached the line labeled Line A?


String name;

String newName = "Nick";

newName = "Jason";

name = "Frieda";

String newestName = name;

name = null;

//Line A


a) 0
b) 1
c) 2
d) 3
e) 4


Iam thought my answer is 1.And this is how I came to that conclusion

1) First a String ref variable name is created.And it is not referring to any String obj.

2)Another String ref variable newname is created and referring to the object NICK

3)Now newname is referring to object Jason. so Nick is eligible now for GC

4)name is referring to "FRIEDA" and newestname also referring to same object.(So here 2 reference variable name and newestname are referring to same object FRIEDA.)

5)name is set to null.

On the whole 1 object only eligible for GC,which is in step 3.

Am I correct?If not please let me know where I had gone wrong.

Actually a bit confused with step 5 also.As name is set to null,is this also eligible for GC? I feel name is a string ref variable only not a string object,so it is not eligible.

If I sound ridiculuous in my doubts Iam really sorry.
As we can extend from any class except the class marked final,I want to extend from an inner class.

My program is like this

class outer
{

class inner
{

public void show()
{

System.out.println("show in inner");
}

}//End of Inner

}//End of Outer


class innerDemo extends outer.inner //Line A
{

public static void main(String ar[])
{
}

}

Is Line A a valid one?
When i compile this Iam getting the error as

An enclosing class instance that contains outer.inner is required.



What does it mean?


One more thing is as we can apply the modifier abstract to an inner class,how to write a concrete subclass which implements this abstract inner class?
Hello Alajandro,

My doubt is

a1 is of type 'SuperBase",but in Superbase there is no print method which
recievs an object of type "Base",then how a1.print(new Base() is being executed?and printing 'Super"

b1 is of type "Superbase"and it doesnt have a print method which takes a Derived object,then how b1.print(new Derived())is being compiled and printing 'Super"again,same is the doubt with c1.print(new Derived()); too.


Confuses in polymorphism and inheritance,pl clarify
Hello Mahesh,

Thank you for your post.Actually I am waiting for the reply of my post as me too posted same question.
Hello Maanshenoy,

What Iam asking is is there anyway to invoke sizzle() which is in annonymous inner class?

If so,how?
In kathy sierra and Bert Bates book in annonymous inner classes topic,in page num 470 the code is as follows

class popcorn
{
public void pop()
{

System.out.println("popcorn");

}

}

class food
{

popcorn p=new popcorn()
{
void sizzle()
{

System.out.println("sizzle in annonynous");

}

public void pop()
{
System.out.println("pop in annonynous");
}


};
public void popIt()
{

p.pop();
//p.sizzle();
}

}

My doubt is how to invoke the method sizzle() in annonymous inner class?

I tried to create an object of type Food,and tried to invoke sizzle() on it,as the annonymous class is in Food.

But getting compile error sayong that it cant find the method sizzle() in Food class.


How to do it?