Lab 1
Lab 1
Lab 1
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:
http://www.oracle.com/technetwork/java/javase/downloads/index.html
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:
https://eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/mars/1/
eclipse-jee-mars-1-win32.zip
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: https://www.youtube.com/watch?v=23tAK5zdQ9c
i. Create a temporary folder. Using Notepad or another text editor, create a sample Java
file HelloWorld.java.
ii. Save your file as HelloWorld.java in that folder. To make sure your file name is
HeloWorld.java, (not HelloWorld.java.txt), first choose "Save as file type:" All files, then
type in the file name HelloWorld.java.
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
This displays the directory contents. You should see HelloWorld.java among the files.
This runs javac.exe, the compiler. You should see nothing but the next system prompt...
( javac has created the HelloWorld.class file. You should see HelloWorld.java and
HelloWorld.class among the files in same folder.)
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!
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.
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
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;
}
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() {
System.out.print("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:
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