Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
963 views

Java Programming

Bytecode is a highly optimized set of instructions designed to be executed by the Java Virtual Machine (JVM). Translating Java programs into bytecode makes programs portable across platforms, as only the JVM needs to be implemented for each platform. To compile a Java program, a compiler converts the source code file (.java) into an intermediate bytecode file (.class) that can run on any JVM. The document then provides an example Java program that performs basic arithmetic operations like addition, subtraction, multiplication and division using different methods and invoking them in the main method. It also briefly explains interfaces in Java and different exception handling techniques available in Java like try, catch, finally blocks. Finally, it provides a code sample for a simple GUI program
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
963 views

Java Programming

Bytecode is a highly optimized set of instructions designed to be executed by the Java Virtual Machine (JVM). Translating Java programs into bytecode makes programs portable across platforms, as only the JVM needs to be implemented for each platform. To compile a Java program, a compiler converts the source code file (.java) into an intermediate bytecode file (.class) that can run on any JVM. The document then provides an example Java program that performs basic arithmetic operations like addition, subtraction, multiplication and division using different methods and invoking them in the main method. It also briefly explains interfaces in Java and different exception handling techniques available in Java like try, catch, finally blocks. Finally, it provides a code sample for a simple GUI program
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Programming BT0052

Set-1
1. Define the concept of Java Bytecode and its importance.

Ans: Bytecode is highly optimized set of instruction deigned to executed by the Java-
run-time system, which is called the Java Virtual Machine (JVM). In standard form
JVM is an interpreter for bytecode.
Translating a Java program into bytecode helps make it much easier to run a
program in a wide variety of environment. The reason is straightforward : only the
JVM need to be implemented for each platform. Once the run time package exists
for a given system, any java program can run on it. JVM will be differ from platform to
platform, all the interpret the same Java bytecode. The interpreter of bytecode is the
easiest way to create truly portable programs. A java program is interpreted also
helps to make it secure.
2. How will you compile a Java program?
Ans: A compiler converts the Java program into an intermediate language
representation called Bytecode which is platform independent. A Java file will have
the extension .java.
Suppose I have created a java file named Dharmendra.java. When this file is
compiled we get a file called as Dharmendra.class. Now after compiling the
program into bytecode(.class file) that run on the Java Virtual Machine which can
interpret and run the program on any operating system.
This make Java programs platrform-independent.
3. Write a program to perform the basic arithmetic operations:

a) Addition

b) Subtraction

c) Multiplication

d) Division

Use 4 different methods for the above and invoke them as necessary from
the main() method.

Ans: import java.io.*;


import java.util.*;
class Arithmetic
{
void add(int a,int b)
{
System.out.println("Sum of " + a + " And " + b + " is " + (a+b));
Cybotech Campus Page 1
Java Programming BT0052
}
void sub(int a,int b)
{
System.out.println("Subtraction of " + a + " And " + b + " is " + (a-b));
}
void mul(int a,int b)
{
System.out.println("Mutiplication of " + a +" And " + b + " is " +
(a*b));
}
void div(int a,int b)
{
System.out.println("Divison of " + a +" And " + b + " is " + (a/b));
}
}
class Main
{
public static void main(String arg[]) throws IOException
{
InputStreamReader byteIn= new InputStreamReader(System.in);
BufferedReader kbin= new BufferedReader(byteIn);
System.out.println("Enter First Number");
int n1=Integer.parseInt(kbin.readLine());
System.out.println("Enter First Number");
int n2=Integer.parseInt(kbin.readLine());
Arithmetic a=new Arithmetic();
System.out.println("1 For Add");
System.out.println("2 For Subtraction");
System.out.println("3 For Mutliplication");
System.out.println("4 For Divison");
System.out.println("Enter your choice");
int ch=Integer.parseInt(kbin.readLine());
switch(ch)
{
case 1:
a.add(n1,n2);
break;
case 2:
a.sub(n1,n2);
break;
case 3:
a.mul(n1,n2);
break;
case 4:
if(n1<n2)
Cybotech Campus Page 2
Java Programming BT0052
System.out.println("Nemurator is Smaller then Demurator");
else
a.div(n1,n2);
break;
default:
System.out.println("This is invalide choice");
}
}
}

