Index
Introduction to Java Programming
Java is a simple and yet powerful object oriented programming language and it is in many respects similar to C++.
Where Java is used?
The programming language Java was developed by Sun Microsystems in the year 1995. Earlier, it was only used to design and program small computing devices but later adopted as one of the platform independent programming languages
The target of Java is to write a program once and then run this program on multiple operating systems.
 Start a new program 
Click File

Select New project

Select JavaStart (Under Categories)

Select Java Application (Under Projects)

Click Next

In the project name, type the name of the project (Calculation)

In the project Location,
Click Browse and select the folder you want to save in(First Project)

Click Finish
 Java source code
Java is an objectoriented programming language developed by James Gosling and colleagues at Sun Microsystems in the early 1990s.
Below is a java sample code for the traditional Hello World program.
A java program is defined by a public class that takes the form:
Public class programname {
Optional variable declarations and methods
Public static void main (String [] args) {
Java code 
}
}
Use System.out.print to print statements, expressions or variables on the same line.
Use System.out.println to print statements, expressions or variables on different lines.

System.out.println("Hello World");

System.out.println(5+3);

System.out.println(NB1);

Note: The text must be written between quotations: " "
 Java Operators
The following are the most Operators used in java:

Assignment Operators =

Arithmetic Operators  + * /
% (Divides left hand operand by right hand operand and returns remainder)
++ (Increases the value of operand by 1)
 (Decreases the value of operand by 1)

Relational Operators ><>= <= == (equal) !=(Not Equal)

Logic operators && (AND)  (OR)
A variable is a place where the program stores data temporarily. As the name implies the value stored in such a location can be changed while a program is executing (compare with constant).
One variable in your program can store numeric or text data. All variables have a name, a type. The programmer assigns the names to variables, known as identifiers. Variables have a data type that indicates the kind of value they can store.
int Age = 18;
The variable "Age" is declared to be an int data type and initialized to a value of 18.
Data Type

Range

Examples

Byte

128 to 127


Short

32,768 to 32,767


Int

2,147,483,648 To 2,147,483,647

Int Year=2009;

Long

9,223,372,036,854,775,808 to 9,223,372,036,854,775,807


Float or Double

For decimal

double rats = 891.32 ;

Boolean

Boolean data type represents one bit of information.
There are only two possible values: true and false.
The default value is false

Boolean test=true;

Char

It is used to store any character

Char letter=’A’

String

Used for many characters

String Name=”SE3”
  Concatenate
The (+) operator can be used for addition and for concatenation
Example Concatenates and adds two numbers and prints the results.
System.out.println ("number 1 :" + (5 + 5));
Output
number 1: 10
 Scanner class 
It Provides methods for reading input of various types from various sources.

