Lab Manual 01 (2 March 2021)
Lab Manual 01 (2 March 2021)
Lab Manual No 01
Dated:
nd
2 March, 2021 to 09th March, 2021
Semester:
Spring 2021
Introduction
• General syntax of java program.
• How to compile the java program.
• How to run the java program.
GENERAL SYNTAX:-
Name of class and java file
class Class_name
{
Key word public static void main(String abc[])
{
Code here
}
} main function where the begins to run
To write, compile and run the java program follow these steps
1) Install JDK8.1 on your computer (in C drive). You can download this directly from the oracle
Web site at http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-
2133151.html
2) Install Eclipse Neon from website https://www.eclipse.org/downloads/?
3) After Installation Double click on Eclipse IDE.
4) When eclipse starts up for the first time it prompts you for the location of the workspace
folder. All your data will be stored in the workspace folder. You can accept the default or
choose a new location
3. By clicking on the New button ( ) in the Tool bar and selecting Java Project
7. Select the Project Layout which determines whether there would be a separate folder for the
source codes and class files. The recommended option is to create separate folders for sources
and class files
If you are creating a sub package, before opening the Java Package wizard select the parent package so
that name field can have a default value in it.
Before bringing up the New Java Class wizard, if possible, select the package in which the class is to be
created so that the wizard can automatically fill in the package name for you.
Now let us learn how to code, compile and run a Java program in Eclipse. First copy and paste the
following method into the Welcome_to_uet class definition:
package com.helloworldjava;
}}
Program # 2
class Program2
{
public static void main(String args[])
{
int a,b,c;
a=2;
b=3;
c=a+b;
System.out.println("Sum of two numbers = "+c);
}
}
Program # 3
class Program3
{
public static void main(String args[])
{
for(int i=0;i<10;i++)
System.out.println("Output = "+i);
}
}
int a=10,b=20,temp;
System.out.println("Befor Swapping");
System.out.println("a = "+a);
System.out.println("b = "+b);
temp=a;
a=b;
b=temp;
System.out.println("after Swapping");
System.out.println("a = "+a);
System.out.println("b = "+b);
}
Syntax
1. Scanner sc=new Scanner(System.in);
The above statement creates a constructor of the Scanner class having System.inM as
an argument. It means it is going to read from the standard input stream of the
program. The java.util package should be import while using Scanner class.It also
converts the Bytes (from the input stream) into characters using the platform's default
charset.
Method Description
boolean nextBoolean() It is used to scan the next token of the input into a
boolean value.
The following example allows user to read an integer form the System.in.
1. import java.util.*;
2. class UserInputDemo1
3. { public static void main(String[] args)
4. { Scanner sc= new Scanner(System.in); //System.in is a standard input
5. System.out.print("Enter a string: ");
6. String str= sc.nextLine(); //reads string
7. System.out.print("You have entered: "+str);
8. }
9. }
Drill Tasks
Question No.04
Write a C++ Program that input the height of Triangle and display the triangle of
character?
Output
A
BC
DEF
GHIJ
KLMNO
Question No.05
Write a Program that Display First five number with their Square using while
Question No.06
Write a Program that Display Back Counting from 100 to 1 using Do... While
using For loop and While Loop
Question No.07
Write a Program that Swap two number using three variable and display after swapping?
Question No.08
Write a program to take two integer inputs from user and print sum and product of them.
Question No.09
Take name, roll number and field of interest from user and print in the format below :Hey,
my name is xyz and my roll number is xyz. My field of interest are xyz.
Question No.10
Take side of a square from user and print area and perimeter of it.
Question No.11
Take 3 inputs from user and check :
all are equal
any of two are equal
( use && || )
Instructions
• Run all 10 Example
• Write Java Code for above 11 Problems
• Submit your Work in PDF In below Format
• Late submission Is not allowed (you have only 4 Hours to Submit Your Report after Lab)
• Absent from Lab can get Zero Marks
Code
--------------------------------------------
-------------------------------------------
Output