COMP
295
JAVA
Programming
Spring
2016
FORMAN
CHRISTIAN
COLLEGE
(A
Chartered
University)
COMP 295 Java Programming
Spring 2016
Section: A
Instructor: Tahir Javaid
Lab 1
1) JDK Installation:
The Java Development Kit (JDK) is a Software Development Kit (SDK) released by Oracle
Corporation. Without JDK installed, you would be unable to compile a java program.
To download JDK, go to the link mentioned below:
[Link]
2) Eclipse Installation:
A Java IDE (Integrated Development Environment) is a software application which enables
users to more easily write and debug Java programs. Many IDEs provide features like syntax
highlighting and code completion, which help the user to code more easily.
Among Different IDEs, Eclipse is a Free and Open Source IDE, plus a developer tool
framework that can be extended for a particular development need. Eclipse cannot be used
without JDK or JRE installed. It is recommended to install JDK before installing Eclipse.
To download Eclipse IDE, go to the link mentioned below:
[Link]
[Link]
Note: When you start Eclipse for the very first time, it prompts to set your workspace. All
you java projects will get stored in workspace so carefully access it in an appropriate place.
For Eclipse tutorial go to: [Link]
3) Run Java program on Command Prompt:
We can run a java program on System Command Prompt without executing an IDE. Follow
these steps:
i. Create a temporary folder. Using Notepad or another text editor, create a sample Java
file [Link].
ii. Save your file as [Link] in that folder. To make sure your file name is
[Link], (not [Link]), first choose "Save as file type:" All files, then
type in the file name [Link].
iii. Run Command Prompt (found under All Programs/Accessories in the Start menu). Type
cd [space] (path of your folder) eg: cd C:\ Desktop\hello
This makes given path the current directory.
1
COMP
295
JAVA
Programming
Spring
2016
Folder
Path
C:\ Desktop\hello > dir
This displays the directory contents. You should see [Link] among the files.
C:\ Desktop\hello > javac [Link]
This runs [Link], the compiler. You should see nothing but the next system prompt...
C:\ Desktop\hello > dir
( javac has created the [Link] file. You should see [Link] and
[Link] among the files in same folder.)
C:\ Desktop\hello > java HelloWorld
This runs the Java interpreter. You should see the program output:
Hello World!
If the system cannot find javac, then follow next step. If javac runs but you get errors, check
your Java text. If the program compiles but you get an exception, check the spelling and
capitalization in the file name and the class name and the java HelloWorld command. Java is
case-sensitive!
4) Set Java Path:
It is possible to make the path setting permanent but you have to be very careful because
your system might crash if you make a mistake.
Proceed with extreme caution!
1. Go to Control Panel, choose "System," click on the "Advanced system settings" link,
click on the "Environment variables" button. In the lower list, "System variables,"
click on Path.
2. Click "Edit" and at the end append
;C:\Program Files\Java\jdk1.8.0_51\bin
(or the path to the appropriate folder where the latest version of JDK is installed). Do
not put spaces before the appended path string.
3. Click OK on the path edit box and OK on the Environment Variables box. The new
setting will go into effect next time you run Command Prompt.
2
COMP
295
JAVA
Programming
Spring
2016
5) Sample Java program:
// Declaring a Class
public class HelloWorld
{
// Declaring Main Method
public static void main(String[] args)
{
// To Print a Message on Screen.
[Link]("Hello World!");
}
}
Activity 1: Make a java program in Notepad as shown above and compile it
on command prompt.
Syntax: (To print something on console using print() Method)
// Any message inside double quotes gets displayed.
[Link]("Hello World!");
Syntax: (To print something on console using println() Method)
// Any message inside double quotes gets displayed in a line.
[Link]("Hello World!");
3
COMP
295
JAVA
Programming
Spring
2016
6) Java program:
Java Class: A class is the blueprint from which individual objects are created.
Syntax:
public class MyClass
{
Attributes;
Methods;
}
Java Objects: An Object is referred to a specific instance of a class.
Syntax: (for creating an Object)
MyClass c = new MyClass();
// where “c” is an Object/instance of class “MyClass”
Main Class: A class that have main method.
Main Method: A standard method which is used by JVM to start execution of a program.
Syntax:
public static void main(String[] args) // Declaration
{
Body;
Contains Statements;
}
Constructor: Constructor is a method that is used to initialize the object. It has same name as
that of the class and it does not have a return type.
Syntax:
public MyClass(parameters)
{
Statements;
}
Activity 2:
i. Declare a class named MyClass that has a constructor method
that prints “My First Class!”
ii. Declare main class that constructs an object of MyClass.
Syntax:
public class MyClass
{
public MyClass() {
[Link]("My First Class!");
}
}
Main class
public class TestMyClass
{
public static void main(String[] args) {
MyClass c=new MyClass();
}
}
4
COMP
295
JAVA
Programming
Spring
2016
Variables: In java, variables are named data containers. Different types of data can be stored
in variables. We can perform several operations on variable, i.e.: addition, subtraction,
multiplication…. Etc. (except on char or boolean type variables)
Syntax:
datatype variableName = value; // eg: int a = 5;
Some build-in primitive data types and operations on them used in java are shown below:
Table
1:
Primitive
Data
Types
in
JAVA
Data Type Example
int 2, -5
short 1.1, -5.3
float -22.7, 3.14
long 23.5456, -1000.434
double 345.65, -45.55
char a, B, 1, $...
boolean true, false
Table
2:
Operations
on
variables
Operation Operator Syntax
Sum + a+b
Difference - a–b
Product * a*b
Division / a / b (return quotient)
Modulus % a % b (returns remainder)
Equals == a == b(true if a & b are equal)
Not Equal != a != b(true if a & b are not equal)
Less than, <,> a < b (true if a is less than b)
Greater than a > b (true if a is greater than b)
Less than or <= , >= a <= b (true if a is less or equal b)
equal to, a >= b (true if a is less or equal b)
Greater than
or equal to,
And, && a && b (true if both are true)
OR, || a | | b (true if any, one is true)
NOT ! !a (inverts Boolean value)
Syntax: (to print a variable)
// To Print a Variable on console.
[Link](variableName);
Syntax: (to print a variable with a message)
// To Print a Variable on console.
[Link](“ Message ” + variableName);
5
COMP
295
JAVA
Programming
Spring
2016
Activity 3:
i. Declare a variable of type int, store your current semester number in it
and then display it on console.
ii. Declare a variable of type double, store value of PI (π) in it and then
display it on console.
Activity 4:
i. Declare 4 variables of type int, such that a=15 and b=10. Store their sum
in variable c and difference in d. Display results.
ii. Declare 3 variables of type double, such that x=10.5 and y=7.2. Store
their product in variable z and ratio in w. Display results.
Activity 5: Declare a class named Calculate that displays sum, product and
difference of three numbers.
6