Java Tutorial Day 2
Learning Objective: Let’s start with Java Practical List and it’s example with detail explanations.
prog1.java
Write a program in Java to generate first n prime numbers
public class prog1 {
public static void main(String[] args)
{
System.out.println("---------------------------------");
System.out.println(" Write a program in Java to generate first n prime numbers");
System.out.println("---------------------------------");
int ct=0,n=0,i=1,j=1;
while(n<10)
{
j=1;
ct=0;
while(j<=i)
{
if(i%j==0)
ct++;
j++;
}
if(ct==2)
{
System.out.printf("%d ",i);
n++;
}
i++;
}
}
}
prog2.java
Write a program in Java to find maximum of three numbers using conditional operator
public class prog2 {
public static void main(String[] args)
{
System.out.println("---------------------------------");
System.out.println(" Write a program in Java to find maximum of three numbers using conditional operator");
System.out.println("---------------------------------");
//initializing numbers to compare
int a=40, b=78, c=19;
if(a>=b && a>=c)
{
System.out.println(a+" is the largest Number");
}
else if (b>=a && b>=c)
{
System.out.println(b+" is the largest Number");
}
else
{
System.out.println(c+" is the largest number");
}
}
}
prog3.java
Write a program in Java to find second maximum of n numbers without using arrays
import java.util.*;
public class prog9 {
public static void main(String[] args)
{
System.out.println("---------------------------------");
System.out.println(" Write a program in Java to find second maximum of n numbers without using arrays");
System.out.println("---------------------------------");
Scanner y = new Scanner(System.in);
System.out.println("Enter the number of values to be entered");
int n = y.nextInt();
System.out.println("Enter number 1");
int num = y.nextInt();
int max = num, max2 = num, min = num;
for (int i = 2; i <= n; i++) {
System.out.println("Enter number " + i);
num = y.nextInt();
if (num < min) {
min = num;
} else if (num > max) {
max2 = max; // shift previous max to become the 2nd largest
max = num; // store max
} else if (num > max2) {
max2 = num;
}
}
System.out.println("The largest number is " + max);
System.out.println("The 2nd largest number is " + max2);
System.out.println("The smallest number is " + min);
}
}
prog4.java
Write a program in Java to reverse the digits of a number using while loop
public class prog4 {
public static void main(String[] args)
{
System.out.println("---------------------------------");
System.out.println(" Write a program in Java to reverse the digits of a number using while loop");
System.out.println("---------------------------------");
int number = 987654, reverse = 0;
while(number != 0)
{
int remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number/10;
}
System.out.println("The reverse of the given number is: " + reverse);
}
}
prog5.java
Write a program in Java to convert number into words & print it
public class prog5 {
static void numberToWords(char num[]) {
int len = num.length;
if (len == 0) {
System.out.println("The string is empty.");
return;
}
if (len > 4) {
System.out.println("\n The given number has more than 4 digits.");
return;
}
String[] onedigit = new String[] {
"Zero",
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine"
};
String[] twodigits = new String[] {
"",
"Ten",
"Eleven",
"Twelve",
"Thirteen",
"Fourteen",
"Fifteen",
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen"
};
String[] multipleoftens = new String[] {
"",
"",
"Twenty",
"Thirty",
"Forty",
"Fifty",
"Sixty",
"Seventy",
"Eighty",
"Ninety"
};
String[] poweroftens = new String[] {
"Hundred",
"Thousand"
};
System.out.print(String.valueOf(num) + ": ");
if (len == 1) {
System.out.println(onedigit[num[0] - '0']);
return;
}
int x = 0;
while (x < num.length) {
if (len >= 3) {
if (num[x] - '0' != 0) {
System.out.print(onedigit[num[x] - '0'] + " ");
System.out.print(poweroftens[len - 3] + " ");
}
--len;
}
else {
if (num[x] - '0' == 1) {
int sum = num[x] - '0' + num[x + 1] - '0';
System.out.println(twodigits[sum]);
return;
}
else if (num[x] - '0' == 2 && num[x + 1] - '0' == 0) {
System.out.println("Twenty");
return;
}
else {
int i = (num[x] - '0');
if (i > 0)
System.out.print(multipleoftens[i] + " ");
else
System.out.print("");
++x;
if (num[x] - '0' != 0)
//prints the ith index element of the array onedigit[]
System.out.println(onedigit[num[x] - '0']);
}
}
//increments the variable i by 1
++x;
}
}
public static void main(String args[]) {
System.out.println("---------------------------------");
System.out.println(" Write a program in Java to convert number into words & print it");
System.out.println("---------------------------------");
numberToWords("5555".toCharArray());
}
}
prog6.java
Write a program in Java to multiply two matrix
public class prog6 {
public static void main(String args[]) {
System.out.println("---------------------------------");
System.out.println(" Write a program in Java to multiply two matrix");
System.out.println("---------------------------------");
int a[][] = {
{
1,
1,
1
},
{
2,
2,
2
},
{
3,
3,
3
}
};
int b[][] = {
{
1,
1,
1
},
{
2,
2,
2
},
{
3,
3,
3
}
};
int c[][] = new int[3][3]; //3 rows and 3 columns
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
c[i][j] = 0;
for (int k = 0; k < 3; k++) {
c[i][j] += a[i][k] * b[k][j];
}
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
}
prog7.java
Write a program to show the use of static keyword.
public class prog7 {
public static void main(String args[]) {
System.out.println("---------------------------------");
System.out.println(" Write a program to show the use of static keyword");
System.out.println("---------------------------------");
Student s1 = new Student(101, "Abc");
Student s2 = new Student(102, "Xyz");
s1.display();
s2.display();
}
}
class Student {
int rollno;
String name;
static String college = "Parul";
Student(int r, String n) {
rollno = r;
name = n;
}
void display() {
System.out.println(rollno + " " + name + " " + college);
}
}
prog8.java
Write a program in Java to develop overloaded constructor. Also develop the copy constructor to create a new object with the state of the existing object
public class prog8 {
public static void main(String arg[]) {
System.out.println("---------------------------------");
System.out.println(" Write a program in Java to develop overloaded constructor. Also develop the copy constructor to create a new object with the state of the existing object");
System.out.println("---------------------------------");
OverloadCopyConst cp1 = new OverloadCopyConst(5, 6);
OverloadCopyConst cp2 = new OverloadCopyConst(5);
OverloadCopyConst cp3 = new OverloadCopyConst(cp2, 5);
}
}
class OverloadCopyConst {
int n;
OverloadCopyConst() {
System.out.println("Default Constructor");
}
OverloadCopyConst(int x, int y) {
System.out.println("Addition is " + (x + y));
}
OverloadCopyConst(int a) {
n = a;
System.out.println("Number is " + n);
int i, j, flag;
for (i = 1; i <= n; i++) {
flag = 0;
for (j = 2; j <= i / 2; j++) {
if (i % j == 0) {
flag++;
break;
}
}
if (flag == 0) {
System.out.println("Prime is " + i);
}
}
}
OverloadCopyConst(OverloadCopyConst cp, int a) {
n = cp.n;
int max;
max = a > n ? a : n;
System.out.println("Maximum number is " + max);
}
}
prog9.java
Write a program in Java to demonstrate the use of ‘final’ keyword in the field declaration. How it is accessed using the objects
public class prog9 {
final int speedlimit = 90; //final variable
void run() {
speedlimit = 400;
}
public static void main(String args[]) {
System.out.println("---------------------------------");
System.out.println(" Write a program in Java to demonstrate the use of 'final' keyword in the field declaration. How it is accessed using the objects.");
System.out.println("---------------------------------");
prog15 obj = new prog15();
obj.run();
}
}
prog10.java
Develop minimum 4 program based on variation in methods i.e. passing by value, passing by reference, returning values
and returning objects from methods.
/**
* Develop minimum 4 program based on variation in methods i.e. passing by value, passing by reference, returning values
and returning objects from methods
*
*/
//=================================
// PassByValue
//==================================
public class prog10 {
String name="java";
void display(String name)
{
name="Java Programming";
}
public static void main(String arg[])
{
prog16 p=new prog16();
System.out.println("Before changes : "+p.name);
p.display("C++");
System.out.println("After changes : "+p.name);
}
}
//=================================
// PassByReference
//==================================
public class prog10 {
String name="Java";
void display(PassByReference p)
{
name="Java Programming";
}
public static void main(String arg[])
{
PassByReference p=new PassByReference();
System.out.println("Before changes : "+p.name);
p.display(p);
System.out.println("After changes : "+p.name);
}
}
//=================================
// ReturningValue
//==================================
public class prog10 {
int n1,n2;
void getValue(int a,int b)
{
n1=a;
n2=b;
}
int returnMax()
{
return n1>n2?n1:n2;
}
public static void main(String arg[])
{
ReturningValue R=new ReturningValue();
R.getValue(12,44);
System.out.println("Maximum is : "+R.returnMax());
}
}
//=================================
// ReturningObject
//==================================
public class prog10 {
int n1,n2;
ReturningObject(int a,int b)
{
n1=a;
n2=b;
}
ReturningObject minNumber()
{
ReturningObject min=new ReturningObject(55,35);
return min;
}
public static void main(String arg[])
{
ReturningObject R1=new ReturningObject(66,234);
ReturningObject R2=R1.minNumber();
int minR1=R1.n1<R1.n2?R1.n1:R1.n2;
System.out.println("Minimum of (R1) "+R1.n1+" & "+R1.n2+" is "+minR1);
int minR2=R2 .n1<R2.n2?R2.n1:R2.n2;
System.out.println("Minimum of (R2) "+R2.n1+" & "+R2.n2+" is "+minR2);
}
}