OOP Lab 1
OOP Lab 1
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:
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>
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
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:
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