It is a part of java standard library.
The resulting tokens may then be converted into values of different types using the various next methods.
For example, this code allows a user to read a number from System.in:
Import java.util.Scanner;
Public class class Main {
Public static void main (String[] args) {
Scanner scan=new Scanner (System.in);
Read an integer number
inta;
a = scan.nextInt();
Read a double number
Double b;
b = scan.nextDouble();
Read a string of characters
String c;
c = scan.next();
Read a character
d = scan.next().charAt(0);
Example: The user reads and displays a number.
importjava.util.Scanner;
public class Readnumber {
public static void main (String[] args) {
Scanner scan = new Scanner (System.in);
Int nb;
System.out.print ("Enter a number: ");
nb = scan.nextInt();
System.out.println ("The number is: " + nb);
}
}
Output
Enter a number: 10
The number is: 10
The if/else statement is an extension of the if statement. If the statements in the if statement fails, the statements in the else block are executed. You can either have a single statement or a block of code within ifelse blocks. Note that the conditional expression must be a Boolean expression.
The ifelse statement has the following syntax:
if (condition) {
statement action }
else {
statement action }
Below is an example that demonstrates conditional execution based on if else statement condition.
int a = 40, b = 50;
if (a > b) {
System.out.println("a > b");
}
Else {
System.out.println("b > a");
}
Output
A > b
 If  else if Statement
You can test for more than two choices. For example, what if we wanted to test for more age ranges, say 19 to 39, and 40 and over? For more than two choices, the IF … ELSE IF statement can be used. The structure of an IF … ELSE IF is this:
if ( condition 1 ) {
statement action}
else if ( condition 2 ) {
statement action }
else {
statement action}
The new part is this:
else if ( condition_two ) {
}
So the first IF tests for condition number one (18 or under, for example). Next comes else if, followed by a pair of round brackets. The second condition goes between these new round brackets. Anything not caught by the first two conditions will be caught be the final else. Before trying out some new code, you'll need to learn some more conditional operators. The ones you have used so far are these:
Here's four more you can use:
&& AND
 OR
== Equal to
!= Not equal to
The first one is two ampersand symbols, and is used to test for more than one condition at the same time. We can use it to test for two age ranges:
else if ( user > 18 && user < 40 )
Here, we want to check if the user is older than 18 but younger than 40. Remember, we're trying to check what is inside of the user variable. The first condition is "Greater than 18" ( user> 18 ). The second condition is "Less than 40" ( user< 40). In between the two we have our AND operator ( &&). So the whole line says "else if user is greater than 18 AND user is less than 40."
We'll get to the other three conditional operators in a moment. But here's some new code to try out:
Int user=21;
If(user<=18) {
System.out.println(“User is 18 or younger”);
}
Else if (user>18 && user<40){
System.out.println(“User is between 19 and 39”);
}
Else {
System.out.println(“User is older than 40”);
}
}
Problem 1
Write a java program that calculates the Area or the Perimeter of a square.

The user should enter the side (integer number)

The user should enter one of the two letters: A (for area) and P (for perimeter) to perform the needed function; otherwise the program should display (The letter is not accepted).
The output of the program should appear as the following form:
Enter the side: 5
Enter A for area – P for perimeter : P
Perimeter = 20
int s,a,p;
char x;
System.out.print("Enter the side : ");
s=scan.nextInt();
System.out.print("Enter A for area – P for perimeter : ");
x = scan.next().charAt(0);
if(x=='a') {
a=s*s;
System.out.println("Area = " + a);
}
else if (x=='p') {
p=s*4;
System.out.println("Perimeter = " + p);
}
else {
System.out.println("The letter is not accepted ");
}
Problem 2
Write a java program that reads three integers and prints the number of unique integers among the three. For example, the 18 3 4 should print 3 because they have 3 different values. By contrast, 6 7 6 would print 2 because there are only 2 unique numbers among the three which are 6 and 7.
Sample Input Sample Output
18 3 4 3
6 7 6 2
1 1 1 1
int a,b,c;
System.out.print("Enter a ");
a=scan.nextInt();
System.out.print("Enter b ");
b=scan.nextInt();
System.out.print("Enter c ");
c=scan.nextInt();
if(a==b && a!=c) {
System.out.println("2");
}
else if(a==c && a!=b) {
System.out.println("2");
}
else if(b==c && b!=a) {
System.out.println("2");
}
else if(a==b && a==c) {
System.out.println("1");
}
else {
System.out.println("3");
}
Problem 3
Write a java program that reads three integers and determines if one of the integers is the midpoint between the other two integers; that is, if one integer is exactly halfway between them. Your program should print "No midpoint" if no such midpoint relationship exists otherwise print the midpoint. The integers could be passed in any order; the midpoint could be the 1st, 2nd, or 3rd. You must check all cases.
Sample Input Sample Output
1 2 3 2
6 4 8 6
7 9 2 No midpoint
double a,b,c;
System.out.print("Enter a ");
a=scan.nextDouble();
System.out.print("Enter b ");
b=scan.nextDouble();
System.out.print("Enter c ");
c=scan.nextDouble();
if((a+b)/2==c) {
System.out.println("MidPoint = " + c);
}
else if((a+c)/2==b) {
System.out.println("MidPoint = " + b);
}
else if((b+c)/2==a) {
System.out.println("MidPoint = " + a);
}
else {
System.out.println("No Midpoint");
}
 Dowhile Statements
The dowhile statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as:
do {
statement(s)
} while (expression);
Example:
int count = 1;
do {
System.out.println("Count is: " + count);
count++;
} while (count < 11);
Problem 4
Write a java program that keeps on reading numbers by the user until the user enters a number >=1000 and the program should display the sum of these numbers.
Integer a,s=0;
do {
System.out.print("Enter a nb ");
a=scan.nextInt();
s=s+a;
}while(a<1000);
System.out.println("Sum = " + s);
}
Problem 5
Write a java program that keeps on reading numbers nonnegative numbers by the user (repeat reading if the number is <=0) and the program should display the average of these numbers. The program should stop when the user enters a number >=1000.
Double a, s=0; c=0;
do {
do {
System.out.print("Enter a nb ");
a=scan.nextDouble();
}while(a<=0);
c++;
s=s+a;
}while(a<1000);
avg=s/c;
System.out.println("Average = " + avg);
 Modulo
