0% found this document useful (0 votes)
61 views24 pages

Oop I - Lab Manual

Object oriented programming Oop Oop Oop

Uploaded by

Jaypalsinh Rana
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)
61 views24 pages

Oop I - Lab Manual

Object oriented programming Oop Oop Oop

Uploaded by

Jaypalsinh Rana
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/ 24

LABORATORY MANUAL

OBJECT ORIENTED PROGRAMMING-I


SUBJECT CODE: 3140705
COMPUTER SCIENCE ENGINEERING
DEPARTMENT
B.E. 4 SEMESTER
TH

NAME: _______________________________________________

ENROLLMENT NO: ______________________________________

BATCH NO: ____________________________________________

YEAR: ________________________________________________

Amiraj College of Engineering and Technology

Nr.Tata Nano Plant, Khoraj, Sanand, Ahmedabad.


Amiraj College of Engineering and Technology

Nr.Tata Nano Plant, Khoraj, Sanand, Ahmedabad.

CERTIFICATE

This is to certify that Mr. / Ms. ______________________________________


Of class_________________Enrolment No__________________________has
Satisfactorily completed the course in
____________________________________as by the Gujarat Technological
University for ____ Year (B.E.) semester___ of Computer Science Engineering
in the Academic year_____.

Date of Submission:-

Faculty Name and Signature Head of Department


PROF.NENSI KANSAGARA (COMPUTER)
COMPUTER SCIENCE
ENGINEERING DEPARTMENT
B.E. 4th SEMESTER
SUBJECT: OBJECT ORIENTED PROGRAMMING-I
SUBJECT CODE: 3140705
List Of Experiment

Sr. Date of Date of


Title Sign Remark
No. Performance submission

Write a Program that displays


1.
Welcome to Java, Learning Java
Now and Programming is fun.

2 Write a program that reads a


number in meters, converts it to
feet, and displays the result.

3 Write a program that prompts


the user to enter a letter and
check whether a letter is a
vowel or constant.
Write a test program that
4
prompts the user to enter ten
numbers, invoke a method to
reverse the numbers, display
the numbers.
5 Assume a vehicle plate number
consists of three uppercase letters
followed by four digits. Write a
program to generate a plate
number
6
Write a generic method
that returns the minimum
elements in a two
dimensional array.
7
Write a program that moves a
circle up, down, left or right
using arrow keys.
8 Write a GUI program that use
button to move the message to
the left and right and use the
radio button to change the
color for the message
displayed
9 Define MYPriorityQueue class
that extends Priority Queue to
implement the Cloneable
interface and implement the
clone() method to clone a priority
queue.
10
Write a program that reads
words from a text file and
displays all the non-duplicate
words in descending order. The
text file is passed as a command-
line argument.
PRACTICAL -1

OBJECTIVE: Write a Program that displays Welcome to Java,


Learning Java Now and Programming is fun.
PROGRAM:

public class fun


