Вы находитесь на странице: 1из 10

JAVA PROGRAMMING LABORATORY

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:

JDK Java Development Kit


JVM Java virtual machine Download JDK
You can download JDK from www.javasoft.com/j2se Latest version of JDK jdk1.8.0_15
http://java.sun.com/j2se/Downloads/index.jsp
Program Structure of java in Java Language:
The fundamental structure of any Java program should look like:
[package declarations]
[import statements]
[class declaration]
An example is given below:
package abc;
import java.lang;
class Demo {
public static void main(String[] args) {
Sytem.out.println("Hello! World");
}
}
//The file containing this class must be named Demo.java
How to set the path for Java
1. Go to MyComputer properties->Advanced ->Environment Variables.
2. Find the path in SystemVariables and Edit it to your Jdk/bin folder.
3. Now your path is set and you can run the programs from anywhere.
A. Program to evaluate mathematical expression based on OPERATOR PRECEDENCE

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:

AREA OF RECTANGLE IS= 200.00


AREA OF RECTANGLE IS=110.00

C. To write a sample program using SWITCH - CASE

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

D. To write a sample program using WHILE STATEMENT


PROGRAM:

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:

Please Enter No.:


123
Sum:6

E. To write a sample program using FOR STATEMENT

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.

A) Member Access and Inheritance

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:

final class XYZ


{
}
class ABC extends XYZ
{
void demo()
{
System.out.println("My Method");
}
public static void main(String args[]){
ABC obj= new ABC();
obj.demo();
}
}

OUTPUT:

C) PREVENTING INHERITANCE USING ABSTRACT CLASSES

PROGRAM:

// Using abstract methods and classes.


abstract class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
// area is now an abstract method
abstract double area();
}
class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure {
Triangle(double a, double b) {
super(a, b);
}
double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
class AbstractAreas {
public static void main(String args[]) {
// Figure f = new Figure(10, 10); // illegal now
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref; // this is OK, no object is created
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
}
}
OUTPUT:
VIVA QUESTIONS:

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?

Вам также может понравиться