Modulo is nothing more than "remainder after division."
So 20 modulo 5 is 0 because 20 divided by 5 is 4 with no remainder.
21 modulo 5 is 1 ; 22 modulo 5 is 2
Modulo is represented as the percent sign.
So
int a = 20 % 5 ;
Sets a to be 0.
Problem 6
Write a java program that enters integer number by the user, and then counts and displays the number of even numbers entered by the user.
The program should stop when the user enters a number less than 1 or greater than 500.
int a, c=0;
do{
System.out.print("enter a number ");
a=scan.nextInt();
if(a%2==0) c++;
}while(a>=1 && a<=500);
System.out.println("Even numbers = " + c);
Problem 7
Write a java program that enters integer number by the user and calculate the multiplication of them. The program should stop when the multiplication become over than 5000.
 Switch statement
Another way to control the flow of your program is with something called a switch statement. A switch statement gives you the option to test for a range of values for your variables. They can be used instead of long, complex if … else if statements. The structure of the switch statement is this:
switch ( variable_to_test ) {
case value:
code_here;
break;
case value:
code_here;
break;
default:
values_not_caught_above;
}
So you start with the word switch, followed by a pair of round brackets. The variable you want to check goes between the round brackets of switch. You then have a pair of curly brackets. The other parts of the switch statement all go between the two curly brackets. For every value that you want to check, you need the word case. You then have the value you want to check for:
case value:
After case value comes a colon. You then put what you want to happen if the value matches. This is your code that you want executed. The keyword break is needed to break out of each case of the switch statement.
The default value at the end is optional. It can be included if there are other values that can be held in your variable but that you haven't checked for elsewhere in the switch statement.
Example
Int user=18;
Switch (user) {
Case 18:
System.out.println(“you’re 18”);
Break;
Case 19:
System.out.println(“you’re 19”);
Break;
Case 20:
System.out.println(“you’re 20”);
Break;
Default:
System.out.println(“you’re not 18, 19 or 20”);
}
 Math.sqrt()
The function Math.sqrt() returns the square root (Radical) of an integer number.
Example 1:
int x=9;
System.out.println("Square Root of " + x + " = " + Math.sqrt(x));
OutPut
Square Root of 9 = 3.0
Example 2:
System.out.println(Math.sqrt(9));
OutPut
3.0
Problem 8
Write a java program that calculates the needed operation between two integer numbers given by the user. In case user entered number different from 1 or 2, the program should display "Invalid number"
The output of the program should appear as the following form
Enter nb1: 25
Enter nb2: 4

Find the maximum square root of nb1 and nb2

