0% found this document useful (0 votes)
88 views

Java Lab 3

This document is a lab manual for a Java software development course. It outlines topics to be covered, including Java functions, function parameters, recursion, and arrays. It provides examples of creating and calling methods, passing parameters to methods, and overloading methods. It also lists several practice programming problems for students to work on, such as generating random numbers, ordering cities, validating passwords, and more.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Java Lab 3

This document is a lab manual for a Java software development course. It outlines topics to be covered, including Java functions, function parameters, recursion, and arrays. It provides examples of creating and calling methods, passing parameters to methods, and overloading methods. It also lists several practice programming problems for students to work on, such as generating random numbers, ordering cities, validating passwords, and more.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab Manual of Java Software Development Paradigm (CS-02309)

Mr. Usman Asghar


Dept. CS&IT UOL Lahore

THE UNIVERSITY OF LAHORE


Department of CS & IT

Java Software Development (Lab)


07-Oct-2021

Course Instructor: Mr. Usman Asghar

1 Department of CS&IT, The University of Lahore, Lahore Campus


Lab Manual of Java Software Development Paradigm (CS-02309)
Mr. Usman Asghar
Dept. CS&IT UOL Lahore

Java Lab 3
Lab Outlines
 Java Functions introduction
 Function parameters and function overloading
 Java recursion
 Practice programs of Java Arrays and Functions

Function
A Function or method is a block of code which only runs when it is called.

You can pass data, known as parameters, into a method.

Methods are used to perform certain actions, and they are also known
as functions.

 Create a Method
A method must be declared within a class. It is defined with the name of the
method, followed by parentheses ().

Java provides some pre-defined methods, such as System.out.println(), but


you can also create your own methods to perform certain actions:

2 Department of CS&IT, The University of Lahore, Lahore Campus


Lab Manual of Java Software Development Paradigm (CS-02309)
Mr. Usman Asghar
Dept. CS&IT UOL Lahore
Example
Create a method inside Main:

public class Main {

static void myMethod() {

// code to be executed

}
}

Example Explained
 myMethod() is the name of the method
 static means that the method belongs to the Main class and not an object
of the Main class.
 void means that this method does not have a return value.

 Call a Method
To call a method in Java, write the method's name followed by two
parentheses () and a semicolon;

In the following example, myMethod() is used to print a text (the action),


when it is called. A method can also be called multiple times:

3 Department of CS&IT, The University of Lahore, Lahore Campus


Lab Manual of Java Software Development Paradigm (CS-02309)
Mr. Usman Asghar
Dept. CS&IT UOL Lahore
Example
Inside main, call the myMethod() method:

public class Main {

static void myMethod() {

System.out.println("I just got executed!");

public static void main (String [] args) {

myMethod();

// Outputs "I just got executed!"

 Parameters and Arguments


Information can be passed to methods as parameter. Parameters act as
variables inside the method.

Parameters are specified after the method name, inside the parentheses. You
can add as many parameters as you want, just separate them with a comma.

The following example has a method that takes a String called fname as
parameter. When the method is called, we pass along a first name, which is
used inside the method to print the full name:

4 Department of CS&IT, The University of Lahore, Lahore Campus


Lab Manual of Java Software Development Paradigm (CS-02309)
Mr. Usman Asghar
Dept. CS&IT UOL Lahore
public class Main {

static void myMethod(String fname) {

System.out.println(fname + " Refsnes");

public static void main(String[] args) {

myMethod("Liam");

myMethod("Jenny");

myMethod("Anja");

 Multiple parameters and return a value from a function


 You can send many and different parameters while calling the function
like as myFunction (age, name, Id) or myFunction (20, “usman”, 123) etc.
 The void keyword we use, whenever we don’t want to return anything
from the function.
 The return keyword we use whenever we want to return a value from a
function, the value can be an integer, string, boolean etc.
 Methods or Function Overloading and Java Recursion
 Multiple functions can have same name and return type but with different
number of parameters, type of parameters, order of parameters.
 Recursion is the technique of making a function call itself. This technique
provides a way to break complicated problems down into simple problems
which are easier to solve.

5 Department of CS&IT, The University of Lahore, Lahore Campus


Lab Manual of Java Software Development Paradigm (CS-02309)
Mr. Usman Asghar
Dept. CS&IT UOL Lahore

Practice Programs
1. (Print a table) Write a program that displays the following table:
a a^2 a^3
1 1 1
2 4 8
3 9 27
4 16 64

2. (Average speed in kilometers) Assume a runner runs 24 miles in 1


hour, 40 minutes, and 35 seconds. Write a program that displays the
average speed in kilometers per hour. (Note that 1 mile is 1.6
kilometers.)
3. Sum the digits in an integer) Write a program that reads an integer
between 0 and 1000 and adds all the digits in the integer. For
example, if an integer is 932, the sum of all its digits is 14.
a. Hint: Use the % operator to extract digits, and use the / operator
to remove the extracted digit. For instance, 932 % 10 = 2 and
932 / 10 = 93.

4. (Game: scissor, rock, paper) Write a program that plays the popular
scissor-rockpaper game. (A scissor can cut a paper, a rock can knock
a scissor, and a paper can wrap a rock.) The program randomly
generates a number 0, 1, or 2 representing scissor, rock, and paper.
The program prompts the user to enter a number 0, 1, or 2 and
displays a message indicating whether the user or the computer wins,
loses, or draws. Here are sample runs:

6 Department of CS&IT, The University of Lahore, Lahore Campus


Lab Manual of Java Software Development Paradigm (CS-02309)
Mr. Usman Asghar
Dept. CS&IT UOL Lahore
a. scissor (0), rock (1), paper (2): you entered 1
The computer is scissor. You are rock. You won
5. (Order three cities) Write a program that prompts the user to enter
three cities and displays them in ascending order. Here is a sample
run:
a. Enter the first city: Chicago
Enter the second city: Los Angeles
Enter the third city: Atlanta
The three cities in alphabetical order are Atlanta Chicago Los
Angeles
6. (Generate vehicle plate numbers) Assume a vehicle plate number
consists of three uppercase letters followed by four digits. Write a
program to generate a plate number.
7. Prompts the user to enter two string with same or different length. Check both
strings are equal or not, apply maximum string built in functions in your program
and show the results of those built in functions.
a. Enter a string and check whether it is palindrome or not using Loop.
8. Write a java program that takes two values from user and an operator +,*,/ etc
and show the result, make functions for all operations and return results from
those functions.
9. (Check password) Some websites impose certain rules for passwords. Write a
method that checks whether a string is a valid password. Suppose the password
rules are as follows:
a. A password must have at least eight characters.
b. A password consists of only letters and digits.
c. A password must contain at least two digits.

Write a program that prompts the user to enter a password and displays
Valid Password if the rules are followed or Invalid Password otherwise.

7 Department of CS&IT, The University of Lahore, Lahore Campus

You might also like