Sunteți pe pagina 1din 8

Java GUI Programming

Revision Tour-III
Type A Very Short Answer Questions
1 Fill in the blanks
(i) In java, methods reside in _____________________.
(ii) The number and type of arguments of a method are known as _________________________.
(iii) The first line of method definition that tells about the type of return value along with number
and type of arguments is called ___________________________.
(iv) A member method having the same name as that of its class is called ____________ method.
(v) A constructor method has ______________________ return type.
(vi) A private constructor allows object creation only inside ______________________methods.
(vii) A _________________ constructor takes no arguments.
(viii) A _______________________constructor creates objects through values passed to it.
(ix) The keyword _____________ refers to current object.
Ans (i) In java, methods reside in classes.
(ii) The number and type of arguments of a method are known as parameter list.
(iii) The first line of method definition that tells about the type of return value along with number
and type of arguments is called method prototype.
(iv) A member method having the same name as that of its class is called constructor method.
(v) A constructor method has no return type.
(vi) A private constructor allows object creation only inside private methods.
(vii) A non-parameterized constructor takes no arguments.
(viii) A parameterized constructor creates objects through values passed to it.
(ix) The keyword this refers to current object.
2 What is the principal reason for passing arguments by value?
Ans The principal reason for passing arguments by value is that you cannot alter the variables that are used
to call the method because any change that occurs inside called method is on the method’s copy of the
argument value.
3 When an argument is passed by reference,
(a) A variable is created in the method to hold the argument’s value.
(b) The method cannot access the argument’s value.
(c) A temporary variable is created in the calling program to hold the argument’s value.
(d) The method accesses the argument’s original value in the calling program.
Ans (d) The method accesses the argument’s original value in the calling program.
4 What is the role of return statement in a method?
Ans The return statement is used to return a value to the calling method. When a return statement does not
return a value it returns the control to the calling method.
5 At what time is the constructor method automatically invoked?
Ans The constructor method is automatically invoked when an object is created class.
6 What is composite datatype? What is user defined datatype?
Ans The data types that are based on fundamental or primitive data types, are known as Composite
Datatypes. Since these data types are created by users, these are also known as User-Defined Datatypes.
7 Can you refer to a class as a composite type/ user defined type?
Ans yes
8 Can every class be reffered to as a user-defined datatype?
Ans no
Type B Short Answer Questions
Page 1 of 8
1. Define a method. What is method prototype?
Ans Methods are functions that operate on instances of classes in which they are defined. Objects can
communicate with each other using methods and can call methods in other classes.
Method definition has four parts. They are name of the method; type of object or primitive type the
method returns, a list of parameters and the body of the method.
Method Prototype tells the compiler about the type of the value returned by the method, the number
and type of arguments.
For example - int absval(int a);
2. What are actual and formal parameters of a method?
Ans Actual parameter A variable or expression contained in a method call and passed to that method is
called actual parameter.
Formal parameter A name, introduced in a method definition that is replaced by an actual parameter
when the method is called.
3. How many values can be returned from a method?
Ans one value can be returned from a method.
4. Differentiate between CALL by reference and CALL by value.
Ans Call by reference Call By Value
 Call by reference is used to share the  Call by value is used to create a temporary copy of
same memory location for actual and the data which is transferred from the actual
formal parameters parameter in the final parameter.
 The changes done in the function are  The changes done in the function in formal
reflected back in the calling environment. parameter are not reflected back in the calling
environment.
 It makes the use of the & sign as the  It does not use & sign
reference operator.
5. How do we invoke a constructor?
Ans Constructors are invoked by the compiler through new constructor-name().
6. How are parameterized constructors different from non-parameterized constructors?
Ans
Parameterized constructor Non-Parameterized constructor
 A Constructor which receives parameters.  A constructor which does not receive any
parameter.
 Example XYZ 01 = new XYZ(2,13.5F);  Example XYZ 01 = new XYZ();
7. When a compiler can automatically generate a constructor if it is not defined then why is it considered
that writing constructors for a class is a good practice?
Ans It is good practice to write constructor for a class because we have total control over constructor and
objects. Apart from this we can make constructor to do jobs which we want to perform at the time of
object creation.
8. List some of the special properties of the constructor methods.
Ans  You need not code them explicitly. Java will automatically place a default constructor
 You can pass arguments to the constructor
 They can return only an object of type of that class
 They can be made private
 They would be executed always (Everytime a class is instantiated)
9. What is parameterized constructor? How is it useful?
Ans A parameterized constructor is just a constructor which take some kind of parameter (variable) when is
invoked. Parameterized constructor is used to provide different values to the distinct objects.
10. Why can’t every class be termed as user-define datatype? How is a user-defined datatype different
Page 2 of 8
from an application?
Ans Every class can’t be termed as user-defined datatype because a user-defined datatype is defined through
a class but it does not contain main method.
The difference is that there is the main function in an application whereas a user defined data type does
not implement main() method.
TYPE C Long/Practical Answer Questions
1. How is call-by-value method of method invoking different from call-by-reference method? Give
appropriate examples supporting your answer.
Ans Call By Value Call by reference
 Call by value is used to create a temporary  Call by reference is used to share the same memory
copy of the data which is transferred from the location for actual and formal parameters
actual parameter in the final parameter.
 The changes done in the function in formal  The changes done in the function are reflected back
parameter are not reflected back in the calling in the calling environment.
environment.
 All primitive data types are passed by value  All reference data types are passed by reference
class Operation class Operation
{ {
int data=50; int data=50;
void change(int data) void change(Operation op)
{ {
data=data+100;//changes will be op.data=op.data+100; //changes will
in the local variable only be in the instance variable
} }
public static void main(String public static void main(String
args[]) args[])
{ {
Operation op=new Operation(); Operation op=new Operation();
System.out.println("before System.out.println("before change
change "+op.data); "+op.data);
op.change(500); op.change(op);//passing object
System.out.println("after change System.out.println("after change
"+op.data); "+op.data);
} }
} }
2. Design a class to represent a bank account. Include the following members Data members
Name of the depositor Account number Type of account Balance amt in the account
Design a GUI application having following interface. Add functionality to above application by creating
required methods and implementing them.

Ans public class NewJFrame1 extends javax.swing.JFrame {


String name="ABC";
int number=1234;
Page 3 of 8
String type;
int bal=6000;
int deposit(int bal,int tr_am)
{
int depo;
depo=bal+tr_am;
return depo;
}
int withdraw(int bal,int tr_am)
{
int wd;
wd=bal-tr_am;
return wd;
}
//Deposite
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int a1;
a1=Integer.parseInt(jTextField1.getText());
int sum;
type="Deposit";
sum=deposit(bal,a1);
jLabel6.setText(Integer.toString(sum));
jLabel5.setText(Integer.toString(number));
jLabel7.setText(type);
}
//Withdraw
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
int a1;
a1=Integer.parseInt(jTextField1.getText());
int w;
type="withdraw";
w=withdraw(bal,a1);
jLabel6.setText(Integer.toString(w));
jLabel5.setText(Integer.toString(number));
jLabel7.setText(type);
}
//Statement
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here
jLabel6.setText(Integer.toString(bal));
jLabel5.setText(Integer.toString(number));
}
}
3. Define class for Dates. Add require functions e.g., constructors, printing a date, validating a date etc.
Ans //Code is also checking for leap year
public class DateClass {
int d,m,y;
DateClass(int dd,int mm,int yy)
{
d=dd;
m=mm;
y=yy;
}
void printdate()
{
System.out.println(d+"-"+m+"-"+y);
}
Page 4 of 8
void validate()
{
if(m==1 || m==3 || m==5 || m==7 || m==10 || m==12)
{
if(d<=31 && d>0)
{
System.out.println("Valid date");
}
else
{
System.out.println("Invalid Date");
}
}
else
if(m==4 || m==6 || m==9 || m==11)
{
if(d<=30 && d>0)
{
System.out.println("Valid date");
}
else
{
System.out.println("invalid date");
}
}
else
{
if(m==2)
{
if((y%100==0)&&(y%400==0)||(y%100==0)&&(y%4==0))
{
if(d>0 && d<=29)
{
System.out.println("valid date");
}
else
{
System.out.println("invalid date");
}
}
else
{
if(d>0 && d<=28)
{
System.out.println("valid date");
}
else
{
System.out.println("invalid date");
}
}
}
else
{
System.out.println("invalid date");
}
}

Page 5 of 8
}
public static void main(String[] args) {
DateClass d= new DateClass(30,3,12);
d.printdate();
d.validate();
}
}
4. Use the above defined Date datatype in a different class for creating objects such as birthDate,
joinDate, examDate, etc.
Validate these dates. Try verifying them by passing invalid date e.g., 33/13/03.
Ans public class DateClass {
int d,m,y;
DateClass(int dd,int mm,int yy)
{
d=dd;
m=mm;
y=yy;
validate();
}
void printdate()
{
System.out.println(d+"-"+m+"-"+y);
}
void validate()
{
if(m==1 || m==3 || m==5 || m==7 || m==10 || m==12)
{
if(d<=31 && d>0)
{
System.out.println("Valid date");
}
else
{
System.out.println("Invalid Date");
}
}
else
if(m==4 || m==6 || m==9 || m==11)
{
if(d<=30 && d>0)
{
System.out.println("Valid date");
}
else
{
System.out.println("invalid date");
}
}
else
{
if(m==2)
{
if((y%100==0)&&(y%400==0)||(y%100==0)&&(y%4==0))
{
if(d>0 && d<=29)
{
System.out.println("valid date");
Page 6 of 8
}
else
{
System.out.println("invalid date");
}
}
else
{
if(d>0 && d<=28)
{
System.out.println("valid date");
}
else
{
System.out.println("invalid date");
}
}
}
else
{
System.out.println("invalid date");
}
}
}
}
class validate
{
public static void main(String[] args) {
System.out.println("BirthDate");
DateClass birthdate=new DateClass(33, 13, 03);
System.out.println("JoinDate");
DateClass joindate=new DateClass(32, 4, 04);
System.out.println("ExamDate");
DateClass examdate=new DateClass(30, 4, 04);

}
}
5. Design an application having an interface like

Implement functionality by writing methods calcSum(), CalcAng() and CalcMax(). Invoke these
methods from buttons, event handlers.
Ans //Sum Method
int addition(int a,int b,int c)
{
int total;
total=a+b+c;
return total;

Page 7 of 8
}
//Sum Button
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int a1,b1,c1;
a1=Integer.parseInt(jTextField1.getText());
b1=Integer.parseInt(jTextField2.getText());
c1=Integer.parseInt(jTextField3.getText());
int sum;
sum=addition(a1, b1, c1);
jTextField4.setText(“sum=”+Integer.toString(sum));
}
//Average Method
float average(float a,float b,float c)
{
float avg;
avg=(a+b+c)/3;
return avg;
}
//Average Button
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
float a1,b1,c1;
a1=Float.parseFloat(jTextField1.getText());
b1=Float.parseFloat(jTextField2.getText());
c1=Float.parseFloat(jTextField3.getText());
float a;
a=average(a1,b1,c1);
jTextField4.setText(“Average=”+Float.toString(a));
}
//Maximum Method
int maximum(int a,int b,int c)
{
int max;
if((a>b)&&(a>c))
{
max=a;
}
else if((b>a)&&(b>c))
{
max=b;
}
else
{
max=c;
}
return max;
}
//Maximum Button
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
int a1,b1,c1;
a1=Integer.parseInt(jTextField1.getText());
b1=Integer.parseInt(jTextField2.getText());
c1=Integer.parseInt(jTextField3.getText());
int m;
m=maximum(a1,b1,c1);
jTextField4.setText(“Maximum=”+Integer.toString(m));
}

Page 8 of 8

S-ar putea să vă placă și