{
public static void main(String[] args)
{
System.out.print("Welcome to Java, ");
System.out.print("Learning Java Now and Programming is
fun");
}
}

OUTPUT:-

Welcome to Java, Learning Java Now and Programming is fun


PRACTICAL -2

OBJECTIVE: Write a program that reads a number in meters,


converts it to feet, and displays the result.
PROGRAM:
import
java.util.Scanner;
class
MeterToFeetConv
{
public static void main(String []args)
{
Scanner s = new
Scanner(System.in); double n;
System.out.println("Enter The Value as
Meter: "); n = s.nextInt();
System.out.println("Ans
is:");
System.out.println(n/0.30
48); s.close();
}
}

OUTPUT:-

Enter the Value as Meter: 10


Ans is: 32.808398950131235
PRACTICAL -3

OBJECTIVE: Write a program that prompts the user to enter a


letter and check whether a letter is a vowel or constant.
PROGRAM:
import
java.util.Scanner;
import
java.util.ArrayList;
import java. util. List;
class VovCon {
public static void main(String[]
args) { Scanner s = new
Scanner(System.in); String str;
int count = 0;
System.out.println("Enetr a single
Alphabet : "); str = s.nextLine();
String cs;
cs = str.toUpperCase();
Character ch = new Character(cs.charAt(0));
List<Character> vowels = new
ArrayList<Character>(); vowels.add('A');
vowels.add('E');
vowels.add('I');
vowels.add('O');
vowels.add('U');
for(int j=0 ; j < str.length() ;
j++) {
if(vowels.contains(ch)) {
count = count + 1;
}

}
if(count == 1){
System.out.println("Vow
el");
}
else {
System.out.println("Consona
nt");
}

s.close();
}
}

OUTPUT:-

Enter a single Alphabet: a


Vowel
PRACTICAL -4

OBJECTIVE: Write a test program that prompts the user to


enter ten numbers, invoke a method to reverse the numbers,
display the numbers.
PROGRAM:
import java.util.*;
class ReverseNurnDemo
{

public static void main(String args[])


{

Scanner input = new


Scanner(System.in); Int[] a = new
int[10]; Systemout.printin("Enter
ten numbers :"); for(int i = 0;i
<a.length; i+ +)
a[i] =
input.nextInt();
reverse(a);
System.out.println("The numbers in reverse order
are...''); for(int i = 0; i<a.length; i+ +)
Systern.out.println(" " +a[i1);
}
public static void reverse(int[] list)
{

int temp;
for(int i = 0, j = list.length - 1; i<list.length/2; i+ +, j--)
{
temp =
list[i];
list[i] =
list[j]

list[j] = temp;
}
}
}

OUTPUT:-

Enter ten numbers:

1
2
3
4
5
6
7

8
9
10

The numbers in reverse order are...


10987654321
PRACTICAL -5

OBJECTIVE: Assume a vehicle plate number consists of three


uppercase letters followed by four digits. Write a program to
generate a plate number.
PROGRAM:
import java.util.*;
class NumberPlateDemo
{

public static void main(String args[])


{
System.out.println("The Plate munber is: ");
String alphabet -= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(int i=0; i<3; i++)
{

char ch =
alphabet.charAt((int)(Math.random() * 10));
System.out.println(ch);
}

for(int j=0; j<4; j++)


{ int num = (int)(Math.random()
* 10); System.out.println(num);
}
}
}

OUTPUT:-
The Plate number is: CED5032
The Plate number is: CDI1441
PRACTICAL -6

OBJECTIVE: Write a generic method that returns the minimum


elements in a two dimensional array
PROGRAM:
import java.util.*;
class
GenericMinDemo
{

public static void main(String[]args)


{

Integer111] a = new
Integer[3][3]; int value = 0;
Scanner input = new Scanner(System.in);
System.out.println("Enter the elements in 3.3
matrix"); for (int i = 0; i < a.length; i++)
{

for (int j = 0; j < anlength; j+ +)


{

value =
input.nextInt();
a[i][j] = value;
}
}

System.out.println("Minimum value is = "+min(a));


}
//Generic method to find minimum value from 2D
array public static <E extends Comparable <E > >
E min(E[][]list)
{

E min = list[0][0];

for (E[] elements : list)


{

for (E element : elements)


if (element.compareTo(min) < 0)
{

min = element;
}
}
}

return min;
}
}

OUTPUT:-

Enter the elements in 3.3 matrix


123
456
789
Minimum value is = 1
PRACTICAL -7

OBJECTIVE: Write a program that moves a circle up, down, left


or right using arrow keys.
PROGRAM:
package myjav-abcapplication;
import
javafx.application.Application;
import
javafx,event.Eventliandler;
importjavafx.scene.Group;
import javafx,scene,Scene;
import
javatx.scene.input.MouseEvent;
import javafx.scene.shape.Circle;
import ja.vabc.scenepaint.Color;
import javatg,scerie.text.Text;

import javeix.stage, Stage;

public class MyJav FXApplication extends Application {

@Override
public void start(Stage
primaryStage) { Circle circle
new Circle(50,50,20);
circle.setFill(Color,RED);
circle.setStroke(Color.BLACK);
Text text = new Text(20,20,''Use arrow keys to move
the circle"); Group root = new Group(text,circle);
Scene scene=new Scene(root,400,200);

//Handling the keyboard


event scene.
setOnKeyPressed (e- > {
switch(e.getCode())
{

case
DOWN:circle.setCenterY(circle.getCenterY0
+10); break;
case
UP:circle.setCenterY(circle.getCenterY0-
10); break;
case
LEFT:circle.setCenterX(circle.getCenterX0
} -10); break;
}) case
;
RIGHT:circle.setCenterX(circle,geteenterX0
+10); break;

primaryStage.setScene(scene);
primaryStage.setTitle("Move Circle Using
Arrow Keys"); primaryStage.show();
}

public static void main(String[]


args) { launch(args);
}
}
OUTPUT:-
PRACTICAL -8

OBJECTIVE: Write a GUI program that use button to move the


message to the left and right and use the radio button to change
the color for the message displayed.
PROGRAM:
package myjavafxapplication;
import
javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import
javafx.scene.control.RadioButton;
import
javafx.scene.control.ToggleGroup;
import
javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import
javafx.scene.layout.Pane;
import
javafx.scene.paint.Color;
import
javafx.scene.text.Text;
import javafx.stage.Stage;
public class MyJavaFXApplication extends Application {
public Text text = new Text(100,100,"JavaFX is wonderful
Programming language"); @Override
Public void start(Stage
primaryStage) { HBox hBox
LRbuttons = new HBox(20);
Button left = new Button(Left");
Button right = new
Button("Right");
hBoxLRbuttons.getChildrenaaddAll(left,right);

HBox hBox_ColorRadioButtons = new HBox(20);

RadioButton red = new


RadioButton("RED"); RadioButton blue
= new RadioButton("BLUE");
RadioButton green = new RadioButton("GREEN");
hBox_ColorRadioButtons.getChildren().addAll(red,bl
ue,green);
//radio button is toggled no that only one is active at
a time ToggleGroup group = new ToggleGroup();
red.setToggleGroup(group);
blue.setToggleGroup(grou
p);
green.setToggleGroup(gro
up);

Pane Pane Message = new


Pane();
PaneMessage.getChildren().add(
text);

BorderPane borderPane = new BorderPane();


borderPane. setCenter(Pane_Message);
borderPane.setTop(hBox_ColorRaclioB
uttons);
borderPane.setBottom(hBox_LRbuttons
);
//Handling the button event
left .setOnAction(e - > text. setX(text.
getX0- 10)); right.setOnAction(e-
>text.setX(text.getX()+10));
red.setOnAction(e-> {
if(red.isSelected())
{

text.setFill(Color.RED);
}
});

blue.setOnAction(e
-> {
if(blue.isSelected()
)
{

text.setFill(Color.BLUE);
}
});

green.setOnAction(e- >
{
if(green.isSelecte
d())
{
text.setFill(Color.GREEN);
}
});
Scene scene =new
Scene(borderPane,400,200);
primaryStage.setScene(scene);
primaryStage.setTitle("Arrow Button and Color Radio
Button Demo"); primaryStage.show();
}
public static void main(String[]
args) { launch(args);
}
}

OUTPUT:-
PRACTICAL -9

OBJECTIVE: Define MYPriorityQueue class that extends


Priority Queue to implement the Cloneable interface and
implement the clone() method to clone a priority queue.
PROGRAM:
Step 1: We will first define the MyPriorityQueue class in a
separate Java file import java.util.*;
public class MyPriorityQueue<E> extends PriorityQueue<E> implements Cloneable
{

@Override /** Override the protected clone method defined in the


Object class */ Public MyPriorityQueue<E> clone() throws
CloneNotSupportedException
{

MyPriorityQueue<E> temp = new


MyPriorityQueue<>();
temp.addAll((MyPriorityQueue<E>)super.clone
()); return temp;
}
}

Step 2: Now, we will implement the above created MyPriorityQueue class.


Following is a driver program that invokes the MyPriorityQueue class for
implementing the cloneable interface and implement the clone() method.
class PriorityQDemo
{

public static void main(String[] args)throws


CloneNotSupportedException MyPriorityQueue<Integer> q1 =
new MyPriorityQueue<>();
q1.offer(10);
q1.offer(20);
q1.offer(30);
MyPriorityQueue<Integer> q2 = q1.clone();
System.out . println("Queue
1: "); while (q1.size() > 0)
{

System.out.println(q1.remove() + " ");


}

System.out.println();
System.out.println("Queue
2: "); while (q2.size() > 0)
{

System.out.println(q2.remove() + " ");


}
}
}

OUTPUT:-

Queue1: 20 30
10
Queue2: 20 30
10
PRACTICAL -10

OBJECTIVE: Write a program that reads words from a text file


and displays all the non-duplicate words in descending order. The
text file is passed as a command-line argument.
PROGRAM:
Step 1: Create a text file named input.txt as follows --
<input.txt>
Java is useful language
Java is object oriented programming
language I like Java programming

Step 2: Write a Java program that reads the file name using command line
argument and displays all the nonduplicate words in descending order.
import java.io.*;
import
java.util.*; class
ReadFileWords
{

public static void main(String[] args) throws Exception


{

File fin=new File(args[0]);


BufferedReader br=new BufferedReader(new
FileReader(fin)); StringBuffer buffer =new
StringBuffer();
String str;
while((str=br.readLine())!=null) //reading the text file
{

buffer.append(str); //storing the text in StringBuffer


buffer.append(" "); //Separating words by spaces

}
ArrayList list =new ArrayList(); //Declaring ArrayList to store
words of file StringTokenizer st = new
StringTokenizer(buffer.toString().toLowerCase());
while(st.hasMoreTokens()) //creating a list of words read
from file
{

String s =
st.nextToken.();
list.add(s);
}

HashSet set = new HashSet(list); //Hashset is created to avoid


duplicates
List arrayList = new ArrayList(set); //creating a list of words
from file without duplicates
Comparator c = Collections.reverseOrder();
Collections.sort(arrayList,c); //sorting the words in
descending order for (Object obj : arrayList)
System. out.println(obj .toString()); //displaying the contents
}
}

Output:

D:\>java ReadFileWords input.txt


Useful
Programming
Oriented Object
Like Language
Java
Is i
D :\>

You might also like