Check if nb1 is divisible by nb2.
Choose from 1 or 2 : 1
Maximum = 5.0
Note: For the second choice display: 25 is not divisible by 4
int nb1, nb2;
int choose;
System.out.print("Enter nb1 : ");
nb1=scan.nextInt();
System.out.print("Enter nb2 : ");
nb2=scan.nextInt();
System.out.println("1 Find the maximum square root of nb1 and nb2");
System.out.println("2 Check if nb1 is divisible by nb2");
System.out.print("Choose from 1 or 2 : ");
choose = scan.nextInt();
switch (choose) {
case 1: if(Math.sqrt(nb1)>Math.sqrt(nb2))
System.out.println("Maximum = " + Math.sqrt(nb1));
else
System.out.println("Maximum = " + Math.sqrt(nb2));
break;
case 2: if(nb1%nb2==0)
System.out.println(nb1 + " is divisible by " + nb2);
else System.out.println(nb1 + " is not divisible by " + nb2);
break;
default: System.out.println("Invalid Number ");
Problem 10  Extracting Digits:
Write a java program that extracts each digit from an integer number in the reverse order. For example, if the number is 1542, the output shall be "2,4,5,1", with a comma separating the digits.
Hints: Use n % 10 to extract a digit; and n = n / 10 to discard the last digit
int nb,r;
System.out.print("enter a nb : ");
nb=scan.nextInt();
do{
r=nb%10;
System.out.print(r + ",");
nb=nb/10;
}while (nb>0);
Problem 11  Reverse Digits:
Write a java program that reverses a number composed of 4 digits within an integer number given by the user.
Example: 1234 becomes 4321.
int nb ,r, S=0;
System.out.print("enter a nb : ");
nb=scan.nextInt();
INT T=1000;
do{
r=nb%10;
s=s+r*t;
t=t/10;
nb=nb/10;
}while (nb1>0);
Nb=s ;
System.out.println();
System.out.println(s);
int nb,nb1,r;
System.out.print("enter a nb : ");
nb=scan.nextInt();
nb1=nb;
int t=1, s=0; int c=0;
do {`
nb=nb/10;
c++;
}while (nb>0);
for(int i=1; i
t=t*10;
}
do{
r=nb1%10;
s=s+r*t;
t=t/10;
nb1=nb1/10;
}while (nb1>0);
System.out.println();
System.out.println(s);
Problem 12
Write a java program that reads a string and displays it in reverse order.
Sample Input Sample Output
java avaj
math htam
song gons
Problem 13
Write a program that reads a string and an integer N and displays the string N times next to each other.
Sample Input Sample Output
Batman 2 BatmanBatman
IronMan 3 IronManIronManIronMan
 Generate a Randomize number
Java can generate an integer number as the following form:
Random rand = new Random():
Int random number=rand.nextInt();

Returns a Random integer in the Range [0, max)
Int random number=rand.nextInt(max); from 0 to max1
Int random number=rand.nextInt(10); from 0 to 9
Int random number=rand.nextInt(10)+1; from 1 to 10
Int random number=rand.nextInt(51)+50; from 50 to 100
Problem 14  Dice:
Write a java program that simulates rolling of two 6sided dice until their combined result comes up as 7.
2 + 4 = 6
3 + 5 = 8
5 + 6 = 11
1 + 1 = 2
4 + 3 = 7
You won after 5 tries!
import java.util.Random;
Random rand = new Random();
int a, b,s,c=0;
do {
a=rand.nextInt(6)+1;
b=rand.nextInt(6)+1;
s=a+b;
System.out.println(a + " + " + b + " = " + s);
c++;
}while (s!=7);
if (s==7) System.out.println("You win after " + c + " tries!");
Problem 15
Write a java program that reads an integer and checks whether the integer is a leap year. The integer must be within the Gregorian calendar which started after 1582. If the year is a leap year then display "It is a leap year!" otherwise if it is not a leap year then display “ It is not a leap year ”, and finally if it is not in the gregorian calendar display “Unknown ”.

Leap Years are any year that can be evenly divided by 4 (such as 2012, 2016, etc)

except if it can be evenly divided by 100 , then it isn't (such as 2100, 2200, etc)

except if it can be evenly divided by 400 , then it is
(such as 2000, 2400)
int year;
System.out.print("Enter the year ");
year=scan.nextInt();
if(year<1582)
System.out.println("Unknown");
else if(year%4==0 && year%100!=0)
System.out.println("Leap Year");
else if(year%4==0 && year%400==0)
System.out.println("Leap Year");
else
System.out.println("is not a leap Year");
if(year<1582)
System.out.println("Unknown");
else if(year%4==0 && year%100!=0  year%400==0)
System.out.println("Leap Year");
else
System.out.println("is not a leap Year");
 Loop
A programming loop is one that forces the program to go back up again. If it is forced back up again you can execute lines of code repeatedly.
As an example, suppose you wanted to add up the numbers 1 to 10. You could do it quite easily in Java like this:
int addition = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10;
But you wouldn't really want to use that method if you needed to add up the numbers 1 to a 1000. Instead, you can use a loop to go over a line of code repeatedly until you've reached 1000. Then you can exit the loop and continue on your way.
We'll start with For Loops, one of the most common types of loops. The "For" part of "For Loop" seems to have lost its meaning. But you can think of it like this: "Loop FOR a set number of times." The structure of the For Loop is this:
for ( start_value; end_value; increment_number ) {
YOUR_CODE_HERE
}
So after the word "for" (in lowercase) you have a pair of round brackets. Inside of the round brackets you need three things: the start value for the loop, the end value for the loop, and a way to get from one number to another. This is called the increment number, and is usually 1. But it doesn't have to be. You can go up in chunks of 10, if you want.
After the round brackets you need a pair of curly brackets. The curly brackets are used to section off the code that you want to execute repeatedly. An example might clear things up.
Example
inti;
intendval=4;
for (i=1; i<=endval; i++) {
System.out.displayln("Hello SE3"); }
Output
Hello SE3
Hello SE3
Hello SE3
Hello SE3
Example
Int i;
for (i=1; i<=10; i++) {
System.out.println("The value of i is : " + i); }
Output
The value of i is : 1
The value of i is : 2
The value of i is : 3
The value of i is : 4
The value of i is : 5
The value of i is : 6
The value of i is : 7
The value of i is : 8
The value of i is : 9
The value of i is : 10
We start by setting up an integer variable, which we've called (i). The next line sets up another integer variable. This variable will be used for the end value of the loop, and is set to 10. What we're going to do is to loop round displaying out the numbers from 1 to 10.
Inside the round brackets of the forloop, we have this:
i =1; i<= end_value; i++
The first part tells Java at what number you want to start looping. Here, we're assigning a value of 1 to the (i) variable. This will be the first number in the loop. The second part uses some conditional logic: i
This says (i)is less than end_value. The for loop will then keep going round and round while the value inside the (i) variable is less than the value in the variable called end_value. As long as it's true that (i)is less than end_value, Java will keep looping over the code between the curly brackets.
The final part between the round brackets of the forloop is this: i++
What we're doing here is telling Java how to go from the starting value in (i) to the next number in the sequence. We want to count from 1 to 10. The next number after 1 is 2.
i++ is a shorthand way of saying "add 1 to the value in the variable".
Instead of saying i++ we could have wrote (i=i+1) like this:
i = i + 1;
for (i=1; i<=endval; i=i+1)
Some different syntax:
for (i=0; i<10; i++)
for (i=0; i<=9; i++)
for (i=10; i>=1; i)
Problem 16
Write a java program that displays the following sequence of numbers from 100 to 100.
The output of the program should be as the following form
100 95 90 … 90 95 100
int i;
for(i=100;i<=100;i=i+5) {
System.out.print( i+ " ");
}
Problem 17
Write a java program that calculates and displays the sum of a harmonic series as shown below where n <100. (n is positive less than 100)
Harmonic (n) =
double s=0,d,i,n;
dina Mansour<3
do {
System.out.print("Enter n :");
n=scan.nextDouble();
}while(n<1  n>=100);
for(i=1;i<=n;i++) {
d=1/i;
s=s+d;
}
System.out.println("Total = " + s);
Problem 18
Write a java program that enters N integer numbers by the user, and display the number of occurrence (count) of even numbers.
Int nb, n, I, c=0;
System.out.print("Enter n :");
n=scan.nextInt();
for(i=1;i<=n; i++) {
System.out.print("Enter a number :");
nb=scan.nextInt();
if(nb%2==0) c++;
}
System.out.println("Number of even : " + c);
Problem 19
Write a java program that enters integer numbers by the user, and then displays the total of them. The program must stop when the user enter the number 0.
Int nb, s=0;
Do {
System.out.print("Enter a number :");
nb=scan.nextInt();
s=s+nb;
}while (nb!=0);
System.out.println("Total = " + s);
Problem 20
Write a java program that enters 20 integer numbers by the user, and then displays the multiplication of even numbers of them.
Int nb, I, p=1;
for(i=1;i<=20; i++) {
System.out.print("Enter a number :");
nb=scan.nextInt();
if(nb%2==0) p=p*nb;
}
System.out.println("Total " + p);
Problem 21
Write a java program that calculates the total of numbers starting from 1 as follows:
1+3+5+7+…
The program should stop when the total becomes over than 150.
Int i=1, s=0;
Do {
s=s+i;
i=i+2;
}while (s<=150);
System.out.println("Total = " + s);
Problem 22
Write a java program that calculates the total of numbers starting from 2 as follows:
2+4+6+12+24+…
2+4+8+16+32+…
The program should stop when the total becomes over than 150.
Display the number of integer numbers that are added.
Int p=2, s=0, c=0;
Do {
S=s+p;
P=p*2;
C++;
}while (s<=150);
System.out.println("Total = " + s);
System.out.println("count = " + c);
Problem 23
Write a Java program that enters an integer number by the user and then displays the divisible numbers of it.
Example: The divisible numbers of 16 are: 1, 2, 4, 8, 16.
Int nb; I;
System.out.print("Enter a number");
nb=scan.nextInt();
For(i=1; i<=nb; i++) {
If (nb%i==0) System.out.print (I + “, “);
}
Problem 24
Write a java program that calculates the factorial of an integer number given by the user.
The output of the program should appear as the following form
Enter a number: 5
The factorial of 5 is 120
Int nb; I, f=1;
System.out.print("Enter a number");
nb=scan.nextInt();
For(i=1; i<=nb; i++) {
F=f*I;
}
System.out.println("The factorial of " + nb + “is “ + f);
Problem 25

What will the value of "X" be after the following code executes?
Int i, x=0, y, z=5;
for(i=3; i<=8; i=i+2) {
y=z%3;
x=x+y; answer: 6
}

What will the value of "X" be after the following code executes?
int i, x=20;
for(i=3; i>=1; i) {
x=x+x%i;
answer: 22
}

What will the value of "X" be after the following code executes?
int x=524378;
do {
x=x/100; answer: 52
}while (x>=60);

What will the value of "X" be after the following code executes?
int x=100, s=0;
do {
s=s+x; answer: 800
x=s;
}while (s<=400);
Problem 26
Write a Java program that calculates X^{N}. X and N are integer numbers given by the user.
X^{N }= X * X * X … * X
N
Int x,n, I, p=1;
System.out.print("Enter N");
n=scan.nextInt();
System.out.print("Enter X");
X=scan.nextInt();
For(i=1; i<=n; i++) {
P=p*x;
}
Problem 27
Write a Java program that calculates the following sequence.
R = 1 + X^{1} + X^{2} + X^{3} + X^{4} + X^{5} + …. + X^{10}
X is an integer number given by the user.
Int x,n, i, s=1; p=1;
System.out.print("Enter X");
X=scan.nextInt();
For(i=1; i<=10; i++) {
p=p*x;
s=s+p;
}
 Method
A method performs a task that completes the operations of a class. Sometimes, a method would need one or more values in order to carry the task. The particularity of values is that another method that calls this one must supply the needed value(s).
Like a variable, a method has a type such as character, string, decimal or integer… etc. This means that the method or class that calls a method is responsible for supplying the right value.
The value supplied to a method is typed in the parentheses of the method and it is called an argument. In order to declare a method that takes an argument (value), you must specify its name and the argument between its parentheses. Because a method must specify the type of value it would need, the argument is represented by its data type and a name.
Suppose you want to define a method that displays the side length of a square. Since you would have to supply the length, you can start the method as follows:
static void displaySide(double length) {
}
Name of the method
value
In the body of the method, you may or may not use the value of the argument. Otherwise, you can manipulate the supplied value as you see fit. In this example, you can display the value of the argument as follows:
public class Exercise {
static void displaySide(double length) {
System.out.display("Length: " + length);
}
}
Important: Once a method is created it should be called from the main() program, otherwise it will not be used.
When calling a method that takes an argument, you must supply a value for the argument; otherwise you would receive an error. Also, you should supply the right value; otherwise, the method may not work as expected or it may produce an unreliable result. Here is an example:
public class Exercise {
Main program
static void main(String[] args) {
displaySide(35.55);
}
Method
static void displaySide(double length) {
System.out.displayln("Length: " + length);
}
}
Suppose you want to create a method that calculates and displays the area of a square, it is required to write the needed codes for calculation and for displaying of the area in method displayArea block.
Here is an example:
public class Exercise {
public static void main(String[] args) {
displayArea(35.55);
}
static void displayArea(double length) {
double area;
area = length*length;
System.out.displayln("Area: " + area);
}
}
Important:

Use Static void & name of method in case of displaying the result of a calculation without return it.
Example static void displayArea(double length) {}
Suppose you want to create a method that calculates the area of a square and then returns the result back to the main method, it is required to:

Write the needed codes for calculation of the area in method displayArea block.

You need to declare the name of the function with its data type.

Write the needed codes for to return the result (Return area) in method displayArea block.

Write the needed codes for displaying the result in the main() program.
Here is an example:
public class Exercise {
public static void main(String[] args) {
double displayArea;
displayArea(35.55);
System.out.displayln("Area = " + displayArea(35.55));
}
static double displayArea(double length) {
double area;
area = length*length;
return area;
}
}
Important:

Use Static & data type & name of method in case of returning the calculation. The type of method should be the same as the type of returning value.
Example static double displayArea(double length) {}
Problem 28
Write a java program that:

Create a method "Msqaure" to calculate the square of a given number.

The method should display the result.

Call the method from the main() program.
Problem 29
Write a java program that:

Create a method "Factorial" to calculate the factorial of a given number.

The method should display the result.

Call the method from the main() program.
Problem 30
Write a java program that:

Create a method "Msum" to calculate sum of 2 integer numbers given by the user.

Create a method "Mmul" to calculate multiplication of 2 integer numbers given by the user.

The 2 methods should return the result of the calculation to the main form.

Call the methods from the main() program.
 Arrays
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the above illustration, numbering begins with 0.
The following program, ArrayDemo, creates an array of 8 elements of integers type called T, puts some values in it, and displays each value to standard output.
int [] T;
T = new int [8];
Int i;
for(i=0;i<8;i++){
System.out.orintln("enter a value ");
T[i]=scan.nextInt();
}
for(i=0;i<8;i++){
System.Out.println("Element at index " + i + " : " + T[i]);}
}
The output from this program is:
Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Problem 31
Write a java program that:

Fills an array T [10] with integer numbers

Displays all the elements in one row.

Displays all the elements in reverse order.

Displays if you have entered 2 elements in sequence having the same value.
Display: yes or no.
int t[];
t=new int [10];
int i;
for(i=0;i<4;i++){
System.out.print("Enter a value ");
t[i]=scan.nextInt();
}
for(i=0;i<4;i++){
System.out.print("T[" + t[i] + "] ");
}
System.out.println();
for(i=3;i>=0;i){
System.out.print("T[" + t[i] + "] ");
}
System.out.println();
for(i=1;i<4;i++){
if(t[i]==t[i1]) {
System.out.println("Yes");
break;
}
else System.out.println("No");
}
Problem 32
Write a java program that creates an array T[] of N elements:

The number of elements N is given by the user (N must be less than 100)

Fills the array T[] with integer numbers less than 200

Calculates and displays the summation of even numbers (even elements)

Enters an integer Number named Val, and then finds and displays the number of occurrences (count) of Val.
Problem 33
Write a java program that fills an array T [10] by integers, then create a method that displays whether VAL (an integer given by the user) exists in the array T or not.
Problem 34
Write in java method SUMDIF() that accepts one dimensional array of integers and the number of the array elements and returns the difference between the average of the odd values and the average of the even values in the array.
Problem 35
Write a java program that fills an array T [10] with integer numbers, and then displays the maximum of them.
Problem 36
In mathematics, the Fibonacci numbers or Fibonacci series or Fibonacci sequence are the numbers in the following integer sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144
By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.
Display this sequence, take into consideration the length of the table is 13.
Problem 37
Write a java program that:

Fills an array of 10 integer numbers.

Displays the first element that reoccurs in the array.

If the array contains no duplicates, displays 1.
Sample Input Sample Output
7 10 5 3 4 3 5 5 7 6 5
Problem 38
Write a java program that:

Fills an array of 10 integer numbers of nonnegative integers

Displays the frequencies of occurrences of each integer from 0 to the largest value in the array.
The output of the program should be as follows
Sample Input Sample Output
5 1 2 2 4 0: 0; 1: 1; 2: 2; 3: 0; 4: 1
Problem 39
Write a java program that:

Enters the length of an array T1.

Fills an array T1 with the following sequence 0 5 10 15 20 25 30 35 40 45….

Reverses the elements of T1 into another array T2.
Problem 40
Write a java program that:

Creates the array T1 of 8 integer numbers.

Fills the array T2 by numbers between 0 and 255 (include 0 and 255).

Creates from the values of array T1 the array T2 containing in each of its cases the sum of two successive cases of T1.
Only the first value of T2 corresponds to the first value of T1.

Displays the contents of array T2 in the same line.
Example:
Array VA

Array VB

=6
=6
+2
=2
+4
=4
+8
=8
+0
=0
+5
=5
+3
=3
+9

Displays the contents of array T2 in the same line.
Problem 41
Write a java program that:

Enters the length of the array T[]

Fill the arrays with integer numbers.

Enter an integer number X.

Displays the maximum sum of every X consecutive elements.
For example, if the array passed contains the values {74, 85, 102, 99} and X is 3, then the consecutive sums of every 3 integers are: 74+85+102 = 261, 85+102+99 = 286.
The maximum is then 286.
 TwoDimensional Array
A two dimension array is used to represent a matrix or a table. For example the following table that describes the distances between the cities
Create a Matrix (or table of 2 dimensions, 5 columns, 5 rows)
int [] Matrix;
Matrix = new int [5][5];;
int I, j;
for(i=0;i<5;i++){
for(j=0;j<5;j++){
Matrix[i][j]=scan.nextInt();
}
}
Display a Matrix (or table of 2 dimensions, 5 columns, 5 rows)
Problem 42
Write a java program that:

Creates a table T [4][4]

Fills the table T with values multiple by 5 starting from 0.

Calculates and displays the summation of all the values.

Calculates and displays the summation of the second row.

Calculates and displays the summation of the third column.
Problem 43
Write a java program that:

Creates a table T [6][6]

Fills the 2 diagonals by 0

Display the elements of the table
Problem 44
Write a java program that:

Creates and fills the table T[5][5] by integer numbers.

Determines the biggest number in each line of T and displays the result as follows:
The biggest number in the line 1 =….
The biggest number in the line 2 =….
The biggest number in the line 3 =….
Problem 45
Write a java program that displays the following tables of 2 dimensions as the following form:
*
**
***
****
*****
******
Problem 46
Write a java program that displays the following tables as the following form:
1
12
123
1234
12345
123456
1234567
The number of rows and columns are given by the user.
Problem 47
Write a java program that displays the following tables as the following form:
*******
******
*****
****
***
**
*
Problem 48
Write a java program that displays the following tables as the following form:
55555
4444
333
22
1
The number of columns and rows are given by the user and they must be equal.
Problem 49
Write a java program that displays the following table as the following form:
*
**
***
****
*****
Problem 50
Write a java program that displays the following table as the following form:
1******
12*****
123****
1234***
12345**
123456*
1234567
Problem 51
Write a java program that displays the following table as the following form:
AAAAA
BBBBB
CCCCC
DDDDD
Problem 52
Write a java program that displays the following table as the following form:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Informatics
PROGRAMMING  
