You are on page 1of 22

Examples/Java Basic/PART1

Ex: Write a java program for printing the message “Hello Java”.

Solution:

class Simple{

public static void main(String args[]){

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;

public class read{

public static void main(String args[])

float f1,f2,sub;

int n1,n2,sum;

System.out.println(“input two integer numbers”);

Scanner input= new Scanner (System.in);

n1=input.nextInt();

n2=input.nextInt();

sum=n1+n2;

System.out.println(“the value of sum=”+sum);

System.out.println(“input two float numbers”);

f1=input.nextFloat();

f2=input.nextFloat();

sub=f1-f2;

System.out.println(“the value of subtract=”+sub); } }


Run:

input two int

10

20

the value of sum=30

input two float numbers

the value of subtract=3//

EX:Write a java program that implement most arithmetic operations.

Solution:

public class ArithmeticDemo {

public static void main(String[] args) {

//a few numbers

int i = 37;

int j = 42;

double x = 27.475;

double y = 7.22;

System.out.println(" Variable values...");

System.out.println(" i = " + i);

System.out.println(" j = " + j);

System.out.println(" x = " + x);

System.out.println(" y = " + y);

//adding numbers

System.out.println("Adding...");

System.out.println(" i + j = " + (i + j));

2
System.out.println(" x + y = " + (x + y));

//subtracting numbers

System.out.println("Subtracting...");

System.out.println(" i - j = " + (i - j));

System.out.println(" x - y = " + (x - y));

//multiplying numbers

System.out.println("Multiplying...");

System.out.println(" i * j = " + (i * j));

System,out.println(" x * y = " + (x * y));

//dividing numbers

System.out.println("Dividing...");

System.out.println(" i / j = " + (i / j));

System.out.println(" x / y = " + (x / y));

//computing the remainder resulting from dividing numbers

System.out.println("Computing the remainder...");

System.out.println(" i % j = " + (i % j));

System.out.println(" x % y = " + (x % y));

//Mixing type

System.out.println("Mixing types...");

System.out.println(" j + y = " + (j + y));

System.out.println(" i * x = " + (i * x));

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:

public class IfExample {

public static void main(String[] args) {

int age=20;

if(age>18){

System.out.print("Age is greater than 18"); }

Run:

Age is greater than 18

Ex: Write a java program that check whether the x input integer number is even or odd.

Solution:

import jave.util.Scanner;

public class IfElseExample {

public static void main(String[] args) {

int x;

System.out.println(“input the x integer numbers”);

Scanner input= new Scanner (System.in);

x=input.nextInt();

if(x%2==0){

System.out.println("even number");

else{

System.out.println("odd number"); }

4
Run:

input the x integer numbers

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;

public class IfElseIfExample {

public static void main(String[] args) {

int marks;

System.out.println(“input the marks integer degree”);

Scanner input= new Scanner (System.in);

marks=input.nextInt();

if(marks<50){

System.out.println("fail");

else if(marks>=50 && marks<60){

System.out.println("D grade");

else if(marks>=60 && marks<70){

System.out.println("C grade");

else if(marks>=70 && marks<80){

System.out.println("B grade");

5
}

else if(marks>=80 && marks<90){

System.out.println("A grade");

}else if(marks>=90 && marks<100){

System.out.println("A+ grade");

}else{

System.out.println("Invalid!"); } } }

Run:

input the marks integer degree

65

C grade

Ex: Write a java program to print the integer input value between 1 to 3 to text number.( i.e 2Two).

Solution:

import jave.util.Scanner;

public class SwitchExample {

public static void main(String[] args) {

int number;

System.out.println(“input the integer number between 1 to 3”);

Scanner input= new Scanner (System.in);

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:

input the integer number between 1 to 3

6
3

Three

Looping Statements

Ex:Write a java program that print the sequence number from 1 to 10.

7
Solution:

public class ForExample {

public static void main(String[] args) {

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;

public class WhileExample {

public static void main(String[] args) {

int i=1;

int sum=0;

int n;

System.out.println(“input the total numbers n”);

Scanner input= new Scanner (System.in);

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;

public class DoWhileExample {

public static void main(String[] args) {

int i=1;

int n,x;

int sum=0;

int mul=1;

System.out.println(“input the total numbers n”);

Scanner input= new Scanner (System.in);

n=input.nextInt();

do{

Scanner input= new Scanner (System.in);

x=input.nextInt();

if (x%2==0)

sum=sum+x;

else

mul=mul*x;

i++;
9
}while(i<=n);

System.out.println(“summation of even numbers=%d”,sum);

System.out.println(“multiplication of odd numbers=%d”,mul); } }

Enhanced For loop

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;

public class ForEachExample {

public static void main(String[] args) {

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;

public class BreakExample {

public static void main(String[] args) {

System.out.println(“input the integer number between 1 to 10 which is stoping the loop”);


10
Scanner input= new Scanner (System.in);

n=input.nextInt();

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

if(i==n){

break; }

System.out.println(i);

} } }

Run:

input the integer number between 1 to 10 which is stoping the loop

Array

11
Ex:Write a Java program which is create, initialize, and finding the summation of arrays elements, then print
it.

Public class TestArray {

public static void main(String[] args) {

double[] myList = {1.9, 2.9, 3.4, 3.5};

// Print all the array elements

for (int i = 0; i < myList.length; i++) {

System.out.println(myList[i] + " ");

// Summing all elements

double total = 0;

for (int i = 0; i < myList.length; i++) {

total += myList[i];

System.out.println("Total is " + total);

// Finding the largest element

double max = myList[0];

for (int i = 1; i < myList.length; i++) {

if (myList[i] > max) max = myList[i];

System.out.println("Max is " + max);

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

Public class LargestValueArray {

public static void main(String[] args) {

int n;

int mylist=new int[n];

// input the total numbers of element in array

System.out.println(“Enter the total numbers of element in array”);

Scanner input= new Scanner(System.in);

n=input.nextInt();

// input the n numbers to store in array

System.out.println(“Enter the values”);

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

mylist[i]=input.nextInt(); }

// Finding the largest element

int max = myList[0];

for (int i = 1; i < myList.length; i++) {

if (myList[i] > max)

max = myList[i]; }

System.out.println("Max is " + max); }}

Run:

Enter the total numbers of element in array

Enter the values

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;

public class ExampleVoid {

public static void main(String[] args) {

RankPoints(150.5);

public static void RankPoints(double points) {

if (points >= 100) {

System.out.println("Rank:A1");

}else if (points >= 200) {

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;

public class ExampleMinNumber {

public static void main(String[] args) {

int a = 11;

int b = 6;

int c = minFunction(a, b);


15
System.out.println("Minimum Value = " + c);

/** returns the minimum of two numbers */

public static int minFunction(int n1, int n2) {

int min;

if (n1 > n2)

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;

public class swappingExample {

public static void main(String[] args) {

int a = 30;

int b = 45;

System.out.println("Before swapping, a = " + a + " and b = " + b);

// Invoke the swap method

swapFunction(a, b);

System.out.println("\n**Now, Before and After swapping values will be same here**:");

System.out.println("After swapping, a = " + a + " and b is " + b);

}
16
public static void swapFunction(int a, int b) {

System.out.println("Before swapping(Inside), a = " + a + " b = " + b);

int c = a;

a = b;

b = c;

System.out.println("After swapping(Inside), a = " + a + " b = " + b);

Run:

Before swapping, a = 30 and b = 45

Before swapping(Inside), a = 30 b = 45

After swapping(Inside), a = 45 b = 30

**Now, Before and After swapping values will be same here**:

After swapping, a = 30 and b is 45

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;

static String college = "ITS";

static void change(){

college = "BBDIT";

Student9(int r, String n){

rollno = r;

name = n;

void display (){System.out.println(rollno+" "+name+" "+college);}

public static void main(String args[]){

Student9.change();

Student9 s1 = new Student9 (111,"Karan");

Student9 s2 = new Student9 (222,"Aryan");

Student9 s3 = new Student9 (333,"Sonoo");

s1.display();

s2.display();

s3.display(); } }

Run:

18
111 Karan BBDIT

222 Aryan BBDIT

333 Sonoo 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{

static int cube(int x){

return x*x*x; }

public static void main(String args[]){

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;

public class ExampleOverloading {

public static void main(String[] args) {

int a = 11;

int b = 6;

double c = 7.3;

double d = 9.4;

int result1 = minFunction(a, b);

// same function name with different parameters

double result2 = minFunction(c, d);

System.out.println("Minimum Value = " + result1);

System.out.println("Minimum Value = " + result2);

// for integer

public static int minFunction(int n1, int n2) {

int min;

if (n1 > n2)

min = n2;

else

min = n1;

return min;

20
// for double

public static double minFunction(double n1, double n2) {

double min;

if (n1 > n2)

min = n2;

else

min = n1;

return min;

Run:

Minimum Value = 6

Minimum Value =7.3

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{

void sum(int a,int b){System.out.println(a+b);}

void sum(int a,int b,int c){System.out.println(a+b+c);}

public static void main(String args[]){

Calculation obj=new 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{

void sum(int a,int b){System.out.println(a+b);}

void sum(double a,double b){System.out.println(a+b);}

public static void main(String args[]){

Calculation2 obj=new 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{

void sum(int a,long b){System.out.println(a+b);}

void sum(int a,int b,int c){System.out.println(a+b+c);}

public static void main(String args[]){

OverloadingCalculation1 obj=new OverloadingCalculation1();

obj.sum(20,20);//now second int literal will be promoted to long

obj.sum(20,20,20); } }

Run:

40

60

22

You might also like