Java Journal Program Unit - 1

                             UNIT - 1

_____________________________________________________
//PROGRAM - 26
/*********************************
Write a program in Java with class 
Rectangle with the data fields width,
length, area and color .The length, 
width and area are of double type and
color is of string type .The methods 
are set_ length () , set_width (),
set_color(), and find_ area (). Create
two object of Rectangle and compare
their area and color. If area and color
 both are same for the objects then
display “Matching Rectangles” otherwise
display “Non matching Rectangle”
**********************************/
import java.util.*;
import java.io.*;
class rectangle
{
Scanner obj = new Scanner(System.in);
double width, length, area;
public String color;

void set_width()
{
System.out.print("Enter width : ");
width = obj.nextDouble();
}
void set_length()
{
System.out.print("Enter length : ");
length = obj.nextDouble();
}
String set_color()
{
System.out.print("Enter color : ");
color = obj.next();
return color;
}
void find_area()
{
area = length * width;
System.out.println("Area : " + area);
}
}
class program26
{
public static void main(String[] args) {
rectangle obj1 = new rectangle();
rectangle obj2 = new rectangle();

obj1.set_length();
obj1.set_width();
//obj1.set_color();
obj1.find_area();

obj2.set_length();
obj2.set_width();
//obj2.set_color();
obj2.find_area();

System.out.println("Enter First Rectangle color and Second Rectangle color : ");
if (obj1.area == obj2.area && obj1.set_color().equals(obj2.set_color())) {
System.out.println("Matching Rectangles");
}
else
{
System.out.println("Non Matching Rectangles");
}
}
}

OUTPUT:
Enter length : 12
Enter width : 12
Area : 144.0
Enter length : 12
Enter width : 12
Area : 144.0
Enter First Rectangle color and Second Rectangle color :
Enter color : red
Enter color : red
Matching Rectangles
_______________________________________________________

//PROGRAM 24
/**************************************
Write a program to create a room class,
the attributes of this class isvroomno,
roomtype, roomarea and ACmachine. In 
this class the membervfunctions are 
setdata and displaydata.
**************************************/

import java.util.*;

public class program24
{
Scanner obj = new Scanner(System.in);
int isvroomno;
String roomtype, roomarea, acmachine;

public void setdata()
{
System.out.print("Enter Room NO.");
isvroomno = obj.nextInt();

System.out.print("Enter Room Area : ");
roomarea = obj.nextLine();

System.out.print("Enter Room Type : ");
roomtype = obj.nextLine();

System.out.print("Enter Room ACmachine Yes or No : ");
acmachine = obj.nextLine();
}
public void displaydata()
{
System.out.println("Room No. : " + isvroomno);
System.out.println("Room Type : " + roomtype);
System.out.println("RoomArea : " + roomarea);
System.out.println("AC/NonAC : " + acmachine);
}
public static void main(String[] args) {
program24 r1 = new program24();

r1.setdata();
r1.displaydata();
}
}

OUTPUT:
Enter Room NO.12
Enter Room Area : Enter Room Type : Hotel
Enter Room ACmachine Yes or No : yes
Room No. : 12
Room Type : Hotel
RoomArea :
AC/NonAC : yes
_______________________________________________________
//PROGRAM - 23
/*******************************
Java Program to print the dupli-
cate elements of an array.
*******************************/
class program23
{
public static void main(String[] args) {
int a[] = { 10, 20, 30, 20, 10 };

int n = a.length;
for (int i = 0;i < n; i++) {
for (int j = i + 1;j < n; j++) {
if (a[i] == a[j]) {
System.out.println(a[j]);
}
}
}
}
}
OUTPUT:
10
20
_______________________________________________________

//PROGRAM - 22
/************************************
Write a program to check if two 
given String is Anagram of each other.
Your function should return true if 
two Strings are Anagram, false
otherwise. A string is said to be an 
anagram if it contains the same
characters and same length, but in a 
different order, e.g. army and Mary
are anagrams. You can ignore cases 
for this problem.

(Two strings are called anagrams if 
they contain same set of characters
but in different order.)
************************************/

