Java Practical Workbook New
Java Practical Workbook New
USING OOJAVA
WORKBOOK
FOR
COMPUTER SCIENCE
Level: __300_____________________________________________________________
1
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
2
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
➢ Type in your Project name which is also the same as the Class name e.g. Displayname and
click on Finish button.
3
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
iii) System.out.println ("This is my first program in Java"); This method prints the contents inside
the double quotes into the console and inserts a newline after.
4
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
5
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
OUTPUT
6
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
MODULE 1
WEEK 1 2
WORK TO DO
1. Run the java program below
/*
* WELCOME TO THE WORLD OF JAVA PROGRAMMING LANGUAGE
* Displays The Welcome Message above! to the output window
*
*/
public class WelcomeMessage // class definition header
{
public static void main( String[] args )
{
System.out.println( WELCOME TO THE WORLD OF JAVA PROGRAMMING LANGUAGE
!!! ); // this line prints the message text
} // end main method
} // end class/program WelcomeMessage
2. Write and run a Java program to display your Full Names, Matric No and Course of Study
(Attach a print copy of all source codes and output)
7
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
Week 3-4
WORK TO DO
1. List and explain the two classes of data types in OO Java and their types
2. List and explain the three (3) permissible styles of comments in the Java Programming language.
3. Key in and run the sample program below:
public class Variables {
public static void main(String[ ] args) {
int a;
double b;
char c;
a = 2;
b= 3.4;
c= e
System.out.println (a);
System.out.println (b);
System.out.println (c);
}
}
1. Key in the program below and answer the questions that follow by test running the program.
/*
* HelloWorldGUI.java
import javax.swing.JOptionPane;
public class HelloWorldGUI {
public static void main(String[] args) {
String msg = "Hello World";
String ans ="";
JOptionPane.showMessageDialog(null, msg );
// accept the users name
ans = JOptionPane.showInputDialog( null, "Enter your Name Please");
// say hello to the user
JOptionPane.showMessageDialog(null, "Hello " + ans );
2. (a) Describe the output of the program. (b) Re-run the program and click on the ok button without
entering a name. What is the output presented?
3. Write and run a Java program to add three (3) variables and also find their average. (N.B. Attach
a print copy of the output for all programs).
8
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
WEEK 4
APPARATUS
PC in a networked laboratory loaded with OO-JAVA Compiler and PowerPoint package and
connected to Internet.
WORK TO DO
1. Write short notes on each of the following : (i) Abstraction (ii) Encapsulation
(iii) Inheritance (iv) Polymorphism
2. (a) Run the sample program below and debug where applicable, attach the print out of codes and
output.
public class Operators {
static int a=3, c = 56;
static boolean state;
static double b = 24.6;
public static void main(String[ ] args) {
double args = (a-3+c*a)/c;
System.out.println (args + is cool);
System.out.println (state + is bad);
double x = b/2;
System.out.println (x + is the result);
}
}
2 (b) Write a Java program using the JOptionPane to read in two (2) numbers, multiply them and
display the result.
// Dispaly output
System.out.println(" My name is : "+ name);
System.out.println("My age is : "+ age);
}
}
9
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
From the program segment above, identify, list and comment on the following features.
i. System objects
ii. User Defined Objects
iii. Methods
iv. Class Name
6. Solve the following using JAVA syntax (i) y3 (ii) square root of 12.25 (iii) 2πr (v) !(6<=4) in
a single program.
10
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
WEEK 5 – 6
APPARATUS
PC in a networked laboratory loaded with OO-JAVA Compiler and PowerPoint package and
connected to Internet.
WORK TO DO
1. Using program segments, illustrate the differences between local and instance methods.
3. Write and run the JAVA program below to illustrate IF.ELSE Statement
4. Write a Java program using the Scanner class to read in a student age and indicate if the
Student is a teenager or an adult.
11
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
System.out.format(" ");
for(int i = 1; i<=tableSize;i++ ) {
System.out.format("%4d",i);
}
System.out.println();
System.out.println("
=============================================================");
for(int i = 1 ;i<=tableSize;i++) {
// print left most column first
System.out.format("%4d x",i);
for(int j=1;j<=tableSize;j++) {
System.out.format("%4d",i*j);
}
System.out.println();
}
}
}
12
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
WEEK 7
APPARATUS
PC in a networked laboratory loaded with OO-JAVA Compiler and PowerPoint package and
connected to Internet.
WORK TO DO
1. Write and run the JAVA program below to illustrate IF.ELSE Statement
// Working with SWITCH.CASE STATEMENT
public class IFELSE
{
public static void main(String[ ] args) {
int a = 5;
switch (a)
{
case 1:
System.out.println (a is 1);
break;
case 3:
System.out.println (a is 3);
break;
case 5:
System.out.println (a is 1);
break;
default:
System.out.println (Wrong input);
}
}
}
2. Write and run the JAVA program below to illustrate IF.ELSE Statement
// Working with NESTED IF. STATEMENT
public class IFELSE
{
public static void main(String[ ] args) {
int a = 5, b=9;
if (a>=4)
{
13
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
If (b<8)
{
System.out.println (a is greater than 4 and b is less than 8);
}
else
{
System.out.println (a is greater than 4 and b is greater than 8);
}
}
else
System.out.println (a is less than 4);
}
14
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
WEEK 8
APPARATUS
PC in a networked laboratory loaded with OO-JAVA Compiler and PowerPoint package and
connected to Internet.
WORK TO DO
1. Write a program to compute addition of 10-100 using any of function suitable
2. Write program to compute sum of all even numbers between 5 and 120
3. Write a JAVA program, to output the sum of odd numbers from 1 to 1000, using For Loop
Statement.
4. Write a JAVA program to solve simultaneous equation involving two unknowns x and y NESTED
IF.
15
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
WEEK 9
APPARATUS
PC in a networked laboratory loaded with OO-JAVA Compiler and PowerPoint package and
connected to Internet.
WORK TO DO
1. Run the program below and print the output.
// RecursiveNos.java
public class RecursiveNos
{
public void display1To10( int startNumber )
{
if( startNumber == 10 ) {
System.out.printf("\n%d ", startNumber );
}
else {
System.out.printf("\n%d ", startNumber );
// number display1To10( startNumber + 1);
}
}
}
2. Write a simple program to list all odd and even numbers respectively between 1 – 100
3. Compute nCr using function subprogram
4. Write a recursive JAVA program to calculate the factorial of any N numbers.
5. Write a JAVA program that will calculate the area and circumference of a circle.
6. Write a JAVA program using method overload functions to multiply and add two numbers.
16
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
WEEK 10
APPARATUS
PC in a networked laboratory loaded with OO-JAVA Compiler and PowerPoint package and
connected to Internet.
WORK TO DO
1. (a) Write a simple Java program to declare an array named Governor which can store five (5)
integers and create this array to store the following numbers: {23,45,66,27,87} .
(c) Describe the syntax for creating an array in Java with sample program segment.
18
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
ANSWERS PAGE
WEEK 1-2 NO. 2:
public class StudentDetails{
public static void main(String [] args){
String name = "Daniel Omogbai Oshione";
String matric = "\n21/8947";
String CourseOfStudy = "\nComputer Science";
WEEK 3-4 :
1a. Primitive Data Types:
• These are basic data types built into the Java language.
• Examples include int, double, char, boolean, etc.
• They store simple values directly.
2. Single Line Comments: denoted by "//", used for commenting on a single line.
• Multi-line Comments: enclosed between "/" and "/", used for commenting out multiple lines
or sections of code.
• Documentation Comments: enclosed between "/**" and "*/", used for generating
documentation and can be processed by tools like javadoc.
3a. The output of the code is a separate window displaying a message box telling user to insert
their names.
b. If i click on the OK button without a typing my name the output is just “HELLO”
19
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
int a = 2;
int b = 4;
int c = 6;
int sum = a + b + c;
} }
OUTPUT:
WEEK 4:
1a. (i) Abstraction: Abstraction is the process of representing essential features without including
the background details. In programming, it involves creating classes, objects, and methods that
focus on the necessary characteristics and behavior of an entity, while hiding the implementation
details.
(ii) Encapsulation: Encapsulation is the concept of bundling data and methods that operate on the
data within a single unit, such as a class. It helps in restricting access to certain components of an
object, thus preventing unauthorized access and modification of data.
(iv) Polymorphism: Polymorphism refers to the ability of different classes to be treated as instances
of the same class through a common interface. It allows a single method to have different forms,
such as method overloading and method overriding, based on the context in which it is used. This
promotes flexibility and extensibility in OOP.
2a. Debugged:
20
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
double x = b/2;
} }
OUTPUT:
3.0
is cool
false
is bad
12.3
is the result
2b. program to receive input and multiply two numbers using JOptionPane:
import javax.swing.JOptionPane;
}}
21
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
System Objects:
System.out: This object is used to access the standard output stream and is used to display the
output to the console.
System.in: This object is used to access the standard input stream and is used to accept input from
the console.
Scanner: The 'Scanner' object is a user-defined object used to read input from various sources, such
as files, strings, and the console. In this program, it is used to read input from the standard input
stream (System.in).
Methods:
main: This is the entry point of the program and is a predefined method in Java. It is called when
the program is executed and contains the code to be executed.
Class Name:
Nd1: This is the name of the main class in the program. In Java, the main class name must match
the source file name and contains the code for the program's functionality.
3b.
int A = 5;
int B = 7;
int C = 2;
int G = A + B % C;
int K = (A + B) % C;
}}
OUTPUT:
double PI = 22/7;
}}
OUTPUT:
}}
OUTPUT:
int y = 3;
}}
OUTPUT:
2πr: 50.26548245743669
!(6<=4): true
WEEK 5-6
// Instance method
// Local method
}}
2. Explain Parameter Passing: Parameter passing refers to the way that arguments are passed to a
method. In Java, parameters can be passed to methods in two ways: by value and by reference.
3. OUTPUT:
A is greater than 4.
4. Write a Java program using the Scanner class to read in a student age and indicate if the
Student is a teenager or an adult.:
import java.util.Scanner;
public class StudentDetails{
public static void main (String [] args){
Scanner scan = new Scanner (System.in);
System.out.println("Enter your age: ");
int age = scan.nextInt();
if (age >= 18) {
System.out.println("You are an adult");
}
else{
System.out.println("You are teenager");
}}}
OUTPUT:
Enter your age:
19
WEEK 8
3. JAVA program, to output the sum of odd numbers from 1 to 1000, using For Loop Statement:
The sum of all odd numbers between 1 and 1000 is: 250000.
4. JAVA program to solve simultaneous equation involving two unknowns x and y NESTED IF:
26
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
System.out.println("The solution for the system of equations is: x = " + x + ", y = " +
y);
} else {
System.out.println("The system of equations either has no solution or infinitely
many solutions."); }}}
OUTPUT:
The solution for the system of equations is: x = 3.3076923076923075, y = 3.8461538461538463.
WEEK 9
display1To10(startNumber + 1);
}}
recursiveNos.display1To10(1);
}}
OUTPUT:
1 2 3 4 5 6 7 8 9 10
2. Program to list all odd and even numbers respectively between 1 – 100:
27
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
if (i % 2 == 0) {
}}
if (i % 2 != 0) {
}}}}
OUTPUT:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66
68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67
69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
5. Write a JAVA program that will calculate the area and circumference of a circle.
public class CircleCalculator {
public static double calculateArea(double radius) {
return Math.PI * Math.pow(radius, 2);
}
public static double calculateCircumference(double radius) {
return 2 * Math.PI * radius;
}
public static void main(String[] args) {
double radius = 5.0;
System.out.println("Area of the circle: " + calculateArea(radius));
System.out.println("Circumference of the circle: " + calculateCircumference(radius));
}}
OUTPUT:
Area of the circle: 78.53981633974483
Circumference of the circle: 31.41592653589793
6. Write a JAVA program using method overload functions to multiply and add two numbers:
29
OBJECT ORIENTED JAVA PROGRAMMING LANGUAGE COM 121
WEEK 10
1a. Write a simple Java program to declare an array named Governor which can store five (5)
integers and create this array to store the following numbers: {23,45,66,27,87} :
}}
datatype[] arrayName;
(c) Describe the syntax for creating an array in Java with sample program segment:
int[] numbers;
int[] numbers;
String[] names;
}}
OUTPUT:
2. OUTPUT:
s1 =
s2 = hello
s3 = birth day
s4 = day
3. OUTPUT:
Length of s1: 11
31