Professional Documents
Culture Documents
Ex: Write a java program for printing the message “Hello Java”.
Solution:
class Simple{
System.out.println("Hello Java");
Run:
Hello Java
Ex: Ask the user to read two integer numbers then find and print the sum of these integer numbers, then ask him
to read two float numbers then find and print the subtraction of these two numbers.
Solution:
import jave.util.Scanner;
float f1,f2,sub;
int n1,n2,sum;
n1=input.nextInt();
n2=input.nextInt();
sum=n1+n2;
f1=input.nextFloat();
f2=input.nextFloat();
sub=f1-f2;
10
20
Solution:
int i = 37;
int j = 42;
double x = 27.475;
double y = 7.22;
//adding numbers
System.out.println("Adding...");
2
System.out.println(" x + y = " + (x + y));
//subtracting numbers
System.out.println("Subtracting...");
//multiplying numbers
System.out.println("Multiplying...");
//dividing numbers
System.out.println("Dividing...");
//Mixing type
System.out.println("Mixing types...");
Control Statements
Ex: Write a java program that print the message that explain the whether the age is greater than 18. Note ( where
age=20).
3
Solution:
int age=20;
if(age>18){
Run:
Ex: Write a java program that check whether the x input integer number is even or odd.
Solution:
import jave.util.Scanner;
int x;
x=input.nextInt();
if(x%2==0){
System.out.println("even number");
else{
System.out.println("odd number"); }
4
Run:
odd number
Ex:Write a java program that enter the mark integer value between(0 to 100). Then check the mark whether it
was smaller than 50, print fail. While when mark is between 50 to 60, print the grade is D. When mark is between
60 to 70, print the grade is C. When mark is between 70 to 80, print the grade is B. When mark is between 80
to 90, print the grade is A. When mark is between 90 to 100, print the grade is A+ grad. Otherwise print invalid.
Solution:
import jave.util.Scanner;
int marks;
marks=input.nextInt();
if(marks<50){
System.out.println("fail");
System.out.println("D grade");
System.out.println("C grade");
System.out.println("B grade");
5
}
System.out.println("A grade");
System.out.println("A+ grade");
}else{
System.out.println("Invalid!"); } } }
Run:
65
C grade
Ex: Write a java program to print the integer input value between 1 to 3 to text number.( i.e 2Two).
Solution:
import jave.util.Scanner;
int number;
number=input.nextInt();
switch(number){
case 1: System.out.println("One");break;
case 2: System.out.println("Two");break;
case 3: System.out.println("Three");break;
default:System.out.println("Not in 1, 2 or 3"); } } }
Run:
6
3
Three
Looping Statements
Ex:Write a java program that print the sequence number from 1 to 10.
7
Solution:
for(int i=1;i<=10;i++){
System.out.println(i);
} } }
Run:
10
Ex: write a java program that find the summation of n integer sequence numbers (i.e. 1 to n), using while loop.
Solution:
import jave.util.Scanner;
int i=1;
int sum=0;
int n;
8
n=input.nextInt();
while(i<=n){
sum=sum+i;
i++;
System.out.println(sum);
} }
Ex: Write a java program that find the summation od all even number and multiplication of all odd numbers
between n integer numbers using do..while loop statement.
Solution:
import jave.util.Scanner;
int i=1;
int n,x;
int sum=0;
int mul=1;
n=input.nextInt();
do{
x=input.nextInt();
if (x%2==0)
sum=sum+x;
else
mul=mul*x;
i++;
9
}while(i<=n);
Ex: Write a java program that print the array content, using enhanced for loop.(Suppose the array content is 12,
23,44,56,78).
Solution:
import jave.util.Scanner;
int arr[]={12,23,44,56,78};
for(int i:arr){
System.out.println(i); } } }
Run:
12
23
44
56
78
Break statement
Ex:Write a java program that prints the numbers from 1 to 10, and input y which it is an integer number between
1 to 10 that is used to break the for loop.
Solution:
import jave.util.Scanner;
n=input.nextInt();
for(int i=1;i<=10;i++){
if(i==n){
break; }
System.out.println(i);
} } }
Run:
Array
11
Ex:Write a Java program which is create, initialize, and finding the summation of arrays elements, then print
it.
double total = 0;
total += myList[i];
Run:
1.9
2.9
3.4
3.5
12
Total is 11.7
Ex:Write a Java program that input n integer numbers and store in array, then find and print the largest number
between them print it.
import java.util.Scanner
int n;
n=input.nextInt();
mylist[i]=input.nextInt(); }
max = myList[i]; }
Run:
30
50
13
10
Max is 50
Method
14
Ex: Write a java program that create a method called “RankPoints” which is accept double value start from 100
and Above, which print the Rank level inside this method. (i.e. Rank level is A1 when point>=100, Rank level
is A2 when point>=150, more Rank level is A3).
Solution:
import jave.util.Scanner;
RankPoints(150.5);
System.out.println("Rank:A1");
System.out.println("Rank:A2");
}else {
System.out.println("Rank:A3");
} } }
Run:
Rank:A1
Ex: Write a java program that find the minimum number between 2 integer numbers when minimum number is
found using a method called minFunction which is accepted two integer numbers and returen the minimum
number between them that is print in the main class.
Solution:
import jave.util.Scanner;
int a = 11;
int b = 6;
int min;
min = n2;
else
min = n1;
return min;
Run:
Minimum value= 11
Ex: Write a java program that creat a method called “swapFunction”, that is accept two input integer numbers
and print the two numbers after swapping in the method.
Solution:
import jave.util.Scanner;
int a = 30;
int b = 45;
swapFunction(a, b);
}
16
public static void swapFunction(int a, int b) {
int c = a;
a = b;
b = c;
Run:
Before swapping(Inside), a = 30 b = 45
After swapping(Inside), a = 45 b = 30
Static Method
17
If you apply static keyword with any method, it is known as static method. A static method belongs to the class
rather than object of a class. A static method can be invoked without the need for creating an instance of a class.
Static method can access static data member and can change the value of it.
Ex:Write a java program that define the student information (rollno, name and college), when rollnois an integer,
while student name is string and colleges static variable with initial value “ITS”. Then define a method that
change thedata college variable to be “BBDIT” that is called in the main class at the beginning. Then call
constructor that give data to rollno and name initial value to be displayed in the created method.
Solution:
class Student9{
int rollno;
String name;
college = "BBDIT";
rollno = r;
name = n;
Student9.change();
s1.display();
s2.display();
s3.display(); } }
Run:
18
111 Karan BBDIT
Ex: Write a java program that calculate a cube (x*x*x) using static method that accept one integer value x and
return the result.
Solution:
class Calculate{
return x*x*x; }
int result=Calculate.cube(5);
System.out.println(result);
Run:
125
19
Overlapping Method
Ex: Write a java program that finding minimum numbers of integer type and the minimum number of double
type using overlapping method called “minFunction” . The first on accept two integer number and return the
minimum integer number, while the second one accept two double numbers and return the minimum double
number.
Solution:
import jave.util.Scanner;
int a = 11;
int b = 6;
double c = 7.3;
double d = 9.4;
// for integer
int min;
min = n2;
else
min = n1;
return min;
20
// for double
double min;
min = n2;
else
min = n1;
return min;
Run:
Minimum Value = 6
Ex: Write a java program that created two overloaded methods, first sum method performs addition of two
numbers and second sum method performs addition of three numbers.
Solution:
class Calculation{
obj.sum(10,10,10);
obj.sum(20,20);
} }
Run:
30
40
21
Ex: Write a java program that created two overloaded methods that differs in data type. The first sum method
receives two integer arguments and second sum method receives two double arguments.
Solution:
class Calculation2{
obj.sum(10.5,10.5);
obj.sum(20,20);
} }
Run:
21.0
40
Ex: Write a java program that created two overloaded methods that differs in number of arguments. The first
sum method receives two integer arguments and second sum method receives three integer arguments.
Solution:
class OverloadingCalculation1{
obj.sum(20,20,20); } }
Run:
40
60
22