/* 
  * \s - matches single whitespace character. 
  *  \s+ - matches sequence of one or more whitespace characters.

  * The string \s is a regular expression that means "whitespace",
    and you have to write it with two backslash characters ("\\s") 
    when writing it as a string in Java.
*/
    import java.util.Arrays;


    public class program22
    {
    static void anagram(String str1, String str2)
    {
    String s1 = str1.replaceAll("\\s", "");
    String s2 = str2.replaceAll("\\s", "");

    boolean status = true;

    if (s1.length() != s2.length()) {
    status = false;
    }
    else
    {
    char[] ArrayS1 = s1.toLowerCase().toCharArray();  
            char[] ArrayS2 = s2.toLowerCase().toCharArray();

    Arrays.sort(ArrayS1);
    Arrays.sort(ArrayS2);

    status = Arrays.equals(ArrayS1, ArrayS2);
    }
    if (status) {
    System.out.println(s1 + " and " + s2 + " are anagrams ");
    }
    else{
    System.out.println(s1 + " and " + s2 + " are not anagrams ");
    }
    }


    public static void main(String[] args) {
    anagram("Milan", "Minal");
    anagram("Milan", "Bakotra");
    }
    }
OUTPUT:

Milan and Minal are anagrams
Milan and Bakotra are not anagrams
_______________________________________________________

//PROGRAM - 21
/***********************************
Write a program to remove duplicates 
from an array in Java.
***********************************/
import java.util.*;

class program21
{
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);

int a[] = new int[5];
int flag = 0;
int n = a.length;
for (int i = 0;i < n; i++) {
System.out.print("Enter Any No. :");
a[i] = obj.nextInt();
}
for (int i = 0;i < n; i++) {
for (int j = i + 1;j < n; j++) {
if (a[i] == a[j]) {

a[j] = a[n - 1];
n--;
j--;
}
}
}
int arry2[] = Arrays.copyOf(a,n);
for (int i = 0;i < arry2.length; i++) {
System.out.println(arry2[i]);
}
}
}
OUTPUT:
Enter Any No. :20
Enter Any No. :20
Enter Any No. :23
Enter Any No. :22
Enter Any No. :20
20
22
23
_______________________________________________________
//PROGRAM - 20
/************************************
Design and Develop a java program to
read marks of a student and print the
total and average of marks using
scanner class.
*************************************/

import java.util.*;

class program20
{
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
int m1, m2, m3, total;
double avg;
System.out.print("Enter marks1 : ");
m1 = obj.nextInt();

System.out.print("Enter marks2 : ");
m2 = obj.nextInt();

System.out.print("Enter marks3 : ");
m3 = obj.nextInt();

total = m1 + m2 + m3;
avg = total / 3;

System.out.println("Total : " + total);
System.out.println("Average : " + avg);
}
}
OUTPUT:
Enter marks1 : 88
Enter marks2 : 80
Enter marks3 : 85
Total : 253
Average : 84.0
_______________________________________________________

//PROGRAM- 19
/***************************************
Write a Java program that checks whether
a given string is a palindrome or not. 
Ex: MADAM is a palindrome?
***********************************/
import java.util.*;

class program19
{
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);

System.out.print("Enter String : ");
String str = obj.nextLine();
String rev = "";
int length = str.length();

for (int i = length - 1; i >= 0 ;i--) 
rev = rev + str.charAt(i);
if (str.equals(rev)) {
System.out.println(str + " is palindrome");
}
else
{
System.out.println(str + " is not palindrome");
}
}
}
OUTPUT:
Enter String : MADAM
MADAM is palindrome
_______________________________________________________
//PROGRAM - 18
/************************************
Write a Java program that prompts the
user for an integer and then printsout
all the prime numbers up to that 
Integer?
************************************/

import java.util.Scanner;

class program18
{
public static void main(String[] args)
{
int n;
int p;
Scanner s=new Scanner(System.in);
System.out.print("Enter a number: ");
n=s.nextInt();
for(int i=2;i<n;i++)
{
p=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
p=1;
}
if(p==0)
System.out.println(i);
}
}
}
OUTPUT:
Enter a number: 20
2
3
5
7
11
13
17
_______________________________________________________
//PROGRAM - 17
/**************************************
Write a Java program to replace a 
string "python" with "java" and "java"
with "python" in a given string.
Expected Output:
Input the string:
python is more propular than java
New string:
java is more propular than python
**************************************/
//import java.io.*;
import java.util.*;

