0% found this document useful (0 votes)
93 views13 pages

Lab Manual 01 (2 March 2021)

This document is a lab manual for an Object Oriented Programming class in Java at the University of Engineering and Technology, Taxila. It provides instructions on installing Java, creating Java projects and packages in Eclipse, and writing, compiling, and running basic Java programs. It includes examples of programs to print output, perform math operations, print patterns like triangles and diamonds, find prime numbers, swap variables, and take user input. The document is intended to teach students the basics of writing, compiling, and running Java programs.

Uploaded by

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

Lab Manual 01 (2 March 2021)

This document is a lab manual for an Object Oriented Programming class in Java at the University of Engineering and Technology, Taxila. It provides instructions on installing Java, creating Java projects and packages in Eclipse, and writing, compiling, and running basic Java programs. It includes examples of programs to print output, perform math operations, print patterns like triangles and diamonds, find prime numbers, swap variables, and take user input. The document is intended to teach students the basics of writing, compiling, and running Java programs.

Uploaded by

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

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Object Orienting Programming


(Java)

Lab Manual No 01

Dated:
nd
2 March, 2021 to 09th March, 2021
Semester:
Spring 2021

*For Submission Read the Instructions at the end of Lab Manual

Lab Instructor: SHEHARYAR KHAN Page 1


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

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

Creating Java Project:


After selecting the workspace, we have to create java project.
The New Java Project wizard can be used to create a new java project. There are many
ways to open this wizard:
1. By clicking on the File menu and choosing New Java Project
2. By right clicking anywhere in the Project Explorer and selecting New Java Project
Lab Instructor: SHEHARYAR KHAN Page 2
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

3. By clicking on the New button ( ) in the Tool bar and selecting Java Project

4. File->New-> Java Project


5. Enter the Project Name
6. Select the Java Runtime Environment (JRE) or leave it at the default

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

CREATE JAVA PACKAGE:


You can use the New Java Package wizard to create a Java package. The Java Package
wizard can be opened in different ways:
1. By clicking on the File menu and selecting New Package
2. By right click in the package explorer and selecting New Package

3. By clicking on the package icon which is in the tool bar( )

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.

Lab Instructor: SHEHARYAR KHAN Page 3


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

CREATE JAVA CLASS


Opening the New Java Class Wizard
You can use the New Java Class wizard to create a Java class. The Java Class wizard can be invoked in
different ways:
1. By clicking on the File menu and selecting New Class
2. By right clicking in the package explorer and selecting New Class
3. By clicking on the class drop down button and selecting class

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.

Lab Instructor: SHEHARYAR KHAN Page 4


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

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;

public class Welcome_to_uet {

public static void main(String args[ ])


{
System.out.println("Hello World!!");

}}

System.out.print command in java is same as cout in C++


Run the following programs and observe the output.

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);
}
}

Lab Instructor: SHEHARYAR KHAN Page 5


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Program # 3
class Program3
{
public static void main(String args[])
{
for(int i=0;i<10;i++)
System.out.println("Output = "+i);
}
}

Program#4 Print Triangle


public class Stars {
public static void main(String args[ ])
{
int i,j,k;
for(i=1; i<=5; i++)
{
for(j=4; j>=i; j--)
{
System.out.print(" ");
}
for(k=1; k<=(2*i-1); k++)
{
System.out.print("*");
}
System.out.println("");
}}}

Lab Instructor: SHEHARYAR KHAN Page 6


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Program#05 Print Diamond


class Stars
{
public static void main(String[] args)
{
int i, j, k;
for(i=1;i<=5;i++)
{
for(j=i;j<5;j++)
{
System.out.print(" ");
}
for(k=1;k<(i*2);k++)
{
System.out.print("*");
}
System.out.println();
}
for(i=4;i>=1;i--)
{
for(j=5;j>i;j--)
{
System.out.print(" ");
}
for(k=1;k<(i*2);k++)
{
System.out.print("*");
}
System.out.println();
}
}
}

Lab Instructor: SHEHARYAR KHAN Page 7


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Program 06 Prime Numbers


package com.helloworldjava;
public class PrimeNumber
{
public static void main (String[] args)
{
int i =0;
int num =0;
//Empty String
String primeNumbers = "";

for (i = 1; i <= 100; i++)


{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String
primeNumbers = primeNumbers + i + " ";
}
}
System.out.println("Prime numbers from 1 to 100 are :");
System.out.println(primeNumbers);
}
}
Program 07
1. public class Exercise4 {
2.
3. public static void main(String[] args) {
4. System.out.println(-5 + 8 * 6);
5. System.out.println((55+9) % 9);
6. System.out.println(20 + -3*5 / 8);
7. System.out.println(5 + 15 / 3 * 2 - 8 % 3); }

Lab Instructor: SHEHARYAR KHAN Page 8


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Program No.08 Swapping Two Numbers


public class Swapping {
public static void main(String[] args){

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);
}

How to get input from user in Java


Java Scanner Class
Java Scanner class allows the user to take input from the console. It belongs
to java.util package. It is used to read the input of primitive types like int, double,
long, short, float, and byte. It is the easiest way to read input in Java program.

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.

Methods of Java Scanner Class


Java Scanner class provides the following methods to read different primitives types:

Lab Instructor: SHEHARYAR KHAN Page 9


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Method Description

int nextInt() It is used to scan the next token of the input as an


integer.

float nextFloat() It is used to scan the next token of the input as a


float.

double nextDouble() It is used to scan the next token of the input as a


double.

byte nextByte() It is used to scan the next token of the input as a


byte.

String nextLine() Advances this scanner past the current line.

boolean nextBoolean() It is used to scan the next token of the input into a
boolean value.

long nextLong() It is used to scan the next token of the input as a


long.

short nextShort() It is used to scan the next token of the input as a


Short.

BigInteger It is used to scan the next token of the input as a


nextBigInteger() BigInteger.

BigDecimal It is used to scan the next token of the input as a


nextBigDecimal() BigDecimal.

Example of integer input from user

The following example allows user to read an integer form the System.in.

Lab Instructor: SHEHARYAR KHAN Page 10


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Program No.09 Input Example


1. import java.util.*;
2. class UserInputDemo
3. {
4. public static void main(String[] args)
5. {
6. Scanner sc= new Scanner(System.in); //System.in is a standard input stream

7. System.out.print("Enter first number- ");


8. int a= sc.nextInt();
9. System.out.print("Enter second number- ");
10. int b= sc.nextInt();
11. System.out.print("Enter third number- ");
12. int c= sc.nextInt();
13. int d=a+b+c;
14. System.out.println("Total= " +d);
15. }

Program 10 of String Input from user

Let's see another example, in which we have taken string input.

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. }

Lab Instructor: SHEHARYAR KHAN Page 11


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Drill Tasks

Write a Program that Display the following Shape?


******************
***************
**********
*******
****
**
*

Write a Program that Display the following output?


123456
12345
1234
123
12
1

Write a program that print the following series


1 4 7 10 40 using for loops

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

Lab Instructor: SHEHARYAR KHAN Page 12


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

loop and For loop?

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

Contain Screen Shots

Lab Instructor: SHEHARYAR KHAN Page 13

You might also like