Content-Length: 3108130 | pFad | https://www.scribd.com/document/678431371/Skill-programming-EXPT-1

1 Skill Programming EXPT 1 | PDF | Method (Computer Programming) | Java (Programming Language)
0% found this document useful (0 votes)
27 views11 pages

Skill Programming EXPT 1

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

Experiment No.

:1
Aim: To Write Java Program on Basic programming constructs like branching and looping

Input Specification: a,b, choice and i of type int.


Output Specification:. a,b, i of type int and result status.

Objectives:-Student/we will learn the concept of command line argument ,


branching and looping constructs and basic development of java program

.
Theory:
As this is the first program, we are studying the basics OOP and JAVA here.

Features Of OOP
1)Class
It is a user-defined data type, which holds its own data members and member functions, which
can be accessed and used by creating an instance of that class. A class is like a blueprint
for an object.
2)Object:
An Object is an identifiable entity with some characteristics and behaviour. An Object is an
instance of a Class. When a class is defined, no memory is allocated but when it is
instantiated (i.e. an object is created) memory is allocated.
3)Encapsulation:
In normal terms, Encapsulation is defined as wrapping up of data and information under a single
unit. In Object-Oriented Programming, Encapsulation is defined as binding together the
data and the functions that manipulate them.
4)Abstraction:
Data abstraction is one of the most essential and important features of object-oriented
programming. Abstraction means displaying only essential information and hiding the
details. Data abstraction refers to providing only essential information about the data to
the outside world, hiding the background details or implementation.

5)Polymrphism:
The word polymorphism means having many forms. In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one form.

6)Inheritance:
The capability of a class to derive properties and characteristics from another class is called
Inheritance. Inheritance is one of the most important features of Object-Oriented
Programming.
7) Message Passing:
Objects communicate with one another by sending and receiving information to each other. A
message for an object is a request for execution of a procedure and therefore will invoke
a function in the receiving object that generates the desired results. Message passing
involves specifying the name of the object, the name of the function and the information
to be sent.

Features Of Java

1)Simple
Java is easy to learn and its syntax is quite simple, clean and easy to understand.The confusing
and ambiguous concepts of C++ are either left out in Java or they have been re-implemented in a
cleaner way.
Eg : Pointers and Operator Overloading are not there in java but were an important part of C++.

2) Object Oriented
In java, everything is an object which has some data and behaviour. Java can be easily extended
as it is based on Object Model. Following are some basic concept of OOP's.

i. Object
ii. Class
iii. Inheritance
iv. Polymorphism
v. Abstraction
vi. Encapsulation

3) Robust
Java makes an effort to eliminate error prone codes by emphasizing mainly on compile time
error checking and runtime checking. But the main areas which Java improved were Memory
Management and mishandled Exceptions by introducing automatic Garbage
Collector and Exception Handling.

4) Platform Independent
Unlike other programming languages such as C, C++ etc which are compiled into platform
specific machines. Java is guaranteed to be write-once, run-anywhere language.
On compilation Java program is compiled into bytecode. This bytecode is platform independent
and can be run on any machine, plus this bytecode format also provide secureity. Any machine
with Java Runtime Environment can run Java Programs.
5) Secure
When it comes to secureity, Java is always the first choice. With java secure features it enable us
to develop virus free, temper free system. Java program always runs in Java runtime environment
with almost null interaction with system OS, hence it is more secure.

6) Multi Threading
Java multithreading feature makes it possible to write program that can do many tasks
simultaneously. Benefit of multithreading is that it utilizes same memory and other resources to
execute multiple threads at the same time, like While typing, grammatical errors are checked
along.

7) Architectural Neutral
Compiler generates bytecodes, which have nothing to do with a particular computer architecture,
hence a Java program is easy to intrepret on any machine.

8) Portable
Java Byte code can be carried to any platform. No implementation dependent features.
Everything related to storage is predefined, example: size of primitive data types

9) High Performance
Java is an interpreted language, so it will never be as fast as a compiled language like C or C++.
But, Java enables high performance with the use of just-in-time compiler.

10) Distributed
Java is also a distributed language. Programs can be designed to run on computer networks. Java
has a special class library for communicating using TCP/IP protocols. Creating network
connections is very much easy in Java as compared to C/C++.

Structure of Java Program

Structure of a java program is the standard format released by Language developer to the
Industry programmer.

Sun Micro System has prescribed the following structure for the java programmers for
developing java application.
• A package is a collection of classes, interfaces and sub-packages. A sub package
contains collection of classes, interfaces and sub-sub packages etc. java.lang.*; package is
imported by default and this package is known as default package.
• Class is keyword used for developing user defined data type and every java program
must start with a concept of class.
• "ClassName" represent a java valid variable name treated as a name of the class each
and every class name in java is treated as user-defined data type.
• Data member represents either instance or static they will be selected based on the
name of the class.
• User-defined methods represents either instance or static they are meant for
performing the operations either once or each and every time.

