0% found this document useful (0 votes)
9 views7 pages

Lab 03

Uploaded by

skbhatti003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Lab 03

Uploaded by

skbhatti003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

2nd- Lab-03: Implementing Different Concepts of Java

Semeste

Lab Manual

OOP

Dr. M. Bilal Shahnawaz Lab-03 1


2nd- Lab-03: Implementing Different Concepts of Java
Semeste

Laboratory 03: Implementing Different Concepts of Java in Eclipse


Lab Objectives: After this lab, the students should be able to

1. Use command line arguments in Java


2. Take input from user and display output

1. Simple example of command line arguments


Example 1:
In this example, we are receiving only one argument and printing it. To run this java
program, you must pass at least one argument from the command prompt. Write this simple
code in notepad and save in the workspace.
public class CommandLineExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Your first argument is: "+args[0]);
}
}
Compile by: javac CommandLineExample.java
Run by: java CommandLineExample Bilal
Try to run without passing argument.
Example 2:
Below is the example code that displays the command line arguments.
public class showArgs {
public static void main(String[] args)
{
for(int i=0; i<args.length; i++)
{
System.out.println("Argument " + i + " = " + args[i]);
}
}
}

Giving command line arguments while running the java code:

You can give any number of arguments after giving the .class file name separated by space. Space is
used as a delimiter to specify the number of command line arguments to the interpreter.

Example:

In order to run the above code mentioned:

We will compile the code using

Javac showArgs.java

Run the .class file as:

Java showArgs
Dr. M. Bilal Shahnawaz Lab-03 1
2nd- Lab-03: Implementing Different Concepts of Java
Semeste
Output: code will execute without any output, as no command line arguments are passed in it.

To pass the command line arguments, we will re-run the code as follows:

Java showArgs hello world

Argument 0=hello

Argument 1=world

Java showArgs “hello world” “my first program”

Argument 0=hello world

Argument 1=my first program

By default Arguments are passed as a String array to the main method of a class.

Try to run the following code 3 times. Firstly by giving two arguments as input during run time.
Secondly by giving three arguments and for the last time by giving four number of arguments
and compare the outputs.

public class showArgs {


public static void main(String[] args)
{
for(int i=0; i<3; i++)
{
System.out.println("Argument " + i + " = " + args[i]);
}
}
}

Setting command line arguments in Eclipse

Right click on class in Project -> Run As -> Run Configurations.

The following window opens. Give arguments separated by space as shown below.

Then select Run.

Dr. M. Bilal Shahnawaz Lab-03 2


2nd- Lab-03: Implementing Different Concepts of Java
Semeste

Output on Console:
Argument 0 = OOP
Argument 1 = lab
Argument 2 = 3
Argument 3 = Numl

2. Reading Input from Console: Scanner input


To read input from console we can use the scanner class. Java uses System.out to refer to the
standard output device and System.in to the standard input device. To perform console output,
you simply use the println method to display a primitive value or a string to the console. Console input
is not directly supported in Java, but you can use the Scanner class to create an object to read input
from System.in, as follows:
Scanner input = new Scanner(System.in);

The syntax new Scanner(System.in) creates an object of the Scanner type. The syntax Scanner
input declares that input is a variable whose type is Scanner. The whole line Scanner input =
newScanner(System.in) creates a Scanner object and assigns its reference to the variable input. An
object may invoke its methods. To invoke a method on an object is to ask the object to perform a task.

Dr. M. Bilal Shahnawaz Lab-03 3


2nd- Lab-03: Implementing Different Concepts of Java
Semeste
Methods for Scanner Objects

Example

This program demonstrates the use of Scanner Class.


import java.util.*;

public class UserInputDemo {


public static void main(String[] args)
{
Scanner sc= new Scanner(System.in); //System.in is a
standard input stream
System.out.print("Enter first number- ");
int a= sc.nextInt();
System.out.print("Enter second number- ");
int b= sc.nextInt();
System.out.print("Enter third number- ");
int c= sc.nextInt();
int d=a+b+c;
System.out.println("Total= " +d);

Example 2: Example of String Input from user


import java.util.*;
public class UserStringInputDemo {

public static void main(String[] args)


{
Scanner sc= new Scanner(System.in); //System.in is a standard
input stream
System.out.print("Enter a string: ");
String str= sc.nextLine(); //reads string
System.out.print("You have entered: "+str);

Dr. M. Bilal Shahnawaz Lab-03 4


2nd- Lab-03: Implementing Different Concepts of Java
Semeste
LAB TASKS Total Marks: 10
Task 0:

Pass username and password in command line arguments and display them on the console.

Task 1:

Take your name and registration number as input from user and generate the following
output. (Note: Output must be similar as shown here)

Task 2:

Take radius of circle from user and calculate area and circumference of circle.

Task 3:

Write a program to convert temperature in Fahrenheit to temperature in Celsius

C=(F-32)*5/9; // temperature in Fahrenheit will be entered by user

Task 4:

Take three numbers from user and calculate their average. Output should be like this.

Task 5:

Write a program to convert kilometers into meters where user will input distance in kilometer
and the program will convert distance into meters.

Dr. M. Bilal Shahnawaz Lab-03 5

You might also like