4. Explain the concept of interfaces in Java with a suitable example for the
same.
Ans: With the help of interface you can fully abstract a class’ from its
implementation
That is using interface, you can specify what a class must do but not
how it does it. Interfaces are syntactically similar to classes, but they
lack instance variables, and their methods are declared without any
body. By providing the interface keyword, java allows you to fully
utilize the “one interface, multiple methods” aspect of polymorphisms.
5. What are the different exception handling techniques availablein Java.
Ans: When an unexpected error occurs in a method java creates an
object of the appropriate exception class. After creating the exception
objects, java passes it to the program, by an action called throwing an
excetion. The exception object contains information about the type of
error and the state of the program when the exceptinon-handler and
process the exception.
Following keywords are use in exception-handling in java:
a) try
b) catch
c) finally
The try block :-
The statements that may throw an exception in the try block.
The following skeletal code illustrates the use of the try block.
Try{
//statement that may cause an exception
}
The try block governs the statements that are enclosed within it and
defines the scope of the exception handlers associated with it. In other
words if an exception occurs within try block the appropriate
exception-handler that is associated with the try block handles
The exception. A try block must have at least one catch block that
follow it immediately. The try statement can be nested. That is a try
statement can be inside the block of another try. Each time a try
Cybotech Campus Page 3
Java Programming BT0052
statement is entered, the context of that exception is pushed on the
stack. If an inner try statement does not have a catch handler for a
particular exception the stack is unwound and the next try statement‘s
catch handlers are inspected for a match.
Example:
Class Nesttry{
public static void main(String args[])
{
try{
int a =args.length;
int b=42/a;
System.out.println(“a=”+a);
try{
if(a==1) a=a/(a-a);
if (a==2){
int c[]={1};
c[42]=99;
}
}
catch(ArrayIndexOutOfBoundsException e )
{}
}catch(ArithmeticException e)
{
System.out.println(“Divide by 0:” + e);
}
}
}

6. Write a simple GUI based program to prepare a similar interfaces as shown


below:

Ans: import javax.swing.*;


import java.awt.*;
public class FrameMaker extends JFrame
{JPanel FirstPanel,LastPanel,MainPanel;

Cybotech Campus Page 4


Java Programming BT0052
JTextField Num1,Num2,Result;
JButton Badd,Bsub,Bmul,Bdiv,Bexit;
JLabel Lnum,Lnum1,Rslt;
Color c;
public FrameMaker(String x)
{setTitle(x);
Lnum=new JLabel("Enter First Number",JLabel.CENTER);
Lnum1=new JLabel("Enter Second Number",JLabel.CENTER);
Rslt=new JLabel("The Result",JLabel.CENTER);
Num1=new JTextField(20);
Num2=new JTextField(20);
Result=new JTextField(20);
FirstPanel=new JPanel();
LastPanel=new JPanel();
FirstPanel.setLayout(new GridLayout(5,2,40,40));
LastPanel.setLayout(new GridLayout(7,1,40,20));
MainPanel=new JPanel(new BorderLayout());
MainPanel.add(FirstPanel,BorderLayout.CENTER);
MainPanel.add(LastPanel,BorderLayout.LINE_END);
setContentPane(MainPanel);
FirstPanel.add(new JLabel(""));FirstPanel.add(new JLabel(""));
FirstPanel.add(Lnum);FirstPanel.add(Num1);FirstPanel.add(Lnum1);
FirstPanel.add(Num2);FirstPanel.add(Rslt);FirstPanel.add(Result);
FirstPanel.add(new JLabel(""));FirstPanel.add(new JLabel(""));
Badd=new JButton("Add");Bsub=new JButton("Sub");
Bmul=new JButton("Multiply");Bdiv=new JButton("Divide");
Bexit=new JButton("Exit");
LastPanel.add(new JLabel(""));
LastPanel.add(Badd);LastPanel.add(Bsub);
LastPanel.add(Bmul);LastPanel.add(Bdiv);LastPanel.add(Bexit);
LastPanel.add(new JLabel(""));
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
setBounds((d.width-700)/2,(d.height-500)/2,700,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
c=new Color(120,120,240);

}public static void main(String[] s)

{FrameMaker ss=new FrameMaker("Calculator");

}
public Insets getInsets()
{
setBackground(c);
return new Insets(100,80,80,80);
}
}

Cybotech Campus Page 5


Java Programming BT0052

Cybotech Campus Page 6

You might also like