Você está na página 1de 6

Practical No 1

Object:

TO BECOME FAMILIAR WITH WRITING A SIMPLE JAVA PROGRAM & DIFFERENT OPTIONS OF COMPILING & EXECUTING IT , & OTHER BASIC CONCEPTS OF JAVA PROGRAMMING LANGUAGE.

Compilation and Runtime of Program: Java uses two translators a compiler and an interpreter. First compiler reads the source file (that must be with .java extension) that contains one or more classes and then (after compiling) it produces file with .class extension i.e. class file that contains the bytecode (also called intermediate code or virtual code) that can only be read by Java interpreter. Then Java interpreter also called JVM (Java Virtual Machine) reads that class file and executes the program. For compiling you write the command javac followed by filename.Java.

C:\java\bin>javac Example.java For executing the code you write the command java followed by class name as Example C:\java\bin>java Example

Simple Program class Example { public static void main (String ar[]) { System.out.println(Noman Ahmed Soomro); }}

BASIC CONCEPTS OF JAVA PROGRAMMING LANGUAGE. If you've never used an object-oriented programming language before, you'll need to learn a few basic concepts before you can begin writing any code. This lesson will introduce you to objects, classes, inheritance, interfaces, and packages. Each discussion focuses on how these concepts relate to the real world, while simultaneously providing an introduction to the syntax of the Java programming language. What Is an Object? An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life. This lesson explains how state and behavior are represented within an object, introduces the concept of data encapsulation, and explains the benefits of designing your software in this manner. What Is a Class? A class is a blueprint or prototype from which objects are created. This section defines a class that models the state and behavior of a real-world object. It intentionally focuses on the basics, showing how even a simple class can cleanly model state and behavior. What Is Inheritance? Inheritance provides a powerful and natural mechanism for organizing and structuring your software. This section explains how classes inherit state and behavior from their superclasses, and explains how to derive one class from another using the simple syntax provided by the Java programming language. What Is an Interface? An interface is a contract between a class and the outside world. When a class implements an interface, it promises to provide the behavior published by that interface. This section defines a simple interface and explains the necessary changes for any class that implements it. What Is a Package? A package is a namespace for organizing classes and interfaces in a logical manner. Placing your code into packages makes large software projects easier to manage. This section explains why this is useful, and introduces you to the Application Programming Interface (API) provided by the Java platform.

Lab Task
1. Draw the directory structure of JDK.

Development Files and Directories


This section describes the most important files and directories required to develop applications for the Java platform.
jdk1.6.0 ___________|____________________ | | | bin lib jre | | __________|_____________________ java.exe tools.jar | | javac.exe dt.jar bin lib javap.exe _____|____ __________ ________|_______ ________ ________ javah.exe | | | | | | | | javadoc.exe java.exe client server rt.jar ext security applet fonts java.dll | | charsets.jar | awt.dll jvm.dll jvm.dll localedata.jar

Assuming the JDK software is installed at /jdk1.5.0, here are some of the most important directories:
/jdk1.5.0

The root directory of the JDK software installation.Contains copyright, license, and README files. Also contains src.jar, the archive of source code for the Java 2 platform.
/jdk1.5.0/bin

The executables for all the development tools contained in the Java 2 JDK. The PATH environment variable should contain an entry for this directory. For more information on the tools, see the JDK Tools.
/jdk1.5.0/lib

Files used by the development tools. Includes tools.jar, which contains noncore classes for support of the tools and utilities in the JDK. Also includes dt.jar, the DesignTime archive of BeanInfo files that tell interactive development environments (IDE's) how to display the Java components and how to let the developer customize them for the application.
/jdk1.5.0/jre

The root directory of the Java runtime environment used by the JDK development tools. The runtime environment is an implementation of the Java 2 platform. This is the directory referred to by the java.home system property.
/jdk1.5.0/jre/bin

Executable files for tools and libraries used by the Java platform. The executable files are identical to files in /jdk1.5.0/bin. Thejava launcher tool serves as an application launcher, in place of the old jre tool that shipped with 1.1 versions of the JDK software. This directory does not need to be in the PATH environment variable.
/jdk1.5.0/jre/lib

Code libraries, property settings, and resource files used by the Java runtime environment. Includes:

-- the bootstrap classes (the RunTime classes that comprise the Java platform's core API).
rt.jar

charsets.jar

-- character-conversion classes.

Aside from the ext subdirectory (described below) there are several additional resource subdirectories not described here.
/jdk1.5.0/jre/lib/ext

Default installation directory for Extensions to the Java platform. This is where the JavaHelp jar file goes when it is installed, for example.

localedata.jar

-- locale data for java.text and java.util.

/jdk1.5.0/jre/lib/security

Contains files used for security management. These include the security policy (java.policy) and security properties (java.security) files.
/jdk1.5.0/jre/lib/i386/client

Contains the .so file used by the Java HotSpot Client Virtual Machine, which is implemented with Java HotSpot technology. This is the default VM.
TM

/jdk1.5.0/jre/lib/i386/server

Contains the
/jdk1.5.0/jre/lib/applet

.so

file used by the Java HotSpot Server Virtual Machine.

Jar files containing support classes for applets can be placed in the lib/applet/ directory. This reduces startup time for large applets by allowing applet classes to be pre-loaded from teh local file system by the applet class loader, providing the same protections as if they had been downloaded over the net.
/jdk1.5.0/jre/lib/fonts

Font files for use by platform.

2. Write down a simple Java Program that prints your Complete name(first, middle and last
name) and compile and run it. For the second time modify the program to give your name at command line arguments at run time.
class Name{ public static void main(String args[]){ System.out.println("Noman Ahmed Soomro"); System.out.println(args[0]+" "+args[1]+" "+args[2]); }}

3. Write a console program to declare and initialize a variable of type byte to 1, and
successively multiply it by 2 and display the result for 8 times. Explain the reason for last display.
public class Test1 { public static void main (String [] args) { byte var = 1; for (int i = 0; i < 8; i++) { System.out.println (i *= 2); } } }

4. Write a program to declare and initialize a double variable with some value such as
1234.5678. Then retrieve the integral part and store it in the variable of type long and first four digits of the fractional parts and store it in an integer of type short. Display the value of double variable by outputting the value of two integer variables.
class Task{ public static void main(String args[]){ double a=1234.5678; long b=(long)a; double c= a-b; short d=(short)(c*10000); System.out.println("The Integer Part is "+b); System.out.println("& Fractional Part is "+d); }}

5. Declare any two variables of integer type lets say var1 and var2 one static and another
non-static print their initials values. Then print the result see if any compile or run time error comes explain the reason. class Task{ static int var1=100; int var2=1000; public static void main(String args[]){ System.out.print("the value of var1 is:"+var1); System.out.print("the value of var2 is:"+var2); }}

One compile time error Non-Static variable var2 can not be referenced from a context.

Você também pode gostar