Java Lab Programs JNTUA
Java Lab Programs JNTUA
EXPERIMENT-1
1.1 OBJECTIVE:
Preparing and practice – Installation of Java software, study of any Integrated
development environment, sample programs on operator precedence and associativity,
class and package concept, scope concept, control structures, constructors and
destructors. Learn to compile, debug and execute java programs.
IMPORTANCE OF JAVA
Java is great programming language for the development of enterprise grade applications. This
programming Language is evolved from a language named Oak. Oak was developed in the early
nineties at Sun Microsystems as a platform-independent language aimed at allowing entertainment
appliances such as video game consoles and VCRs to communicate. Oak was first slated to appear
in television set-top boxes designed to provide video-on-demand services. Oak was unsuccessful so
in 1995 Sun changed the name to Java and modified the language to take advantage of the
burgeoning World Wide Web. Java is an object-oriented language, and this is very similar to C++.
Java Programming Language is simplified to eliminate language features that cause common
programming errors. Java source code files are compiled into a format called byte code, which can
then be executed by a Java interpreter.
What is JDK (Java Development Kit)
JDK is a software development program provided by sun Microsystems. Java Development Kit or
JDK comes in various versions and can be downloaded free from the sun Microsystems. JVM
compiler, debugger and other tools are used with JDK for developing java based application & java
applets. So make sure that your JVM compiler & JDK versions are same. JDK also known as Java 2
Platform, That comes in three editions J2ME, J2SE & J2EE. If you are beginner or learning Java
then start by downloading J2SE.Acronyms:
PROGRAM:
class Precedence {
public static void main(String[ ] args)
{
System.out.println( 3 + 3 * 2 );
System.out.println( 3 * 3 - 2 );
System.out.println( 3 * 3 / 2 );
System.out.println("--");
System.out.println( 1 * 1 + 1 * 1);
System.out.println( 1 + 1 / 1 - 1);
System.out.println( 3 * 3 / 2 + 2);
System.out.println("--");
int x = 1;
System.out.println( x++ + x++ * --x );
x = 1;
System.out.println( x << 1 * 2 >> 1);
x = 0xf;
System.out.println( 0xf & 0x5 | 0xa );
}
}
OUTPUT:
B. Program To Find The Area Of A Rectangle Using Constructors With And Without Parameters
PROGRAM:
class rec
{
double l,b;
rec()
{
l=10;
b=20;
}
rec(double le,double br)
{
l=le;
b=br;
}
double area()
{
return(l*b);
}
}
class constructor
{
public static void main(String args[])
{
rec r1=new rec();
rec r2=new rec(11.0,10.0);
System.out.println("AREA OF RECTANGLE IS= "+r1.area());
System.out.println("AREA OF RECTANGLE IS= "+r2.area());
}
}
OUTPUT:
PROGRAM:
class switch1
{
public static void main(String args[])
{
int month=10;
String season;
switch(month)
{
case 12:
case 1:
case 2: season="winter";
break;
case 3:
case 4:
case 5: season="spring";
break;
case 6:
case 7:
case 8: season="summer";
break;
case 9:
case 10:
case 11:season="autum";
break;
default:season="bogusone";
}
System.out.println("october is in "+season);
}
}
OUTPUT:
october is in autum
class SumDigits
{
public static void main(String args[])
{
int n, res;
Scanner scan= new Scanner(System.in);
System.out.println("Please Enter No.: ");
n=scan.nextInt();
while(n>0)
{
res=n%10;
n=n/10;
sum+=res; //sum=sum+res;
}
System.out.println("Sum: " + sum);
}
}
OUTPUT:
PROGRAM:
class Sum10
{
public static void main(String args[])
{
int n, sum=0;
for(n=1;n<=10;n++)
{
sum+=n; //or sum=sum+n;
}
System.out.println(sum);
}
}
OUTPUT:
55
F. To write a Java program to implement the concept of importing classes from user defined
package and creating packages.
PROGRAM:
package p1;
public class Student
{
int regno;
String name;
public void getdata(int r,String s)
{
regno=r;
name=s;
}
public void putdata()
{
System.out.println("regno = " +regno);
System.out.println("name = " + name);
}
}
/* Source code of the main function under C:\jdk1.6.0_26\bin>edit StudentTest.java */
import p1.*;
class StudentTest
{
public static void main(String arg[])
{
student s=new student();
s.getdata(123,"xyz");
s.putdata();
}
}
OUTPUT
C:\jdk1.6.0_26\bin>javac p1\Student.java
C:\jdk1.6.0_26\bin>javac StudentTest.java
C:\jdk1.6.0_26\bin>java StudentTest
regno = 123
name = xyz
VIVA QUESTIONS
1. What is object?
2. What is class?
3. What is object oriented programming?
4. Differentiate C++ vs. Java?
5. What is Encapsulation?
6. What is JVM ? Why is Java called the “Platform Independent Programming Language”
EXPERIMENT – 2
OBJECTIVE: Write Java program(s) on use of inheritance, preventing inheritance using
final, abstract classes.
PROGRAM:
class A {
int i; // public by default
private int j; // private to A
void setij(int x, int y) {
i = x;
j = y;
}
}
// A's j is not accessible here.
class B extends A {
int total;
void sum() {
total = i + j; // ERROR, j is not accessible here
}
}
class Access {
public static void main(String args[]) {
B subOb = new B();
subOb.setij(10, 12);
subOb.sum();
System.out.println("Total is " + subOb.total);
}
}
OUTPUT:
B) PREVENTING INHERITANCE USING FINAL
PROGRAM:
OUTPUT:
PROGRAM:
1. What does the “static” keyword mean? Can you override private or static method in
Java ?
2. Can you access non static variable in static context?
3. What is Function Overriding and Overloading in Java?
4. Does Java support multiple inheritance?
5. What is the difference between an Interface and an Abstract class?