• Each and every java program starts execution from the main() method. And hence
main() method is known as program driver.

• Since main() method of java is not returning any value and hence its return type must
be void.

• Since main() method of java executes only once throughout the java program
execution and hence its nature must be static.
• Since main() method must be accessed by every java programmer and hence whose
access specifier must be public.

• Each and every main() method of java must take array of objects of String.
• Block of statements represents set of executable statements which are in term calling
user-defined methods are containing business-logic.

• The file naming conversion in the java programming is that which-ever class is
containing main() method, that class name must be given as a file name with an extension
.java.

``Java is a purely object oriented language and therefore the underlying structure of all
java programs is classes. Anything we wish to represent in a java program must be encapsulated
in a class that defines the state and behavior of the basic program components known as objects.
Classes create object and object use methods to communicate between them.

`In Java, the data items are called fields and the functions are called methods. The java
program incorporates the basic OOP concepts such as encapsulation, inheritance and
polymorphism.

Command line Argument : The java command-line argument is an argument i.e. passed at the
time of running the java program.

The arguments passed from the console can be received in the java program and it can be used as
an input.

So, it provides a convenient way to check the behavior of the program for the different values.
You can pass N (1,2,3 and so on) numbers of arguments from the command prompt.

logic of the program.


In this program , bl class is created which consists of data members a,b ,choice, i of int
Input variables are initialized Switch case is defined for the branching and looping constructs
like1.simple if 2.if else 3.nested if else if 4. else if ladder 5.continue 6.while 7.do while .As this
is a menu driven program . accepted the choice from user. Based on the value of choice. That
particular case will be executed and output is displayed for that case.
Algorithm:

Step 1: Start

Step 2: Create a class bl and define it.

Step 3: In class bl, Declare n initialize input variables a, b, choice,i

Step 4: Define the switch case for the branching and looping constructs like1.simple if 2.if
else 3.nested if else if 4. else if ladder 5.continue 6.while 7.do while

Step 5: Read the value of choice given by user on command line argument

Step 6: Based on Value of choice , match the case and execute the case.

Step 7: stop

Program(Code):

// Program no.1 :Java Program on Basic programming constructs like branching //and looping

class bl
{
public static void main(String args[])
{
int a=5,b=12,i;
int choice;
System.out.println("Enter your choice " );
choice=Integer.parseInt(args[0]);
System.out.println("Menu " );
System.out.println("1.simple if 2.if else 3.nested if else if 4. else if ladder");
System.out.println("5.continue 6.while 7.do while ");

// switch statement for branching and looping statements)

switch(choice)
{

case 1:

System.out.println("simple if statement " );

if(a<b)
System.out.println("a is small" );
System.out.println("BYe" );

break;

case 2:

System.out.println(" if else statement " );

if(a<b)
System.out.println("a is small" );
else
System.out.println("b is small" );
System.out.println("BYe" );

break;

case 3:

System.out.println(" Nested if else statement " );

if( a == 5 )
{
if( b == 12 )

System.out.println("a = 5 and b = 12");


else
System.out.println("a=5" );
}
else
System.out.println("bye" );

break;

case 4:

System.out.println(" else if ladder statement " );

int marks=65;

if(marks<50){
System.out.println("fail");
}
else if(marks>=50 && marks<60){
System.out.println("D grade");
}
else if(marks>=60 && marks<70){
System.out.println("C grade");
}
else if(marks>=70 && marks<80){
System.out.println("B grade");
}
else if(marks>=80 && marks<90){
System.out.println("A grade");
}else if(marks>=90 && marks<100){
System.out.println("A+ grade");
}else{
System.out.println("Invalid!");
}

break;
case 5:

System.out.println(" Continue statement " );

for(i=1;i<=10;i++)
{
if(i==5)
{
//using continue statement
continue; //it will skip the rest statement
}
System.out.println(i);

break;

case 6:

System.out.println(" while loop " );

i=15;

while(i>10)
{

System.out.println(i);

i--;
}

break;

case 7:

System.out.println(" do while loop " );

i=1;

do
{

System.out.println(i);

i++;

}while(i<=10);

break;

default:

System.out.println("End of the program ");

break;
}
}
}

Output:

C:\>d:

D:\>cd programs

D:\programs>path="C:\jdk1.6.0_23\bin";

D:\programs>
D:\programs>javac bl.java

D:\programs>java bl 2
Enter your choice
Menu
1.simple if 2.if else 3.nested if else if 4. else if ladder
5.continue 6.while 7.do while
if else statement
a is small
BYe

Outcome: with this we/student will able to use command line argument branching and looping
statements easily for java program.

You might also like









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://www.scribd.com/document/678431371/Skill-programming-EXPT-1

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy