[Go to site: main page, start]

0% found this document useful (0 votes)
3 views9 pages

Java Practical Guide

The document provides a series of Java programming exercises, each with a question, concept, explanation, and solution. Topics include basic Java structure, user input, arithmetic operations, control flow, arrays, methods, and object-oriented programming. Each example is accompanied by a code snippet demonstrating the solution.

Uploaded by

dunsinolugbaro
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Java Practical Guide

The document provides a series of Java programming exercises, each with a question, concept, explanation, and solution. Topics include basic Java structure, user input, arithmetic operations, control flow, arrays, methods, and object-oriented programming. Each example is accompanied by a code snippet demonstrating the solution.

Uploaded by

dunsinolugbaro
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1️⃣ Hello World Program

✅ Question:

Write a Java program that prints:

Welcome to Java Programming!

💡 Concept:

 Basic Java structure

 main() method

 [Link]()

🧠 Explanation:

Every Java program starts from the main() method.


[Link]() is used to print output to the screen.

💻 Solution:

public class Welcome {

public static void main(String[] args) {

[Link]("Welcome to Java Programming!");

2️⃣ Input and Output (Scanner)

✅ Question:

Write a program that asks the user for their name and prints:

Hello, [name]

💡 Concept:

 User input

 Scanner class

 Variables

🧠 Explanation:

Scanner is used to take input from the keyboard.


We store the input inside a variable and display it.

💻 Solution:

import [Link];
public class Greeting {

public static void main(String[] args) {

Scanner input = new Scanner([Link]);

[Link]("Enter your name: ");

String name = [Link]();

[Link]("Hello, " + name);

3️⃣ Simple Calculator

✅ Question:

Write a program that takes two numbers and prints their sum.

💡 Concept:

 Variables

 Arithmetic operators

 User input

🧠 Explanation:

Java supports arithmetic operators like +, -, *, /.

💻 Solution:

import [Link];

public class SumProgram {

public static void main(String[] args) {

Scanner input = new Scanner([Link]);

[Link]("Enter first number: ");

int num1 = [Link]();

[Link]("Enter second number: ");


int num2 = [Link]();

int sum = num1 + num2;

[Link]("Sum = " + sum);

4️⃣ Even or Odd Checker

✅ Question:

Write a program to check if a number is even or odd.

💡 Concept:

 if-else

 Modulus operator %

🧠 Explanation:

The modulus operator % gives the remainder.


If a number divided by 2 gives remainder 0, it is even.

💻 Solution:

import [Link];

public class EvenOdd {

public static void main(String[] args) {

Scanner input = new Scanner([Link]);

[Link]("Enter a number: ");

int number = [Link]();

if (number % 2 == 0) {

[Link]("Even number");

} else {

[Link]("Odd number");
}

5️⃣ Find the Largest of Three Numbers

✅ Question:

Write a program to find the largest of three numbers.

💡 Concept:

 Multiple if-else

 Logical operators

🧠 Explanation:

We compare numbers using relational operators like >.

💻 Solution:

import [Link];

public class LargestNumber {

public static void main(String[] args) {

Scanner input = new Scanner([Link]);

[Link]("Enter first number: ");

int a = [Link]();

[Link]("Enter second number: ");

int b = [Link]();

[Link]("Enter third number: ");

int c = [Link]();

if (a >= b && a >= c) {

[Link]("Largest is: " + a);

} else if (b >= a && b >= c) {


[Link]("Largest is: " + b);

} else {

[Link]("Largest is: " + c);

6️⃣ Loop Example – Print 1 to 10

✅ Question:

Write a program that prints numbers from 1 to 10.

💡 Concept:

 for loop

🧠 Explanation:

A loop allows us to repeat code multiple times.

💻 Solution:

public class PrintNumbers {

public static void main(String[] args) {

for (int i = 1; i <= 10; i++) {

[Link](i);

7️⃣ Multiplication Table

✅ Question:

Write a program to print multiplication table of a number entered by the user.

💡 Concept:

 Loops

 Arithmetic operations

🧠 Explanation:

We use a loop to multiply the number by values from 1 to 12.


💻 Solution:

import [Link];

public class MultiplicationTable {

public static void main(String[] args) {

Scanner input = new Scanner([Link]);

[Link]("Enter a number: ");

int number = [Link]();

for (int i = 1; i <= 12; i++) {

[Link](number + " x " + i + " = " + (number * i));

8️⃣ Array Example – Average of Numbers

✅ Question:

Write a program to calculate the average of 5 numbers using an array.

💡 Concept:

 Arrays

 Looping through arrays

🧠 Explanation:

An array stores multiple values in one variable.

💻 Solution:

import [Link];

public class AverageArray {

public static void main(String[] args) {

Scanner input = new Scanner([Link]);

int[] numbers = new int[5];


int sum = 0;

for (int i = 0; i < 5; i++) {

[Link]("Enter number " + (i+1) + ": ");

numbers[i] = [Link]();

sum += numbers[i];

double average = (double) sum / 5;

[Link]("Average = " + average);

9️⃣ Method Example

✅ Question:

Write a method that returns the square of a number.

💡 Concept:

 Methods

 Return type

🧠 Explanation:

Methods help us reuse code.


return sends a value back to where the method was called.

💻 Solution:

public class SquareMethod {

public static int square(int number) {

return number * number;

public static void main(String[] args) {

int result = square(5);


[Link]("Square = " + result);

🔟 Simple OOP Example (Class and Object)

✅ Question:

Create a Student class with:

 name

 age
And display the student details.

💡 Concept:

 Class

 Object

 Constructor

🧠 Explanation:

A class is a blueprint.
An object is created from the class.

💻 Solution:

public class Student {

String name;

int age;

// Constructor

public Student(String name, int age) {

[Link] = name;

[Link] = age;

public void display() {

[Link]("Name: " + name);

[Link]("Age: " + age);

}
public static void main(String[] args) {

Student s1 = new Student("John", 20);

[Link]();

You might also like