class program17
{
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);

System.out.println("Input the String");
String str1 = obj.nextLine();
str1 = str1.replaceAll("java","py_thon");
str1 = str1.replaceAll("python","java");
str1 = str1.replaceAll("py_thon","python");

System.out.println("New string : \n" + str1);
}
}
OUTPUT:
python is more propular than java
New string :
java is more propular than python
_______________________________________________________
//PROGRAM - 16
/************************************
Write a Java program to take the last
 three characters from a given string
and add the three characters at both 
the front and back of the string.
String length must be greater than 
three and more.
Test data: "Python" will be 
"honPythonhon"
Sample Output:
honPythonhon
************************************/
import java.util.*;
import java.io.*;

public class program16
{
public static void main(String[] args) {
String str1 = "Python";
int slength = 3;

if (slength > str1.length()) {
slength = str1.length();
}
String subpart = str1.substring(str1.length() - 3);
System.out.println(subpart + str1 + subpart);
}
}
OUTPUT:
honPythonhon
_______________________________________________________
 
//PROGRAM - 15
/********************************************
Write a Java program to capitalize the
first letter of each word in a sentence.
Sample Output:
Input a Sentence:
the quick brown fox jumps over the lazy dog.
The Quick Brown Fox Jumps Over The Lazy Dog.
********************************************/
import java.util.*;

class program15
{
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);

System.out.println("Input a Sentence:");
String line = obj.nextLine();

String upper_case_line = "";
Scanner lineScan = new Scanner(line);
while(lineScan.hasNext())
{
String word = lineScan.next();
upper_case_line += Character.toUpperCase(word.charAt(0)) + word.substring(1) + " ";
System.out.println(upper_case_line.trim());
}
}
OUTPUT:
Input a Sentence:
the quick brown fox jumps over the lazy dog.
The Quick Brown Fox Jumps Over The Lazy Dog.
_______________________________________________________
//PROGRAM - 13

/*****************************************
Write a Java program to test if a given 
number (positive integer ) is a perfect 
square or not.

Expected Output:
Input a positive integer: 6
Is the said number perfect square? false
*****************************************/
import java.util.*;
class program13
{
public static void main(String[] args) {
int n, flag =  0;
Scanner obj = new Scanner(System.in);

System.out.print("Enter Any Number : ");
n = obj.nextInt();

for (int i = 1;i <= 100; i++) {
if (i * i == n) {
flag = 1;
}
}
if (flag == 1)
{
System.out.println(n + " Number is perfect square");
}
else
{
System.out.println(n + " Number is not perfect square");
}
}
}
OUTPUT:
Enter Any Number : 25
25 Number is perfect square
        OR
Enter Any Number : 12
12 Number is not perfect square
 _______________________________________________________
//PROGRAM - 12

/****************************************
Write a Java program to accept a positive 
number and repeatedly add all its digits
until the result has only one digit.
****************************************/

import java.util.*;

class program12
{
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);

System.out.print("Enter Any Number : ");
int n = obj.nextInt();

while(n > 9)
{
int sum_digit = 0;
while(n != 0)
{
sum_digit += n % 10;
n /= 10;
}
n = sum_digit;
}
System.out.println("Sum of digits : " + n);
}
}
OUTPUT:
Enter Any Number : 789
Sum of digits : 6
____________________________________________________________________
//PROGRAM - 11
/********************************************
Write a Java program to print numbers between
1 to 100 which are divisible by 3, 5 and by 
both.

Sample Output:
Divided by 3:
3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36,
39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 
72, 75, 78, 81, 84, 87, 90, 93, 96, 99,

Divided by 5:
5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 
60, 65, 70, 75, 80, 85, 90, 95,

Divided by 3 & 5:
15, 30, 45, 60, 75, 90,
*********************************************/

class program11
{
public static void main(String[] args) {
int i;
System.out.println("Divided by 3 : ");
for (i = 1; i <= 100; i++) {
if (i % 3 == 0) {
System.out.print(i + ",");
}
}
System.out.print("\n\n\n");
System.out.println("Divided by 5 : ");
for (i = 1; i <= 100; i++) {
if (i % 5 == 0) {
System.out.print(i + ",");
}
}
System.out.print("\n\n\n");
System.out.println("Divided by 3 & 5 : ");
for (i = 1; i <= 100; i++) {
if (i % 3 == 0 && i % 5 == 0) {
System.out.print(i + ",");
}
}
}
}
OUTPUT:
Divided by 3 :
3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,


