Lab 2 - Basic Program in Java
Lab 2 - Basic Program in Java
Lab-02
Basic Program in Java
Lab 2: Basic Program in Java
Table of Contents
1. Introduction 13
4. Concept Map 13
4.1. The if-then and if-then-else Statements 13
4.1.1. The if-then Statement 13
4.1.2. The if-then-else Statement 14
4.2. The for Statement 15
4.3. Methods in Java 16
3. Procedure& Tools 17
4. Tools 17
5. Setting-up JDK 1.7 [Expected time = 5mins] 17
5.1.1 Compile a Program 17
5.1.2 Run a Program 17
6. Walkthrough Task[Expected time = 30mins] 18
7. Practice Tasks 18
Practice Task 1 [Expected time = 15mins] 19
Practice Task 2 [Expected time = 15mins] 19
Practice Task 3 [Expected time = 15mins] 19
Practice Task 4 [Expected time = 15mins] 19
8. Out comes 19
9. Testing 19
10. Evaluation Task (Unseen) [Expected time = 55 mins for two tasks] 20
1. Introduction
You have looked at the basic characteristics of Java and the benefits of using Java over C++. The
objective of this lab is to get basic understanding of java programs using conditional statements,
loops and functions.
To get basic understanding of conditional statements, loops, and functions in java and
how they are implemented using Java.
4. Concept Map
This section provides you the overview of the concepts that will be discussed and implemented
in this lab.
The if-then statement is the most basic of all the control flow statements. It tells your program to
execute a certain section of code only if a particular test evaluates to true. For example, the
Bicycle class could allow the brakes to decrease the bicycle's speed only if the bicycle is already
in motion. One possible implementation of the applyBrakes method could be as follows:
void applyBrakes() {
Department of Computer Page 13
Science,
C.U.S.T.
Lab 2: Basic Program in Java
If this test evaluates to false (meaning that the bicycle is not in motion), control jumps to the end
of the if-then statement. In addition, the opening and closing braces are optional, provided that
the "then" clause contains only one statement:
void applyBrakes() {
// same as above, but without braces
if (isMoving)
currentSpeed--;
}
Deciding when to omit the braces is a matter of personal taste. Omitting them can make the code
more brittle. If a second statement is later added to the "then" clause, a common mistake would
be forgetting to add the newly required braces. The compiler cannot catch this sort of error;
you'll just get the wrong results.
The if-then-else statement provides a secondary path of execution when an "if" clause evaluates
to false. You could use an if-then-else statement in the applyBrakes method to take some action
if the brakes are applied when the bicycle is not in motion. In this case, the action is to simply
print an error message stating that the bicycle has already stopped.
void applyBrakes() {
if (isMoving) {
currentSpeed--;
} else {
System.err.println("The bicycle has already stopped!");
}
}
The following program, IfElseDemo, assigns a grade based on the value of a test score: an A for
a score of 90% or above, a B for a score of 80% or above, and so on.
class IfElseDemo {
public static void main(String[] args) {
char grade;
Grade = C
You may have noticed that the value of testscore can satisfy more than one expression in the
compound statement: 76 >= 70 and 76 >= 60. However, once a condition is satisfied, the
appropriate statements are executed (grade = 'C';) and the remaining conditions are not
evaluated.
The for statement provides a compact way to iterate over a range of values. Programmers often
refer to it as the "for loop" because of the way in which it repeatedly loops until a particular
condition is satisfied. The general form of the for statement can be expressed as follows:
When using this version of the for statement, keep in mind that:
The initialization expression initializes the loop; it's executed once, as the loop begins.
When the termination expression evaluates to false, the loop terminates.
The increment expression is invoked after each iteration through the loop; it is perfectly
acceptable for this expression to increment or decrement a value.
The following program, ForDemo, uses the general form of the for statement to print the
numbers 1 through 10 to standard output:
class ForDemo {
public static void main(String[] args){
for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
}
}
}
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10
A method is a collection of statements that perform some specific task and return result to the
caller. A method can perform some specific task without returning anything. Methods allow us to
reuse the code without retyping the code. In Java, every method must be part of some class
which is different from languages like C, C++ and Python. Methods are time savers and help us
to reuse the code without retyping the code.
Here,
You must solve the following problems at home before the lab.
After reading the reference material mentioned in the introduction, now you are ready to perform
homework assigned to you.
2. Procedure& Tools
In this section you will study how to setup and configure JDK.
Tools
javac Interface.java
After the execution of the above statement bytecode of your class will be generated with same
name as the .java file but its extension will change to .class.
Similarly, compile all classes implementing the interfaces.
Now you will need JVM to run the program.
After successfully completing the section 6.2.1, the only thing left is to execute the bytecode on
the JVM. Run the file containing main method in it. Use the following command to run the
program.
Department of Computer Page 17
Science,
C.U.S.T.
Lab 2: Basic Program in Java
java DriverClass
If the above command executed successfully then you will see output of the program.
This task is designed to guide you towards creating your own abstract class and interface and
running the program.
return min;
}
}
3. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You
need to finish the tasks in the required time. When you finish them, put these tasks in the
following folder:
\\fs\assignments$\Advanced Computer Programming\Lab2
Write a program that asks the user to type 10 integers. The program must compute how many
even and odd numbers were entered.
Department of Computer Page 18
Science,
C.U.S.T.
Lab 2: Basic Program in Java
Write a program in java using repetition structures to print the following pattern. You are not
allowed to use nested loops.
****
***-
**--
*---
Write a java program that creates and integer array having 30 elements. Get input in this array
(in main function). After that, pass that array to a function called “Find_Max_Divisors” using
reference pointer. The function “Find_Max_Divisors” should find (and return) in the array that
number which has highest number of divisors. In the end, the main function displays that
number having highest number of divisors
Write a java functions bool isPalindrome(char arr[],int size) which take character array and
check whether array is palindrome or not. Palindrome is a word, phrase, or sequence that reads
the same backwards as forwards. Example madam, racecar etc
Out comes
After completing this lab, student will be able to implement the concepts of conditions, loops,
and functions in java.
Testing
This section provides you the test cases to test the working of your program. If you get the
desired mentioned outputs for the given set of inputs, then your program is right.
For Practice Tasks, Lab instructor must verify the implementation and required relationships
among all classes and objects. In addition, it is required to confirm that code has clearly
elaborated the concept of aggregation and composition in terms of ownership and life cycle.
T4
The lab instructor will give you unseen task depending upon the progress of the class.
5. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).
Table 3: Evaluation of the Lab
Sr. No. Task No Description Marks
1 4 Problem Modeling 20
2 6 Procedures and Tools 10
3 7 Practice tasks and Testing 35
4 8 Evaluation Tasks (Unseen) 20
5 Comments 5
6 Good Programming Practices 10
6. Further Reading
Books
Text Book:
Java: How to Program by Paul J. Deitel, Harvey M. Deitel. Eighth Edition
Java Beginners Guide:
http://www.oracle.com/events/global/en/java-outreach/resources/java-a-beginners-
guide-1720064.pdf
Slides
The slides and reading material can be accessed from the folder of the class instructor available
at \\fs\jinnah$\