0% found this document useful (0 votes)
4 views

OOP Lab 1

Object Oriented Programming Lab Sheets

Uploaded by

f20220584
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)
4 views

OOP Lab 1

Object Oriented Programming Lab Sheets

Uploaded by

f20220584
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/ 19

CS F213 Object Oriented Programming

Lab 1: Setting up VScode with JDK, writing your


first java program to basics with object creation
and its manipulation

Team OOP, Dept. of CSIS


Installing open JDK 8
OpenJDK 8 is sufficient for all the development we will carry out. Probably it is
installed in your system, if not install it by:

sudo apt-get install openjdk-11-jdk


Ask for the password from the Lab administrator.

To check whether it is installed run the command: java -version


Installing VS code
Open https://code.visualstudio.com/download

Download
Installing VS code
Go to the download location by either opening a terminal there or simply by the cd
command.Then run the command:

sudo dpkg -i code_<vrsion_number>_<architecture>.deb


Writing java source code in VS code
VS code should now be available in menu directly or else just run the command
code from any terminal.

Then go to File -> new text file or simply do ctrl + N and then select the language
as java.

Usually java requires filenames to be same as class containing it, in VS code you
usually need not worry about that, just write the class and save by ctrl + S and the
file will be named automatically. If not then manually name it to class name.

When asked save in a folder with a convenient name, we will see more
complicated examples later in package.
Setting VS code for java
Usually when you select java, the compiler will be set up automatically, if not just
select the JDK and JRE from the bottom right corner.

A menu will open up and you have to select the JDK from there, if multiple JDKs
are not there in the system then it is pretty straightforward.

Now to build and run the code you can simply click the play button.
Without the VS code
If you want to compile manually outside VS code just open a terminal in the folder
containing the source files and run the command:

javac *.java which will try to compile all .java files. A good convention is to
name the class containing the main function as Main and write other classes in
separate files. Though You may write all classes in a single file also.

After compilation one or several .class files will be created. To run the code, run
the command java Main <input arguments>

You are now set for java development!


Your first Java code
As usual!

Expect an output like:


Let us skip the “C” part
basic if else else-if ladder nested if else for loop while loop (nested also) switch
case are same as that of C so we will skip that!

Also install the following extension after clicking this on left panel
Basic IO in Java
Output printing in Java:
System.out.println() or System.out.print() [without newline] and
System.out.printf() [for c style formatted output]
To take command line arguments as input there are two ways:
The terminal way: Simple! just pass the arguments as a list separated by space
after the command java <MainFileName> <list of arguments>
The VScode way: Though a bit complicated let us first rewrite our basic code for
taking command line arguments.
Basic IO in Java
Basic IO in Java
You have to create launch.json to edit the configurations for your source code by
clicking the left panel run start menu

This will appear, click on the “create a launch.json” and edit

Add args here


Basic IO in Java
Now if you run with VS code expect the following output:
Get the base code from.

https://github.com/Aritra/OOPLabMaterials/
Parse<datatype> to get numbers.
An example of RISC type code
for minimal calculator, change
the args and run to see the
effects. Like…

Gives:
Let us now get to objects
Let us write a student class and a main to create some students and iterate
through them. We will also use a basic constructor to ease our job.
Let us now get to objects
The main class has c type formatted output and iterator for loop
Let us now get to objects
The output will be:

Now you have to do something!!!


Exercise
Add a field called “campus” in the student class

Recall any sorting algorithm from CS F111 and rewrite the code so that it takes the
following two arguments in command line: <Parameter> <Direction>; where
<Parameter> can be ‘Name’, ‘ID’, ‘CGPA’, or ‘Campus’ and
<Direction> can be ‘A’ and ‘D’ [for ascending and descending
respectively] and sorts as per the input line arguments.

Keep the students name ID CGPA same, campus you can get from ID and the
formatting same.

Try to do this with a single function called sort() where you can pass the
array of students and the parameters

You might also like