Divided by 5 :
5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,


Divided by 3 & 5 :
15,30,45,60,75,90,
_______________________________________________________
//PROGRAM - 10

/********************************************
Write a Java program to compute the number of
trailing zeros in a factorial.
7! = 5040, therefore the output should be 1
********************************************/

import java.util.Scanner;
public class program10 {
     public static void main(String[] arg) 
{

Scanner in = new Scanner(System.in);
System.out.print("Input a number: ");
    int n = in.nextInt(); 
        int n1 = n;
long ctr = 0;
while (n != 0) 
{
ctr += n / 5;
n /= 5;
}
System.out.printf("Number of trailing zeros of the factorial %d is %d ",n1,ctr);
System.out.printf("\n");         
}
}
_______________________________________________________
//PROGRAM - 9

/******************************
Write a Java program to convert
a decimal number to binary 
number.
Input Data:
Input a Decimal Number : 5
Expected Output
Binary number is: 101
****************************/

import java.util.*;
class program9
{
public static void main(String[] args) {
int dec_num, i = 1;
int bin_num[] = new int[100];

Scanner obj = new Scanner(System.in);

System.out.print("Input a Decimal Number : ");
dec_num = obj.nextInt();

while(dec_num != 0)
{
bin_num[i++] = dec_num % 2;
dec_num = dec_num / 2;
}
System.out.print("Binary number is : ");
for (int j = i - 1; j > 0; j--) {
System.out.print(bin_num[j]);
}
System.out.println("\n");
}
}
OUTPUT:

Input a Decimal Number : 5
Binary number is : 101
_______________________________________________________

//PROGRAM - 8

/****************************

Write a Java program to check

whether an given integer is a

power of 4 or not.

Given num = 64, return true. Given num = 6, return false.

*****************************/

import java.util.*;


class program8

{

public static void main(String[] args) {


Scanner obj = new Scanner(System.in);

int i, no1, no2;


System.out.print("Enter Any Number : ");

no2 = obj.nextInt();

int flag = 0;

for (i = 1; i <= 100 ; i ++ ) {

no1 = i * i * i * i;

if (no2 == no1) {

flag = 1;

}

}

if (flag == 1) {

System.out.println(no2 + " is power of 4");

}

else

{

System.out.println(no2 + " is not power of 4");

}

}

}

OUTPUT:
Enter Any Number : 2
2 is not power of 4

        OR
Enter Any Number : 16
16 is power of 4

_______________________________________________________

//Program - 7

/***********************

Write a Java program to 

multiply two given int-

egers without using the

multiply operator(*).

***********************/


import java.util.*;




class program7

{

public static void main(String[] args) {

Scanner obj = new Scanner(System.in);

int no1, no2;

int mul = 0, i;

System.out.print("Enter Value of A : ");

no1 = obj.nextInt();


System.out.print("Enter Value of B : ");

no2 = obj.nextInt();


for (i = 0; i < no2; i++) {

mul = add(mul, no1);

}

System.out.println("multiplication : " + mul);

}

static int add(int num1,int num2){

for(int i=0; i<num2; i++)

num1++;

return num1;

}

}

OUTPUT:

Enter Value of A : 10
Enter Value of B : 20
multiplication : 200

_______________________________________________________

//PROGRAM - 6

/**********************************

Write a Java program to print the 

ascii value of a given character.

Expected Output

The ASCII value of Z is :90

**********************************/

import java.util.*;


class program6

{

public static void main(String[] args) {

char ch;


Scanner obj = new Scanner(System.in);


System.out.print("Enter any Character : ");

ch = obj.next().charAt(0); // for get A character.


int ascii = ch;


System.out.print("ASCII value of " + ch + " is " + ascii);

}

}

OUTPUT:

Enter any Character : Z
ASCII value of Z is 90
_______________________________________________________
//PROGRAM - 5

/***********************************

Write a Java program to convert seconds

 to hour, minute and seconds.

Sample Output:

Input seconds: 86399

23:59:59

************************************/

import java.util.*;


class program5

