Java Programming Practical
Exercise 1
Exercise 1
Java
Basics
1. A) Write a java program to display a message
B) Write a Java program to display employee details (employee
name, salary) using class and method invocation
Objective:
A) The objective of this program is to display a message
B) The objective of this program enables you to display employee details
using class and method invocation.
Procedure and description:
A) Java program to display a message
Java is powerful programming language and it is used to develop robust
applications. In java to display message on output screen you can use
System.out.println() method. This exercise shows how to write first java
application and compile and run it. Before running java program first install
latest version of JDK on your machine.
Algorithm:
Algorithm for display /print data on output screen
Step 1: declare class
/* class hello */
Step 2: declare main method /*Public static void main (String [] args) */
Step 3: print output by using println() /*System.out.println (Hello world) */
Step 4: end
How to save: to run the program save it in a file with the name hello.java.it
must be sure that the file name must match the name of the class.
Compile the program:
Javac hello.java command is used to compile source code. When you
compile the program it will create a class file (byte code file) name hello.
class.
Execute the program:
Now you can execute the byte code in the java interpreter with this
command: Java hello
Expected output
Sikkim Manipal University
Page No: 1
Java Programming Practical
Exercise 1
When the program executed successfully, the message entered in println(),
displays at command line as output.
B) Java program to display employee details using class and method
invocation
Procedure and description:
In java application, you may have many classes. Within those classes, you
may have many methods. The method that you need to execute first should
be the main() method.
Invoking a Method: To invoke a method, the method name must be
followed by parentheses and semicolon, one method of a class can be
invoked another method of the same class using the name of the method.
Algorithm:
Step 1; declare class Employee
Step 2: declare variables String employeeName;
int employeesal;
Step 3: create constructer of class Public Employee()
Initialize the values for variables
employeeName =Bala;
employeesal = 30000;
Step 4: create a method Display to display variable values
Step 5: call main () method and create object for class
Employee emp=new Employee ();
Step 6: call display method by using object,
emp.display();
Step 7: End
Expected Output:
When the program executed successfully, the output at command line
Name = Bala
Salary= 30000
Sikkim Manipal University
Page No: 2