LAB 1to 4
LAB 1to 4
LAB 1to 4
The first step in OOP is to identify all the objects the programmer wants to manipulate and how
they relate to each other, known as data modeling. Once an object has been identified, it is
generalized as a class of objects which defines the kind of data it contains and any logic
sequences that can manipulate it.
1. Abstraction
2. Encapsulation.
3. Inheritance.
4. Polymorphism.
Java
Java is a programming language and a platform based on object-oriented programming concepts.
It was first released by Sun Microsystems in 1995.
There are lots of applications and websites that will not work unless Java is being installed, and
more are created every day. Java is high level, fast, secure, and reliable.
From laptops to datacenters, game consoles to scientific supercomputers, cell phones to the
Internet, Java is everywhere!
1 Introduction to Java
7 Inheritance in Java
10 Exception Handling
12 Exploring JavaFx
13 JavaFx Application
Introduction to Java
1 Objective: Getting familiar with the Java development kit (JDK). Running
your first Java program using CMD and an IDE.
Student Feedback:
Student Feedback:
Student Feedback:
Student Feedback:
Student Feedback:
Student Feedback:
Inheritance in Java
7 Objective: Understanding the concept of inheritance, the superclass and
subclass.
Student Feedback:
Student Feedback:
Packages & Interfaces
9
Objective: Understanding the concept packages & interfaces of Java.
Student Feedback:
Exception Handling
10
Objective: Understand how runtime errors are being handled in Java.
Student Feedback:
Student Feedback:
Exploring JavaFx
12
Objective: Show different JavaFx Layouts and Charts.
Student Feedback:
13 JavaFx Application
Objective: Design a Login page using JavaFx components.
Student Feedback:
Student Feedback:
Student Feedback:
Note: Students are advised to write their comments about whether the objective of the lab was achieved in
Student Feedback.
Theory:
What is JDK?
It's the full featured Software Development Kit for Java, including JRE, and the compilers and tools (like Java
Debugger) to create and compile programs.
JRE is required to run Java programs while JDK is required when you have to do some Java programming.
2. Run the .exe file on your System and follow the steps as given by the installer.
Figure 1.2 Installing JDK
3. Note down the location where j2se has been installed. Java Platform is also called as J2SE (Java 2
Platform Standard Edition)
To set the temporary path of JDK, you need to follow following steps:
For setting the permanent path of JDK, you need to follow these steps:
Go to
MyComputer properties -> advanced tab -> environment variables -> new tab of user variable ->
write path in variable name -> write path of bin folder in variable value -> ok -> ok -> ok
Copy the path of the bin folder of the JDK and paste it in the variable value field of the path
variable. So add ";C:\Program Files\Java\jdk1.7.0_02\bin" in the path string.
Note:
The PATH environment variable is a series of directories separated by semicolons (;) and is notcase-
sensitive. Microsoft Windows looks for programs in the PATH directories in order, from left to right.
The new path takes effect in each new command window you open after setting the PATH variable.
Lab Task:
class HelloWorld {
public static void main(String args[]){
System.out.println(“Hello World!!”);
}
}
2. Save it as HelloWorld.java. Naming is strictly case-sensitive; make sure you type as it is given above.
3. Now open a command prompt and change to the directory where you have saved HelloWorld.java
4. Enter SET CLASSPATH.
5. Press enter; this sets the classloader path to search for class files in the current directory.
6. Type the command javac to compile your code. If the compilation is successful, you would not be
shown any errors.
javac HelloWorld.java
Type the command java HelloWorld and press enter. Please note that you did not include any
extension such as .java or .class to the class name you wish to run.
javaHelloWorld
7. You would get nice output greeting “Hello World!!”
What is an IDE?
There are different IDE’s used for Java development including NetBeans and Eclipse.
Before starting to develop the complex programs, you need an IDE. In this workbook we will be using
Eclipse IDE for code writing, compilation and execution.
After installation, open eclipse. You will see the welcome screen.
Create new project in eclipse. Click on file menu, select Project, then select Java Project and click
Next. You will see the following screen. Give your project a name and click Finish.
Figure 1.11: Creating new project in Eclipse
Now right click on your project available in project explorer and create new class.
Figure 1.12: Add new Class
A window similar to one shown below will appear. Enter the class name and check the checkbox for
public static void main (String[] args). Click finish.
A java file will open with some added code. Just add the following line inside the main()
method.System.out.println("Hello World!!");
Figure 1.14: Adding Java Code
The output of this code will be generated in the console window present below.
Figure 1.16: Console Output
Lab Assignment:
1. Write a simple java program with command-line argument in java.
Theory:
Console input
System.in to the standard input device. Console input is not directly supported in Java, but Scanner class is
used to create an object to read input from System.in, as follows:
Scanner input = new Scanner(System.in);
double radius = input.nextDouble();
Console output
Java uses System.out to refer to the standard output device.To perform console output, println method is used
to display a primitive value or a string to the console.
System.out.print("Hello ");
System.out.println("world");
You can use the System.out.printf method to display formatted output on the console.
System.out.printf(“Your Total amount is %4.2f", total);
System.out.printf("count is %d and amount is %f", count, amount);
System.out.println((int)1.7);
The above statement displays 1. When a double value is cast into an int value, the fractional part is
truncated.
Math
Math class file is included for the definitions of math functions listed below. It is written as
java.lang.Math.
Date
Java provides a system-independent
encapsulation of date and time in the
java.util.Date class. The no-arg
constructor of the Date class can be
used to create an instance for the
current date and time.
Lab Task:
1. Write a Java program to take different input from user and store the input in variables with
respective data type and then display the data on the console.
import java.util.Scanner;
}
}
System.out.println("\nTrignometric Functions");
System.out.println("sin 45 = " + sn);
System.out.println("cos 45 =" + cs);
System.out.println("tan 45 =" + tn);
System.out.println("\nHyperbolic Functions");
System.out.println("sinh 1 = " + snh);
System.out.println("cosh 1 = " + csh);
System.out.println("tanh 1 = " + tnh);
}
}
import java.util.Date;
class DateDemo
{
public static void main(String args[]) {
// Instantiate a Date object
Date date = new Date();
// display time and date using toString()
System.out.println(date);
// Display number of milliseconds since midnight, January 1, 1970
long msec = date.getTime();
System.out.println("Milliseconds since Jan. 1, 1970 " + msec);
}
}
Lab Assignment:
1. Program the following.
Objective:Understanding the control statements of Java including Loops & if-else.Exploring different
operators used in Java.
Theory:
IterationStatements
A loop can be used to tell a program to execute statements repeatedly.Java provides a powerful construct
called a loop that controls how many times an operation or a sequence of operations is performed in
succession.A loop can be nested inside another loop.Different form of loops can be nested with one another.
1. while loops,
2. do-while loops, and
3. for loops.
Loop Syntax
Selection Statements
Selection Statements of Java programming language decides the next statement for execution.Selection
statements use conditions that are Boolean expressions.A Boolean expression is an expression that evaluates
to a Boolean value: true or false.An if statement can be inside another if statement to form a nested if
statement.
if statement executes the statements if the condition is true. if-else statement is two way statement that
decides which statements to execute based on whether the condition is true or false. Multi-way if-else
statement is the preferred coding style for multiple alternative if statements.
If-else Syntax
if do-while for
else {
//for-default-case;
Switch Syntax
switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
.
.
.
casevalueN:
// statement sequence
break;
default:
// default statement sequence
}
Operators in Java
There are many operators provide by Java. Following are the most commonly used operators.
Comparison Operators:
Comparison operators can be used to compare two values. The result of the comparison is a Boolean value:
true or false. Java provides six comparison operators.
LogicalOperators:
The logical can be used to create a compound Boolean expression. Sometimes, whether a statement is
executed is determined by a combination of several conditions.
Lab Task:
1. Write a Java program to use an if-else-if ladder to determine which season a particular month
is in.
..........
...
..
Lab Assignment:
1. Write a program 1 of this lab using switch statement.
2. The Fibonacci sequence is defined by the following rule. The first 2 values in the sequence are
1, 1. Every subsequent value is the sum of the 2 values preceding it. Write a Java Program that
print the nth value of the Fibonacci sequence?
3. Write a Java program that prompts the user for an integer and then prints out all the prime
numbers up to that Integer?
Classes & Objects Lab 4
Objective: Understanding concepts of class and object in java. Implementing a class with members
including data, methods and constructors.
Theory:
Class
A class consists of
Data(variables)
Methods
Constructors
Lab Task:
class Box {
double width;
double height;
double depth;
// compute and return volume
double volume() {
return width * height * depth;
}
}
// ___________ Demo Class ___________
class BoxDemo4 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
/* assign different values to mybox2's
instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
Adding Constructor
class Box {
double width;
double height;
double depth;
// This is the constructor for Box.
Box() {
System.out.println("Constructing Box");
width = 10;
height = 10;
depth = 10;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
Lab Assignment:
The class should contain default and parameterized constructor. The constructors should just print
the statement as follows.
“Inside Default Constructor”
“Inside Parameterized Constructor”.
calculateSquare(int x)
calculateCube(int x)
calculateFactorial (int x)
generateTable(int x)