{

public static void main(String[] args) {

int h, m, s , r;


Scanner obj = new Scanner(System.in);


System.out.print("Enter Seconds : ");

s = obj.nextInt();


h = s / 3600;

r = s % 3600;

m = r / 60;

s = r % 60;


System.out.println(h + ":" + m + ":" + s );

}

}

OUTPUT:

Enter Seconds : 86399
23:59:59

_______________________________________________________

//PROGRAM - 4 

/***********************************

Write a Java program to find the 

numbers greater than the average of 

the numbers of a given array.

***********************************/

import java.util.*;


class program4

{

public static void main(String[] args) {

Integer nums[] = new Integer[]{ 1, 2, 4, 45, 56, 67, 87, 22, 32, 25};

int sum = 0;


System.out.println("Arrays : " + Arrays.toString(nums));


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

sum = sum + nums[i];

}

double avg = sum / nums.length;


System.out.println("Sum of Array : " + sum);

System.out.println("Average : " + avg);


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

if(avg < nums[i])

System.out.println("Greater numbers of than average : " + nums[i]);

}

}

}

OUTPUT:

Arrays : [1, 2, 4, 45, 56, 67, 87, 22, 32, 25]

Sum of Array : 341

Average : 34.0

Greater numbers of than average : 45

Greater numbers of than average : 56

Greater numbers of than average : 67

Greater numbers of than average : 87

_______________________________________________________

//PROGRAM - 3

/**********************************

Write a Java program to add two 

numbers without using any arithmetic

operators.

Given x = 10 and y = 12; result = 22

***********************************/


class math

{

int Add(int x, int y) 

if (y == 0) 

return x; 

else

return Add( x ^ y, (x & y) << 1); 

}

}

class program3

{

public static void main(String[] args) {

math obj = new math();

System.out.println("Addition : " + obj.Add(10, 15));

}

OUTPUT:

Addition : 25

_______________________________________________________

//PROGRAM - 2

/**************************************

Write a Java program to print the sum 

(addition), multiply, subtract, divide

and remainder of two numbers.

***************************************/


class program2

{

public static void main(String[] args) {

int a = 5, b = 6, c;


c = a + b;

System.out.println("Addition  =  " + c);


c = a - b;

System.out.println("Subtraction  =  " + c);


c = a * b;

System.out.println("multiplication  =  " + c);


c = a / b;

System.out.println("divition  =  " + c);


c = a % b;

System.out.println("Remainder  =  " + c);

}

}

OUTPUT:

Addition  =  11

Subtraction  =  -1

multiplication  =  30

divition  =  0

Remainder  =  5

_______________________________________________________

/**********************************************

Write a Java program to print 'Hello World' on 

screen and then print your name on a separate 

line.

***********************************************/


//PROGRAM - 1


class program1

{

public static void main(String[] args) {

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

System.out.println("Milan Bakotra");

}

}

OUTPUT:

Hello World

Milan Bakotra

_______________________________________________________


Comments

Post a Comment

Popular posts from this blog

Questions 2 : Assume there are three small caches, each consisting of four one-word blocks. On cache is direct-mapped, a second is two-way set-associative, and the third is fully associative. Find the number of hits for each cache organization given the following sequence of block addresses: 0, 8, 6, 5, 10, 15 and 8 are accessed twice in the same sequence. Make a tabular column as given below to show the cache content on each of columns as required. Show all the pass independently pass. Draw as many numbers Assume the writing policy is LRU. Memory location Hit/Mis Add as many columns as required

Quetion 6 : Consider the "in-order-issue/in-order-completion" execution sequence shown in f In Figure Decode OWE Execute 12 12 12 14 16 13 16 13 15 15 16 Write 024/06/02 11 3 4 11 12 13 13 N 15 16 a. Identify the most likely reason why I could not enter the execute fourth cycle. stage until the [2] b. Will "in-order issue/out-of-order completion" or "out-of-order issue/out-of-order completion" fix this? If so, which? Explain

7.Write a program to read a list containing item name, item code and cost interactively and produce a three-column output as shown below. NAME CODE COST Turbo C++ 1001 250.95 C Primer 905 95.70 ------------- ------- ---------- ------------- ------- ---------- Note that the name and code are left-justified and the cost is right-justified with a precision of two digits. Trailing zeros are shown.