0% found this document useful (0 votes)
38 views7 pages

Java Lab Manual for CS&IT Students

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views7 pages

Java Lab Manual for CS&IT Students

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 [Link](), 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() {

[Link]("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) {

[Link](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

Common questions

Powered by AI

Java's parameterized methods modify printed content by accepting input data, such as a string parameter, which alters the message produced in the method body. For example, passing a first name to a printing method can change the full output name dynamically, showcasing both data manipulation and flexible method utility.

Methods with the same name and return type can be differentiated through method overloading, which involves changing the number, type, or order of parameters. This allows a single method name to handle different types of data inputs effectively, enabling code flexibility and reusability.

Recursion simplifies complex problems by breaking them down into smaller, more manageable sub-problems. This is done by having the function call itself with modified parameters until a base condition is met, thereby reducing the complexity at each level and simplifying the overall problem-solving approach.

Using loops to determine if a string is a palindrome is significant as it allows the comparison of characters from both ends of the string towards the center. This iterative approach verifies symmetry effectively, providing a logical solution to check if a string reads the same backward as forward.

The 'void' keyword in Java indicates that a method does not return any value, making it suitable for procedures that execute actions without producing output. In contrast, a method can return values of different types, specified via the return type, allowing it to provide computed data back to the calling code.

Implementing a Java program to check for valid passwords poses challenges such as verifying character count, content type, and digit presence. Solutions involve using conditionals and string manipulation methods to enforce rules like minimum length, digit inclusion, and character type constraints, ensuring robust password validation.

Function overloading in Java benefits program design by enabling multiple methods with the same name and different parameters, fostering code modularity and flexibility. This allows developers to design functions that cater to various data types and use cases, enhancing readability and reducing the need for multiple method names.

Java's method calling mechanism can be demonstrated by defining a method that prints a message, such as 'I just got executed!', and invoking it within the main method. This showcases the structure where the method is defined with a specific task and called to perform that task, visible through the printed output.

Generating vehicle plate numbers programmatically in Java ensures consistency, automation, and error reduction. The program must include random generation of uppercase letters and numerical digits to form a valid combination that adheres to format specifications, ensuring uniqueness and adherence to standards.

Parameters and arguments enhance method functionality by allowing data to be input and manipulated within the method. Parameters act as placeholders for the arguments passed during method calls, enabling the method to operate dynamically based on external data inputs and perform specific actions accordingly.

You might also like